如何获得可变原始类型
假设我对原始类型有注释,
@Parameter // first data value (0) is default
public /* NOT private */ int fInput;
是否有一种方法可以使用注释处理工具来获取@parameter注释的元素类型?在这种情况下,它是原始类型的INT。如果类型不是使用以下内容,我可以获得类型,但不能将其应用于原始类型。
MoreTypes.asTypeElement(element.asType()).getQualifiedName().toString()
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您调用
element.astype()
时,您将获得typemirror
。类型摩擦有多种类型。typeElement
只能为declaredType
获得,该代表代码中某个地方的实际声明类。如果类型是原始的,则您获得的实际接口是
PrimitiveType
,它扩展了typemirror
。要区分所有可能的
typemirror
可能会检查其getKind()
property(instanceof
是由API Spec否认的)。typekind
枚举将告诉您实际的类型类型;所有原始类型已经包含在枚举中,例如typekind.int
或typekind.byte
。因此,这基本上是知道一种类型是否是原始的,以及哪种原始类型的方式。
when you call
element.asType()
, you get aTypeMirror
. There are multiple kinds of type-mirrors.TypeElement
may be obtained only forDeclaredType
, which represent an actual declared class somewhere in code.If a type is primitive, the actual interface that you get is
PrimitiveType
, which extendsTypeMirror
.To distinguish between all possible kinds of
TypeMirror
one may check it'sgetKind()
property (instanceof
is discouraged by the API spec). AndTypeKind
enum will tell you the actual type kind; All the primitive types are already included into the enum, e. g.TypeKind.INT
orTypeKind.BYTE
.So that's basically is a way to know whether a type is a primitive, and what kind of primitive.