-
Notifications
You must be signed in to change notification settings - Fork 8
Description
In ideal world the following snippet would both compile and be inspectable from annotation processors:
void methodWithListParameter(List<@NonNull ? extends CustomParcelable>);
In reality ECJ does not appear to recognize type annotations as anything, but opaque source code tokens, and so does Javac.
Current situation
There is a way to make introspection on type annotations "work" in Javac:
- Postpone your compilation until type analysis completes using Oracle-proprietary
JavacTaskAPI - Obtain the annotated type using Oracle proprietary
Trees.getTypeMirrorAPI.
Unfortunately, as of Javac 1.8.0_112 doing this requires significant effort (manually walking trees using tree API) and does not really work for any types, only for simple, non-composite types. The following TreePath will have no annotations:
List<@NonNull ? extends CustomParcelable>
You will have to cast it to parametrized tree and walk down to
@NonNull ? extends CustomParcelable
For which Trees.getTypeMirror will finally produce correctly annotated WildcardType.
The alternative is to use non-documented Javac classes, which will likely become unavailable in Java 9. And that still does not guarantee correct outcome.
Oncoming improvements
Javac have had a major injection of code to actually support type annotations. That code isn't included in Java 8 and will be available in Java 9 only.