-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Rule EC82: Make variable constant
- Loading branch information
Hachemi ATROUNE
committed
May 30, 2024
1 parent
716fe9b
commit 9017297
Showing
7 changed files
with
116 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/main/java/fr/greencodeinitiative/java/checks/MakeVariableConstant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* 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 <http://www.gnu.org/licenses/>. | ||
*/ | ||
package fr.greencodeinitiative.java.checks; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.sonar.check.Rule; | ||
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; | ||
import org.sonar.plugins.java.api.semantic.Symbol; | ||
import org.sonar.plugins.java.api.tree.*; | ||
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; | ||
|
||
@Rule(key = "EC82") | ||
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S82") | ||
public class MakeVariableConstant extends IssuableSubscriptionVisitor { | ||
public static final String ERROR_MESSAGE = "A variable is never reassigned and can be made constant"; | ||
|
||
@Override | ||
public List<Tree.Kind> nodesToVisit() { | ||
return List.of(Tree.Kind.VARIABLE); | ||
} | ||
|
||
@Override | ||
public void visitNode(Tree tree) { | ||
VariableTree variableTree = (VariableTree) tree; | ||
Symbol symbol = variableTree.symbol(); | ||
if (symbol.isVariableSymbol() && !symbol.isFinal()) { | ||
boolean reassigned = isReassigned(symbol); | ||
if (!reassigned) { | ||
reportIssue(variableTree.simpleName(), ERROR_MESSAGE); | ||
} | ||
} | ||
} | ||
|
||
private boolean isReassigned(Symbol symbol) { | ||
Tree parent = symbol.declaration().parent(); | ||
if (parent instanceof BlockTree) { | ||
return isReassignedInBlock(symbol, (BlockTree) parent); | ||
} | ||
return false; | ||
} | ||
|
||
private boolean isReassignedInBlock(Symbol symbol, BlockTree blockTree) { | ||
List<AssignmentExpressionTree> assignments = new ArrayList<>(); | ||
List<StatementTree> body = blockTree.body(); | ||
|
||
for (StatementTree statement : body) { | ||
if (statement.is(Tree.Kind.EXPRESSION_STATEMENT)) { | ||
ExpressionTree expression = ((ExpressionStatementTree) statement).expression(); | ||
if (expression.is(Tree.Kind.ASSIGNMENT)) { | ||
assignments.add((AssignmentExpressionTree) expression); | ||
} | ||
} | ||
} | ||
|
||
for (AssignmentExpressionTree assignment : assignments) { | ||
if (((IdentifierTree) assignment.variable()).symbol().equals(symbol)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
public class MakeVariableConstantTest { | ||
|
||
public void testEc82() { | ||
String test = "test"; // Noncompliant {{A variable is never reassigned and can be made constant}} | ||
System.out.println("Hello World"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/test/java/fr/greencodeinitiative/java/checks/MakeVariableConstantTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package fr.greencodeinitiative.java.checks; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.sonar.java.checks.verifier.CheckVerifier; | ||
|
||
public class MakeVariableConstantTest { | ||
|
||
@Test | ||
void test() { | ||
CheckVerifier.newVerifier() | ||
.onFile("src/test/files/MakeVariableConstantTest.java") | ||
.withCheck(new MakeVariableConstant()) | ||
.verifyIssues(); | ||
} | ||
} | ||
|
||
|