kaqqao
kaqqao

Reputation: 15479

Is there something like TypeToken for AnnotatedTypes

Since there's no type literals in Java, TypeToken trick is commonly used instead. Say, you want to get a Type instance representing List<String>, you'd do:

new TypeToken<List<String>>(){}.getType()

Now, I wonder if similar trickery is possible to get an instance of an AnnotatedType. For example, to represent List<@NonNull String> or even @NonNull List<@NonNull String>.

EDIT: Here's a full AnnotatedType-enabled TypeToken implementation, provided by GeAnTyRef.

The gist:

public abstract class TypeToken<T> {
    private final AnnotatedType type;

    /**
     * Constructs a type token.
     */
    protected TypeToken() {
        this.type = extractType();
    }

    private TypeToken(AnnotatedType type) {
        this.type = type;
    }

    public Type getType() {
        return type.getType();
    }

    public AnnotatedType getAnnotatedType() {
        return type;
    }

    private AnnotatedType extractType() {
        AnnotatedType t = getClass().getAnnotatedSuperclass();
        if (!(t instanceof AnnotatedParameterizedType)) {
            throw new RuntimeException("Invalid TypeToken; must specify type parameters");
        }
        AnnotatedParameterizedType pt = (AnnotatedParameterizedType) t;
        if (((ParameterizedType) pt.getType()).getRawType() != TypeToken.class) {
            throw new RuntimeException("Invalid TypeToken; must directly extend TypeToken");
        }
        return pt.getAnnotatedActualTypeArguments()[0];
    }
}

Upvotes: 2

Views: 406

Answers (1)

the8472
the8472

Reputation: 43125

Just create a type token - or any anonymous subclass with annotated type parameters - with the annotations and then use reflection to extract the annotated types.

Note that this only works for annotations with runtime retention.

Using the fullType() method from the linked answer:

    List<?> token = new ArrayList<@NonNull String>() {};
    fullType("", token.getClass().getAnnotatedSuperclass());

prints

java.util.ArrayList
<
    @org.checkerframework.checker.nullness.qual.NonNull()
    java.lang.String
>

Upvotes: 1

Related Questions