Sonar-Java如何获取变量类的注解

huangapple go评论58阅读模式
英文:

Sonar-Java how to get annotations of a variable class

问题

@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{
}
英文:
@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{
}

I am writing a custom plugin to detect classes which declare variable of type MyOtherClass and give a warning because MyOtherClass is of type prototype.

Basically I need to get field from MyClass and need to get Annotation on the field(MyOtherClass) class and need to find if the annotation value contains prototype

答案1

得分: 0

@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("找到了原型注解 " + variableTree.symbol().metadata().toString());
        reportIssue(variableTree.simpleName(), "这个 Spring Bean 的类型是原型");
    }
}

Found a way to read annotations on a variable class.

英文:
 @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");
    }
}

Found a way to read annotations on a variable class.

答案2

得分: -1

这是您的答案吗?

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();
}
英文:

Is this your answer?

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();
}

huangapple
  • 本文由 发表于 2020年8月4日 07:55:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63238459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定