sonarqube自定义规则插件,想要获取类注解里的标签怎么做

公司要开发一个检测Controller必须要含有@Api注解的插件,

现在已经可以检测到是否含有@Api,

但是还有个需求是@Api注解里必须要包含tags属性,如@Api(tags = "机构管理"),

请问各位大佬如何可以获取到注解里的标签,我试了好多办法都不行

目前代码如下:

@Rule(
        // 规则id
        key = "ControllerAnnotitionCheck",
        // 规则名称
        name = "Controller和Action必须包含@Api注解和tags标签",
        // 规则介绍
        description = "Controller和Action必须包含@Api注解和tags标签",
        // 规则标签
        tags = {"wxtx-java"},
        // 规则级别
        priority = Priority.CRITICAL)


public class ControllerAnnotitionCheck extends BaseTreeVisitor implements JavaFileScanner {
    private JavaFileScannerContext context;
    private String className;
    private List<AnnotationTree> annotations;
    private boolean isApiAnno = false;
    @Override
    public void scanFile(JavaFileScannerContext javaFileScannerContext) {
        this.context = javaFileScannerContext;
        scan(context.getTree());
    }
    @Override
    public void visitClass(ClassTree tree) {
        if(tree == null || tree.simpleName() == null){
            super.visitClass(tree);
            return;
        }
        annotations = tree.modifiers().annotations();
        className = tree.simpleName().name();
        if(isControllerOrAction(className)){
            for (AnnotationTree annotationTree : annotations){
                String annotationName = annotationTree.annotationType().firstToken().text();
                if (annotations != null && "Api".equals(annotationName)) {
                    isApiAnno = true;
                    String text = annotationTree.arguments().openParenToken().text();
//                    String arg = annotationTree.arguments().lastToken().text();
                    System.out.println("arg=========================="+text);
                }
            }
            if (!isApiAnno){
                context.reportIssue(this, tree, "Controller必须包含@Api注解");
            }
        }
        super.visitClass(tree);
    }

    /**
     * 判断是否为Controller或Action
     * @param className
     * @return
     */
    private boolean isControllerOrAction(String className){
        return className.endsWith("Controller") || className.endsWith("ApiAction");
    }
}

 

同问 作者 解决了吗

你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答

本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。

​​​​因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。