variablesParentLevelMap = pDataMap.get(i);
- nbParentUsed = variablesParentLevelMap.get(variableName);
+ if (variablesParentLevelMap != null) {
+ nbParentUsed = variablesParentLevelMap.get(variableName);
+ }
}
return nbParentUsed;
diff --git a/src/main/java/fr/greencodeinitiative/java/checks/AvoidRegexPatternNotStatic.java b/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRegexPatternNotStatic.java
similarity index 88%
rename from src/main/java/fr/greencodeinitiative/java/checks/AvoidRegexPatternNotStatic.java
rename to src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRegexPatternNotStatic.java
index 2561a6c0..beb1cea6 100644
--- a/src/main/java/fr/greencodeinitiative/java/checks/AvoidRegexPatternNotStatic.java
+++ b/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRegexPatternNotStatic.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.checks;
+package org.greencodeinitiative.creedengo.java.checks;
import java.util.Collections;
import java.util.List;
@@ -32,7 +32,8 @@
import org.sonar.plugins.java.api.tree.Tree;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;
-@Rule(key = "EC77")
+@Rule(key = "GCI77")
+@DeprecatedRuleKey(repositoryKey = "ecocode-java", ruleKey = "EC77")
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S77")
public class AvoidRegexPatternNotStatic extends IssuableSubscriptionVisitor {
diff --git a/src/main/java/fr/greencodeinitiative/java/checks/AvoidSQLRequestInLoop.java b/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidSQLRequestInLoop.java
similarity index 92%
rename from src/main/java/fr/greencodeinitiative/java/checks/AvoidSQLRequestInLoop.java
rename to src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidSQLRequestInLoop.java
index 1e3aab82..0e830dcf 100644
--- a/src/main/java/fr/greencodeinitiative/java/checks/AvoidSQLRequestInLoop.java
+++ b/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidSQLRequestInLoop.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.checks;
+package org.greencodeinitiative.creedengo.java.checks;
import java.util.Arrays;
import java.util.List;
@@ -30,7 +30,8 @@
import org.sonar.plugins.java.api.tree.Tree.Kind;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;
-@Rule(key = "EC72")
+@Rule(key = "GCI72")
+@DeprecatedRuleKey(repositoryKey = "ecocode-java", ruleKey = "EC72")
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S72")
public class AvoidSQLRequestInLoop extends IssuableSubscriptionVisitor {
diff --git a/src/main/java/fr/greencodeinitiative/java/checks/AvoidSetConstantInBatchUpdate.java b/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidSetConstantInBatchUpdate.java
similarity index 82%
rename from src/main/java/fr/greencodeinitiative/java/checks/AvoidSetConstantInBatchUpdate.java
rename to src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidSetConstantInBatchUpdate.java
index 4fe31abd..76f9ed51 100644
--- a/src/main/java/fr/greencodeinitiative/java/checks/AvoidSetConstantInBatchUpdate.java
+++ b/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidSetConstantInBatchUpdate.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,14 +15,14 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.checks;
+package org.greencodeinitiative.creedengo.java.checks;
import java.sql.PreparedStatement;
import java.util.List;
import java.util.stream.Stream;
-import fr.greencodeinitiative.java.checks.enums.ConstOrLiteralDeclare;
-import static fr.greencodeinitiative.java.checks.enums.ConstOrLiteralDeclare.isLiteral;
+import org.greencodeinitiative.creedengo.java.checks.enums.ConstOrLiteralDeclare;
+import static org.greencodeinitiative.creedengo.java.checks.enums.ConstOrLiteralDeclare.isLiteral;
import static java.util.Arrays.asList;
import org.sonar.check.Rule;
@@ -39,11 +39,12 @@
import static org.sonar.plugins.java.api.tree.Tree.Kind.MEMBER_SELECT;
import static org.sonar.plugins.java.api.tree.Tree.Kind.METHOD_INVOCATION;
-@Rule(key = "EC78")
+@Rule(key = "GCI78")
+@DeprecatedRuleKey(repositoryKey = "ecocode-java", ruleKey = "EC78")
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S78")
public class AvoidSetConstantInBatchUpdate extends IssuableSubscriptionVisitor {
- protected static final String MESSAGERULE = "Avoid setting constants in batch update";
+ protected static final String MESSAGE_RULE = "Avoid setting constants in batch update";
private final AvoidSetConstantInBatchUpdateVisitor visitorInFile = new AvoidSetConstantInBatchUpdateVisitor();
@Override
@@ -70,14 +71,14 @@ private class AvoidSetConstantInBatchUpdateVisitor extends BaseTreeVisitor {
@Override
public void visitMethodInvocation(MethodInvocationTree tree) {
if (setters.matches(tree) && isConstant(tree.arguments().get(1))) {
- reportIssue(tree, MESSAGERULE);
+ reportIssue(tree, MESSAGE_RULE);
} else {
super.visitMethodInvocation(tree);
}
}
}
- private static final boolean isConstant(Tree arg) {
+ private static boolean isConstant(Tree arg) {
if (arg.is(METHOD_INVOCATION)) {
MethodInvocationTree m = (MethodInvocationTree) arg;
diff --git a/src/main/java/fr/greencodeinitiative/java/checks/AvoidSpringRepositoryCallInLoopOrStreamCheck.java b/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidSpringRepositoryCallInLoopOrStreamCheck.java
similarity index 93%
rename from src/main/java/fr/greencodeinitiative/java/checks/AvoidSpringRepositoryCallInLoopOrStreamCheck.java
rename to src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidSpringRepositoryCallInLoopOrStreamCheck.java
index 3222799c..a71777c0 100644
--- a/src/main/java/fr/greencodeinitiative/java/checks/AvoidSpringRepositoryCallInLoopOrStreamCheck.java
+++ b/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidSpringRepositoryCallInLoopOrStreamCheck.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.checks;
+package org.greencodeinitiative.creedengo.java.checks;
import java.util.Arrays;
import java.util.List;
@@ -26,7 +26,8 @@
import org.sonar.plugins.java.api.tree.*;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;
-@Rule(key = "EC1")
+@Rule(key = "GCI1")
+@DeprecatedRuleKey(repositoryKey = "ecocode-java", ruleKey = "EC1")
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "GRC1")
public class AvoidSpringRepositoryCallInLoopOrStreamCheck extends IssuableSubscriptionVisitor {
diff --git a/src/main/java/fr/greencodeinitiative/java/checks/AvoidStatementForDMLQueries.java b/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidStatementForDMLQueries.java
similarity index 88%
rename from src/main/java/fr/greencodeinitiative/java/checks/AvoidStatementForDMLQueries.java
rename to src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidStatementForDMLQueries.java
index 592f92cb..cb335e04 100644
--- a/src/main/java/fr/greencodeinitiative/java/checks/AvoidStatementForDMLQueries.java
+++ b/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidStatementForDMLQueries.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.checks;
+package org.greencodeinitiative.creedengo.java.checks;
import java.util.Collections;
import java.util.List;
@@ -31,7 +31,8 @@
import org.sonar.plugins.java.api.tree.Tree;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;
-@Rule(key = "EC5")
+@Rule(key = "GCI5")
+@DeprecatedRuleKey(repositoryKey = "ecocode-java", ruleKey = "EC5")
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "SDMLQ1")
public class AvoidStatementForDMLQueries extends IssuableSubscriptionVisitor {
diff --git a/src/main/java/fr/greencodeinitiative/java/checks/AvoidUsageOfStaticCollections.java b/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidUsageOfStaticCollections.java
similarity index 87%
rename from src/main/java/fr/greencodeinitiative/java/checks/AvoidUsageOfStaticCollections.java
rename to src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidUsageOfStaticCollections.java
index 18a7fb7f..8017381a 100644
--- a/src/main/java/fr/greencodeinitiative/java/checks/AvoidUsageOfStaticCollections.java
+++ b/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidUsageOfStaticCollections.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.checks;
+package org.greencodeinitiative.creedengo.java.checks;
import java.util.Collections;
import java.util.List;
@@ -30,7 +30,8 @@
import org.sonar.plugins.java.api.tree.VariableTree;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;
-@Rule(key = "EC76")
+@Rule(key = "GCI76")
+@DeprecatedRuleKey(repositoryKey = "ecocode-java", ruleKey = "EC76")
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S76")
public class AvoidUsageOfStaticCollections extends IssuableSubscriptionVisitor {
diff --git a/src/main/java/org/greencodeinitiative/creedengo/java/checks/FreeResourcesOfAutoCloseableInterface.java b/src/main/java/org/greencodeinitiative/creedengo/java/checks/FreeResourcesOfAutoCloseableInterface.java
new file mode 100644
index 00000000..2fc5c542
--- /dev/null
+++ b/src/main/java/org/greencodeinitiative/creedengo/java/checks/FreeResourcesOfAutoCloseableInterface.java
@@ -0,0 +1,162 @@
+/*
+ * 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.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Deque;
+import java.util.List;
+
+import javax.annotation.Nonnull;
+import javax.annotation.ParametersAreNonnullByDefault;
+
+import org.sonar.check.Rule;
+import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
+import org.sonar.plugins.java.api.JavaFileScannerContext;
+import org.sonar.plugins.java.api.tree.NewClassTree;
+import org.sonar.plugins.java.api.tree.Tree;
+import org.sonar.plugins.java.api.tree.TryStatementTree;
+import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;
+
+/**
+ * This rule checks that objects implementing AutoCloseable interface are properly managed
+ * using try-with-resources statement instead of try-finally blocks.
+ *
+ * Try-with-resources ensures proper resource management and reduces the risk of resource leaks.
+ * It also reduces boilerplate code and improves code readability.
+ *
+ * From an environmental perspective, proper resource management prevents resource leaks
+ * which can lead to increased memory consumption and unnecessary CPU cycles.
+ *
+ * @see Try-with-resources
+ */
+@Rule(key = "GCI79")
+@DeprecatedRuleKey(repositoryKey = "ecocode-java", ruleKey = "EC79")
+@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S79")
+public class FreeResourcesOfAutoCloseableInterface extends IssuableSubscriptionVisitor {
+
+ /**
+ * Stack to track nested try statements while traversing the AST
+ */
+ private final Deque tryStack = new ArrayDeque<>();
+
+ private static final String JAVA_LANG_AUTOCLOSEABLE = "java.lang.AutoCloseable";
+ protected static final String MESSAGE_RULE = "try-with-resources Statement needs to be implemented for any object that implements the AutoCloseable interface.";
+
+ @Override
+ @ParametersAreNonnullByDefault
+ public void leaveFile(JavaFileScannerContext context) {
+ tryStack.clear();
+ }
+
+ @Override
+ @Nonnull
+ public List nodesToVisit() {
+ return List.of(Tree.Kind.TRY_STATEMENT, Tree.Kind.NEW_CLASS);
+ }
+
+ @Override
+ public void visitNode(@Nonnull Tree tree) {
+ if (tree.is(Tree.Kind.TRY_STATEMENT)) {
+ handleTryStatement((TryStatementTree) tree);
+ } else if (tree.is(Tree.Kind.NEW_CLASS)) {
+ handleNewClass((NewClassTree) tree);
+ }
+ }
+
+ @Override
+ public void leaveNode(@Nonnull Tree tree) {
+ if (tree.is(Tree.Kind.TRY_STATEMENT)) {
+ leaveTryStatement();
+ }
+ }
+
+ /**
+ * Handle entering a try statement by pushing it onto the stack
+ */
+ private void handleTryStatement(@Nonnull TryStatementTree tryStatement) {
+ tryStack.push(new TryStatementContext(tryStatement));
+ }
+
+ /**
+ * Handle leaving a try statement by popping it from the stack and reporting issues if needed
+ */
+ private void leaveTryStatement() {
+ if (!tryStack.isEmpty()) {
+ TryStatementContext context = tryStack.pop();
+ if (!context.autoCloseableInstances.isEmpty()) {
+ reportIssue(context.tryStatement, MESSAGE_RULE);
+ }
+ }
+ }
+
+ /**
+ * Handle new class instantiation to detect AutoCloseable objects
+ * that are created inside a try-finally block (without try-with-resources)
+ */
+ private void handleNewClass(@Nonnull NewClassTree newClass) {
+ // Check if the new instance is an AutoCloseable
+ if (!newClass.symbolType().isSubtypeOf(JAVA_LANG_AUTOCLOSEABLE)) {
+ return;
+ }
+
+ // Check if we are inside a non-compliant try statement
+ if (isInNonCompliantTry()) {
+ TryStatementContext context = tryStack.peek();
+ if (context != null) {
+ context.autoCloseableInstances.add(newClass);
+ }
+ }
+ }
+
+ /**
+ * Check if we are currently inside a try statement that:
+ * - Does NOT use try-with-resources (no resource list)
+ * - Has a finally block (indicating manual resource management)
+ *
+ * @return true if inside a non-compliant try statement
+ */
+ private boolean isInNonCompliantTry() {
+ if (tryStack.isEmpty()) {
+ return false;
+ }
+
+ TryStatementTree currentTry = tryStack.peek().tryStatement;
+
+ // If try-with-resources is already used, it's compliant
+ if (!currentTry.resourceList().isEmpty()) {
+ return false;
+ }
+
+ // If there's a finally block, it suggests manual resource management
+ return currentTry.finallyBlock() != null;
+ }
+
+ /**
+ * Context class to track information about a try statement during AST traversal
+ */
+ private static class TryStatementContext {
+ final TryStatementTree tryStatement;
+ final List autoCloseableInstances;
+
+ TryStatementContext(@Nonnull TryStatementTree tryStatement) {
+ this.tryStatement = tryStatement;
+ this.autoCloseableInstances = new ArrayList<>();
+ }
+ }
+}
diff --git a/src/main/java/fr/greencodeinitiative/java/checks/IncrementCheck.java b/src/main/java/org/greencodeinitiative/creedengo/java/checks/IncrementCheck.java
similarity index 63%
rename from src/main/java/fr/greencodeinitiative/java/checks/IncrementCheck.java
rename to src/main/java/org/greencodeinitiative/creedengo/java/checks/IncrementCheck.java
index 00c3094d..c418ad6d 100644
--- a/src/main/java/fr/greencodeinitiative/java/checks/IncrementCheck.java
+++ b/src/main/java/org/greencodeinitiative/creedengo/java/checks/IncrementCheck.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,18 +15,22 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.checks;
+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.Arguments;
+import org.sonar.plugins.java.api.tree.BinaryExpressionTree;
import org.sonar.plugins.java.api.tree.Tree;
+import org.sonar.plugins.java.api.tree.UnaryExpressionTree;
import org.sonar.plugins.java.api.tree.Tree.Kind;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;
-@Rule(key = "EC67")
+@Rule(key = "GCI67")
+@DeprecatedRuleKey(repositoryKey = "ecocode-java", ruleKey = "EC67")
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S67")
public class IncrementCheck extends IssuableSubscriptionVisitor {
@@ -39,6 +43,13 @@ public List nodesToVisit() {
@Override
public void visitNode(Tree tree) {
+ UnaryExpressionTree unaryExprTree = (UnaryExpressionTree) tree;
+
+ if (unaryExprTree.parent() instanceof BinaryExpressionTree
+ || unaryExprTree.parent() instanceof Arguments
+ || unaryExprTree.expression().is(Tree.Kind.MEMBER_SELECT)) {
+ return ;
+ }
reportIssue(tree, MESSAGERULE);
}
}
diff --git a/src/main/java/fr/greencodeinitiative/java/checks/InitializeBufferWithAppropriateSize.java b/src/main/java/org/greencodeinitiative/creedengo/java/checks/InitializeBufferWithAppropriateSize.java
similarity index 84%
rename from src/main/java/fr/greencodeinitiative/java/checks/InitializeBufferWithAppropriateSize.java
rename to src/main/java/org/greencodeinitiative/creedengo/java/checks/InitializeBufferWithAppropriateSize.java
index 44caa872..7bcb3b73 100644
--- a/src/main/java/fr/greencodeinitiative/java/checks/InitializeBufferWithAppropriateSize.java
+++ b/src/main/java/org/greencodeinitiative/creedengo/java/checks/InitializeBufferWithAppropriateSize.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.checks;
+package org.greencodeinitiative.creedengo.java.checks;
import java.util.Collections;
import java.util.List;
@@ -27,7 +27,8 @@
import org.sonar.plugins.java.api.tree.Tree.Kind;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;
-@Rule(key = "EC32")
+@Rule(key = "GCI32")
+@DeprecatedRuleKey(repositoryKey = "ecocode-java", ruleKey = "EC32")
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "GRSP0032")
public class InitializeBufferWithAppropriateSize extends IssuableSubscriptionVisitor {
diff --git a/src/main/java/org/greencodeinitiative/creedengo/java/checks/MakeNonReassignedVariablesConstants.java b/src/main/java/org/greencodeinitiative/creedengo/java/checks/MakeNonReassignedVariablesConstants.java
new file mode 100644
index 00000000..64fbe177
--- /dev/null
+++ b/src/main/java/org/greencodeinitiative/creedengo/java/checks/MakeNonReassignedVariablesConstants.java
@@ -0,0 +1,316 @@
+package org.greencodeinitiative.creedengo.java.checks;
+
+import org.sonar.api.utils.log.Logger;
+import org.sonar.api.utils.log.Loggers;
+import org.sonar.check.Rule;
+import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
+import org.sonar.plugins.java.api.location.Position;
+import org.sonar.plugins.java.api.semantic.Type;
+import org.sonar.plugins.java.api.tree.*;
+import org.sonar.plugins.java.api.tree.Tree.Kind;
+
+import javax.annotation.CheckForNull;
+import javax.annotation.Nonnull;
+import java.util.List;
+import java.util.Objects;
+
+@Rule(key = "GCI82")
+public class MakeNonReassignedVariablesConstants extends IssuableSubscriptionVisitor {
+
+ protected static final String MESSAGE_RULE = "The variable is never reassigned and can be 'final'";
+
+ private static final Logger LOGGER = Loggers.get(MakeNonReassignedVariablesConstants.class);
+
+ private static final String LOMBOK_PACKAGE = "lombok";
+ private static final String SETTER = "Setter";
+ private static final String DATA = "Data";
+ private static final String ACCESS_LEVEL_NONE = "AccessLevel.NONE";
+ private static final String NONE = "NONE";
+
+ @Override
+ public List nodesToVisit() {
+ return List.of(Kind.VARIABLE);
+ }
+
+ @Override
+ public void visitNode(@Nonnull Tree tree) {
+ VariableTree variableTree = (VariableTree) tree;
+ if (LOGGER.isDebugEnabled()) {
+ LOGGER.debug("Variable > {}", getVariableNameForLogger(variableTree));
+ LOGGER.debug(" => isNotFinalAndNotStatic(variableTree) = {}", isNotFinalAndNotStatic(variableTree));
+ LOGGER.debug(" => usages = {}", variableTree.symbol().usages().size());
+ LOGGER.debug(" => isNotReassigned = {}", isNotReassigned(variableTree));
+ LOGGER.debug(" => isPassedAsNonFinalParameter = {}", isPassedAsNonFinalParameter(variableTree));
+ }
+
+ if (isParameterOfAbstractMethod(variableTree))
+ return;
+
+ // the Lombok check is the most expensive predicate : it is evaluated last, on actual candidates only
+ if (isNotFromRecord(variableTree) &&
+ isNotFinalAndNotStatic(variableTree) &&
+ isNotReassigned(variableTree) &&
+ !isLombokManaged(variableTree)) {
+ reportIssue(tree, MESSAGE_RULE);
+ } else {
+ super.visitNode(tree);
+ }
+ }
+
+ private static boolean isParameterOfAbstractMethod(VariableTree variableTree) {
+ Tree parent = variableTree.parent();
+ return parent != null && parent.is(Kind.METHOD) && ((MethodTree) parent).block() == null;
+ }
+
+ private static boolean isNotFromRecord(VariableTree variableTree) {
+ Tree parent = variableTree.parent();
+ if (parent == null) return false;
+
+ return !parent.is(Kind.RECORD);
+ }
+
+ private static boolean isNotReassigned(VariableTree variableTree) {
+ return variableTree.symbol()
+ .usages()
+ .stream()
+ .noneMatch(MakeNonReassignedVariablesConstants::parentIsAssignment)
+ && !isPassedAsNonFinalParameter(variableTree); // if a variable is passed into a method as a non-final parameter, it may have been reassigned
+ }
+
+ private static boolean isPassedAsNonFinalParameter(VariableTree variableTree) {
+ return variableTree.symbol()
+ .usages()
+ .stream()
+ .anyMatch(MakeNonReassignedVariablesConstants::parentIsNonFinalParameter);
+ }
+
+ private static boolean parentIsNonFinalParameter(Tree tree) {
+ // Skip the parent if it is a member select (e.g. "this.myVar")
+ while (tree.parent().is(Kind.MEMBER_SELECT)) {
+ tree = tree.parent();
+ }
+ if(!parentIsKind(tree, Kind.ARGUMENTS))
+ return false;
+ if(tree.parent() == null)
+ return false;
+ Arguments arguments = (Arguments) tree.parent();
+ if (parentIsKind(arguments, Kind.METHOD_INVOCATION, Kind.NEW_CLASS)) {
+ MethodTree methodTree = arguments.parent().is(Kind.METHOD_INVOCATION)
+ ? ((MethodInvocationTree) arguments.parent()).methodSymbol().declaration()
+ : ((NewClassTree) arguments.parent()).methodSymbol().declaration();
+ int argument_idx = arguments.indexOf(tree);
+ return methodTree != null && !hasModifier(methodTree.parameters().get(argument_idx).modifiers(), Modifier.FINAL);
+ }
+ return false;
+
+ }
+
+ private static boolean parentIsAssignment(Tree tree) {
+ // Skip the parent if it is a member select (e.g. "this.myVar")
+ while (tree.parent().is(Kind.MEMBER_SELECT)) {
+ tree = tree.parent();
+ }
+ return parentIsKind(tree,
+ Kind.ASSIGNMENT,
+ Kind.MULTIPLY_ASSIGNMENT,
+ Kind.DIVIDE_ASSIGNMENT,
+ Kind.REMAINDER_ASSIGNMENT,
+ Kind.PLUS_ASSIGNMENT,
+ Kind.MINUS_ASSIGNMENT,
+ Kind.LEFT_SHIFT_ASSIGNMENT,
+ Kind.RIGHT_SHIFT_ASSIGNMENT,
+ Kind.UNSIGNED_RIGHT_SHIFT_ASSIGNMENT,
+ Kind.AND_ASSIGNMENT,
+ Kind.XOR_ASSIGNMENT,
+ Kind.OR_ASSIGNMENT,
+ Kind.POSTFIX_INCREMENT,
+ Kind.POSTFIX_DECREMENT,
+ Kind.PREFIX_INCREMENT,
+ Kind.PREFIX_DECREMENT
+ );
+ }
+
+ private static boolean parentIsKind(Tree tree, Kind... orKind) {
+ Tree parent = tree.parent();
+ if (parent == null) return false;
+
+ for (Kind k : orKind) {
+ if (parent.is(k)) return true;
+ }
+
+ return false;
+ }
+
+ private boolean isNotFinalAndNotStatic(VariableTree variableTree) {
+ return hasNoneOf(variableTree.modifiers(), Modifier.FINAL, Modifier.STATIC) && !isFinalPatternVariable(variableTree);
+ }
+
+ /**
+ * For a pattern variable ({@code instanceof final Type var}), the parser does not attach the
+ * {@code final} keyword to {@link VariableTree#modifiers()} : the keyword sits between the
+ * {@code instanceof} keyword and the pattern type, outside of any tree node's token range.
+ * It is recovered here by reading the raw source in that gap.
+ */
+ private boolean isFinalPatternVariable(VariableTree variableTree) {
+ Tree parent = variableTree.parent();
+ if (parent == null || !parent.is(Kind.TYPE_PATTERN) || !(parent.parent() instanceof PatternInstanceOfTree patternInstanceOf)) {
+ return false;
+ }
+ String textBeforeType = textBetween(
+ patternInstanceOf.instanceofKeyword().range().end(),
+ variableTree.type().firstToken().range().start()
+ );
+ return "final".equals(textBeforeType.trim());
+ }
+
+ private String textBetween(Position start, Position end) {
+ List lines = context.getFileLines();
+ if (start.line() == end.line()) {
+ return lines.get(start.line() - 1).substring(start.columnOffset(), end.columnOffset());
+ }
+ StringBuilder result = new StringBuilder(lines.get(start.line() - 1).substring(start.columnOffset()));
+ for (int line = start.line() + 1; line < end.line(); line++) {
+ result.append(lines.get(line - 1));
+ }
+ result.append(lines.get(end.line() - 1), 0, end.columnOffset());
+ return result.toString();
+ }
+
+ private static boolean hasNoneOf(ModifiersTree modifiersTree, Modifier... unexpectedModifiers) {
+ return !hasAnyOf(modifiersTree, unexpectedModifiers);
+ }
+
+ private static boolean hasAnyOf(ModifiersTree modifiersTree, Modifier... expectedModifiers) {
+ for(Modifier expectedModifier : expectedModifiers) {
+ if (hasModifier(modifiersTree, expectedModifier)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public static boolean hasModifier(ModifiersTree modifiersTree, Modifier expectedModifier) {
+ for(ModifierKeywordTree modifierKeywordTree : modifiersTree.modifiers()) {
+ if (modifierKeywordTree.modifier() == expectedModifier) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ private String getVariableNameForLogger(VariableTree variableTree) {
+ String name = variableTree.simpleName().name();
+
+ if (variableTree.parent() != null) return name;
+
+ if (variableTree.parent().is(Kind.CLASS)) {
+ ClassTree cTree = (ClassTree) variableTree.parent();
+ name += " --- from CLASS '" + cTree.simpleName() + "'";
+ }
+ if (variableTree.parent().is(Kind.BLOCK)) {
+ BlockTree bTree = (BlockTree) variableTree.parent();
+ if (bTree.parent() != null && bTree.parent().is(Kind.METHOD)) {
+ MethodTree mTree = (MethodTree) bTree.parent();
+ name += " --- from METHOD '" + mTree.simpleName() + "'";
+ }
+ }
+
+ return name;
+
+ }
+
+ /**
+ * A variable is "Lombok managed" when Lombok generates a setter for it : making it {@code final}
+ * would not compile, so the rule must stay silent.
+ *
+ * This happens when the field itself is annotated with {@code @Setter}, or when its owner class is
+ * annotated with {@code @Setter} or {@code @Data}. A field level {@code @Setter(AccessLevel.NONE)}
+ * explicitly disables the generation and therefore wins over the class level annotation.
+ */
+ private static boolean isLombokManaged(VariableTree variableTree) {
+ AnnotationTree fieldSetter = findLombokAnnotation(variableTree.modifiers(), SETTER);
+ if (fieldSetter != null) {
+ return !isSetterDisabled(fieldSetter);
+ }
+
+ // covers CLASS, but also ENUM and INTERFACE owners, which Kind.CLASS alone would miss
+ if (variableTree.parent() instanceof ClassTree classTree) {
+ ModifiersTree classModifiers = classTree.modifiers();
+ return findLombokAnnotation(classModifiers, SETTER) != null
+ || findLombokAnnotation(classModifiers, DATA) != null;
+ }
+
+ return false;
+ }
+
+ @CheckForNull
+ private static AnnotationTree findLombokAnnotation(ModifiersTree modifiers, String simpleName) {
+ for (AnnotationTree annotation : modifiers.annotations()) {
+ if (isLombokAnnotation(annotation, simpleName)) {
+ return annotation;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Relies on the semantic model when it is available : the resolved type handles the regular import,
+ * the wildcard import ({@code import lombok.*}) and the fully qualified usage ({@code @lombok.Setter})
+ * indifferently, and rules out a same named annotation coming from another library.
+ *
+ * When Lombok is missing from the analysis classpath the type cannot be resolved, so we fall back on the
+ * written form and accept both {@code @Setter} and {@code @lombok.Setter}.
+ */
+ private static boolean isLombokAnnotation(AnnotationTree annotation, String simpleName) {
+ String fullyQualifiedName = LOMBOK_PACKAGE + "." + simpleName;
+
+ Type annotationType = annotation.symbolType();
+ if (!annotationType.isUnknown()) {
+ return annotationType.is(fullyQualifiedName);
+ }
+
+ String writtenName = writtenNameOf(annotation.annotationType());
+ return simpleName.equals(writtenName) || fullyQualifiedName.equals(writtenName);
+ }
+
+ /**
+ * Detects {@code AccessLevel.NONE}, whatever the way it is written : positional or named argument
+ * ({@code value = ...}), simple, fully qualified or statically imported constant.
+ */
+ private static boolean isSetterDisabled(AnnotationTree annotation) {
+ return annotation.arguments()
+ .stream()
+ .map(MakeNonReassignedVariablesConstants::annotationArgumentValue)
+ .map(MakeNonReassignedVariablesConstants::writtenNameOf)
+ .filter(Objects::nonNull)
+ .anyMatch(value -> value.endsWith(ACCESS_LEVEL_NONE) || NONE.equals(value));
+ }
+
+ private static ExpressionTree annotationArgumentValue(ExpressionTree argument) {
+ return argument.is(Kind.ASSIGNMENT)
+ ? ((AssignmentExpressionTree) argument).expression()
+ : argument;
+ }
+
+ /**
+ * Rebuilds the name as written in the source ({@code Setter}, {@code lombok.Setter},
+ * {@code lombok.AccessLevel.NONE}) by walking the tree : {@code toString()} only returns the source
+ * text for identifiers, not for member selects.
+ *
+ * @return {@code null} when the tree is neither an identifier nor a member select
+ */
+ @CheckForNull
+ private static String writtenNameOf(Tree tree) {
+ if (tree.is(Kind.IDENTIFIER)) {
+ return ((IdentifierTree) tree).name();
+ }
+ if (tree.is(Kind.MEMBER_SELECT)) {
+ MemberSelectExpressionTree memberSelect = (MemberSelectExpressionTree) tree;
+ String qualifier = writtenNameOf(memberSelect.expression());
+ return qualifier == null ? null : qualifier + "." + memberSelect.identifier().name();
+ }
+ return null;
+ }
+
+}
diff --git a/src/main/java/fr/greencodeinitiative/java/checks/NoFunctionCallWhenDeclaringForLoop.java b/src/main/java/org/greencodeinitiative/creedengo/java/checks/NoFunctionCallWhenDeclaringForLoop.java
similarity index 80%
rename from src/main/java/fr/greencodeinitiative/java/checks/NoFunctionCallWhenDeclaringForLoop.java
rename to src/main/java/org/greencodeinitiative/creedengo/java/checks/NoFunctionCallWhenDeclaringForLoop.java
index d4088bd2..1e57e86b 100644
--- a/src/main/java/fr/greencodeinitiative/java/checks/NoFunctionCallWhenDeclaringForLoop.java
+++ b/src/main/java/org/greencodeinitiative/creedengo/java/checks/NoFunctionCallWhenDeclaringForLoop.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.checks;
+package org.greencodeinitiative.creedengo.java.checks;
import java.util.ArrayList;
import java.util.Collection;
@@ -26,6 +26,7 @@
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
+import org.sonar.plugins.java.api.semantic.MethodMatchers;
import org.sonar.plugins.java.api.tree.BaseTreeVisitor;
import org.sonar.plugins.java.api.tree.ClassTree;
import org.sonar.plugins.java.api.tree.CompilationUnitTree;
@@ -37,12 +38,27 @@
import org.sonar.plugins.java.api.tree.Tree;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;
-@Rule(key = "EC69")
+@Rule(key = "GCI69")
+@DeprecatedRuleKey(repositoryKey = "ecocode-java", ruleKey = "EC69")
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S69")
public class NoFunctionCallWhenDeclaringForLoop extends IssuableSubscriptionVisitor {
protected static final String MESSAGERULE = "Do not call a function when declaring a for-type loop";
+ private static final String ITERATOR = "java.util.Iterator";
+ private static final MethodMatchers ITERATOR_METHODS = MethodMatchers.create()
+ .ofSubTypes(ITERATOR)
+ .names("hasNext", "next")
+ .withAnyParameters()
+ .build();
+ private static final String ENUMERATION = "java.util.Enumeration";
+ private static final MethodMatchers ENUMERATION_METHODS = MethodMatchers.create()
+ .ofSubTypes(ENUMERATION)
+ .names("hasMoreElements", "nextElement")
+ .withAnyParameters()
+ .build();
+
+
private static final Map> linesWithIssuesByClass = new HashMap<>();
@Override
@@ -58,23 +74,26 @@ public void visitNode(Tree tree) {
if (null != condition) {
method.condition().accept(invocationMethodVisitor);
}
+
// update
- // initaliser
method.update().accept(invocationMethodVisitor);
- method.initializer().accept(invocationMethodVisitor);
}
private class MethodInvocationInForStatementVisitor extends BaseTreeVisitor {
@Override
public void visitMethodInvocation(MethodInvocationTree tree) {
- if (!lineAlreadyHasThisIssue(tree)) {
+ if (!lineAlreadyHasThisIssue(tree) && !isMethodAllowed(tree)) {
report(tree);
return;
}
super.visitMethodInvocation(tree);
}
+ private boolean isMethodAllowed(MethodInvocationTree tree) {
+ return ITERATOR_METHODS.matches(tree) || ENUMERATION_METHODS.matches(tree);
+ }
+
private boolean lineAlreadyHasThisIssue(Tree tree) {
if (tree.firstToken() != null) {
final String classname = getFullyQualifiedNameOfClassOf(tree);
diff --git a/src/main/java/fr/greencodeinitiative/java/checks/OptimizeReadFileExceptions.java b/src/main/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptions.java
similarity index 92%
rename from src/main/java/fr/greencodeinitiative/java/checks/OptimizeReadFileExceptions.java
rename to src/main/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptions.java
index 2362404b..76a74b28 100644
--- a/src/main/java/fr/greencodeinitiative/java/checks/OptimizeReadFileExceptions.java
+++ b/src/main/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptions.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.checks;
+package org.greencodeinitiative.creedengo.java.checks;
import java.util.Arrays;
@@ -32,7 +32,8 @@
import org.sonar.plugins.java.api.tree.TryStatementTree;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;
-@Rule(key = "EC28")
+@Rule(key = "GCI28")
+@DeprecatedRuleKey(repositoryKey = "ecocode-java", ruleKey = "EC28")
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "GRSP0028")
public class OptimizeReadFileExceptions extends IssuableSubscriptionVisitor {
diff --git a/src/main/java/org/greencodeinitiative/creedengo/java/checks/UseOptionalOrElseGetVsOrElse.java b/src/main/java/org/greencodeinitiative/creedengo/java/checks/UseOptionalOrElseGetVsOrElse.java
new file mode 100644
index 00000000..28f1ea50
--- /dev/null
+++ b/src/main/java/org/greencodeinitiative/creedengo/java/checks/UseOptionalOrElseGetVsOrElse.java
@@ -0,0 +1,88 @@
+/*
+ * 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.sonar.check.Rule;
+import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
+import org.sonar.plugins.java.api.tree.BaseTreeVisitor;
+import org.sonar.plugins.java.api.tree.MemberSelectExpressionTree;
+import org.sonar.plugins.java.api.tree.MethodInvocationTree;
+import org.sonar.plugins.java.api.tree.NewArrayTree;
+import org.sonar.plugins.java.api.tree.NewClassTree;
+import org.sonar.plugins.java.api.tree.Tree;
+import javax.annotation.Nonnull;
+import java.util.Collections;
+import java.util.List;
+
+@Rule(key = "GCI94")
+public class UseOptionalOrElseGetVsOrElse extends IssuableSubscriptionVisitor {
+
+ private static final String MESSAGE_RULE = "Use optional orElseGet instead of orElse.";
+ private final UseOptionalOrElseGetVsOrElseVisitor visitorInFile = new UseOptionalOrElseGetVsOrElseVisitor();
+
+ @Override
+ public List nodesToVisit() {
+ return Collections.singletonList(Tree.Kind.METHOD_INVOCATION);
+ }
+
+ @Override
+ public void visitNode(@Nonnull Tree tree) {
+ tree.accept(visitorInFile);
+ }
+
+ private class UseOptionalOrElseGetVsOrElseVisitor extends BaseTreeVisitor {
+ @Override
+ public void visitMethodInvocation(MethodInvocationTree tree) {
+ if (!tree.methodSelect().is(Tree.Kind.MEMBER_SELECT)) {
+ return;
+ }
+ MemberSelectExpressionTree memberSelect = (MemberSelectExpressionTree) tree.methodSelect();
+ if (memberSelect.identifier().name().equals("orElse") &&
+ memberSelect.expression().symbolType().is("java.util.Optional") &&
+ !tree.arguments().isEmpty() &&
+ containsComputation(tree.arguments().get(0))) {
+ reportIssue(memberSelect, MESSAGE_RULE);
+ }
+ }
+ }
+
+ private static boolean containsComputation(Tree argument) {
+ ComputationDetector detector = new ComputationDetector();
+ argument.accept(detector);
+ return detector.found;
+ }
+
+ private static class ComputationDetector extends BaseTreeVisitor {
+ boolean found = false;
+
+ @Override
+ public void visitMethodInvocation(@Nonnull MethodInvocationTree tree) {
+ found = true;
+ }
+
+ @Override
+ public void visitNewClass(@Nonnull NewClassTree tree) {
+ found = true;
+ }
+
+ @Override
+ public void visitNewArray(@Nonnull NewArrayTree tree) {
+ found = true;
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/fr/greencodeinitiative/java/checks/enums/ConstOrLiteralDeclare.java b/src/main/java/org/greencodeinitiative/creedengo/java/checks/enums/ConstOrLiteralDeclare.java
similarity index 96%
rename from src/main/java/fr/greencodeinitiative/java/checks/enums/ConstOrLiteralDeclare.java
rename to src/main/java/org/greencodeinitiative/creedengo/java/checks/enums/ConstOrLiteralDeclare.java
index db01a76b..1ef973a5 100644
--- a/src/main/java/fr/greencodeinitiative/java/checks/enums/ConstOrLiteralDeclare.java
+++ b/src/main/java/org/greencodeinitiative/creedengo/java/checks/enums/ConstOrLiteralDeclare.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.checks.enums;
+package org.greencodeinitiative.creedengo.java.checks.enums;
import java.math.BigDecimal;
import java.util.Set;
@@ -226,4 +226,4 @@ public static final boolean isLiteral(Tree arg) {
arg.is(STRING_LITERAL) ||
arg.is(CHAR_LITERAL);
}
-}
\ No newline at end of file
+}
diff --git a/src/main/java/fr/greencodeinitiative/java/utils/PrinterVisitor.java b/src/main/java/org/greencodeinitiative/creedengo/java/utils/PrinterVisitor.java
similarity index 89%
rename from src/main/java/fr/greencodeinitiative/java/utils/PrinterVisitor.java
rename to src/main/java/org/greencodeinitiative/creedengo/java/utils/PrinterVisitor.java
index 2b709d79..fa245893 100644
--- a/src/main/java/fr/greencodeinitiative/java/utils/PrinterVisitor.java
+++ b/src/main/java/org/greencodeinitiative/creedengo/java/utils/PrinterVisitor.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.utils;
+package org.greencodeinitiative.creedengo.java.utils;
import java.util.List;
import java.util.function.Consumer;
diff --git a/src/main/java/fr/greencodeinitiative/java/utils/StringUtils.java b/src/main/java/org/greencodeinitiative/creedengo/java/utils/StringUtils.java
similarity index 81%
rename from src/main/java/fr/greencodeinitiative/java/utils/StringUtils.java
rename to src/main/java/org/greencodeinitiative/creedengo/java/utils/StringUtils.java
index b9b81c52..ce66a882 100644
--- a/src/main/java/fr/greencodeinitiative/java/utils/StringUtils.java
+++ b/src/main/java/org/greencodeinitiative/creedengo/java/utils/StringUtils.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.utils;
+package org.greencodeinitiative.creedengo.java.utils;
public final class StringUtils {
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
new file mode 100644
index 00000000..059bf0f5
--- /dev/null
+++ b/src/main/resources/org/greencodeinitiative/creedengo/java/creedengo_way_profile.json
@@ -0,0 +1,23 @@
+{
+ "name": "creedengo way",
+ "language": "java",
+ "ruleKeys": [
+ "GCI1",
+ "GCI2",
+ "GCI3",
+ "GCI5",
+ "GCI27",
+ "GCI28",
+ "GCI32",
+ "GCI67",
+ "GCI69",
+ "GCI72",
+ "GCI74",
+ "GCI76",
+ "GCI77",
+ "GCI78",
+ "GCI79",
+ "GCI82",
+ "GCI94"
+ ]
+}
diff --git a/src/test/files/AvoidConcatenateStringsInLoop.java b/src/test/files/AvoidConcatenateStringsInLoop.java
deleted file mode 100644
index 4e2a5877..00000000
--- a/src/test/files/AvoidConcatenateStringsInLoop.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
- *
- * 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 fr.greencodeinitiative.java.utils;
-
-public class AvoidConcatenateStringsInLoop {
-
- public String concatenateStrings(String[] strings) {
- String result1 = "";
-
- for (String string : strings) {
- result1 += string; // Noncompliant {{Don't concatenate Strings in loop, use StringBuilder instead.}}
- }
- return result1;
- }
-
- public String concatenateStrings2() {
- String result2 = "";
-
- for (int i = 0; i < 1000; ++i) {
- result2 += "another"; // Noncompliant {{Don't concatenate Strings in loop, use StringBuilder instead.}}
- }
- return result2;
- }
-
- public String concatenateStrings3() {
- String result3 = "";
-
- for (int i = 0; i < 1000; ++i) {
- result3 = result3 + "another"; // Noncompliant {{Don't concatenate Strings in loop, use StringBuilder instead.}}
- }
- return result3;
- }
-
-}
diff --git a/src/test/files/AvoidGettingSizeCollectionInForLoopBad.java b/src/test/files/AvoidGettingSizeCollectionInForLoopBad.java
deleted file mode 100644
index 3c74c86d..00000000
--- a/src/test/files/AvoidGettingSizeCollectionInForLoopBad.java
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
- *
- * 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 fr.greencodeinitiative.java.checks;
-
-import java.util.Collection;
-import java.util.ArrayList;
-import java.util.List;
-
-class AvoidGettingSizeCollectionInForLoopBad {
- AvoidGettingSizeCollectionInForLoopBad() {
-
- }
-
- public void badForLoop() {
- List numberList = new ArrayList();
- numberList.add(10);
- numberList.add(20);
-
- for (int i = 0; i < numberList.size(); i++) { // Noncompliant {{Avoid getting the size of the collection in the loop}}
- System.out.println("numberList.size()");
- }
- }
-}
\ No newline at end of file
diff --git a/src/test/files/AvoidUsingGlobalVariablesCheck.java b/src/test/files/AvoidUsingGlobalVariablesCheck.java
deleted file mode 100644
index 557d6fc9..00000000
--- a/src/test/files/AvoidUsingGlobalVariablesCheck.java
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
- *
- * 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 .
- */
-public class Openclass {
- public static double price = 15.24; // Noncompliant {{Avoid using global variables}}
- public static long pages = 1053; // Noncompliant {{Avoid using global variables}}
-
- public static void main(String[] args) {
- double newPrice = Openclass.price;
- long newPages = Openclass.pages;
- System.out.println(newPrice);
- System.out.println(newPages);
- static long years = 3000; // Noncompliant {{Avoid using global variables}}
- }
- static{ // Noncompliant {{Avoid using global variables}}
- int a = 4;
- }
-
- public void printingA() {
- System.out.println(a);
- }
-
-}
\ No newline at end of file
diff --git a/src/test/files/FreeResourcesOfAutoCloseableInterface.java b/src/test/files/FreeResourcesOfAutoCloseableInterface.java
deleted file mode 100644
index e1eaab59..00000000
--- a/src/test/files/FreeResourcesOfAutoCloseableInterface.java
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
- *
- * 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 fr.greencodeinitiative.java.checks;
-
-import java.io.*;
-
-class FreeResourcesOfAutoCloseableInterface {
- FreeResourcesOfAutoCloseableInterface(FreeResourcesOfAutoCloseableInterface mc) {
-
- }
-
- public void foo1() {
- String fileName = "./FreeResourcesOfAutoCloseableInterface.java";
- try (FileReader fr = new FileReader(fileName);
- BufferedReader br = new BufferedReader(fr)) {
- } catch (IOException e) {
- System.err.println(e.getMessage());
- }
- }
-
- public void foo2() {
- String fileName = "./FreeResourcesOfAutoCloseableInterface.java";
- try { // Noncompliant
- FileReader fr = new FileReader(fileName);
- BufferedReader br = new BufferedReader(fr);
- System.out.printl(br.readLine());
- } catch (IOException e) {
- System.err.println(e.getMessage());
- } finally {
- if (fr) {
- fr.close();
- }
- if (br) {
- br.close();
- }
- }
- }
-}
\ No newline at end of file
diff --git a/src/test/files/GoodWayConcatenateStringsLoop.java b/src/test/files/GoodWayConcatenateStringsLoop.java
deleted file mode 100644
index c11c1ab6..00000000
--- a/src/test/files/GoodWayConcatenateStringsLoop.java
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
- *
- * 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 fr.greencodeinitiative.java.utils;
-
-public class GoodWayConcatenateStringsLoop {
-
- public String concatenateStrings(String[] strings) {
- StringBuilder result = new StringBuilder();
-
- for (String string : strings) {
- result.append(string);
- }
- return result.toString();
- }
-
- public void testConcateOutOfLoop() {
- String result = "";
- result += "another";
- }
-
- public void testConcateOutOfLoop2() {
- String result = "";
- result = result + "another";
- }
-
- public String changeValueStringInLoop() {
- String result3 = "";
-
- for (int i = 0; i < 1; ++i) {
- result3 = "another";
- }
- return result3;
- }
-
-}
diff --git a/src/test/files/IncrementCheck.java b/src/test/files/IncrementCheck.java
deleted file mode 100644
index 54b20bf9..00000000
--- a/src/test/files/IncrementCheck.java
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
- *
- * 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 .
- */
-class MyClass {
- MyClass(MyClass mc) {
- }
-
- int foo1() {
- int counter = 0;
- return counter++; // Noncompliant {{Use ++i instead of i++}}
- }
-
- int foo11() {
- int counter = 0;
- return ++counter;
- }
-
- void foo2(int value) {
- int counter = 0;
- counter++; // Noncompliant {{Use ++i instead of i++}}
- }
-
- void foo22(int value) {
- int counter = 0;
- ++counter;
- }
-
- void foo3(int value) {
- int counter = 0;
- counter = counter + 197845 ;
- }
-
- void foo4(int value) {
- int counter =0;
- counter = counter + 35 + 78 ;
- }
-
- void foo50(int value) {
- for (int i=0; i < 10; i++) { // Noncompliant {{Use ++i instead of i++}}
- System.out.println(i);
- }
- }
-
- void foo51(int value) {
- for (int i=0; i < 10; ++i) {
- System.out.println(i);
- }
- }
-}
\ No newline at end of file
diff --git a/src/test/files/NoFunctionCallWhenDeclaringForLoop.java b/src/test/files/NoFunctionCallWhenDeclaringForLoop.java
deleted file mode 100644
index da72a3ef..00000000
--- a/src/test/files/NoFunctionCallWhenDeclaringForLoop.java
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
- *
- * 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 .
- */
-class NoFunctionCallWhenDeclaringForLoop {
- NoFunctionCallWhenDeclaringForLoop(NoFunctionCallWhenDeclaringForLoop mc) {
- }
-
- public int getMyValue() {
- return 6;
- }
-
- public int incrementeMyValue(int i) {
- return i + 100;
- }
-
- public void test1() {
- for (int i = 0; i < 20; i++) {
- System.out.println(i);
- boolean b = getMyValue() > 6;
- }
- }
-
- public void test2() {
- String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
- for (String i : cars) {
- System.out.println(i);
- }
-
- }
-
- public void test3() {
- for (int i = getMyValue(); i < 20; i++) { // Noncompliant {{Do not call a function when declaring a for-type loop}}
- System.out.println(i);
- boolean b = getMyValue() > 6;
- }
- }
-
- public void test4() {
- for (int i = 0; i < getMyValue(); i++) { // Noncompliant {{Do not call a function when declaring a for-type loop}}
- System.out.println(i);
- boolean b = getMyValue() > 6;
- }
- }
-
- public void test5() {
- for (int i = 0; i < getMyValue(); incrementeMyValue(i)) { // Noncompliant {{Do not call a function when declaring a for-type loop}}
- System.out.println(i);
- boolean b = getMyValue() > 6;
- }
- }
-
- public void test6() {
- for (int i = getMyValue(); i < getMyValue(); i++) { // Noncompliant {{Do not call a function when declaring a for-type loop}}
- System.out.println(i);
- boolean b = getMyValue() > 6;
- }
- }
-
-}
\ No newline at end of file
diff --git a/src/test/files/UnnecessarilyAssignValuesToVariablesTestCheck.java b/src/test/files/UnnecessarilyAssignValuesToVariablesTestCheck.java
deleted file mode 100644
index 17eb069a..00000000
--- a/src/test/files/UnnecessarilyAssignValuesToVariablesTestCheck.java
+++ /dev/null
@@ -1,95 +0,0 @@
-/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
- *
- * 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 fr.greencodeinitiative.java.checks;
-
-class UnnecessarilyAssignValuesToVariablesTestCheck {
- UnnecessarilyAssignValuesToVariablesTestCheck(UnnecessarilyAssignValuesToVariablesTestCheck mc) {
- }
-
- public int testSwitchCase() throws Exception {
- int variableFor = 5;
- int variableIf = 5;
- int variableWhile = 5;
- int variableExp = 5;
- int variableReturn = 5;
- int variableCLass = 5;
- int[] intArray = {10, 20, 30, 40, 50};
-
- Exception variableException = new Exception("message");
- int variableNotUse = 5; // Noncompliant {{The variable is declared but not really used}}
-
-
- variableNotUse = 10;
- for (variableFor = 0; variableFor < 5; ++variableFor) {
- System.out.println(variableFor);
- }
-
- for (int ia : intArray) {
- System.out.println((char) ia);
- }
-
- if (variableIf > 10) {
- System.out.println(variableIf);
- }
-
- while (variableWhile > 10) {
- System.out.println(variableWhile);
- }
-
- variableExp += 1;
- variableNotUse = variableExp;
- TestClass testClass = new TestClass(variableCLass);
- if (testClass.isTrue()) {
- throw variableException;
- }
- return variableReturn;
- }
-
- private class TestClass {
- TestClass(int i) {
- ++i;
- }
-
- public boolean isTrue() {
- return true;
- }
- }
-
-
- private int getIntValue() {
- return 3;
- }
-
- public int testNonCompliantReturn() {
- int i = getIntValue(); // Noncompliant {{Immediately return this expression instead of assigning it to the temporary variable}}
- return i;
- }
-
- public int testCompliantReturn() {
- return getIntValue();
- }
-
- public void testNonCompliantThrow() throws Exception {
- Exception exception = new Exception("dummy"); // Noncompliant {{Immediately throw this expression instead of assigning it to the temporary variable}}
- throw exception;
- }
-
- public void testCompliantThrow() throws Exception {
- throw new Exception("dummy");
- }
-}
\ No newline at end of file
diff --git a/src/test/files/UnnecessarilyAssignValuesToVariablesTestCheckWithEmptyReturn.java b/src/test/files/UnnecessarilyAssignValuesToVariablesTestCheckWithEmptyReturn.java
deleted file mode 100644
index f70c4c41..00000000
--- a/src/test/files/UnnecessarilyAssignValuesToVariablesTestCheckWithEmptyReturn.java
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
- *
- * 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 fr.greencodeinitiative.java.checks;
-
-class UnnecessarilyAssignValuesToVariablesTestCheck {
- UnnecessarilyAssignValuesToVariablesTestCheck(UnnecessarilyAssignValuesToVariablesTestCheck mc) {
- }
-
- public void testSwitchCase() {
-
- ArrayList lst = new ArrayList(0);
- if (lst == null) {
- return;
- }
- System.out.println(lst);
- }
-
-}
\ No newline at end of file
diff --git a/src/test/files/UseCorrectForLoopCheck.java b/src/test/files/UseCorrectForLoopCheck.java
deleted file mode 100644
index 350ed6eb..00000000
--- a/src/test/files/UseCorrectForLoopCheck.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
- *
- * 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 fr.greencodeinitiative.java.checks;
-
-import java.util.Arrays;
-import java.util.List;
-
-class UseCorrectForLoopCheck {
- UseCorrectForLoopCheck(UseCorrectForLoopCheck mc) {
- }
-
- private final Integer[] intArray = new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
- private final List intList = Arrays.asList(intArray);
-
- public void testForEachLoop() {
- int dummy = 0;
- for (Integer i : intArray) { // Noncompliant {{Avoid the use of Foreach with Arrays}}
- dummy += i;
- }
-
- for (Integer i : intList) {
- dummy += i;
- }
- System.out.println(dummy);
- }
-}
\ No newline at end of file
diff --git a/src/test/java/fr/greencodeinitiative/java/checks/AvoidConcatenateStringsInLoopTest.java b/src/test/java/fr/greencodeinitiative/java/checks/AvoidConcatenateStringsInLoopTest.java
deleted file mode 100644
index fc857883..00000000
--- a/src/test/java/fr/greencodeinitiative/java/checks/AvoidConcatenateStringsInLoopTest.java
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
- *
- * 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 fr.greencodeinitiative.java.checks;
-
-import org.junit.jupiter.api.Test;
-import org.sonar.java.checks.verifier.CheckVerifier;
-
-@Deprecated
-class AvoidConcatenateStringsInLoopTest {
-
- @Test
- void checkNonCompliantTests() {
- CheckVerifier.newVerifier()
- .onFile("src/test/files/AvoidConcatenateStringsInLoop.java")
- .withCheck(new AvoidConcatenateStringsInLoop())
- .verifyIssues();
- }
-
- @Test
- void checkCompliantTests() {
- CheckVerifier.newVerifier()
- .onFile("src/test/files/GoodWayConcatenateStringsLoop.java")
- .withCheck(new AvoidConcatenateStringsInLoop())
- .verifyNoIssues();
- }
-
-}
diff --git a/src/test/java/fr/greencodeinitiative/java/checks/UnnecessarilyAssignValuesToVariablesTest.java b/src/test/java/fr/greencodeinitiative/java/checks/UnnecessarilyAssignValuesToVariablesTest.java
deleted file mode 100644
index dda74370..00000000
--- a/src/test/java/fr/greencodeinitiative/java/checks/UnnecessarilyAssignValuesToVariablesTest.java
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
- *
- * 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 fr.greencodeinitiative.java.checks;
-
-import org.junit.jupiter.api.Test;
-import org.sonar.java.checks.verifier.CheckVerifier;
-
-@Deprecated
-class UnnecessarilyAssignValuesToVariablesTest {
-
- @Test
- void test() {
- CheckVerifier.newVerifier()
- .onFile("src/test/files/UnnecessarilyAssignValuesToVariablesTestCheck.java")
- .withCheck(new UnnecessarilyAssignValuesToVariables())
- .verifyIssues();
- }
-
- @Test
- void testIgnoredEmptyReturn() {
- CheckVerifier.newVerifier()
- .onFile("src/test/files/UnnecessarilyAssignValuesToVariablesTestCheckWithEmptyReturn.java")
- .withCheck(new UnnecessarilyAssignValuesToVariables())
- .verifyNoIssues();
- }
-
-}
\ No newline at end of file
diff --git a/src/test/java/fr/greencodeinitiative/java/checks/UseCorrectLoopCheckTest.java b/src/test/java/fr/greencodeinitiative/java/checks/UseCorrectLoopCheckTest.java
deleted file mode 100644
index a853a6f5..00000000
--- a/src/test/java/fr/greencodeinitiative/java/checks/UseCorrectLoopCheckTest.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
- *
- * 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 fr.greencodeinitiative.java.checks;
-
-import org.junit.jupiter.api.Test;
-import org.sonar.java.checks.verifier.CheckVerifier;
-
-@Deprecated
-class UseCorrectLoopCheckTest {
-
- @Test
- void test() {
- CheckVerifier.newVerifier()
- .onFile("src/test/files/UseCorrectForLoopCheck.java")
- .withCheck(new UseCorrectForLoop())
- .verifyIssues();
- }
-
-}
\ No newline at end of file
diff --git a/src/test/java/fr/greencodeinitiative/java/JavaCheckRegistrarTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/JavaCheckRegistrarTest.java
similarity index 60%
rename from src/test/java/fr/greencodeinitiative/java/JavaCheckRegistrarTest.java
rename to src/test/java/org/greencodeinitiative/creedengo/java/JavaCheckRegistrarTest.java
index c38b4c09..89541a50 100644
--- a/src/test/java/fr/greencodeinitiative/java/JavaCheckRegistrarTest.java
+++ b/src/test/java/org/greencodeinitiative/creedengo/java/JavaCheckRegistrarTest.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,9 +15,13 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java;
+package org.greencodeinitiative.creedengo.java;
+
+import java.util.Set;
import org.junit.jupiter.api.Test;
+import org.reflections.Reflections;
+import org.sonar.check.Rule;
import org.sonar.plugins.java.api.CheckRegistrar;
import static org.assertj.core.api.Assertions.assertThat;
@@ -30,10 +34,16 @@ void checkNumberRules() {
final JavaCheckRegistrar registrar = new JavaCheckRegistrar();
registrar.register(context);
-
- assertThat(context.checkClasses()).hasSize(19);
+ assertThat(context.checkClasses())
+ .describedAs("All implemented rules must be registered into " + JavaCheckRegistrar.class)
+ .containsExactlyInAnyOrder(getDefinedRules().toArray(new Class[0]));
assertThat(context.testCheckClasses()).isEmpty();
}
+ static Set> getDefinedRules() {
+ Reflections r = new Reflections(JavaCheckRegistrar.class.getPackageName() + ".checks");
+ return r.getTypesAnnotatedWith(Rule.class);
+ }
+
}
diff --git a/src/test/java/org/greencodeinitiative/creedengo/java/JavaCreedengoWayProfileTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/JavaCreedengoWayProfileTest.java
new file mode 100644
index 00000000..7db81345
--- /dev/null
+++ b/src/test/java/org/greencodeinitiative/creedengo/java/JavaCreedengoWayProfileTest.java
@@ -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 .
+ */
+package org.greencodeinitiative.creedengo.java;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+import org.junit.jupiter.api.Test;
+import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition;
+import org.sonar.check.Rule;
+
+import static org.greencodeinitiative.creedengo.java.JavaCheckRegistrarTest.getDefinedRules;
+import static org.greencodeinitiative.creedengo.java.JavaCreedengoWayProfile.PROFILE_NAME;
+import static org.greencodeinitiative.creedengo.java.JavaCreedengoWayProfile.PROFILE_PATH;
+import static org.greencodeinitiative.creedengo.java.JavaRulesDefinition.LANGUAGE;
+import static org.assertj.core.api.Assertions.assertThat;
+
+class JavaCreedengoWayProfileTest {
+ @Test
+ void should_create_creedengo_profile() {
+ BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context();
+
+ JavaCreedengoWayProfile definition = new JavaCreedengoWayProfile();
+ definition.define(context);
+
+ BuiltInQualityProfilesDefinition.BuiltInQualityProfile profile = context.profile(LANGUAGE, PROFILE_NAME);
+
+ assertThat(profile.language()).isEqualTo(LANGUAGE);
+ assertThat(profile.name()).isEqualTo(PROFILE_NAME);
+ List definedRuleIds = getDefinedRules().stream().map(c -> c.getAnnotation(Rule.class).key()).collect(Collectors.toList());
+ assertThat(profile.rules())
+ .describedAs("All implemented rules must be declared in '%s' profile file: %s", PROFILE_NAME, PROFILE_PATH)
+ .map(BuiltInQualityProfilesDefinition.BuiltInActiveRule::ruleKey)
+ .containsExactlyInAnyOrderElementsOf(definedRuleIds);
+ }
+}
diff --git a/src/test/java/fr/greencodeinitiative/java/JavaPluginTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/JavaPluginTest.java
similarity index 84%
rename from src/test/java/fr/greencodeinitiative/java/JavaPluginTest.java
rename to src/test/java/org/greencodeinitiative/creedengo/java/JavaPluginTest.java
index 0f2167d3..b54f59bc 100644
--- a/src/test/java/fr/greencodeinitiative/java/JavaPluginTest.java
+++ b/src/test/java/org/greencodeinitiative/creedengo/java/JavaPluginTest.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java;
+package org.greencodeinitiative.creedengo.java;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
diff --git a/src/test/java/fr/greencodeinitiative/java/JavaRulesDefinitionTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/JavaRulesDefinitionTest.java
similarity index 56%
rename from src/test/java/fr/greencodeinitiative/java/JavaRulesDefinitionTest.java
rename to src/test/java/org/greencodeinitiative/creedengo/java/JavaRulesDefinitionTest.java
index 2c655143..d0f53da3 100644
--- a/src/test/java/fr/greencodeinitiative/java/JavaRulesDefinitionTest.java
+++ b/src/test/java/org/greencodeinitiative/creedengo/java/JavaRulesDefinitionTest.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java;
+package org.greencodeinitiative.creedengo.java;
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.BeforeEach;
@@ -28,42 +28,17 @@
import org.sonar.api.server.rule.RulesDefinition.Rule;
import org.sonar.api.utils.Version;
+import static org.greencodeinitiative.creedengo.java.JavaCheckRegistrar.ANNOTATED_RULE_CLASSES;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.fail;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
class JavaRulesDefinitionTest {
private RulesDefinition.Repository repository;
- private RulesDefinition.Context context;
@BeforeEach
void init() {
- // TODO: Remove this check after Git repo split
- /*
- On an IDE (like IntelliJ), if the developer runs the unit tests without building/generating the Maven goals on the
- "ecocode-rules-specifications" module before, the unit tests will not see the generated HTML descriptions (from ASCIIDOC files).
- The developer must therefore configure his IDE to build the `ecocode-rules-specifications` module before launching the Tests.
-
- When the `java-plugin` submodule is in a specific Git repository, `ecocode-rules-specifications` will be fetched from a classic
- external Maven dependency. There will therefore no longer be any need to perform this specific configuration.
- */
- if (JavaRulesDefinition.class.getResource("/io/ecocode/rules/java/EC4.json") == null) {
- String message = "'ecocode-rules-specification' resources corrupted. Please check build of 'ecocode-rules-specification' module";
- if (System.getProperties().keySet().stream().anyMatch(k -> k.toString().startsWith("idea."))) {
- message += "\n\nOn 'IntelliJ IDEA':" +
- "\n1. go to settings :" +
- "\n > Build, Execution, Deployment > Build Tools > Maven > Runner" +
- "\n2. check option:" +
- "\n > Delegate IDE build/run actions to Maven" +
- "\n3. Click on menu: " +
- "\n > Build > Build Project"
- ;
- }
- fail(message);
- }
-
final SonarRuntime sonarRuntime = mock(SonarRuntime.class);
doReturn(Version.create(0, 0)).when(sonarRuntime).getApiVersion();
JavaRulesDefinition rulesDefinition = new JavaRulesDefinition(sonarRuntime);
@@ -75,30 +50,29 @@ On an IDE (like IntelliJ), if the developer runs the unit tests without building
@Test
@DisplayName("Test repository metadata")
void testMetadata() {
- assertThat(repository.name()).isEqualTo("ecoCode");
+ assertThat(repository.name()).isEqualTo("creedengo");
assertThat(repository.language()).isEqualTo("java");
- assertThat(repository.key()).isEqualTo("ecocode-java");
- assertThat(repository.rules()).hasSize(19);
+ assertThat(repository.key()).isEqualTo("creedengo-java");
}
@Test
void testRegistredRules() {
- assertThat(repository.rules()).hasSize(19);
+ assertThat(repository.rules()).hasSize(ANNOTATED_RULE_CLASSES.size());
}
@Test
- @DisplayName("All rule keys must be prefixed by 'EC'")
+ @DisplayName("All rule keys must be prefixed by 'GCI'")
void testRuleKeyPrefix() {
SoftAssertions assertions = new SoftAssertions();
repository.rules().forEach(
- rule -> assertions.assertThat(rule.key()).startsWith("EC")
+ rule -> assertions.assertThat(rule.key()).startsWith("GCI")
);
assertions.assertAll();
}
@Test
void assertRuleProperties() {
- Rule rule = repository.rule("EC67");
+ Rule rule = repository.rule("GCI67");
assertThat(rule).isNotNull();
assertThat(rule.name()).isEqualTo("Use ++i instead of i++");
assertThat(rule.debtRemediationFunction().type()).isEqualTo(Type.CONSTANT_ISSUE);
diff --git a/src/test/java/fr/greencodeinitiative/java/checks/AvoidSpringRepositoryCallInLoopCheckTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI1/AvoidSpringRepositoryCallInLoopCheckTest.java
similarity index 66%
rename from src/test/java/fr/greencodeinitiative/java/checks/AvoidSpringRepositoryCallInLoopCheckTest.java
rename to src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI1/AvoidSpringRepositoryCallInLoopCheckTest.java
index 2a855e0c..f3db2b53 100644
--- a/src/test/java/fr/greencodeinitiative/java/checks/AvoidSpringRepositoryCallInLoopCheckTest.java
+++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI1/AvoidSpringRepositoryCallInLoopCheckTest.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,9 +15,10 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.checks;
+package org.greencodeinitiative.creedengo.java.checks.GCI1;
-import fr.greencodeinitiative.java.utils.FilesUtils;
+import org.greencodeinitiative.creedengo.java.checks.AvoidSpringRepositoryCallInLoopOrStreamCheck;
+import org.greencodeinitiative.creedengo.java.utils.FilesUtils;
import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;
@@ -26,7 +27,7 @@ class AvoidSpringRepositoryCallInLoopCheckTest {
@Test
void test() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/AvoidSpringRepositoryCallInLoopCheck.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI1/AvoidSpringRepositoryCallInLoopCheck.java")
.withCheck(new AvoidSpringRepositoryCallInLoopOrStreamCheck())
.withClassPath(FilesUtils.getClassPath("target/test-jars"))
.verifyIssues();
diff --git a/src/test/java/fr/greencodeinitiative/java/checks/AvoidSpringRepositoryCallInStreamCheckTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI1/AvoidSpringRepositoryCallInStreamCheckTest.java
similarity index 66%
rename from src/test/java/fr/greencodeinitiative/java/checks/AvoidSpringRepositoryCallInStreamCheckTest.java
rename to src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI1/AvoidSpringRepositoryCallInStreamCheckTest.java
index e9fd4f35..222c90f8 100644
--- a/src/test/java/fr/greencodeinitiative/java/checks/AvoidSpringRepositoryCallInStreamCheckTest.java
+++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI1/AvoidSpringRepositoryCallInStreamCheckTest.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,9 +15,10 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.checks;
+package org.greencodeinitiative.creedengo.java.checks.GCI1;
-import fr.greencodeinitiative.java.utils.FilesUtils;
+import org.greencodeinitiative.creedengo.java.checks.AvoidSpringRepositoryCallInLoopOrStreamCheck;
+import org.greencodeinitiative.creedengo.java.utils.FilesUtils;
import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;
@@ -26,7 +27,7 @@ class AvoidSpringRepositoryCallInStreamCheckTest {
@Test
void test() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/AvoidSpringRepositoryCallInStreamCheck.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI1/AvoidSpringRepositoryCallInStreamCheck.java")
.withCheck(new AvoidSpringRepositoryCallInLoopOrStreamCheck())
.withClassPath(FilesUtils.getClassPath("target/test-jars"))
.verifyIssues();
diff --git a/src/test/java/fr/greencodeinitiative/java/checks/AvoidMultipleIfElseStatementTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI2/AvoidMultipleIfElseStatementTest.java
similarity index 55%
rename from src/test/java/fr/greencodeinitiative/java/checks/AvoidMultipleIfElseStatementTest.java
rename to src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI2/AvoidMultipleIfElseStatementTest.java
index f3f8ecdb..4eb1e99c 100644
--- a/src/test/java/fr/greencodeinitiative/java/checks/AvoidMultipleIfElseStatementTest.java
+++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI2/AvoidMultipleIfElseStatementTest.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,8 +15,9 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.checks;
+package org.greencodeinitiative.creedengo.java.checks.GCI2;
+import org.greencodeinitiative.creedengo.java.checks.AvoidMultipleIfElseStatement;
import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;
@@ -24,11 +25,11 @@ class AvoidMultipleIfElseStatementTest {
@Test
void test() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/AvoidMultipleIfElseStatement.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI2/AvoidMultipleIfElseStatement.java")
.withCheck(new AvoidMultipleIfElseStatement())
.verifyIssues();
CheckVerifier.newVerifier()
- .onFile("src/test/files/AvoidMultipleIfElseStatementNoIssue.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI2/AvoidMultipleIfElseStatementNoIssue.java")
.withCheck(new AvoidMultipleIfElseStatement())
.verifyNoIssues();
}
@@ -36,15 +37,23 @@ void test() {
@Test
void testInterfaceMethodStatement() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/AvoidMultipleIfElseStatementInterface.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI2/AvoidMultipleIfElseStatementInterfaceNoIssue.java")
.withCheck(new AvoidMultipleIfElseStatement())
.verifyNoIssues();
}
@Test
- void testNotBlockStatement() {
+ void testNoBlockStatement() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/AvoidMultipleIfElseStatementNotBlock.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI2/AvoidMultipleIfElseStatementNoBlockNoIssue.java")
+ .withCheck(new AvoidMultipleIfElseStatement())
+ .verifyNoIssues();
+ }
+
+ @Test
+ void testCompareMethod() {
+ CheckVerifier.newVerifier()
+ .onFile(System.getProperty("testfiles.path") + "/GCI2/AvoidMultipleIfElseStatementCompareMethodNoIssue.java")
.withCheck(new AvoidMultipleIfElseStatement())
.verifyNoIssues();
}
diff --git a/src/test/java/fr/greencodeinitiative/java/checks/ArrayCopyCheckTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI27/ArrayCopyCheckTest.java
similarity index 70%
rename from src/test/java/fr/greencodeinitiative/java/checks/ArrayCopyCheckTest.java
rename to src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI27/ArrayCopyCheckTest.java
index 5f0b1276..1ab0c890 100644
--- a/src/test/java/fr/greencodeinitiative/java/checks/ArrayCopyCheckTest.java
+++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI27/ArrayCopyCheckTest.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,8 +15,9 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.checks;
+package org.greencodeinitiative.creedengo.java.checks.GCI27;
+import org.greencodeinitiative.creedengo.java.checks.ArrayCopyCheck;
import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;
@@ -28,9 +29,9 @@ class ArrayCopyCheckTest {
@Test
void test() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/ArrayCopyCheck.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI27/ArrayCopyCheck.java")
.withCheck(new ArrayCopyCheck())
.verifyIssues();
}
-}
\ No newline at end of file
+}
diff --git a/src/test/java/fr/greencodeinitiative/java/checks/OptimizeReadFileExceptionCheckTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI28/OptimizeReadFileExceptionCheckTest.java
similarity index 64%
rename from src/test/java/fr/greencodeinitiative/java/checks/OptimizeReadFileExceptionCheckTest.java
rename to src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI28/OptimizeReadFileExceptionCheckTest.java
index b3ef8c89..4491fe3b 100644
--- a/src/test/java/fr/greencodeinitiative/java/checks/OptimizeReadFileExceptionCheckTest.java
+++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI28/OptimizeReadFileExceptionCheckTest.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,8 +15,9 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.checks;
+package org.greencodeinitiative.creedengo.java.checks.GCI28;
+import org.greencodeinitiative.creedengo.java.checks.OptimizeReadFileExceptions;
import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;
@@ -25,7 +26,7 @@ class OptimizeReadFileExceptionCheckTest {
@Test
void test() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/OptimizeReadFileExceptionCheck.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI28/OptimizeReadFileExceptionCheck.java")
.withCheck(new OptimizeReadFileExceptions())
.verifyIssues();
}
@@ -33,7 +34,7 @@ void test() {
@Test
void test2() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/OptimizeReadFileExceptionCheck2.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI28/OptimizeReadFileExceptionCheck2.java")
.withCheck(new OptimizeReadFileExceptions())
.verifyIssues();
}
@@ -41,7 +42,7 @@ void test2() {
@Test
void test3() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/OptimizeReadFileExceptionCheck3.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI28/OptimizeReadFileExceptionCheck3.java")
.withCheck(new OptimizeReadFileExceptions())
.verifyIssues();
}
@@ -49,7 +50,7 @@ void test3() {
@Test
void test4() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/OptimizeReadFileExceptionCheck4.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI28/OptimizeReadFileExceptionCheck4.java")
.withCheck(new OptimizeReadFileExceptions())
.verifyIssues();
}
@@ -57,7 +58,7 @@ void test4() {
@Test
void test5() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/OptimizeReadFileExceptionCheck5.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI28/OptimizeReadFileExceptionCheck5.java")
.withCheck(new OptimizeReadFileExceptions())
.verifyIssues();
}
diff --git a/src/test/java/fr/greencodeinitiative/java/checks/AvoidGettingSizeCollectionInLoopTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI3/AvoidGettingSizeCollectionInLoopTest.java
similarity index 63%
rename from src/test/java/fr/greencodeinitiative/java/checks/AvoidGettingSizeCollectionInLoopTest.java
rename to src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI3/AvoidGettingSizeCollectionInLoopTest.java
index 8083c1db..adfad5ab 100644
--- a/src/test/java/fr/greencodeinitiative/java/checks/AvoidGettingSizeCollectionInLoopTest.java
+++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI3/AvoidGettingSizeCollectionInLoopTest.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,16 +15,17 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.checks;
+package org.greencodeinitiative.creedengo.java.checks.GCI3;
+import org.greencodeinitiative.creedengo.java.checks.AvoidGettingSizeCollectionInLoop;
import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;
class AvoidGettingSizeCollectionInLoopTest {
@Test
- public void testBadForLoop() {
+ void testBadForLoop() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/AvoidGettingSizeCollectionInForLoopBad.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI3/AvoidGettingSizeCollectionInForLoopBad.java")
.withCheck(new AvoidGettingSizeCollectionInLoop())
.verifyIssues();
}
@@ -32,7 +33,7 @@ public void testBadForLoop() {
@Test
void testIgnoredForLoop() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/AvoidGettingSizeCollectionInForLoopIgnored.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI3/AvoidGettingSizeCollectionInForLoopIgnored.java")
.withCheck(new AvoidGettingSizeCollectionInLoop())
.verifyNoIssues();
}
@@ -40,7 +41,7 @@ void testIgnoredForLoop() {
@Test
void testGoodForLoop() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/AvoidGettingSizeCollectionInForLoopGood.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI3/AvoidGettingSizeCollectionInForLoopGood.java")
.withCheck(new AvoidGettingSizeCollectionInLoop())
.verifyNoIssues();
}
@@ -48,7 +49,7 @@ void testGoodForLoop() {
@Test
void testBadWhileFoop() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/AvoidGettingSizeCollectionInWhileLoopBad.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI3/AvoidGettingSizeCollectionInWhileLoopBad.java")
.withCheck(new AvoidGettingSizeCollectionInLoop())
.verifyIssues();
}
@@ -56,7 +57,7 @@ void testBadWhileFoop() {
@Test
void testIgnoredWhileFoop() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/AvoidGettingSizeCollectionInWhileLoopIgnored.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI3/AvoidGettingSizeCollectionInWhileLoopIgnored.java")
.withCheck(new AvoidGettingSizeCollectionInLoop())
.verifyNoIssues();
}
@@ -64,7 +65,7 @@ void testIgnoredWhileFoop() {
@Test
void testGoodWhileLoop() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/AvoidGettingSizeCollectionInWhileLoopGood.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI3/AvoidGettingSizeCollectionInWhileLoopGood.java")
.withCheck(new AvoidGettingSizeCollectionInLoop())
.verifyNoIssues();
}
@@ -72,7 +73,7 @@ void testGoodWhileLoop() {
@Test
void testIgnoredForEachLoop() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/AvoidGettingSizeCollectionInForEachLoopIgnored.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI3/AvoidGettingSizeCollectionInForEachLoopIgnored.java")
.withCheck(new AvoidGettingSizeCollectionInLoop())
.verifyNoIssues();
}
diff --git a/src/test/java/fr/greencodeinitiative/java/checks/InitializeBufferWithAppropriateSizeTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI32/InitializeBufferWithAppropriateSizeTest.java
similarity index 68%
rename from src/test/java/fr/greencodeinitiative/java/checks/InitializeBufferWithAppropriateSizeTest.java
rename to src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI32/InitializeBufferWithAppropriateSizeTest.java
index deef83e8..9c92dae3 100644
--- a/src/test/java/fr/greencodeinitiative/java/checks/InitializeBufferWithAppropriateSizeTest.java
+++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI32/InitializeBufferWithAppropriateSizeTest.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,8 +15,9 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.checks;
+package org.greencodeinitiative.creedengo.java.checks.GCI32;
+import org.greencodeinitiative.creedengo.java.checks.InitializeBufferWithAppropriateSize;
import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;
@@ -25,9 +26,9 @@ class InitializeBufferWithAppropriateSizeTest {
@Test
void test() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/InitializeBufferWithAppropriateSize.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI32/InitializeBufferWithAppropriateSize.java")
.withCheck(new InitializeBufferWithAppropriateSize())
.verifyIssues();
}
-}
\ No newline at end of file
+}
diff --git a/src/test/java/fr/greencodeinitiative/java/checks/AvoidStatementForDMLQueriesTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI5/AvoidStatementForDMLQueriesTest.java
similarity index 68%
rename from src/test/java/fr/greencodeinitiative/java/checks/AvoidStatementForDMLQueriesTest.java
rename to src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI5/AvoidStatementForDMLQueriesTest.java
index b74683eb..e593914c 100644
--- a/src/test/java/fr/greencodeinitiative/java/checks/AvoidStatementForDMLQueriesTest.java
+++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI5/AvoidStatementForDMLQueriesTest.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,8 +15,9 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.checks;
+package org.greencodeinitiative.creedengo.java.checks.GCI5;
+import org.greencodeinitiative.creedengo.java.checks.AvoidStatementForDMLQueries;
import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;
@@ -24,7 +25,7 @@ class AvoidStatementForDMLQueriesTest {
@Test
void test() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/AvoidStatementForDMLQueries.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI5/AvoidStatementForDMLQueries.java")
.withCheck(new AvoidStatementForDMLQueries())
.verifyIssues();
}
diff --git a/src/test/java/fr/greencodeinitiative/java/checks/IncrementCheckTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI67/IncrementCheckTest.java
similarity index 69%
rename from src/test/java/fr/greencodeinitiative/java/checks/IncrementCheckTest.java
rename to src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI67/IncrementCheckTest.java
index e9d5b98e..1a5171f8 100644
--- a/src/test/java/fr/greencodeinitiative/java/checks/IncrementCheckTest.java
+++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI67/IncrementCheckTest.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,8 +15,9 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.checks;
+package org.greencodeinitiative.creedengo.java.checks.GCI67;
+import org.greencodeinitiative.creedengo.java.checks.IncrementCheck;
import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;
@@ -25,9 +26,9 @@ class IncrementCheckTest {
@Test
void test() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/IncrementCheck.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI67/IncrementCheck.java")
.withCheck(new IncrementCheck())
.verifyIssues();
}
-}
\ No newline at end of file
+}
diff --git a/src/test/java/fr/greencodeinitiative/java/checks/NoFunctionCallWhenDeclaringForLoopTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI69/NoFunctionCallWhenDeclaringForLoopTest.java
similarity index 68%
rename from src/test/java/fr/greencodeinitiative/java/checks/NoFunctionCallWhenDeclaringForLoopTest.java
rename to src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI69/NoFunctionCallWhenDeclaringForLoopTest.java
index ef3c983f..d9d83fdf 100644
--- a/src/test/java/fr/greencodeinitiative/java/checks/NoFunctionCallWhenDeclaringForLoopTest.java
+++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI69/NoFunctionCallWhenDeclaringForLoopTest.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,8 +15,9 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.checks;
+package org.greencodeinitiative.creedengo.java.checks.GCI69;
+import org.greencodeinitiative.creedengo.java.checks.NoFunctionCallWhenDeclaringForLoop;
import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;
@@ -24,7 +25,7 @@ class NoFunctionCallWhenDeclaringForLoopTest {
@Test
void test() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/NoFunctionCallWhenDeclaringForLoop.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI69/NoFunctionCallWhenDeclaringForLoop.java")
.withCheck(new NoFunctionCallWhenDeclaringForLoop())
.verifyIssues();
}
diff --git a/src/test/java/fr/greencodeinitiative/java/checks/AvoidSQLRequestInLoopCheckTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI72/AvoidSQLRequestInLoopCheckTest.java
similarity index 68%
rename from src/test/java/fr/greencodeinitiative/java/checks/AvoidSQLRequestInLoopCheckTest.java
rename to src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI72/AvoidSQLRequestInLoopCheckTest.java
index 5abed41f..7e99d797 100644
--- a/src/test/java/fr/greencodeinitiative/java/checks/AvoidSQLRequestInLoopCheckTest.java
+++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI72/AvoidSQLRequestInLoopCheckTest.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,8 +15,9 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.checks;
+package org.greencodeinitiative.creedengo.java.checks.GCI72;
+import org.greencodeinitiative.creedengo.java.checks.AvoidSQLRequestInLoop;
import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;
@@ -25,9 +26,9 @@ class AvoidSQLRequestInLoopCheckTest {
@Test
void test() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/AvoidSQLRequestInLoopCheck.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI72/AvoidSQLRequestInLoopCheck.java")
.withCheck(new AvoidSQLRequestInLoop())
.verifyIssues();
}
-}
\ No newline at end of file
+}
diff --git a/src/test/java/fr/greencodeinitiative/java/checks/AvoidFullSQLRequestCheckTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI74/AvoidFullSQLRequestCheckTest.java
similarity index 68%
rename from src/test/java/fr/greencodeinitiative/java/checks/AvoidFullSQLRequestCheckTest.java
rename to src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI74/AvoidFullSQLRequestCheckTest.java
index 25ead7c4..28f27390 100644
--- a/src/test/java/fr/greencodeinitiative/java/checks/AvoidFullSQLRequestCheckTest.java
+++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI74/AvoidFullSQLRequestCheckTest.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,8 +15,9 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.checks;
+package org.greencodeinitiative.creedengo.java.checks.GCI74;
+import org.greencodeinitiative.creedengo.java.checks.AvoidFullSQLRequest;
import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;
@@ -25,9 +26,9 @@ class AvoidFullSQLRequestCheckTest {
@Test
void test() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/AvoidFullSQLRequestCheck.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI74/AvoidFullSQLRequestCheck.java")
.withCheck(new AvoidFullSQLRequest())
.verifyIssues();
}
-}
\ No newline at end of file
+}
diff --git a/src/test/java/fr/greencodeinitiative/java/checks/AvoidUsageOfStaticCollectionsTests.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI76/AvoidUsageOfStaticCollectionsTests.java
similarity index 67%
rename from src/test/java/fr/greencodeinitiative/java/checks/AvoidUsageOfStaticCollectionsTests.java
rename to src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI76/AvoidUsageOfStaticCollectionsTests.java
index 2ac4ed35..b777f820 100644
--- a/src/test/java/fr/greencodeinitiative/java/checks/AvoidUsageOfStaticCollectionsTests.java
+++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI76/AvoidUsageOfStaticCollectionsTests.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,8 +15,9 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.checks;
+package org.greencodeinitiative.creedengo.java.checks.GCI76;
+import org.greencodeinitiative.creedengo.java.checks.AvoidUsageOfStaticCollections;
import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;
@@ -25,7 +26,7 @@ class AvoidUsageOfStaticCollectionsTests {
@Test
void testHasIssues() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/AvoidUsageOfStaticCollections.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI76/AvoidUsageOfStaticCollections.java")
.withCheck(new AvoidUsageOfStaticCollections())
.verifyIssues();
}
@@ -33,7 +34,7 @@ void testHasIssues() {
@Test
void testNoIssues() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/GoodUsageOfStaticCollections.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI76/AvoidUsageOfStaticCollectionsGoodWay.java")
.withCheck(new AvoidUsageOfStaticCollections())
.verifyNoIssues();
}
diff --git a/src/test/java/fr/greencodeinitiative/java/checks/AvoidRegexPatternNotStaticTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI77/AvoidRegexPatternNotStaticTest.java
similarity index 60%
rename from src/test/java/fr/greencodeinitiative/java/checks/AvoidRegexPatternNotStaticTest.java
rename to src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI77/AvoidRegexPatternNotStaticTest.java
index 17502322..470658bc 100644
--- a/src/test/java/fr/greencodeinitiative/java/checks/AvoidRegexPatternNotStaticTest.java
+++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI77/AvoidRegexPatternNotStaticTest.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,8 +15,9 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.checks;
+package org.greencodeinitiative.creedengo.java.checks.GCI77;
+import org.greencodeinitiative.creedengo.java.checks.AvoidRegexPatternNotStatic;
import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;
@@ -25,7 +26,7 @@ class AvoidRegexPatternNotStaticTest {
@Test
void testHasIssues() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/AvoidRegexPatternNotStatic.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI77/AvoidRegexPatternNotStatic.java")
.withCheck(new AvoidRegexPatternNotStatic())
.verifyIssues();
}
@@ -34,9 +35,9 @@ void testHasIssues() {
void testHasNoIssues() {
CheckVerifier.newVerifier()
.onFiles(
- "src/test/files/ValidRegexPattern.java",
- "src/test/files/ValidRegexPattern2.java",
- "src/test/files/ValidRegexPattern3.java"
+ System.getProperty("testfiles.path") + "/GCI77/AvoidRegexPatternNotStaticValid1.java",
+ System.getProperty("testfiles.path") + "/GCI77/AvoidRegexPatternNotStaticValid2.java",
+ System.getProperty("testfiles.path") + "/GCI77/AvoidRegexPatternNotStaticValid3.java"
)
.withCheck(new AvoidRegexPatternNotStatic())
.verifyNoIssues();
diff --git a/src/test/java/fr/greencodeinitiative/java/checks/AvoidSetConstantInBatchInsertTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI78/AvoidSetConstantInBatchInsertTest.java
similarity index 68%
rename from src/test/java/fr/greencodeinitiative/java/checks/AvoidSetConstantInBatchInsertTest.java
rename to src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI78/AvoidSetConstantInBatchInsertTest.java
index 8c138fde..8c6464a9 100644
--- a/src/test/java/fr/greencodeinitiative/java/checks/AvoidSetConstantInBatchInsertTest.java
+++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI78/AvoidSetConstantInBatchInsertTest.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,8 +15,9 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.checks;
+package org.greencodeinitiative.creedengo.java.checks.GCI78;
+import org.greencodeinitiative.creedengo.java.checks.AvoidSetConstantInBatchUpdate;
import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;
@@ -25,9 +26,9 @@ class AvoidSetConstantInBatchInsertTest {
@Test
void test() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/AvoidSetConstantInBatchUpdateCheck.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI78/AvoidSetConstantInBatchUpdateCheck.java")
.withCheck(new AvoidSetConstantInBatchUpdate())
.verifyIssues();
}
-}
\ No newline at end of file
+}
diff --git a/src/test/java/fr/greencodeinitiative/java/checks/FreeResourcesOfAutoCloseableInterfaceTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI79/FreeResourcesOfAutoCloseableInterfaceTest.java
similarity index 66%
rename from src/test/java/fr/greencodeinitiative/java/checks/FreeResourcesOfAutoCloseableInterfaceTest.java
rename to src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI79/FreeResourcesOfAutoCloseableInterfaceTest.java
index 9a9ca492..d0836297 100644
--- a/src/test/java/fr/greencodeinitiative/java/checks/FreeResourcesOfAutoCloseableInterfaceTest.java
+++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI79/FreeResourcesOfAutoCloseableInterfaceTest.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,17 +15,18 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.checks;
+package org.greencodeinitiative.creedengo.java.checks.GCI79;
+import org.greencodeinitiative.creedengo.java.checks.FreeResourcesOfAutoCloseableInterface;
import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;
class FreeResourcesOfAutoCloseableInterfaceTest {
@Test
- void test() {
+ void test_with_java7() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/FreeResourcesOfAutoCloseableInterface.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI79/FreeResourcesOfAutoCloseableInterface.java")
.withCheck(new FreeResourcesOfAutoCloseableInterface())
.withJavaVersion(7)
.verifyIssues();
@@ -34,8 +35,9 @@ void test() {
@Test
void test_no_java_version() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/FreeResourcesOfAutoCloseableInterface.java")
+ .onFile(System.getProperty("testfiles.path") + "/GCI79/FreeResourcesOfAutoCloseableInterface.java")
.withCheck(new FreeResourcesOfAutoCloseableInterface())
.verifyIssues();
}
-}
\ No newline at end of file
+
+}
diff --git a/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI82/MakeNonReassignedVariablesConstantsTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI82/MakeNonReassignedVariablesConstantsTest.java
new file mode 100644
index 00000000..313bd5cb
--- /dev/null
+++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI82/MakeNonReassignedVariablesConstantsTest.java
@@ -0,0 +1,74 @@
+/*
+ * 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.GCI82;
+
+import org.greencodeinitiative.creedengo.java.checks.MakeNonReassignedVariablesConstants;
+import org.junit.jupiter.api.Test;
+import org.sonar.java.checks.verifier.CheckVerifier;
+
+class MakeNonReassignedVariablesConstantsTest {
+
+ @Test
+ void test() {
+ CheckVerifier.newVerifier()
+ .onFile(System.getProperty("testfiles.path") + "/GCI82/MakeNonReassignedVariablesConstants.java")
+ .withCheck(new MakeNonReassignedVariablesConstants())
+ .verifyIssues();
+ }
+
+ @Test
+ void testLombok() {
+ CheckVerifier.newVerifier()
+ .onFile(System.getProperty("testfiles.path") + "/GCI82/MakeNonReassignedVariablesConstantsForLombok.java")
+ .withCheck(new MakeNonReassignedVariablesConstants())
+ .verifyIssues();
+ }
+
+ @Test
+ void testWithoutLombokImport() {
+ CheckVerifier.newVerifier()
+ .onFile(System.getProperty("testfiles.path") + "/GCI82/MakeNonReassignedVariablesConstantsWithoutLombokImport.java")
+ .withCheck(new MakeNonReassignedVariablesConstants())
+ .verifyIssues();
+ }
+
+ @Test
+ void testRecord() {
+ CheckVerifier.newVerifier()
+ .onFile(System.getProperty("testfiles.path") + "/GCI82/MakeNonReassignedVariablesConstantsForRecord.java")
+ .withCheck(new MakeNonReassignedVariablesConstants())
+ .verifyNoIssues();
+ }
+
+ @Test
+ void testInstanceOf() {
+ CheckVerifier.newVerifier()
+ .onFile(System.getProperty("testfiles.path") + "/GCI82/MakeNonReassignedVariablesConstantsForInstanceOf.java")
+ .withCheck(new MakeNonReassignedVariablesConstants())
+ .verifyIssues();
+ }
+
+ @Test
+ void testAbstractMethods() {
+ CheckVerifier.newVerifier()
+ .onFile(System.getProperty("testfiles.path") + "/GCI82/MakeNonReassignedVariablesConstantsForAbstractMethod.java")
+ .withCheck(new MakeNonReassignedVariablesConstants())
+ .verifyIssues();
+ }
+
+}
diff --git a/src/test/java/fr/greencodeinitiative/java/checks/AvoidUsingGlobalVariablesCheckCheckTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI94/UseOptionalOrElseGetVsOrElseTest.java
similarity index 61%
rename from src/test/java/fr/greencodeinitiative/java/checks/AvoidUsingGlobalVariablesCheckCheckTest.java
rename to src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI94/UseOptionalOrElseGetVsOrElseTest.java
index 31e05b15..5818615e 100644
--- a/src/test/java/fr/greencodeinitiative/java/checks/AvoidUsingGlobalVariablesCheckCheckTest.java
+++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/GCI94/UseOptionalOrElseGetVsOrElseTest.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,20 +15,19 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.checks;
+package org.greencodeinitiative.creedengo.java.checks.GCI94;
+import org.greencodeinitiative.creedengo.java.checks.UseOptionalOrElseGetVsOrElse;
import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;
-@Deprecated
-class AvoidUsingGlobalVariablesCheckCheckTest {
+class UseOptionalOrElseGetVsOrElseTest {
@Test
void test() {
CheckVerifier.newVerifier()
- .onFile("src/test/files/AvoidUsingGlobalVariablesCheck.java")
- .withCheck(new AvoidUsingGlobalVariablesCheck())
+ .onFile(System.getProperty("testfiles.path") + "/GCI94/UseOptionalOrElseGetVsOrElse.java")
+ .withCheck(new UseOptionalOrElseGetVsOrElse())
.verifyIssues();
}
-
}
diff --git a/src/test/java/fr/greencodeinitiative/java/utils/FilesUtils.java b/src/test/java/org/greencodeinitiative/creedengo/java/utils/FilesUtils.java
similarity index 92%
rename from src/test/java/fr/greencodeinitiative/java/utils/FilesUtils.java
rename to src/test/java/org/greencodeinitiative/creedengo/java/utils/FilesUtils.java
index ed06d4ce..d32c90de 100644
--- a/src/test/java/fr/greencodeinitiative/java/utils/FilesUtils.java
+++ b/src/test/java/org/greencodeinitiative/creedengo/java/utils/FilesUtils.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.utils;
+package org.greencodeinitiative.creedengo.java.utils;
import java.io.File;
import java.io.IOException;
diff --git a/src/test/java/fr/greencodeinitiative/java/utils/StringUtilsTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/utils/StringUtilsTest.java
similarity index 84%
rename from src/test/java/fr/greencodeinitiative/java/utils/StringUtilsTest.java
rename to src/test/java/org/greencodeinitiative/creedengo/java/utils/StringUtilsTest.java
index 9e6d9328..de5082de 100644
--- a/src/test/java/fr/greencodeinitiative/java/utils/StringUtilsTest.java
+++ b/src/test/java/org/greencodeinitiative/creedengo/java/utils/StringUtilsTest.java
@@ -1,6 +1,6 @@
/*
- * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
- * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ * 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
@@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package fr.greencodeinitiative.java.utils;
+package org.greencodeinitiative.creedengo.java.utils;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
diff --git a/tool_build.sh b/tool_build.sh
index bfac031a..a5f6a094 100755
--- a/tool_build.sh
+++ b/tool_build.sh
@@ -1,3 +1,3 @@
#!/usr/bin/env sh
-mvn clean package -DskipTests
+./mvnw clean package -DskipTests
diff --git a/tool_compile.sh b/tool_compile.sh
index 3d3e0d89..9d4da3c7 100755
--- a/tool_compile.sh
+++ b/tool_compile.sh
@@ -1,3 +1,3 @@
#!/usr/bin/env sh
-mvn clean compile
+./mvnw clean compile
diff --git a/tool_release_1_prepare.sh b/tool_release_1_prepare.sh
index e87b946d..7b996e3c 100755
--- a/tool_release_1_prepare.sh
+++ b/tool_release_1_prepare.sh
@@ -5,9 +5,9 @@
###
# creation of 2 commits with release and next SNAPSHOT
-mvn release:prepare -B -ff -DpushChanges=false -DtagNameFormat=@{project.version}
+./mvnw release:prepare -B -ff -DpushChanges=false -DtagNameFormat=@{project.version}
sleep 2
# clean temporary files
-mvn release:clean
+./mvnw release:clean