From b35f75815d492e66374abb058fe0838fc66d4d57 Mon Sep 17 00:00:00 2001 From: Maxime DANIEL Date: Tue, 20 May 2025 16:53:22 +0200 Subject: [PATCH 1/8] implement rule "don't catch runtime exceptions" --- .../java/integration/tests/GCIRulesIT.java | 11 ++++ .../checks/DontCatchRuntimeExceptions.java | 65 +++++++++++++++++++ .../creedengo/java/JavaCheckRegistrar.java | 3 +- .../checks/DontCatchRuntimeExceptions.java | 47 ++++++++++++++ .../creedengo/java/creedengo_way_profile.json | 3 +- .../files/DontCatchRuntimeExceptions.java | 65 +++++++++++++++++++ .../DontCatchRuntimeExceptionsTest.java | 33 ++++++++++ 7 files changed, 225 insertions(+), 2 deletions(-) create mode 100644 src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptions.java create mode 100644 src/main/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptions.java create mode 100644 src/test/files/DontCatchRuntimeExceptions.java create mode 100644 src/test/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptionsTest.java diff --git a/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java b/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java index dfd7957c..7ea60304 100644 --- a/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java +++ b/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java @@ -548,4 +548,15 @@ void testGCI94() { checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_1MIN); } + @Test + void testGCI1166() { + String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptions.java"; + String ruleId = "creedengo-java:GCI1166"; + String ruleMsg = "Don't catch RuntimeExceptions"; + int[] startLines = new int[]{27, 35, 43, 50}; + int[] endLines = new int[]{27, 35, 43, 50}; + + checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_1MIN); + } + } diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptions.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptions.java new file mode 100644 index 00000000..35dca748 --- /dev/null +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptions.java @@ -0,0 +1,65 @@ +/* + * 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 . + */ +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}} + } + } + + 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) { + } + } +} diff --git a/src/main/java/org/greencodeinitiative/creedengo/java/JavaCheckRegistrar.java b/src/main/java/org/greencodeinitiative/creedengo/java/JavaCheckRegistrar.java index 791f0cef..13ffe49a 100644 --- a/src/main/java/org/greencodeinitiative/creedengo/java/JavaCheckRegistrar.java +++ b/src/main/java/org/greencodeinitiative/creedengo/java/JavaCheckRegistrar.java @@ -50,7 +50,8 @@ public class JavaCheckRegistrar implements CheckRegistrar { FreeResourcesOfAutoCloseableInterface.class, AvoidMultipleIfElseStatement.class, UseOptionalOrElseGetVsOrElse.class, - MakeNonReassignedVariablesConstants.class + MakeNonReassignedVariablesConstants.class, + DontCatchRuntimeExceptions.class ); /** diff --git a/src/main/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptions.java b/src/main/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptions.java new file mode 100644 index 00000000..153ac82a --- /dev/null +++ b/src/main/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptions.java @@ -0,0 +1,47 @@ +/* + * 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 . + */ +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"; + + @Override + public List nodesToVisit() { + return Collections.singletonList(Kind.CATCH); + } + + @Override + public void visitNode(Tree tree) { + CatchTree catchTree = (CatchTree) tree; + if(catchTree.parameter().type().symbolType().isSubtypeOf(RUNTIME_EXCEPTION)){ + reportIssue(tree, MESSAGERULE); + } + } +} diff --git a/src/main/resources/org/greencodeinitiative/creedengo/java/creedengo_way_profile.json b/src/main/resources/org/greencodeinitiative/creedengo/java/creedengo_way_profile.json index 059bf0f5..54186b68 100644 --- a/src/main/resources/org/greencodeinitiative/creedengo/java/creedengo_way_profile.json +++ b/src/main/resources/org/greencodeinitiative/creedengo/java/creedengo_way_profile.json @@ -18,6 +18,7 @@ "GCI78", "GCI79", "GCI82", - "GCI94" + "GCI94", + "GCI1166" ] } diff --git a/src/test/files/DontCatchRuntimeExceptions.java b/src/test/files/DontCatchRuntimeExceptions.java new file mode 100644 index 00000000..35dca748 --- /dev/null +++ b/src/test/files/DontCatchRuntimeExceptions.java @@ -0,0 +1,65 @@ +/* + * 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 . + */ +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}} + } + } + + 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) { + } + } +} diff --git a/src/test/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptionsTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptionsTest.java new file mode 100644 index 00000000..baed374a --- /dev/null +++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptionsTest.java @@ -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 . + */ +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(); + } + +} From a276ce5a3960093bc9f62f9ad6b7f827748d0ec5 Mon Sep 17 00:00:00 2001 From: Maxime DANIEL Date: Tue, 20 May 2025 17:39:31 +0200 Subject: [PATCH 2/8] exception for IllegalArgumentExceptions --- .../java/checks/DontCatchRuntimeExceptions.java | 17 +++++++++++++++++ .../java/checks/DontCatchRuntimeExceptions.java | 4 +++- src/test/files/DontCatchRuntimeExceptions.java | 17 +++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptions.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptions.java index 35dca748..7f3dc4eb 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptions.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptions.java @@ -62,4 +62,21 @@ public void testCompliant1() { } 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) { + } + } + } diff --git a/src/main/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptions.java b/src/main/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptions.java index 153ac82a..cfcd9587 100644 --- a/src/main/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptions.java +++ b/src/main/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptions.java @@ -31,6 +31,7 @@ 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 nodesToVisit() { @@ -40,7 +41,8 @@ public List nodesToVisit() { @Override public void visitNode(Tree tree) { CatchTree catchTree = (CatchTree) tree; - if(catchTree.parameter().type().symbolType().isSubtypeOf(RUNTIME_EXCEPTION)){ + if(catchTree.parameter().type().symbolType().isSubtypeOf(RUNTIME_EXCEPTION) && + !catchTree.parameter().type().symbolType().isSubtypeOf(ILLEGAL_ARGUMENT_EXCEPTION)) { reportIssue(tree, MESSAGERULE); } } diff --git a/src/test/files/DontCatchRuntimeExceptions.java b/src/test/files/DontCatchRuntimeExceptions.java index 35dca748..7f3dc4eb 100644 --- a/src/test/files/DontCatchRuntimeExceptions.java +++ b/src/test/files/DontCatchRuntimeExceptions.java @@ -62,4 +62,21 @@ public void testCompliant1() { } 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) { + } + } + } From 3c078b8a566a16a248f21ed375fee03d8bd38bb6 Mon Sep 17 00:00:00 2001 From: Maxime DANIEL Date: Wed, 21 May 2025 13:03:07 +0200 Subject: [PATCH 3/8] add changelog, rename rule, correct compilation --- CHANGELOG.md | 2 +- .../java/integration/tests/GCIRulesIT.java | 8 ++--- ...tions.java => AvoidRuntimeExceptions.java} | 32 ++++++++++++------- .../creedengo/java/JavaCheckRegistrar.java | 2 +- ...tions.java => AvoidRuntimeExceptions.java} | 4 +-- ...tions.java => AvoidRuntimeExceptions.java} | 32 ++++++++++++------- ...t.java => AvoidRuntimeExceptionsTest.java} | 6 ++-- 7 files changed, 51 insertions(+), 35 deletions(-) rename src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/{DontCatchRuntimeExceptions.java => AvoidRuntimeExceptions.java} (66%) rename src/main/java/org/greencodeinitiative/creedengo/java/checks/{DontCatchRuntimeExceptions.java => AvoidRuntimeExceptions.java} (92%) rename src/test/files/{DontCatchRuntimeExceptions.java => AvoidRuntimeExceptions.java} (66%) rename src/test/java/org/greencodeinitiative/creedengo/java/checks/{DontCatchRuntimeExceptionsTest.java => AvoidRuntimeExceptionsTest.java} (86%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 23418684..f44c7107 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added ### Changed - +- [#106](https://github.com/green-code-initiative/creedengo-java/pull/106) Add new Java rule GCI1166 - Avoid Runtime exceptions - compatibility updates for SonarQube 25.5.0 - upgrade libraries versions - correction of technical problem with Integration tests (because of Maven format in technical answer to "sonar-orchestrator-junit5" library) diff --git a/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java b/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java index 7ea60304..3ebfe5d4 100644 --- a/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java +++ b/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java @@ -550,11 +550,11 @@ void testGCI94() { @Test void testGCI1166() { - String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptions.java"; + String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java"; String ruleId = "creedengo-java:GCI1166"; - String ruleMsg = "Don't catch RuntimeExceptions"; - int[] startLines = new int[]{27, 35, 43, 50}; - int[] endLines = new int[]{27, 35, 43, 50}; + String ruleMsg = "Avoid Runtime exceptions"; + int[] startLines = new int[]{35, 43, 51, 58}; + int[] endLines = new int[]{35, 43, 51, 58}; checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_1MIN); } diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptions.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java similarity index 66% rename from src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptions.java rename to src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java index 7f3dc4eb..5fe53185 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptions.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java @@ -19,41 +19,49 @@ 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; -public class DontCatchRuntimeExceptions { - public void test1() { +public class AvoidRuntimeExceptions { + public void nominalRuntimeException() { try { // some code that may throw an exception - } catch (RuntimeException e) { // Noncompliant {{Don't catch RuntimeExceptions}} + } catch (RuntimeException e) { // Noncompliant {{Avoid Runtime exceptions}} } } - public void test2() { + public void nominalRuntimeExceptionDependant() { int[] array = new int[10]; try { array[10] = 0; - } catch (IndexOutOfBoundsException e) { // Noncompliant {{Don't catch RuntimeExceptions}} + } catch (IndexOutOfBoundsException e) { // Noncompliant {{Avoid Runtime exceptions}} } } - public void test3(){ + public void nominalRuntimeExceptionDependant_2(){ Object obj = null; try { obj.toString(); - } catch (NullPointerException e) { // Noncompliant {{Don't catch RuntimeExceptions}} + } catch (NullPointerException e) { // Noncompliant {{Avoid Runtime exceptions}} } } - public void test4(){ + public void nominalRuntimeExceptionDependant_3(){ try { int result = 1 / 0; - } catch (ArithmeticException e) { // Noncompliant {{Don't catch RuntimeExceptions}} + } catch (ArithmeticException e) { // Noncompliant {{Avoid Runtime exceptions}} } } //these exceptions are ok because they aren't runtime exceptions - public void testCompliant1() { + public void compliantNonRuntimeException() { try { File file = new File("nonexistent.txt"); FileReader fr = new FileReader(file); @@ -65,14 +73,14 @@ public void testCompliant1() { // these exceptions are ok because they are IllegalArgumentExceptions, and can hardly be avoided - public void testCompliant2() { + public void compliantIllegalArgumentException() { try { // some code that may throw an exception } catch (IllegalArgumentException e) { } } - public void testCompliant3() { + public void compliantIllegalArgumentExceptionDependant() { try { Integer.parseInt("abc"); } catch (NumberFormatException e) { diff --git a/src/main/java/org/greencodeinitiative/creedengo/java/JavaCheckRegistrar.java b/src/main/java/org/greencodeinitiative/creedengo/java/JavaCheckRegistrar.java index 13ffe49a..cf3357d9 100644 --- a/src/main/java/org/greencodeinitiative/creedengo/java/JavaCheckRegistrar.java +++ b/src/main/java/org/greencodeinitiative/creedengo/java/JavaCheckRegistrar.java @@ -51,7 +51,7 @@ public class JavaCheckRegistrar implements CheckRegistrar { AvoidMultipleIfElseStatement.class, UseOptionalOrElseGetVsOrElse.class, MakeNonReassignedVariablesConstants.class, - DontCatchRuntimeExceptions.class + AvoidRuntimeExceptions.class ); /** diff --git a/src/main/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptions.java b/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java similarity index 92% rename from src/main/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptions.java rename to src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java index cfcd9587..4508daad 100644 --- a/src/main/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptions.java +++ b/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java @@ -27,9 +27,9 @@ import org.sonar.plugins.java.api.tree.Tree.Kind; @Rule(key = "GCI1166") -public class DontCatchRuntimeExceptions extends IssuableSubscriptionVisitor { +public class AvoidRuntimeExceptions extends IssuableSubscriptionVisitor { - protected static final String MESSAGERULE = "Don't catch RuntimeExceptions"; + protected static final String MESSAGERULE = "Avoid Runtime exceptions"; protected static final String RUNTIME_EXCEPTION = "java.lang.RuntimeException"; protected static final String ILLEGAL_ARGUMENT_EXCEPTION = "java.lang.IllegalArgumentException"; diff --git a/src/test/files/DontCatchRuntimeExceptions.java b/src/test/files/AvoidRuntimeExceptions.java similarity index 66% rename from src/test/files/DontCatchRuntimeExceptions.java rename to src/test/files/AvoidRuntimeExceptions.java index 7f3dc4eb..5fe53185 100644 --- a/src/test/files/DontCatchRuntimeExceptions.java +++ b/src/test/files/AvoidRuntimeExceptions.java @@ -19,41 +19,49 @@ 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; -public class DontCatchRuntimeExceptions { - public void test1() { +public class AvoidRuntimeExceptions { + public void nominalRuntimeException() { try { // some code that may throw an exception - } catch (RuntimeException e) { // Noncompliant {{Don't catch RuntimeExceptions}} + } catch (RuntimeException e) { // Noncompliant {{Avoid Runtime exceptions}} } } - public void test2() { + public void nominalRuntimeExceptionDependant() { int[] array = new int[10]; try { array[10] = 0; - } catch (IndexOutOfBoundsException e) { // Noncompliant {{Don't catch RuntimeExceptions}} + } catch (IndexOutOfBoundsException e) { // Noncompliant {{Avoid Runtime exceptions}} } } - public void test3(){ + public void nominalRuntimeExceptionDependant_2(){ Object obj = null; try { obj.toString(); - } catch (NullPointerException e) { // Noncompliant {{Don't catch RuntimeExceptions}} + } catch (NullPointerException e) { // Noncompliant {{Avoid Runtime exceptions}} } } - public void test4(){ + public void nominalRuntimeExceptionDependant_3(){ try { int result = 1 / 0; - } catch (ArithmeticException e) { // Noncompliant {{Don't catch RuntimeExceptions}} + } catch (ArithmeticException e) { // Noncompliant {{Avoid Runtime exceptions}} } } //these exceptions are ok because they aren't runtime exceptions - public void testCompliant1() { + public void compliantNonRuntimeException() { try { File file = new File("nonexistent.txt"); FileReader fr = new FileReader(file); @@ -65,14 +73,14 @@ public void testCompliant1() { // these exceptions are ok because they are IllegalArgumentExceptions, and can hardly be avoided - public void testCompliant2() { + public void compliantIllegalArgumentException() { try { // some code that may throw an exception } catch (IllegalArgumentException e) { } } - public void testCompliant3() { + public void compliantIllegalArgumentExceptionDependant() { try { Integer.parseInt("abc"); } catch (NumberFormatException e) { diff --git a/src/test/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptionsTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptionsTest.java similarity index 86% rename from src/test/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptionsTest.java rename to src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptionsTest.java index baed374a..e56b9cbe 100644 --- a/src/test/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptionsTest.java +++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptionsTest.java @@ -20,13 +20,13 @@ import org.junit.jupiter.api.Test; import org.sonar.java.checks.verifier.CheckVerifier; -class DontCatchRuntimeExceptionsTest { +class AvoidRuntimeExceptionsTest { @Test void test() { CheckVerifier.newVerifier() - .onFile("src/test/files/DontCatchRuntimeExceptions.java") - .withCheck(new DontCatchRuntimeExceptions()) + .onFile("src/test/files/AvoidRuntimeExceptions.java") + .withCheck(new AvoidRuntimeExceptions()) .verifyIssues(); } From da1a3edcd4e420b0371e25f1b90cb872f8f1cebf Mon Sep 17 00:00:00 2001 From: Maxime DANIEL Date: Wed, 21 May 2025 13:04:53 +0200 Subject: [PATCH 4/8] missing import --- .../creedengo/java/integration/tests/GCIRulesIT.java | 4 ++-- .../creedengo/java/checks/AvoidRuntimeExceptions.java | 1 + src/test/files/AvoidRuntimeExceptions.java | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java b/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java index 3ebfe5d4..d480824a 100644 --- a/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java +++ b/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java @@ -553,8 +553,8 @@ void testGCI1166() { String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java"; String ruleId = "creedengo-java:GCI1166"; String ruleMsg = "Avoid Runtime exceptions"; - int[] startLines = new int[]{35, 43, 51, 58}; - int[] endLines = new int[]{35, 43, 51, 58}; + int[] startLines = new int[]{36, 44, 52, 59}; + int[] endLines = new int[]{36, 44, 52, 59}; checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_1MIN); } diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java index 5fe53185..5bf94164 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java @@ -27,6 +27,7 @@ import java.lang.NullPointerException; import java.lang.IllegalArgumentException; import java.lang.NumberFormatException; +import java.lang.Integer; public class AvoidRuntimeExceptions { public void nominalRuntimeException() { diff --git a/src/test/files/AvoidRuntimeExceptions.java b/src/test/files/AvoidRuntimeExceptions.java index 5fe53185..5bf94164 100644 --- a/src/test/files/AvoidRuntimeExceptions.java +++ b/src/test/files/AvoidRuntimeExceptions.java @@ -27,6 +27,7 @@ import java.lang.NullPointerException; import java.lang.IllegalArgumentException; import java.lang.NumberFormatException; +import java.lang.Integer; public class AvoidRuntimeExceptions { public void nominalRuntimeException() { From d25f001387ffa7cfab004f32896ec72eafad714e Mon Sep 17 00:00:00 2001 From: Maxime DANIEL Date: Wed, 21 May 2025 13:11:29 +0200 Subject: [PATCH 5/8] different message to inform of the exception that needs not to be caught --- .../java/integration/tests/GCIRulesIT.java | 40 +++++++++++++++++-- .../java/checks/AvoidRuntimeExceptions.java | 8 ++-- .../java/checks/AvoidRuntimeExceptions.java | 4 +- src/test/files/AvoidRuntimeExceptions.java | 8 ++-- 4 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java b/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java index d480824a..479b458a 100644 --- a/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java +++ b/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java @@ -549,14 +549,46 @@ void testGCI94() { } @Test - void testGCI1166() { + void testGCI1166_1() { String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java"; String ruleId = "creedengo-java:GCI1166"; - String ruleMsg = "Avoid Runtime exceptions"; - int[] startLines = new int[]{36, 44, 52, 59}; - int[] endLines = new int[]{36, 44, 52, 59}; + String ruleMsg = "Avoid Runtime exceptions : RuntimeException"; + int[] startLines = new int[]{36}; + int[] endLines = new int[]{36}; checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_1MIN); } + @Test + void testGCI1166_2() { + String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java"; + String ruleId = "creedengo-java:GCI1166"; + String ruleMsg = "Avoid Runtime exceptions : IndexOutOfBoundsException"; + int[] startLines = new int[]{44}; + int[] endLines = new int[]{44}; + + checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_1MIN); + } + + @Test + void testGCI1166_3() { + String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java"; + String ruleId = "creedengo-java:GCI1166"; + String ruleMsg = "Avoid Runtime exceptions : NullPointerException"; + int[] startLines = new int[]{52}; + int[] endLines = new int[]{52}; + + checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_1MIN); + } + + @Test + void testGCI1166_4() { + String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java"; + String ruleId = "creedengo-java:GCI1166"; + String ruleMsg = "Avoid Runtime exceptions : ArithmeticException"; + int[] startLines = new int[]{59}; + int[] endLines = new int[]{59}; + + checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_1MIN); + } } diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java index 5bf94164..e9ea80c0 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java @@ -33,7 +33,7 @@ public class AvoidRuntimeExceptions { public void nominalRuntimeException() { try { // some code that may throw an exception - } catch (RuntimeException e) { // Noncompliant {{Avoid Runtime exceptions}} + } catch (RuntimeException e) { // Noncompliant {{Avoid Runtime exceptions : RuntimeException}} } } @@ -41,7 +41,7 @@ public void nominalRuntimeExceptionDependant() { int[] array = new int[10]; try { array[10] = 0; - } catch (IndexOutOfBoundsException e) { // Noncompliant {{Avoid Runtime exceptions}} + } catch (IndexOutOfBoundsException e) { // Noncompliant {{Avoid Runtime exceptions : IndexOutOfBoundsException}} } } @@ -49,14 +49,14 @@ public void nominalRuntimeExceptionDependant_2(){ Object obj = null; try { obj.toString(); - } catch (NullPointerException e) { // Noncompliant {{Avoid Runtime exceptions}} + } catch (NullPointerException e) { // Noncompliant {{Avoid Runtime exceptions : NullPointerException}} } } public void nominalRuntimeExceptionDependant_3(){ try { int result = 1 / 0; - } catch (ArithmeticException e) { // Noncompliant {{Avoid Runtime exceptions}} + } catch (ArithmeticException e) { // Noncompliant {{Avoid Runtime exceptions : ArithmeticException}} } } diff --git a/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java b/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java index 4508daad..d43d12be 100644 --- a/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java +++ b/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java @@ -43,7 +43,9 @@ 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); + String Type = catchTree.parameter().type().symbolType().name(); + String message = MESSAGERULE + " : " + Type; + reportIssue(tree, message); } } } diff --git a/src/test/files/AvoidRuntimeExceptions.java b/src/test/files/AvoidRuntimeExceptions.java index 5bf94164..e9ea80c0 100644 --- a/src/test/files/AvoidRuntimeExceptions.java +++ b/src/test/files/AvoidRuntimeExceptions.java @@ -33,7 +33,7 @@ public class AvoidRuntimeExceptions { public void nominalRuntimeException() { try { // some code that may throw an exception - } catch (RuntimeException e) { // Noncompliant {{Avoid Runtime exceptions}} + } catch (RuntimeException e) { // Noncompliant {{Avoid Runtime exceptions : RuntimeException}} } } @@ -41,7 +41,7 @@ public void nominalRuntimeExceptionDependant() { int[] array = new int[10]; try { array[10] = 0; - } catch (IndexOutOfBoundsException e) { // Noncompliant {{Avoid Runtime exceptions}} + } catch (IndexOutOfBoundsException e) { // Noncompliant {{Avoid Runtime exceptions : IndexOutOfBoundsException}} } } @@ -49,14 +49,14 @@ public void nominalRuntimeExceptionDependant_2(){ Object obj = null; try { obj.toString(); - } catch (NullPointerException e) { // Noncompliant {{Avoid Runtime exceptions}} + } catch (NullPointerException e) { // Noncompliant {{Avoid Runtime exceptions : NullPointerException}} } } public void nominalRuntimeExceptionDependant_3(){ try { int result = 1 / 0; - } catch (ArithmeticException e) { // Noncompliant {{Avoid Runtime exceptions}} + } catch (ArithmeticException e) { // Noncompliant {{Avoid Runtime exceptions : ArithmeticException}} } } From 37c05294b8a762309c396014156663ba1ccce1a1 Mon Sep 17 00:00:00 2001 From: Luc Fouin Date: Wed, 21 May 2025 14:32:56 +0200 Subject: [PATCH 6/8] Fixing tests --- .../java/integration/tests/GCIRulesIT.java | 16 ++++++++-------- .../java/checks/AvoidRuntimeExceptions.java | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java b/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java index 479b458a..b2a43105 100644 --- a/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java +++ b/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java @@ -554,9 +554,9 @@ void testGCI1166_1() { String ruleId = "creedengo-java:GCI1166"; String ruleMsg = "Avoid Runtime exceptions : RuntimeException"; int[] startLines = new int[]{36}; - int[] endLines = new int[]{36}; + int[] endLines = new int[]{37}; - checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_1MIN); + checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_10MIN); } @Test @@ -565,9 +565,9 @@ void testGCI1166_2() { String ruleId = "creedengo-java:GCI1166"; String ruleMsg = "Avoid Runtime exceptions : IndexOutOfBoundsException"; int[] startLines = new int[]{44}; - int[] endLines = new int[]{44}; + int[] endLines = new int[]{45}; - checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_1MIN); + checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_10MIN); } @Test @@ -576,9 +576,9 @@ void testGCI1166_3() { String ruleId = "creedengo-java:GCI1166"; String ruleMsg = "Avoid Runtime exceptions : NullPointerException"; int[] startLines = new int[]{52}; - int[] endLines = new int[]{52}; + int[] endLines = new int[]{53}; - checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_1MIN); + checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_10MIN); } @Test @@ -587,8 +587,8 @@ void testGCI1166_4() { String ruleId = "creedengo-java:GCI1166"; String ruleMsg = "Avoid Runtime exceptions : ArithmeticException"; int[] startLines = new int[]{59}; - int[] endLines = new int[]{59}; + int[] endLines = new int[]{60}; - checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_1MIN); + checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_10MIN); } } diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java index e9ea80c0..62502f78 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java @@ -21,7 +21,7 @@ import java.io.FileReader; import java.io.FileNotFoundException; import java.io.IOException; -import java.lang.runtime.RuntimeException; +import java.lang.RuntimeException; import java.lang.ArithmeticException; import java.lang.IndexOutOfBoundsException; import java.lang.NullPointerException; @@ -87,5 +87,5 @@ public void compliantIllegalArgumentExceptionDependant() { } catch (NumberFormatException e) { } } - + } From a32580fce1dd5267c6b9e2d7e47fcc96454dc056 Mon Sep 17 00:00:00 2001 From: Maxime DANIEL Date: Thu, 22 May 2025 09:29:04 +0200 Subject: [PATCH 7/8] change number tot GCI96 --- CHANGELOG.md | 2 +- .../java/integration/tests/GCIRulesIT.java | 16 ++++++++-------- .../java/checks/AvoidRuntimeExceptions.java | 2 +- .../creedengo/java/creedengo_way_profile.json | 2 +- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f44c7107..fdd2e474 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added ### Changed -- [#106](https://github.com/green-code-initiative/creedengo-java/pull/106) Add new Java rule GCI1166 - Avoid Runtime exceptions +- [#106](https://github.com/green-code-initiative/creedengo-java/pull/106) Add new Java rule GCI96 - Avoid Runtime exceptions - compatibility updates for SonarQube 25.5.0 - upgrade libraries versions - correction of technical problem with Integration tests (because of Maven format in technical answer to "sonar-orchestrator-junit5" library) diff --git a/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java b/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java index b2a43105..eed6a282 100644 --- a/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java +++ b/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java @@ -549,9 +549,9 @@ void testGCI94() { } @Test - void testGCI1166_1() { + void testGCI96_1() { String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java"; - String ruleId = "creedengo-java:GCI1166"; + String ruleId = "creedengo-java:GCI96"; String ruleMsg = "Avoid Runtime exceptions : RuntimeException"; int[] startLines = new int[]{36}; int[] endLines = new int[]{37}; @@ -560,9 +560,9 @@ void testGCI1166_1() { } @Test - void testGCI1166_2() { + void testGCI96_2() { String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java"; - String ruleId = "creedengo-java:GCI1166"; + String ruleId = "creedengo-java:GCI96"; String ruleMsg = "Avoid Runtime exceptions : IndexOutOfBoundsException"; int[] startLines = new int[]{44}; int[] endLines = new int[]{45}; @@ -571,9 +571,9 @@ void testGCI1166_2() { } @Test - void testGCI1166_3() { + void testGCI96_3() { String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java"; - String ruleId = "creedengo-java:GCI1166"; + String ruleId = "creedengo-java:GCI96"; String ruleMsg = "Avoid Runtime exceptions : NullPointerException"; int[] startLines = new int[]{52}; int[] endLines = new int[]{53}; @@ -582,9 +582,9 @@ void testGCI1166_3() { } @Test - void testGCI1166_4() { + void testGCI96_4() { String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java"; - String ruleId = "creedengo-java:GCI1166"; + String ruleId = "creedengo-java:GCI96"; String ruleMsg = "Avoid Runtime exceptions : ArithmeticException"; int[] startLines = new int[]{59}; int[] endLines = new int[]{60}; diff --git a/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java b/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java index d43d12be..b08698bd 100644 --- a/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java +++ b/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java @@ -26,7 +26,7 @@ import org.sonar.plugins.java.api.tree.Tree; import org.sonar.plugins.java.api.tree.Tree.Kind; -@Rule(key = "GCI1166") +@Rule(key = "GCI96") public class AvoidRuntimeExceptions extends IssuableSubscriptionVisitor { protected static final String MESSAGERULE = "Avoid Runtime exceptions"; diff --git a/src/main/resources/org/greencodeinitiative/creedengo/java/creedengo_way_profile.json b/src/main/resources/org/greencodeinitiative/creedengo/java/creedengo_way_profile.json index 54186b68..3ed057d5 100644 --- a/src/main/resources/org/greencodeinitiative/creedengo/java/creedengo_way_profile.json +++ b/src/main/resources/org/greencodeinitiative/creedengo/java/creedengo_way_profile.json @@ -19,6 +19,6 @@ "GCI79", "GCI82", "GCI94", - "GCI1166" + "GCI96" ] } From 99ed057cc740b731ea9bc2775f2542cf0d04db09 Mon Sep 17 00:00:00 2001 From: Maxime DANIEL Date: Mon, 7 Jul 2025 14:18:15 +0200 Subject: [PATCH 8/8] fixed rule name, error message, changelog, naming conventions --- CHANGELOG.md | 2 +- .../java/integration/tests/GCIRulesIT.java | 24 +++++++++---------- .../java/checks/AvoidRuntimeExceptions.java | 8 +++---- .../creedengo/java/JavaCheckRegistrar.java | 2 +- ...va => AvoidCatchingRuntimeExceptions.java} | 8 +++---- ...va => AvoidCatchingRuntimeExceptions.java} | 10 ++++---- ...> AvoidCatchingRuntimeExceptionsTest.java} | 6 ++--- 7 files changed, 30 insertions(+), 30 deletions(-) rename src/main/java/org/greencodeinitiative/creedengo/java/checks/{AvoidRuntimeExceptions.java => AvoidCatchingRuntimeExceptions.java} (84%) rename src/test/files/{AvoidRuntimeExceptions.java => AvoidCatchingRuntimeExceptions.java} (89%) rename src/test/java/org/greencodeinitiative/creedengo/java/checks/{AvoidRuntimeExceptionsTest.java => AvoidCatchingRuntimeExceptionsTest.java} (85%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b712b78..51ba8d8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,9 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added +- [#106](https://github.com/green-code-initiative/creedengo-java/pull/106) Add new Java rule GCI96 - Avoid Catching Runtime exceptions ### Changed -- [#106](https://github.com/green-code-initiative/creedengo-java/pull/106) Add new Java rule GCI96 - Avoid Runtime exceptions - compatibility updates for SonarQube 25.5.0 - upgrade libraries versions - correction of technical problem with Integration tests (because of Maven format in technical answer to "sonar-orchestrator-junit5" library) diff --git a/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java b/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java index ff04c7dd..52b45858 100644 --- a/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java +++ b/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java @@ -549,10 +549,10 @@ void testGCI94() { } @Test - void testGCI96_1() { - String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java"; + void testGCI96_nominalRuntimeException() { + String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidCatchingRuntimeExceptions.java"; String ruleId = "creedengo-java:GCI96"; - String ruleMsg = "Avoid Runtime exceptions : RuntimeException"; + String ruleMsg = "Avoid catching Runtime exceptions : RuntimeException"; int[] startLines = new int[]{36}; int[] endLines = new int[]{37}; @@ -560,10 +560,10 @@ void testGCI96_1() { } @Test - void testGCI96_2() { - String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java"; + void testGCI96_IndexOutOfBoundsException() { + String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidCatchingRuntimeExceptions.java"; String ruleId = "creedengo-java:GCI96"; - String ruleMsg = "Avoid Runtime exceptions : IndexOutOfBoundsException"; + String ruleMsg = "Avoid catching Runtime exceptions : IndexOutOfBoundsException"; int[] startLines = new int[]{44}; int[] endLines = new int[]{45}; @@ -571,10 +571,10 @@ void testGCI96_2() { } @Test - void testGCI96_3() { - String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java"; + void testGCI96_NullPointerException() { + String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidCatchingRuntimeExceptions.java"; String ruleId = "creedengo-java:GCI96"; - String ruleMsg = "Avoid Runtime exceptions : NullPointerException"; + String ruleMsg = "Avoid catching Runtime exceptions : NullPointerException"; int[] startLines = new int[]{52}; int[] endLines = new int[]{53}; @@ -582,10 +582,10 @@ void testGCI96_3() { } @Test - void testGCI96_4() { - String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java"; + void testGCI96_ArithmeticException() { + String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidCatchingRuntimeExceptions.java"; String ruleId = "creedengo-java:GCI96"; - String ruleMsg = "Avoid Runtime exceptions : ArithmeticException"; + String ruleMsg = "Avoid catching Runtime exceptions : ArithmeticException"; int[] startLines = new int[]{59}; int[] endLines = new int[]{60}; diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java index 62502f78..647e6075 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java @@ -33,7 +33,7 @@ public class AvoidRuntimeExceptions { public void nominalRuntimeException() { try { // some code that may throw an exception - } catch (RuntimeException e) { // Noncompliant {{Avoid Runtime exceptions : RuntimeException}} + } catch (RuntimeException e) { // Noncompliant {{Avoid catching Runtime exceptions : RuntimeException}} } } @@ -41,7 +41,7 @@ public void nominalRuntimeExceptionDependant() { int[] array = new int[10]; try { array[10] = 0; - } catch (IndexOutOfBoundsException e) { // Noncompliant {{Avoid Runtime exceptions : IndexOutOfBoundsException}} + } catch (IndexOutOfBoundsException e) { // Noncompliant {{Avoid catching Runtime exceptions : IndexOutOfBoundsException}} } } @@ -49,14 +49,14 @@ public void nominalRuntimeExceptionDependant_2(){ Object obj = null; try { obj.toString(); - } catch (NullPointerException e) { // Noncompliant {{Avoid Runtime exceptions : NullPointerException}} + } catch (NullPointerException e) { // Noncompliant {{Avoid catching Runtime exceptions : NullPointerException}} } } public void nominalRuntimeExceptionDependant_3(){ try { int result = 1 / 0; - } catch (ArithmeticException e) { // Noncompliant {{Avoid Runtime exceptions : ArithmeticException}} + } catch (ArithmeticException e) { // Noncompliant {{Avoid catching Runtime exceptions : ArithmeticException}} } } diff --git a/src/main/java/org/greencodeinitiative/creedengo/java/JavaCheckRegistrar.java b/src/main/java/org/greencodeinitiative/creedengo/java/JavaCheckRegistrar.java index cf3357d9..e0566553 100644 --- a/src/main/java/org/greencodeinitiative/creedengo/java/JavaCheckRegistrar.java +++ b/src/main/java/org/greencodeinitiative/creedengo/java/JavaCheckRegistrar.java @@ -51,7 +51,7 @@ public class JavaCheckRegistrar implements CheckRegistrar { AvoidMultipleIfElseStatement.class, UseOptionalOrElseGetVsOrElse.class, MakeNonReassignedVariablesConstants.class, - AvoidRuntimeExceptions.class + AvoidCatchingRuntimeExceptions.class ); /** diff --git a/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java b/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidCatchingRuntimeExceptions.java similarity index 84% rename from src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java rename to src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidCatchingRuntimeExceptions.java index b08698bd..e7f82b7c 100644 --- a/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptions.java +++ b/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidCatchingRuntimeExceptions.java @@ -27,9 +27,9 @@ import org.sonar.plugins.java.api.tree.Tree.Kind; @Rule(key = "GCI96") -public class AvoidRuntimeExceptions extends IssuableSubscriptionVisitor { +public class AvoidCatchingRuntimeExceptions extends IssuableSubscriptionVisitor { - protected static final String MESSAGERULE = "Avoid Runtime exceptions"; + 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"; @@ -43,8 +43,8 @@ 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 Type = catchTree.parameter().type().symbolType().name(); - String message = MESSAGERULE + " : " + Type; + String caughtExceptionType = catchTree.parameter().type().symbolType().name(); + String message = MESSAGERULE + " : " + caughtExceptionType; reportIssue(tree, message); } } diff --git a/src/test/files/AvoidRuntimeExceptions.java b/src/test/files/AvoidCatchingRuntimeExceptions.java similarity index 89% rename from src/test/files/AvoidRuntimeExceptions.java rename to src/test/files/AvoidCatchingRuntimeExceptions.java index e9ea80c0..82c0f4e0 100644 --- a/src/test/files/AvoidRuntimeExceptions.java +++ b/src/test/files/AvoidCatchingRuntimeExceptions.java @@ -29,11 +29,11 @@ import java.lang.NumberFormatException; import java.lang.Integer; -public class AvoidRuntimeExceptions { +public class AvoidCatchingRuntimeExceptions { public void nominalRuntimeException() { try { // some code that may throw an exception - } catch (RuntimeException e) { // Noncompliant {{Avoid Runtime exceptions : RuntimeException}} + } catch (RuntimeException e) { // Noncompliant {{Avoid catching Runtime exceptions : RuntimeException}} } } @@ -41,7 +41,7 @@ public void nominalRuntimeExceptionDependant() { int[] array = new int[10]; try { array[10] = 0; - } catch (IndexOutOfBoundsException e) { // Noncompliant {{Avoid Runtime exceptions : IndexOutOfBoundsException}} + } catch (IndexOutOfBoundsException e) { // Noncompliant {{Avoid catching Runtime exceptions : IndexOutOfBoundsException}} } } @@ -49,14 +49,14 @@ public void nominalRuntimeExceptionDependant_2(){ Object obj = null; try { obj.toString(); - } catch (NullPointerException e) { // Noncompliant {{Avoid Runtime exceptions : NullPointerException}} + } catch (NullPointerException e) { // Noncompliant {{Avoid catching Runtime exceptions : NullPointerException}} } } public void nominalRuntimeExceptionDependant_3(){ try { int result = 1 / 0; - } catch (ArithmeticException e) { // Noncompliant {{Avoid Runtime exceptions : ArithmeticException}} + } catch (ArithmeticException e) { // Noncompliant {{Avoid catching Runtime exceptions : ArithmeticException}} } } diff --git a/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptionsTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidCatchingRuntimeExceptionsTest.java similarity index 85% rename from src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptionsTest.java rename to src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidCatchingRuntimeExceptionsTest.java index e56b9cbe..923f91af 100644 --- a/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidRuntimeExceptionsTest.java +++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidCatchingRuntimeExceptionsTest.java @@ -20,13 +20,13 @@ import org.junit.jupiter.api.Test; import org.sonar.java.checks.verifier.CheckVerifier; -class AvoidRuntimeExceptionsTest { +class AvoidCatchingRuntimeExceptionsTest { @Test void test() { CheckVerifier.newVerifier() - .onFile("src/test/files/AvoidRuntimeExceptions.java") - .withCheck(new AvoidRuntimeExceptions()) + .onFile("src/test/files/AvoidCatchingRuntimeExceptions.java") + .withCheck(new AvoidCatchingRuntimeExceptions()) .verifyIssues(); }