Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EC34][Java][5R] - Rules "try..catch.." #128

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,7 @@
import java.util.Collections;
import java.util.List;

import fr.greencodeinitiative.java.checks.ArrayCopyCheck;
import fr.greencodeinitiative.java.checks.AvoidConcatenateStringsInLoop;
import fr.greencodeinitiative.java.checks.AvoidFullSQLRequest;
import fr.greencodeinitiative.java.checks.AvoidGettingSizeCollectionInLoop;
import fr.greencodeinitiative.java.checks.AvoidMultipleIfElseStatement;
import fr.greencodeinitiative.java.checks.AvoidRegexPatternNotStatic;
import fr.greencodeinitiative.java.checks.AvoidSQLRequestInLoop;
import fr.greencodeinitiative.java.checks.AvoidSetConstantInBatchUpdate;
import fr.greencodeinitiative.java.checks.AvoidSpringRepositoryCallInLoopCheck;
import fr.greencodeinitiative.java.checks.AvoidStatementForDMLQueries;
import fr.greencodeinitiative.java.checks.AvoidUsageOfStaticCollections;
import fr.greencodeinitiative.java.checks.AvoidUsingGlobalVariablesCheck;
import fr.greencodeinitiative.java.checks.FreeResourcesOfAutoCloseableInterface;
import fr.greencodeinitiative.java.checks.IncrementCheck;
import fr.greencodeinitiative.java.checks.InitializeBufferWithAppropriateSize;
import fr.greencodeinitiative.java.checks.NoFunctionCallWhenDeclaringForLoop;
import fr.greencodeinitiative.java.checks.OptimizeReadFileExceptions;
import fr.greencodeinitiative.java.checks.UnnecessarilyAssignValuesToVariables;
import fr.greencodeinitiative.java.checks.UseCorrectForLoop;
import fr.greencodeinitiative.java.checks.*;
import org.sonar.plugins.java.api.JavaCheck;

public final class RulesList {
Expand Down Expand Up @@ -77,7 +59,8 @@ public static List<Class<? extends JavaCheck>> getJavaChecks() {
AvoidUsingGlobalVariablesCheck.class,
AvoidSetConstantInBatchUpdate.class,
FreeResourcesOfAutoCloseableInterface.class,
AvoidMultipleIfElseStatement.class
AvoidMultipleIfElseStatement.class,
AvoidTryCatchFinallyCheck.class
));
}

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

import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.tree.SyntaxToken;
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.TryStatementTree;
import java.util.Arrays;
import java.util.List;

@Rule(
key = "EC34",
name = "Developpement",
description = AvoidTryCatchFinallyCheck.MESSAGE,
priority = Priority.MINOR,
tags = {"ecocode", "eco-design"})
public class AvoidTryCatchFinallyCheck extends IssuableSubscriptionVisitor {

public static final String MESSAGE = "Avoid using try-catch-finally";

@Override
public List<Tree.Kind> nodesToVisit() {
return Arrays.asList(Tree.Kind.TRY_STATEMENT);
}

@Override
public void visitNode(Tree tree) {
TryStatementTree tryStatement = (TryStatementTree) tree;
reportIssue(tree, MESSAGE);

}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<p>
Use try catch... only if necessary
</p>
<p>When an exception is thrown, a variable (the exception itself) is created in the catch block and destroyed at the end of the block. Creating this variable and destroying it consumes CPU cycles and RAM unnecessarily. That is why it is important not to use this construction and to prefer, as much as possible, a logical test.</p>
<h2> Noncompliant Code Example</h2>
<pre>
try{
isNumber(2); // isNumber is a function which checks if argument is number
echo "It's a number";
}catch(CheckTypeException $exception){ // CheckTypeException personalized exception
echo "Erreur:" $execption->getMessage();
}
</pre>
<h2> Compliant Code Example</h2>
<pre>
if(isNumber(2){
echo "It's a number";
}else{
echo "It's not a number";
}
</pre>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"title": "Avoid using try-catch-finally",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "50min"
},
"tags": [
"eco-design",
"ecocode"
],
"defaultSeverity": "Info"
}
27 changes: 27 additions & 0 deletions java-plugin/src/test/files/AvoidTryCatchFinallyCheck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class AvoidUsingFinallyInTryCatchCheckClass{
public void inverse(int x) {
if (!x) {
System.out.println("Division par zero");
}
return 1/x;
}

public void function() {
try {// Noncompliant {{Avoid using try-catch-finally}}
System.out.println(inverse(1)+"\n");
} catch (Exception e) {
System.out.println("Exception reçue : "+ e.printStackTrace()+ "\n");
}
finally {
System.out.println("Première fin.\n");
}
}
public void functionEncore() {
try {// Noncompliant {{Avoid using try-catch-finally}}
System.out.println(inverse(1)+"\n");
} catch (Exception e) {
System.out.println("Exception reçue : "+ e.printStackTrace()+ "\n");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void checkNumberRules() {
final JavaCheckRegistrar registrar = new JavaCheckRegistrar();
registrar.register(context);

assertThat(context.checkClasses()).hasSize(19);
assertThat(context.checkClasses()).hasSize(20);
assertThat(context.testCheckClasses()).isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package fr.greencodeinitiative.java.checks;

import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;


class AvoidAvoidTryCatchFinallyCheckTest {

@Test
public void test() {
CheckVerifier.newVerifier()
.onFile("src/test/files/AvoidTryCatchFinallyCheck.java")
.withCheck(new AvoidTryCatchFinallyCheck())
.verifyIssues();
}
}