-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(EC22): Implementing Avoid Method Usage for Only Basic Operations
- Loading branch information
Showing
7 changed files
with
248 additions
and
21 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
52 changes: 52 additions & 0 deletions
52
.../main/java/fr/greencodeinitiative/java/checks/AvoidMethodUsageForOnlyBasicOperations.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,52 @@ | ||
package fr.greencodeinitiative.java.checks; | ||
|
||
import org.sonar.check.Rule; | ||
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; | ||
import org.sonar.plugins.java.api.tree.*; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.function.Predicate; | ||
|
||
import static fr.greencodeinitiative.java.checks.AvoidMethodUsageForOnlyBasicOperations.RULE_MESSAGE; | ||
import static org.sonar.check.Priority.MINOR; | ||
|
||
/** | ||
* @author Mohamed Oussama A. | ||
* Created on 05/04/2023 | ||
* @version 1.0 | ||
*/ | ||
|
||
@Rule(key = "EC22", | ||
name = RULE_MESSAGE, | ||
description = RULE_MESSAGE, | ||
priority = MINOR, | ||
tags = {"smell", "ecocode", "eco-design", "memory", "performance"}) | ||
public class AvoidMethodUsageForOnlyBasicOperations extends IssuableSubscriptionVisitor { | ||
|
||
protected static final String RULE_MESSAGE = "Avoid Method Usage For Only Basic Operations"; | ||
|
||
@Override | ||
public List<Tree.Kind> nodesToVisit() { | ||
return Collections.singletonList(Tree.Kind.METHOD); | ||
} | ||
|
||
|
||
@Override | ||
public void visitNode(Tree tree) { | ||
Optional.ofNullable(((MethodTree) tree).block()) | ||
.map(BlockTree::body) | ||
.filter(list -> list.size() == 1) | ||
.flatMap(list -> list.stream().findFirst()) | ||
.filter(ReturnStatementTree.class::isInstance) | ||
.map(statementTree -> ((ReturnStatementTree) statementTree).expression()) | ||
.filter(this.isBasicOperation) | ||
.ifPresent(expressionTree -> reportIssue(expressionTree, RULE_MESSAGE)); | ||
} | ||
|
||
|
||
private Predicate<ExpressionTree> isBasicOperation = expression -> expression instanceof BinaryExpressionTree | ||
|| expression instanceof UnaryExpressionTree | ||
|| expression.is(Tree.Kind.CONDITIONAL_EXPRESSION); | ||
} |
13 changes: 13 additions & 0 deletions
13
java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC22.html
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,13 @@ | ||
<p>we should not use methods for only basic operations</p> | ||
<h2>Non compliant Code Example</h2> | ||
<pre> | ||
public int sum(int a, int b) { | ||
return a + b; // Noncompliant | ||
} | ||
|
||
x = sum(5, 4); // Noncompliant | ||
</pre> | ||
<h2>Compliant Solution</h2> | ||
<pre> | ||
x = a + b; | ||
</pre> |
16 changes: 16 additions & 0 deletions
16
java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC22.json
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,16 @@ | ||
{ | ||
"title": "we should not use methods for only basic operations", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "5min" | ||
}, | ||
"tags": [ | ||
"eco-design", | ||
"memory", | ||
"performance", | ||
"ecocode" | ||
], | ||
"defaultSeverity": "Minor" | ||
} |
143 changes: 143 additions & 0 deletions
143
java-plugin/src/test/files/AvoidMethodUsageForOnlyBasicOperations.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,143 @@ | ||
/** | ||
* @author Mohamed Oussama A. | ||
* Created on 05/04/2023 | ||
* @version 1.0 | ||
*/ | ||
public class AvoidBasicOperationsUsageMethods { | ||
|
||
private int x =0; | ||
|
||
/* | ||
* Below method must be reported by an other Rule of unnecessarily assignment of variable | ||
* (but not with this rule - SRP - SOLID ) | ||
* */ | ||
|
||
public static int getMin(int a, int b) { | ||
int c = a < b ? a : b; // Compliant | ||
return c; | ||
} | ||
|
||
public int getX() { | ||
return x; // Compliant | ||
} | ||
|
||
/* | ||
* Conditional expressions using ternanry operator | ||
*/ | ||
public static int getMin(int a, int b) { | ||
return a < b ? a : b; // Noncompliant {{Avoid Method Usage For Only Basic Operations}} | ||
} | ||
|
||
public static int calculateFormuleX(int a, int b) { | ||
return a / a * b < b ? a - b : b + a; // Noncompliant {{Avoid Method Usage For Only Basic Operations}} | ||
} | ||
|
||
/* | ||
* Binary Operations | ||
*/ | ||
|
||
//Using Arithmetic Operators | ||
|
||
public static int add(int a, int b) { | ||
return a + b; // Noncompliant {{Avoid Method Usage For Only Basic Operations}} | ||
} | ||
|
||
public static int substract(int a, int b) { | ||
return a - b; // Noncompliant {{Avoid Method Usage For Only Basic Operations}} | ||
} | ||
|
||
public static int multiply(int a, int b) { | ||
return a * b; // Noncompliant {{Avoid Method Usage For Only Basic Operations}} | ||
} | ||
|
||
public static int divide(int a, int b) { | ||
return a / b; // Noncompliant {{Avoid Method Usage For Only Basic Operations}} | ||
} | ||
|
||
public static int modulus(int a, int b) { | ||
return a % b; // Noncompliant {{Avoid Method Usage For Only Basic Operations}} | ||
} | ||
|
||
//Using Logical Operators | ||
|
||
public static boolean compareAnd(int a, int b) { | ||
return (a > b) && (b > 0); // Noncompliant {{Avoid Method Usage For Only Basic Operations}} | ||
} | ||
|
||
public static boolean compareOr(int a, int b) { | ||
return (a > b) || (b > 0); // Noncompliant {{Avoid Method Usage For Only Basic Operations}} | ||
} | ||
|
||
public static boolean equalsTo(int a, int b) { | ||
return a == b; // Noncompliant {{Avoid Method Usage For Only Basic Operations}} | ||
} | ||
|
||
public static boolean notEqualsTo(int a, int b) { | ||
return !(a == b); // Noncompliant {{Avoid Method Usage For Only Basic Operations}} | ||
} | ||
|
||
public static boolean compare1(int a, int b) { | ||
return (a > b) && (b > 0); // Noncompliant {{Avoid Method Usage For Only Basic Operations}} | ||
} | ||
|
||
// Using Bitwise operators | ||
|
||
public static boolean andBitwise(int a, int b) { | ||
return a & b; // Noncompliant {{Avoid Method Usage For Only Basic Operations}} | ||
} | ||
|
||
public static boolean orBitwise(int a, int b) { | ||
return a | b; // Noncompliant {{Avoid Method Usage For Only Basic Operations}} | ||
} | ||
|
||
public static boolean xorBitwise(int a, int b) { | ||
return a ^ b; // Noncompliant {{Avoid Method Usage For Only Basic Operations}} | ||
} | ||
|
||
/* | ||
* Unary Operations | ||
*/ | ||
|
||
public static int negate(int a) { | ||
return -a; // Noncompliant {{Avoid Method Usage For Only Basic Operations}} | ||
} | ||
|
||
public static int preIncrement(int a) { | ||
return ++a; // Noncompliant {{Avoid Method Usage For Only Basic Operations}} | ||
} | ||
|
||
public static int postDecrement(int a) { | ||
return a--; // Noncompliant {{Avoid Method Usage For Only Basic Operations}} | ||
} | ||
|
||
public static int notBitwise(int a) { | ||
return ~a; // Noncompliant {{Avoid Method Usage For Only Basic Operations}} | ||
} | ||
|
||
// Using Bitwise operators | ||
|
||
public static int negate(int a) { | ||
return -a; // Noncompliant {{Avoid Method Usage For Only Basic Operations}} | ||
} | ||
|
||
public static int preIncrement(int a) { | ||
return ++a; // Noncompliant {{Avoid Method Usage For Only Basic Operations}} | ||
} | ||
|
||
public static int postDecrement(int a) { | ||
return a--; // Noncompliant {{Avoid Method Usage For Only Basic Operations}} | ||
} | ||
|
||
public static int leftShift(int a) { | ||
return a << 1; // Noncompliant {{Avoid Method Usage For Only Basic Operations}} | ||
} | ||
|
||
public static int rightShift(int a) { | ||
return a >> 1; // Noncompliant {{Avoid Method Usage For Only Basic Operations}} | ||
} | ||
|
||
public static int zeroFillRightShift(int a) { | ||
return a >>> 1; // Noncompliant {{Avoid Method Usage For Only Basic Operations}} | ||
} | ||
|
||
} |
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
20 changes: 20 additions & 0 deletions
20
...t/java/fr/greencodeinitiative/java/checks/AvoidMethodUsageForOnlyBasicOperationsTest.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,20 @@ | ||
package fr.greencodeinitiative.java.checks; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.sonar.java.checks.verifier.CheckVerifier; | ||
|
||
/** | ||
* @author Mohamed Oussama ABIDI. | ||
* Created on 05/04/2023 | ||
* @version 1.0 | ||
*/ | ||
class AvoidMethodUsageForOnlyBasicOperationsTest { | ||
|
||
@Test | ||
void it_should_verify_non_compliant_basic_operations_usage_in_methods() { | ||
CheckVerifier.newVerifier() | ||
.onFile("src/test/files/AvoidMethodUsageForOnlyBasicOperations.java") | ||
.withCheck(new AvoidMethodUsageForOnlyBasicOperations()) | ||
.verifyIssues(); | ||
} | ||
} |