新增“规定循环次数的上限”准则

wuhaoyang
renfengshan 10 months ago
parent d3a5f15271
commit 5c9946bc87
  1. 78
      sonar-keyware-plugins-java/src/main/java/com/keyware/sonar/java/rules/checkers/UpperCycleLimitRuleChecker.java
  2. 9
      sonar-keyware-plugins-java/src/main/resources/org/sonar/l10n/java/rules/java/UpperCycleLimitRuleChecker.html
  3. 13
      sonar-keyware-plugins-java/src/main/resources/org/sonar/l10n/java/rules/java/UpperCycleLimitRuleChecker.json
  4. 17
      sonar-keyware-plugins-java/src/test/files/UpperCycleLimitRule.java
  5. 28
      sonar-keyware-plugins-java/src/test/java/com/keyware/sonar/java/rules/checkers/UpperCycleLimitRuleCheckerTest.java

@ -0,0 +1,78 @@
package com.keyware.sonar.java.rules.checkers;
import org.sonar.check.Rule;
import org.sonar.java.model.expression.BinaryExpressionTreeImpl;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.tree.*;
import java.util.List;
@Rule(key = "UpperCycleLimitRuleChecker")
public class UpperCycleLimitRuleChecker extends IssuableSubscriptionVisitor {
@Override
public List<Tree.Kind> nodesToVisit() {
return List.of(Tree.Kind.METHOD);
}
@Override
public void visitNode(Tree tree) {
//将Tree强转成MethodTree
MethodTree methodTree = (MethodTree) tree;
List<VariableTree> args = methodTree.parameters();
BlockTree blockTree = methodTree.block();
blockTree.accept(new MethodBlockVisitor(this, args));
}
class MethodBlockVisitor extends BaseTreeVisitor {
protected UpperCycleLimitRuleChecker checker;
final List<VariableTree> args;
public MethodBlockVisitor(UpperCycleLimitRuleChecker checker, List<VariableTree> args) {
this.args = args;
this.checker = checker;
}
@Override
public void visitForStatement(ForStatementTree fnode) {
var ffnode = fnode.condition();
if (ffnode instanceof BinaryExpressionTreeImpl) {
ExpressionTree leftOperand = ((BinaryExpressionTreeImpl) ffnode).leftOperand();
checkVar(leftOperand);
ExpressionTree rightOperand = ((BinaryExpressionTreeImpl) ffnode).rightOperand();
checkVar(rightOperand);
}
}
@Override
public void visitWhileStatement(WhileStatementTree wnode) {
var wwnode = wnode.condition();
ExpressionTree teea = ((BinaryExpressionTreeImpl) wwnode).leftOperand();
checkVar(teea);
}
@Override
public void visitDoWhileStatement(DoWhileStatementTree dnode) {
var ddnode = dnode.condition();
ExpressionTree teeas = ((BinaryExpressionTreeImpl) ddnode).leftOperand();
checkVar(teeas);
}
private void checkVar(ExpressionTree operand) {
if (operand instanceof IdentifierTree) {
IdentifierTree identifierTree = (IdentifierTree) operand;
var name = identifierTree.name();
for (VariableTree varTree : args) {
if (varTree.simpleName().name().equals(name)) {
System.out.println(identifierTree.firstToken().range().start().line() + ": " + name + " " + "规定循环次数的上限,在将用户输入的数据用于循环条件前进行验证用户输入的数据是否超过上限");
checker.context.reportIssue(checker, identifierTree, "规定循环次数的上限,在将用户输入的数据用于循环条件前进行验证用户输入的数据是否超过上限");
}
}
}
}
}
}

@ -0,0 +1,9 @@
<h2>规定循环次数的上限</h2>
<p>在将用户输入的数据用于循环条件前进行验证用户输入的数据是否超过上限</p>
<pre>
</pre>
<h2>合规解决方案</h2>
<pre>
</pre>

@ -0,0 +1,13 @@
{
"title": "规定循环次数的上限",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"28suo"
],
"defaultSeverity": "Minor"
}

@ -0,0 +1,17 @@
class UpperCycleLimitRule{
public static void Upper(int number){
for(int i = 0; i < number; i++){ // Noncompliant {{规定循环次数的上限,在将用户输入的数据用于循环条件前进行验证用户输入的数据是否超过上限}}
};
while (number > 0){ // Noncompliant {{规定循环次数的上限,在将用户输入的数据用于循环条件前进行验证用户输入的数据是否超过上限}}
};
do{
}while (number > 0); // Noncompliant {{规定循环次数的上限,在将用户输入的数据用于循环条件前进行验证用户输入的数据是否超过上限}}
};
}

@ -0,0 +1,28 @@
package com.keyware.sonar.java.rules.checkers;
import com.keyware.sonar.java.utils.FilesUtils;
import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;
public class UpperCycleLimitRuleCheckerTest {
@Test
void detected() {
UpperCycleLimitRuleChecker rule = new UpperCycleLimitRuleChecker();
// Verifies that the check will raise the adequate issues with the expected message.
// In the test file, lines which should raise an issue have been commented out
// by using the following syntax: "// Noncompliant {{EXPECTED_MESSAGE}}"
CheckVerifier.newVerifier()
.onFile("src/test/files/UpperCycleLimitRule.java")
.withCheck(rule)
.withClassPath(FilesUtils.getClassPath("target/test-jars"))
.verifyIssues();
}
}
Loading…
Cancel
Save