Skip to content

Annotation Processors Ignore TYPE_USE annotations on compiled classes #365

@agentgt

Description

@agentgt

TLDR; JSpecify might need more java.lang.annotation.Targets then just TYPE_USE for compatibility or some documented work around for annotation processors.

While playing with JSpecify and Eclipse null annotations which both are only TYPE_USE I found the rather disturbing possible bug with the JDK APT framework in that TYPE_USE annotations appear to be stripped from already compiled classes. I stress "appear" because the data is definitely in the class file it just the APT framework does not make it visible.

Let us say we have module ModuleA (aka jar artifact ModuleA) that has a class ClassA. ClassA has a nullable TYPE_USE annotations on some method return type we will call ReturnTypeA:

public class ClassA {
 @Nullable ReturnTypeA someMethod() { return null; }
}

If we compile ClassA with an annotation processor enabled and somehow navigate to the method someMethod and get the TypeMirror for ReturnTypeA and call TypeMirror.getAnnotationMirrors() the @Nullable will be there. Good. The type is preserved.

Now let us say we have ModuleB that has ClassB like:

public class ClassB {
  public ClassA a;
}

And again if we compile ModuleB (and thus ClassB) with an annotation processor enabled and navigate to ClassA method someMethod and get the TypeMirror for ReturnTypeA and call TypeMirror.getAnnotationMirrors() the @Nullable WILL BE MISSING regardless of Retention Policy.

This is pretty darn problematic for annotation processors like MapStruct (see mapstruct/mapstruct#1243) or my own library JStachio where we want to know if the type allows nulls or not.

To be honest this seems like a JDK issue because almost all other information is available with TypeElement or TypeMirror as well as you can reflectively get access to the annotations with java.lang.Method.getAnnotatedReturnType(). Furthermore all other annotations that are NOT TYPE_USE are available. Perhaps this is one of the reasons Checkers target basically everything?

@kevinb9n have you guys ran across this at all?

Metadata

Metadata

Assignees

No one assigned

    Labels

    trackingkeeping track of our needs from projects

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions