- 
                Notifications
    
You must be signed in to change notification settings  - Fork 47
 
[Challenge-25][GCI1166][S.T.E.P] new rule "don't catch runtime exceptions" #106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            max-208
  wants to merge
  11
  commits into
  green-code-initiative:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
max-208:1166-java
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from 2 commits
      Commits
    
    
            Show all changes
          
          
            11 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      b35f758
              
                implement rule "don't catch runtime exceptions"
              
              
                max-208 a276ce5
              
                exception for IllegalArgumentExceptions
              
              
                max-208 3c078b8
              
                add changelog, rename rule, correct compilation
              
              
                max-208 da1a3ed
              
                missing import
              
              
                max-208 d25f001
              
                different message to inform of the exception that needs not to be caught
              
              
                max-208 37c0529
              
                Fixing tests
              
              
                pataluc a32580f
              
                change number tot GCI96
              
              
                max-208 68c2c74
              
                Merge branch 'main' into 1166-java
              
              
                pataluc 99ed057
              
                fixed rule name, error message, changelog, naming conventions
              
              
                max-208 544f75a
              
                Merge branch 'main' into 1166-java
              
              
                dedece35 aa1b0fe
              
                Merge branch 'main' into 1166-java
              
              
                dedece35 File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            82 changes: 82 additions & 0 deletions
          
          82 
        
  ...c/main/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptions.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs | ||
| * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package org.greencodeinitiative.creedengo.java.checks; | ||
| 
     | 
||
| import java.io.File; | ||
| import java.io.FileReader; | ||
| 
     | 
||
| public class DontCatchRuntimeExceptions { | ||
| public void test1() { | ||
| try { | ||
| // some code that may throw an exception | ||
| } catch (RuntimeException e) { // Noncompliant {{Don't catch RuntimeExceptions}} | ||
| } | ||
| } | ||
| 
     | 
||
| public void test2() { | ||
| int[] array = new int[10]; | ||
| try { | ||
| array[10] = 0; | ||
| } catch (IndexOutOfBoundsException e) { // Noncompliant {{Don't catch RuntimeExceptions}} | ||
                
      
                  max-208 marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| } | ||
| } | ||
| 
     | 
||
| public void test3(){ | ||
| Object obj = null; | ||
| try { | ||
| obj.toString(); | ||
| } catch (NullPointerException e) { // Noncompliant {{Don't catch RuntimeExceptions}} | ||
| } | ||
| } | ||
| 
     | 
||
| public void test4(){ | ||
| try { | ||
| int result = 1 / 0; | ||
| } catch (ArithmeticException e) { // Noncompliant {{Don't catch RuntimeExceptions}} | ||
| } | ||
| } | ||
| 
     | 
||
| //these exceptions are ok because they aren't runtime exceptions | ||
| 
     | 
||
| public void testCompliant1() { | ||
| try { | ||
| File file = new File("nonexistent.txt"); | ||
| FileReader fr = new FileReader(file); | ||
| fr.read(); | ||
| } catch (FileNotFoundException e) { | ||
| } catch (IOException e) { | ||
| } | ||
| } | ||
| 
     | 
||
| // these exceptions are ok because they are IllegalArgumentExceptions, and can hardly be avoided | ||
| 
     | 
||
| public void testCompliant2() { | ||
| try { | ||
| // some code that may throw an exception | ||
| } catch (IllegalArgumentException e) { | ||
| } | ||
| } | ||
| 
     | 
||
| public void testCompliant3() { | ||
| try { | ||
| Integer.parseInt("abc"); | ||
| } catch (NumberFormatException e) { | ||
| } | ||
| } | ||
| 
     | 
||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            49 changes: 49 additions & 0 deletions
          
          49 
        
  src/main/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptions.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs | ||
| * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package org.greencodeinitiative.creedengo.java.checks; | ||
| 
     | 
||
| import java.util.Collections; | ||
| import java.util.List; | ||
| 
     | 
||
| import org.sonar.check.Rule; | ||
| import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; | ||
| import org.sonar.plugins.java.api.tree.CatchTree; | ||
| import org.sonar.plugins.java.api.tree.Tree; | ||
| import org.sonar.plugins.java.api.tree.Tree.Kind; | ||
| 
     | 
||
| @Rule(key = "GCI1166") | ||
| public class DontCatchRuntimeExceptions extends IssuableSubscriptionVisitor { | ||
| 
     | 
||
| protected static final String MESSAGERULE = "Don't catch RuntimeExceptions"; | ||
| protected static final String RUNTIME_EXCEPTION = "java.lang.RuntimeException"; | ||
| protected static final String ILLEGAL_ARGUMENT_EXCEPTION = "java.lang.IllegalArgumentException"; | ||
| 
     | 
||
| @Override | ||
| public List<Kind> nodesToVisit() { | ||
| return Collections.singletonList(Kind.CATCH); | ||
| } | ||
| 
     | 
||
| @Override | ||
| public void visitNode(Tree tree) { | ||
| CatchTree catchTree = (CatchTree) tree; | ||
| if(catchTree.parameter().type().symbolType().isSubtypeOf(RUNTIME_EXCEPTION) && | ||
| !catchTree.parameter().type().symbolType().isSubtypeOf(ILLEGAL_ARGUMENT_EXCEPTION)) { | ||
| reportIssue(tree, MESSAGERULE); | ||
                
      
                  max-208 marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| } | ||
| } | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -18,6 +18,7 @@ | |
| "GCI78", | ||
| "GCI79", | ||
| "GCI82", | ||
| "GCI94" | ||
| "GCI94", | ||
| "GCI1166" | ||
| ] | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs | ||
| * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package org.greencodeinitiative.creedengo.java.checks; | ||
| 
     | 
||
| import java.io.File; | ||
| import java.io.FileReader; | ||
| 
     | 
||
| public class DontCatchRuntimeExceptions { | ||
| public void test1() { | ||
                
      
                  max-208 marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| try { | ||
| // some code that may throw an exception | ||
| } catch (RuntimeException e) { // Noncompliant {{Don't catch RuntimeExceptions}} | ||
                
      
                  max-208 marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| } | ||
| } | ||
| 
     | 
||
| public void test2() { | ||
| int[] array = new int[10]; | ||
| try { | ||
| array[10] = 0; | ||
| } catch (IndexOutOfBoundsException e) { // Noncompliant {{Don't catch RuntimeExceptions}} | ||
| } | ||
| } | ||
| 
     | 
||
| public void test3(){ | ||
| Object obj = null; | ||
| try { | ||
| obj.toString(); | ||
| } catch (NullPointerException e) { // Noncompliant {{Don't catch RuntimeExceptions}} | ||
| } | ||
| } | ||
| 
     | 
||
| public void test4(){ | ||
| try { | ||
| int result = 1 / 0; | ||
| } catch (ArithmeticException e) { // Noncompliant {{Don't catch RuntimeExceptions}} | ||
| } | ||
| } | ||
| 
     | 
||
| //these exceptions are ok because they aren't runtime exceptions | ||
| 
     | 
||
| public void testCompliant1() { | ||
| try { | ||
| File file = new File("nonexistent.txt"); | ||
| FileReader fr = new FileReader(file); | ||
| fr.read(); | ||
| } catch (FileNotFoundException e) { | ||
| } catch (IOException e) { | ||
| } | ||
| } | ||
| 
     | 
||
| // these exceptions are ok because they are IllegalArgumentExceptions, and can hardly be avoided | ||
| 
     | 
||
| public void testCompliant2() { | ||
| try { | ||
| // some code that may throw an exception | ||
| } catch (IllegalArgumentException e) { | ||
| } | ||
| } | ||
| 
     | 
||
| public void testCompliant3() { | ||
| try { | ||
| Integer.parseInt("abc"); | ||
| } catch (NumberFormatException e) { | ||
| } | ||
| } | ||
| 
     | 
||
| } | ||
        
          
          
            33 changes: 33 additions & 0 deletions
          
          33 
        
  ...st/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptionsTest.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs | ||
| * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package org.greencodeinitiative.creedengo.java.checks; | ||
| 
     | 
||
| import org.junit.jupiter.api.Test; | ||
| import org.sonar.java.checks.verifier.CheckVerifier; | ||
| 
     | 
||
| class DontCatchRuntimeExceptionsTest { | ||
| 
     | 
||
| @Test | ||
| void test() { | ||
| CheckVerifier.newVerifier() | ||
| .onFile("src/test/files/DontCatchRuntimeExceptions.java") | ||
| .withCheck(new DontCatchRuntimeExceptions()) | ||
| .verifyIssues(); | ||
| } | ||
| 
     | 
||
| } | 
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.