From 0dabc961544e217b17e0775dbeb2a834793b1013 Mon Sep 17 00:00:00 2001 From: Guo XIn <371864209@qq.com> Date: Tue, 9 Jan 2024 15:28:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cxx/rules/SecurityDesignRuleRepository.java | 9 ++++++--- .../cxx/rules/checkers/ABCVarNameChecker.java | 17 +++++++++-------- .../JavaSecurityDesignRulesRepository.java | 2 +- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/rules/SecurityDesignRuleRepository.java b/sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/rules/SecurityDesignRuleRepository.java index 0410b0e..22f4737 100644 --- a/sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/rules/SecurityDesignRuleRepository.java +++ b/sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/rules/SecurityDesignRuleRepository.java @@ -15,19 +15,22 @@ import java.util.Arrays; import java.util.List; /** - * TODO SecurityDesignRuleRepository + * 信息安全性设计准则规则仓库定义实现类 * * @author GuoXin * @date 2024/1/9 */ public class SecurityDesignRuleRepository implements RulesDefinition { private final String REPOSITORY_NAME = "C++信息安全性设计准则"; - private final List RULES_DEFINITIONS = Arrays.asList(ABCVarNameChecker.class); + + // 规则检查器的集合,当有新的规则开发完毕后,需要添加到下面的集合中 + private final List RULE_CHECKERS = Arrays.asList(ABCVarNameChecker.class); + @Override public void define(Context context) { var repository = context.createRepository("cxx-security-design-rules", CxxLanguage.KEY). setName(REPOSITORY_NAME); - new AnnotationBasedRulesDefinition(repository, CxxLanguage.KEY).addRuleClasses(false, RULES_DEFINITIONS); + new AnnotationBasedRulesDefinition(repository, CxxLanguage.KEY).addRuleClasses(false, RULE_CHECKERS); repository.done(); } } diff --git a/sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/rules/checkers/ABCVarNameChecker.java b/sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/rules/checkers/ABCVarNameChecker.java index 9835c35..20adf10 100644 --- a/sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/rules/checkers/ABCVarNameChecker.java +++ b/sonar-keyware-plugins-cxx/src/main/java/com/keyware/sonar/cxx/rules/checkers/ABCVarNameChecker.java @@ -10,17 +10,15 @@ import com.sonar.cxx.sslr.api.AstNode; import com.sonar.cxx.sslr.api.Grammar; import org.sonar.check.Priority; import org.sonar.check.Rule; -import org.sonar.check.RuleProperty; import org.sonar.cxx.parser.CxxGrammarImpl; import org.sonar.cxx.squidbridge.annotations.ActivatedByDefault; import org.sonar.cxx.squidbridge.annotations.SqaleConstantRemediation; import org.sonar.cxx.squidbridge.checks.SquidCheck; import static com.sonar.cxx.sslr.api.GenericTokenType.IDENTIFIER; -import static javax.swing.Action.DEFAULT; /** - * TODO ABCVarNameChecker + * C++规则检查器的实现示例 * * @author GuoXin * @date 2024/1/6 @@ -29,20 +27,23 @@ import static javax.swing.Action.DEFAULT; @ActivatedByDefault @SqaleConstantRemediation("5min") public class ABCVarNameChecker extends SquidCheck { + /** - * format + * 初始化规则检查器 */ - @RuleProperty( - key = "format", - defaultValue = "" + DEFAULT) - public String format = DEFAULT; @Override public void init() { + // 订阅要检查AST节点类型,用于在visitNode方法中检查该类型节点 this.subscribeTo( CxxGrammarImpl.declaratorId ); } + /** + * 检查AST节点 + * + * @param astNode 要处理的AST节点,该节点类型为通过subscribeTo方法订阅的类型 + */ @Override public void visitNode(AstNode astNode) { var idNode = astNode.getLastChild(IDENTIFIER); diff --git a/sonar-keyware-plugins-java/src/main/java/com/keyware/sonar/java/rules/JavaSecurityDesignRulesRepository.java b/sonar-keyware-plugins-java/src/main/java/com/keyware/sonar/java/rules/JavaSecurityDesignRulesRepository.java index b440253..61c64f4 100644 --- a/sonar-keyware-plugins-java/src/main/java/com/keyware/sonar/java/rules/JavaSecurityDesignRulesRepository.java +++ b/sonar-keyware-plugins-java/src/main/java/com/keyware/sonar/java/rules/JavaSecurityDesignRulesRepository.java @@ -30,7 +30,7 @@ public class JavaSecurityDesignRulesRepository implements RulesDefinition { private static final String RESOURCE_BASE_PATH = "org/sonar/l10n/java/rules/java"; public static final String REPOSITORY_KEY = "keyware-java-security-design"; - public static final String REPOSITORY_NAME = "Java安全性设计准则"; + public static final String REPOSITORY_NAME = "Java信息安全性设计准则"; // 添加需要视为模板规则的规则的规则键 private static final Set RULE_TEMPLATES_KEY = Collections.emptySet();