如何获得可变原始类型

发布于 2025-01-27 21:01:07 字数 336 浏览 3 评论 0原文

假设我对原始类型有注释,

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷血 2025-02-03 21:01:07

当您调用element.astype()时,您将获得typemirror。类型摩擦有多种类型typeElement只能为declaredType获得,该代表代码中某个地方的实际声明类。

如果类型是原始的,则您获得的实际接口是PrimitiveType,它扩展了typemirror

要区分所有可能的typemirror可能会检查其getKind() property(instanceof 是由API Spec否认的)。 typekind枚举将告诉您实际的类型类型;所有原始类型已经包含在枚举中,例如typekind.inttypekind.byte

因此,这基本上是知道一种类型是否是原始的,以及哪种原始类型的方式。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文