I’m developing a rule for inclusion in SonarJava and need to access the type of annotations. More specifically, I’m looking to find the fully qualified name of Foo
in types like List<@Foo Object>
(type annotations introduced by Java 8).
For some reason, however, the symbol
for Foo
always returns true
for isUnknown()
, and I consequently cannot access the fully qualified name. Is this a bug or somehow my mistake? I tried switching the navigation code from declarations to metadata as indicated in an answer to a remotely related question, but get the same result.
Here is the shortened version of my reproducer (see this commit in my SonarJava fork):
Check implementation
@Rule(key = "X1234")
public class BugDemoCheck extends IssuableSubscriptionVisitor {
@Override
public List<Tree.Kind> nodesToVisit() {
return ImmutableList.of(Tree.Kind.ANNOTATION);
}
@Override
public void visitNode(Tree tree) {
AnnotationTree annotationTree = (AnnotationTree) tree;
Symbol.TypeSymbol annotationSymbol = annotationTree.symbolType().symbol();
if (annotationSymbol.isUnknown()) {
reportIssue(annotationTree, "Unknown annotation symbol");
}
}
}
Test class
@interface MyAnnotation {
}
class MyClass {
java.util.List<@MyAnnotation Object> field; // Issue reported here
}
Notes
- As written, this reports an issue in the marked line.
- If I change the element type in the test class from
Object
toObject[]
, no issue is reported. Is this maybe somehow related to type erasure?