From 5c9946bc873592cd7ff04ee440a56eca90d1b562 Mon Sep 17 00:00:00 2001 From: renfengshan <1535889807@qqcom> Date: Fri, 12 Jan 2024 17:07:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E2=80=9C=E8=A7=84=E5=AE=9A?= =?UTF-8?q?=E5=BE=AA=E7=8E=AF=E6=AC=A1=E6=95=B0=E7=9A=84=E4=B8=8A=E9=99=90?= =?UTF-8?q?=E2=80=9D=E5=87=86=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../checkers/UpperCycleLimitRuleChecker.java | 78 +++++++++++++++++++ .../java/UpperCycleLimitRuleChecker.html | 9 +++ .../java/UpperCycleLimitRuleChecker.json | 13 ++++ .../src/test/files/UpperCycleLimitRule.java | 17 ++++ .../UpperCycleLimitRuleCheckerTest.java | 28 +++++++ 5 files changed, 145 insertions(+) create mode 100644 sonar-keyware-plugins-java/src/main/java/com/keyware/sonar/java/rules/checkers/UpperCycleLimitRuleChecker.java create mode 100644 sonar-keyware-plugins-java/src/main/resources/org/sonar/l10n/java/rules/java/UpperCycleLimitRuleChecker.html create mode 100644 sonar-keyware-plugins-java/src/main/resources/org/sonar/l10n/java/rules/java/UpperCycleLimitRuleChecker.json create mode 100644 sonar-keyware-plugins-java/src/test/files/UpperCycleLimitRule.java create mode 100644 sonar-keyware-plugins-java/src/test/java/com/keyware/sonar/java/rules/checkers/UpperCycleLimitRuleCheckerTest.java diff --git a/sonar-keyware-plugins-java/src/main/java/com/keyware/sonar/java/rules/checkers/UpperCycleLimitRuleChecker.java b/sonar-keyware-plugins-java/src/main/java/com/keyware/sonar/java/rules/checkers/UpperCycleLimitRuleChecker.java new file mode 100644 index 0000000..5b08479 --- /dev/null +++ b/sonar-keyware-plugins-java/src/main/java/com/keyware/sonar/java/rules/checkers/UpperCycleLimitRuleChecker.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 nodesToVisit() { + return List.of(Tree.Kind.METHOD); + } + + @Override + public void visitNode(Tree tree) { + //将Tree强转成MethodTree + MethodTree methodTree = (MethodTree) tree; + List args = methodTree.parameters(); + BlockTree blockTree = methodTree.block(); + blockTree.accept(new MethodBlockVisitor(this, args)); + } + + + class MethodBlockVisitor extends BaseTreeVisitor { + protected UpperCycleLimitRuleChecker checker; + final List args; + + public MethodBlockVisitor(UpperCycleLimitRuleChecker checker, List 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, "规定循环次数的上限,在将用户输入的数据用于循环条件前进行验证用户输入的数据是否超过上限"); + } + } + } + } + + + } +} diff --git a/sonar-keyware-plugins-java/src/main/resources/org/sonar/l10n/java/rules/java/UpperCycleLimitRuleChecker.html b/sonar-keyware-plugins-java/src/main/resources/org/sonar/l10n/java/rules/java/UpperCycleLimitRuleChecker.html new file mode 100644 index 0000000..32fe7be --- /dev/null +++ b/sonar-keyware-plugins-java/src/main/resources/org/sonar/l10n/java/rules/java/UpperCycleLimitRuleChecker.html @@ -0,0 +1,9 @@ +

规定循环次数的上限

+

在将用户输入的数据用于循环条件前进行验证用户输入的数据是否超过上限

+
+
+
+

合规解决方案

+
+
+
\ No newline at end of file diff --git a/sonar-keyware-plugins-java/src/main/resources/org/sonar/l10n/java/rules/java/UpperCycleLimitRuleChecker.json b/sonar-keyware-plugins-java/src/main/resources/org/sonar/l10n/java/rules/java/UpperCycleLimitRuleChecker.json new file mode 100644 index 0000000..8efffcb --- /dev/null +++ b/sonar-keyware-plugins-java/src/main/resources/org/sonar/l10n/java/rules/java/UpperCycleLimitRuleChecker.json @@ -0,0 +1,13 @@ +{ + "title": "规定循环次数的上限", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "28suo" + ], + "defaultSeverity": "Minor" +} \ No newline at end of file diff --git a/sonar-keyware-plugins-java/src/test/files/UpperCycleLimitRule.java b/sonar-keyware-plugins-java/src/test/files/UpperCycleLimitRule.java new file mode 100644 index 0000000..1e3ef3d --- /dev/null +++ b/sonar-keyware-plugins-java/src/test/files/UpperCycleLimitRule.java @@ -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 {{规定循环次数的上限,在将用户输入的数据用于循环条件前进行验证用户输入的数据是否超过上限}} + }; + +} \ No newline at end of file diff --git a/sonar-keyware-plugins-java/src/test/java/com/keyware/sonar/java/rules/checkers/UpperCycleLimitRuleCheckerTest.java b/sonar-keyware-plugins-java/src/test/java/com/keyware/sonar/java/rules/checkers/UpperCycleLimitRuleCheckerTest.java new file mode 100644 index 0000000..a7e428a --- /dev/null +++ b/sonar-keyware-plugins-java/src/test/java/com/keyware/sonar/java/rules/checkers/UpperCycleLimitRuleCheckerTest.java @@ -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(); + + + } + +}