【问题标题】:how to scan java properties file in sonarqube [closed]如何在sonarqube中扫描java属性文件[关闭]
【发布时间】:2018-11-19 14:03:30
【问题描述】:

我正在使用 SonarQube 编写自定义规则来扫描属性和配置文件。 能否请您指导我如何编写此自定义代码。

【问题讨论】:

    标签: properties sonarqube rules


    【解决方案1】:

    有一个java属性文件https://github.com/racodond/sonar-jproperties-plugin的插件。您可以分叉它并编写您的自定义规则。 这是一个示例规则,用于检查不允许的键和值组合

    public class KeyValueCheck extends DoubleDispatchVisitorCheck {
        private static final String SIMPLE_IS_PATTERN_TEMPLATE = "(%s)";
        protected final Pattern patternKey;
        protected final Pattern patternValue;
        private final String VIOLATION_MESSAGE;
        private final boolean matches;
        boolean checkValue = false;
    
        public KeyValueCheck(String key, String value, String message, boolean matches) {
            VIOLATION_MESSAGE = message;
            this.matches = matches;
            this.patternKey = Pattern.compile(String.format(SIMPLE_IS_PATTERN_TEMPLATE, key), Pattern.CASE_INSENSITIVE);
            this.patternValue = Pattern.compile(String.format(SIMPLE_IS_PATTERN_TEMPLATE, value), Pattern.CASE_INSENSITIVE);
        }
    
        @Override
        public void visitKey(KeyTree tree) {
            Matcher matcher = patternKey.matcher(tree.text());
            if (matcher.matches()) {
                checkValue = true;
            }
    
            super.visitKey(tree);
        }
    
        @Override
        public void visitValue(ValueTree tree) {
            if (checkValue) {
                Matcher matcher = patternValue.matcher(tree.text());
                if (matches == patternValue.matcher(tree.text()).matches()) {
                    addPreciseIssue(tree, VIOLATION_MESSAGE);
                }
    
                checkValue = false;
            }
            super.visitValue(tree);
        }
    }
    

    【讨论】:

    • 谢谢,有没有关于如何编写自定义声纳规则的参考资料。
    • sonarqube 是多语言的,我可以得到德语或法语的用户界面
    • 自定义声纳规则 - 互联网上有很多示例,但最好的来源是插件本身的源代码。您可以在 SonarQube 电子邮件组groups.google.com/forum/#!forum/sonarqube 中获得很多帮助
    • 非常感谢您的意见
    猜你喜欢
    • 2012-04-04
    • 1970-01-01
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    相关资源
    最近更新 更多