Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sonar Java如何获取变量类的注释_Java_Spring_Sonarqube - Fatal编程技术网

Sonar Java如何获取变量类的注释

Sonar Java如何获取变量类的注释,java,spring,sonarqube,Java,Spring,Sonarqube,我正在编写一个自定义插件来检测声明变量类型为MyOtherClass的类,并给出警告,因为MyOtherClass是prototype类型 基本上,我需要从MyClass中获取字段,需要在field(MyOtherClass)类中获取注释,并且需要找到注释值是否包含prototype这是您的答案吗 @Component public MyClass{ private MyOtherClass myOtherClass; @Autowired public MyClass(MyOtherCla

我正在编写一个自定义插件来检测声明变量类型为
MyOtherClass
的类,并给出警告,因为MyOtherClass是prototype类型

基本上,我需要从MyClass中获取字段,需要在field(MyOtherClass)类中获取注释,并且需要找到注释值是否包含
prototype

这是您的答案吗

@Component
public MyClass{

private MyOtherClass myOtherClass;

@Autowired
public MyClass(MyOtherClass myOtherClass){
 this.myOtherClass = myOtherClass;
}

}


@Component
@Scope("prototype")// OR
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE,
        proxyMode = ScopedProxyMode.TARGET_CLASS)
public MyOtherClass{
}

找到了一种读取变量类注释的方法。

这是否回答了您的问题?不。我正在使用声纳源编写自定义规则-。Sonar使用api读取方法、变量和类。
public class MyClass {

    private MyOtherClass myOtherClass;

    public MyClass(MyOtherClass myOtherClass){
        this.myOtherClass = myOtherClass;
    }


    public static void main(String[] args) {
        Field[] declaredFields = MyClass.class.getDeclaredFields();
        for(Field field:declaredFields){
            Scope scope = field.getType().getAnnotation(Scope.class);
            if(scope!=null){
                String value = scope.value();
                System.out.println(value);
            }
        }
    }
}





@Scope("prototype")
public class MyOtherClass {

}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Scope {
    String value();
}
 @Override
public void visitNode(Tree tree) { 
    org.sonar.plugins.java.api.tree.VariableTree variableTree = (VariableTree) tree;

    if (variableTree.type().symbolType().symbol().metadata().isAnnotatedWith(SCOPE_ANNOTATION_FQN)) {
        System.out.println("prototype annotation found " + variableTree.symbol().metadata().toString());
        reportIssue(variableTree.simpleName(), "This spring bean is of type PROTOTYPE");
    }
}