Skip to content

Commit

Permalink
green-code-initiative#92 improves code quality (reduce code duplication)
Browse files Browse the repository at this point in the history
  • Loading branch information
jycr committed Jun 29, 2023
1 parent 351055e commit 5c0d987
Show file tree
Hide file tree
Showing 41 changed files with 44 additions and 266 deletions.
2 changes: 2 additions & 0 deletions ecocode-rules-specifications/src/main/rules/EC3/EC3.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"constantCost": "5min"
},
"tags": [
"bad-practice",
"performance",
"eco-design",
"performance",
"ecocode"
Expand Down
2 changes: 1 addition & 1 deletion ecocode-rules-specifications/src/main/rules/EC34/EC34.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"title": "Avoid using try-catch-finally statement",
"title": "Avoid using try-catch statement",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
Expand Down
1 change: 1 addition & 0 deletions ecocode-rules-specifications/src/main/rules/EC66/EC66.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"constantCost": "5min"
},
"tags": [
"bad-practice",
"eco-design",
"performance",
"ecocode"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree;
Expand Down Expand Up @@ -37,11 +36,7 @@
* @author Aubay
* @formatter:off
*/
@Rule(key = "EC27",
name = "Developpement",
description = ArrayCopyCheck.MESSAGERULE,
priority = Priority.MINOR,
tags = {"performance", "eco-design", "ecocode"})
@Rule(key = "EC27")
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "GRPS0027")
public class ArrayCopyCheck extends IssuableSubscriptionVisitor {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import java.util.Arrays;
import java.util.List;

import javax.annotation.Nonnull;

import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.tree.AssignmentExpressionTree;
Expand All @@ -15,12 +13,7 @@
import org.sonar.plugins.java.api.tree.Tree;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;

@Rule(
key = "EC75",
name = "Developpement",
description = AvoidConcatenateStringsInLoop.MESSAGE_RULE,
priority = Priority.MINOR,
tags = {"performance", "eco-design", "ecocode", "memory"})
@Rule(key = "EC75")
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S75")
public class AvoidConcatenateStringsInLoop extends IssuableSubscriptionVisitor {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,15 @@
import static java.util.Collections.singletonList;
import static java.util.regex.Pattern.CASE_INSENSITIVE;
import static java.util.regex.Pattern.compile;
import org.sonar.check.Priority;

import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.tree.LiteralTree;
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.Tree.Kind;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;

@Rule(
key = "EC74",
name = "Developpement",
description = AvoidFullSQLRequest.MESSAGERULE,
priority = Priority.MINOR,
tags = {"performance", "sql", "eco-design", "ecocode", "network"})
@Rule(key = "EC74")
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S74")
public class AvoidFullSQLRequest extends IssuableSubscriptionVisitor {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.semantic.MethodMatchers;
Expand All @@ -18,11 +17,7 @@
import org.sonar.plugins.java.api.tree.WhileStatementTree;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;

@Rule(key = "EC3",
name = "Developpement",
description = AvoidGettingSizeCollectionInLoop.MESSAGERULE,
priority = Priority.MINOR,
tags = {"performance", "eco-design", "ecocode"})
@Rule(key = "EC3")
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "GSCIL")
public class AvoidGettingSizeCollectionInLoop extends IssuableSubscriptionVisitor {
protected static final String MESSAGERULE = "Avoid getting the size of the collection in the loop";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.Arrays;
import java.util.List;

import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.tree.BlockTree;
Expand All @@ -12,11 +11,7 @@
import org.sonar.plugins.java.api.tree.Tree;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;

@Rule(key = "EC2",
name = "Developpement",
description = AvoidMultipleIfElseStatement.RULE_MESSAGE,
priority = Priority.MINOR,
tags = {"performance", "eco-design", "ecocode"})
@Rule(key = "EC2")
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "AMIES")
public class AvoidMultipleIfElseStatement extends IssuableSubscriptionVisitor {
protected static final String RULE_MESSAGE = "Using a switch statement instead of multiple if-else if possible";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import javax.annotation.Nonnull;

import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.semantic.MethodMatchers;
Expand All @@ -16,12 +15,7 @@
import org.sonar.plugins.java.api.tree.Tree;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;

@Rule(
key = "EC77",
name = "Developpement",
description = AvoidRegexPatternNotStatic.MESSAGE_RULE,
priority = Priority.MINOR,
tags = {"performance", "regex", "eco-design", "ecocode", "memory"})
@Rule(key = "EC77")
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S77")
public class AvoidRegexPatternNotStatic extends IssuableSubscriptionVisitor {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.Arrays;
import java.util.List;

import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.semantic.MethodMatchers;
Expand All @@ -14,8 +13,7 @@
import org.sonar.plugins.java.api.tree.Tree.Kind;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;

@Rule(key = "EC72", name = "Developpement", description = AvoidSQLRequestInLoop.MESSAGERULE, priority = Priority.MINOR,
tags = {"performance", "sql", "spring", "eco-design", "ecocode", "memory", "network"})
@Rule(key = "EC72")
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S72")
public class AvoidSQLRequestInLoop extends IssuableSubscriptionVisitor {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import static fr.greencodeinitiative.java.checks.ConstOrLiteralDeclare.isLiteral;
import static java.util.Arrays.asList;
import org.sonar.check.Priority;

import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.semantic.MethodMatchers;
Expand All @@ -21,10 +21,7 @@
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", name = "Developpement",
description = AvoidSetConstantInBatchUpdate.MESSAGERULE,
priority = Priority.MINOR,
tags = {"eco-design", "ecocode", "sql", "performance", "memory"})
@Rule(key = "EC78")
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S78")
public class AvoidSetConstantInBatchUpdate extends IssuableSubscriptionVisitor {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.Arrays;
import java.util.List;

import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.semantic.MethodMatchers;
Expand All @@ -12,11 +11,7 @@
import org.sonar.plugins.java.api.tree.Tree;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;

@Rule(key = "EC1",
name = "Developpement",
description = AvoidSpringRepositoryCallInLoopCheck.RULE_MESSAGE,
priority = Priority.MINOR,
tags = {"performance", "spring", "eco-design", "ecocode"})
@Rule(key = "EC1")
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "GRC1")
public class AvoidSpringRepositoryCallInLoopCheck extends IssuableSubscriptionVisitor {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.sonar.plugins.java.api.tree.Tree;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;

@Rule(key = "EC5", tags={"performance", "sql", "eco-design", "ecocode"})
@Rule(key = "EC5")
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "SDMLQ1")
public class AvoidStatementForDMLQueries extends IssuableSubscriptionVisitor {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,14 @@

import javax.annotation.Nonnull;

import org.sonar.check.Priority;
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.Tree;
import org.sonar.plugins.java.api.tree.VariableTree;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;

@Rule(
key = "EC76",
name = "Developpement",
description = AvoidUsageOfStaticCollections.MESSAGE_RULE,
priority = Priority.MINOR,
tags = {"cwe", "leak", "eco-design", "ecocode", "memory"})
@Rule(key = "EC76")
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S76")
public class AvoidUsageOfStaticCollections extends IssuableSubscriptionVisitor {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,14 @@
import java.util.List;

import com.google.re2j.Pattern;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.Tree.Kind;
import org.sonar.plugins.java.api.tree.VariableTree;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;

@Rule(
key = "EC4",
name = "Developpement",
description = "<p>Prefer local variables to globals</p>",
priority = Priority.MINOR,
tags = {"performance", "eco-design", "ecocode"})
@Rule(key = "EC4")
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "D4")
public class AvoidUsingGlobalVariablesCheck extends IssuableSubscriptionVisitor {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import javax.annotation.ParametersAreNonnullByDefault;

import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.JavaFileScannerContext;
Expand All @@ -19,12 +18,7 @@
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;


@Rule(
key = "EC79",
name = "Developpement",
description = FreeResourcesOfAutoCloseableInterface.MESSAGE_RULE,
priority = Priority.MINOR,
tags = {"performance", "eco-design", "ecocode"})
@Rule(key = "EC79")
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S79")
public class FreeResourcesOfAutoCloseableInterface extends IssuableSubscriptionVisitor {
private final Deque<TryStatementTree> withinTry = new LinkedList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@
import java.util.Collections;
import java.util.List;

import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.Tree.Kind;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;

@Rule(
key = "EC67",
name = "Developpement",
description = IncrementCheck.MESSAGERULE,
priority = Priority.MINOR,
tags = {"performance", "eco-design", "ecocode"})
@Rule(key = "EC67")
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S67")
public class IncrementCheck extends IssuableSubscriptionVisitor {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,14 @@
import java.util.Collections;
import java.util.List;

import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.tree.NewClassTree;
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.Tree.Kind;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;

@Rule(
key = "EC32",
name = "Developpement",
description = InitializeBufferWithAppropriateSize.RULE_MESSAGE,
priority = Priority.MINOR,
tags = {"performance", "eco-design", "ecocode"})
@Rule(key = "EC32")
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "GRSP0032")
public class InitializeBufferWithAppropriateSize extends IssuableSubscriptionVisitor {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.List;
import java.util.Map;

import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.tree.BaseTreeVisitor;
Expand All @@ -21,8 +20,7 @@
import org.sonar.plugins.java.api.tree.Tree;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;

@Rule(key = "EC69", name = "Developpement", description = NoFunctionCallWhenDeclaringForLoop.MESSAGERULE, priority = Priority.MINOR,
tags = {"performance", "eco-design", "ecocode"})
@Rule(key = "EC69")
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S69")
public class NoFunctionCallWhenDeclaringForLoop extends IssuableSubscriptionVisitor {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.tree.CatchTree;
Expand All @@ -16,12 +15,7 @@
import org.sonar.plugins.java.api.tree.TryStatementTree;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;

@Rule(
key = "EC28",
name = "Developpement",
description = OptimizeReadFileExceptions.MESSAGERULE,
priority = Priority.MINOR,
tags = {"performance", "error-handling", "eco-design", "ecocode"})
@Rule(key = "EC28")
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "GRSP0028")
public class OptimizeReadFileExceptions extends IssuableSubscriptionVisitor {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package fr.greencodeinitiative.java.checks;

import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.JavaFileScanner;
import org.sonar.plugins.java.api.JavaFileScannerContext;
Expand All @@ -11,8 +10,7 @@
import javax.annotation.CheckForNull;
import java.util.*;

@Rule(key = "EC63", name = "Developpement", description = "Do not unnecessarily assign values to variables", priority = Priority.MINOR,
tags = {"eco-design", "ecocode", "memory"})
@Rule(key = "EC63")
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S63")
public class UnnecessarilyAssignValuesToVariables extends BaseTreeVisitor implements JavaFileScanner {

Expand Down
Loading

0 comments on commit 5c0d987

Please sign in to comment.