how to get VariableElement primitive type

89 views Asked by At

supposing I have annotation on primitive type

@Parameter // first data value (0) is default
    public /* NOT private */ int fInput;

is there a way to get the type of the element annotated with @Parameter using annotation processing tool?, in this case it is primitive type int. I can get the type if the type is not primitive using the following, but it cannot be applied to primitive type.

MoreTypes.asTypeElement(element.asType()).getQualifiedName().toString()

Thanks

1

There are 1 answers

0
Jeffset On

when you call element.asType(), you get a TypeMirror. There are multiple kinds of type-mirrors. TypeElement may be obtained only for DeclaredType, which represent an actual declared class somewhere in code.

If a type is primitive, the actual interface that you get is PrimitiveType, which extends TypeMirror.

To distinguish between all possible kinds of TypeMirror one may check it's getKind() property (instanceof is discouraged by the API spec). And TypeKind enum will tell you the actual type kind; All the primitive types are already included into the enum, e. g. TypeKind.INT or TypeKind.BYTE.

So that's basically is a way to know whether a type is a primitive, and what kind of primitive.