-
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 all 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
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
91 changes: 91 additions & 0 deletions
91
...t/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.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,91 @@ | ||
| /* | ||
max-208 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * 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; | ||
| import java.io.FileNotFoundException; | ||
| import java.io.IOException; | ||
| import java.lang.RuntimeException; | ||
| import java.lang.ArithmeticException; | ||
| import java.lang.IndexOutOfBoundsException; | ||
| import java.lang.NullPointerException; | ||
| import java.lang.IllegalArgumentException; | ||
| import java.lang.NumberFormatException; | ||
| import java.lang.Integer; | ||
|
|
||
| public class AvoidRuntimeExceptions { | ||
| public void nominalRuntimeException() { | ||
| try { | ||
| // some code that may throw an exception | ||
| } catch (RuntimeException e) { // Noncompliant {{Avoid catching Runtime exceptions : RuntimeException}} | ||
| } | ||
| } | ||
|
|
||
| public void nominalRuntimeExceptionDependant() { | ||
| int[] array = new int[10]; | ||
| try { | ||
| array[10] = 0; | ||
| } catch (IndexOutOfBoundsException e) { // Noncompliant {{Avoid catching Runtime exceptions : IndexOutOfBoundsException}} | ||
| } | ||
| } | ||
|
|
||
| public void nominalRuntimeExceptionDependant_2(){ | ||
| Object obj = null; | ||
| try { | ||
| obj.toString(); | ||
| } catch (NullPointerException e) { // Noncompliant {{Avoid catching Runtime exceptions : NullPointerException}} | ||
| } | ||
| } | ||
|
|
||
| public void nominalRuntimeExceptionDependant_3(){ | ||
| try { | ||
| int result = 1 / 0; | ||
| } catch (ArithmeticException e) { // Noncompliant {{Avoid catching Runtime exceptions : ArithmeticException}} | ||
| } | ||
| } | ||
|
|
||
| //these exceptions are ok because they aren't runtime exceptions | ||
|
|
||
| public void compliantNonRuntimeException() { | ||
dedece35 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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 compliantIllegalArgumentException() { | ||
| try { | ||
| // some code that may throw an exception | ||
| } catch (IllegalArgumentException e) { | ||
| } | ||
| } | ||
|
|
||
| public void compliantIllegalArgumentExceptionDependant() { | ||
| 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
51 changes: 51 additions & 0 deletions
51
...in/java/org/greencodeinitiative/creedengo/java/checks/AvoidCatchingRuntimeExceptions.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,51 @@ | ||
| /* | ||
| * 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 = "GCI96") | ||
| public class AvoidCatchingRuntimeExceptions extends IssuableSubscriptionVisitor { | ||
|
|
||
| protected static final String MESSAGERULE = "Avoid catching Runtime exceptions"; | ||
| 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)) { | ||
| String caughtExceptionType = catchTree.parameter().type().symbolType().name(); | ||
| String message = MESSAGERULE + " : " + caughtExceptionType; | ||
| reportIssue(tree, message); | ||
| } | ||
| } | ||
| } |
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", | ||
| "GCI96" | ||
| ] | ||
| } | ||
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,91 @@ | ||
| /* | ||
| * 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; | ||
| import java.io.FileNotFoundException; | ||
| import java.io.IOException; | ||
| import java.lang.runtime.RuntimeException; | ||
| import java.lang.ArithmeticException; | ||
| import java.lang.IndexOutOfBoundsException; | ||
| import java.lang.NullPointerException; | ||
| import java.lang.IllegalArgumentException; | ||
| import java.lang.NumberFormatException; | ||
| import java.lang.Integer; | ||
|
|
||
| public class AvoidCatchingRuntimeExceptions { | ||
| public void nominalRuntimeException() { | ||
| try { | ||
| // some code that may throw an exception | ||
| } catch (RuntimeException e) { // Noncompliant {{Avoid catching Runtime exceptions : RuntimeException}} | ||
| } | ||
| } | ||
|
|
||
| public void nominalRuntimeExceptionDependant() { | ||
| int[] array = new int[10]; | ||
| try { | ||
| array[10] = 0; | ||
| } catch (IndexOutOfBoundsException e) { // Noncompliant {{Avoid catching Runtime exceptions : IndexOutOfBoundsException}} | ||
| } | ||
| } | ||
|
|
||
| public void nominalRuntimeExceptionDependant_2(){ | ||
| Object obj = null; | ||
| try { | ||
| obj.toString(); | ||
| } catch (NullPointerException e) { // Noncompliant {{Avoid catching Runtime exceptions : NullPointerException}} | ||
| } | ||
| } | ||
|
|
||
| public void nominalRuntimeExceptionDependant_3(){ | ||
| try { | ||
| int result = 1 / 0; | ||
| } catch (ArithmeticException e) { // Noncompliant {{Avoid catching Runtime exceptions : ArithmeticException}} | ||
| } | ||
| } | ||
|
|
||
| //these exceptions are ok because they aren't runtime exceptions | ||
|
|
||
| public void compliantNonRuntimeException() { | ||
| 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 compliantIllegalArgumentException() { | ||
| try { | ||
| // some code that may throw an exception | ||
| } catch (IllegalArgumentException e) { | ||
| } | ||
| } | ||
|
|
||
| public void compliantIllegalArgumentExceptionDependant() { | ||
| try { | ||
| Integer.parseInt("abc"); | ||
| } catch (NumberFormatException e) { | ||
| } | ||
| } | ||
|
|
||
| } |
33 changes: 33 additions & 0 deletions
33
...ava/org/greencodeinitiative/creedengo/java/checks/AvoidCatchingRuntimeExceptionsTest.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 AvoidCatchingRuntimeExceptionsTest { | ||
|
|
||
| @Test | ||
| void test() { | ||
| CheckVerifier.newVerifier() | ||
| .onFile("src/test/files/AvoidCatchingRuntimeExceptions.java") | ||
| .withCheck(new AvoidCatchingRuntimeExceptions()) | ||
| .verifyIssues(); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please change ID GCI96 to GCI98 in all this PR, because PR green-code-initiative/creedengo-rules-specifications#401 has just been merged