英文:
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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论