Skip to content

Proper support for type annotations #2

Description

@Alexander--

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:

  1. Postpone your compilation until type analysis completes using Oracle-proprietary JavacTask API
  2. Obtain the annotated type using Oracle proprietary Trees.getTypeMirror API.

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions