检查注释处理器中是否缺少超类
在注释处理器中获取 TypeElement
时,您可以使用方法 getSuperClass()TypeMirror
)代码>. 根据JavaDoc,一种不显式扩展任何内容的类型(换句话说,Object
是超类)或者是一个接口,将返回一个 NoType
,其中无
作为TypeKind
。
忽略整个模型/镜像 API 的事情似乎不遗余力地让您在每一个机会都感到困惑的事实,我将如何可靠地检查这一点?以下是一些代码摘录:
//Cast is safe since TypeKind is checked before this
final TypeElement el = (TypeElement)annotatedElement;
...
final TypeMirror parent = el.getSuperclass();
//Checking whether "nothing" is extended
if(parent.getKind().equals(TypeKind.NONE) && parent instanceof NoType) {
...
}
这是正确的方式吗?看起来相当笨重。由于 equals
TypeMirror 方法不应用于语义相等检查,我想知道将其用于 TypeKind
是否安全。我的直觉是肯定的,因为它是一个枚举,但我对那个 instanceof
产生了怀疑。
有更好的方法吗?整个 javax.lang.model 包及其子包在整个商店都有交叉引用,我从来都不清楚什么是最好的方法。有些东西有有用的实用方法,但有些看似简单的任务需要像上面这样的可疑杂技。
When obtaining a TypeElement
in an annotation processor, you can ask for its super class (or more specifically, the TypeMirror
of it) using method getSuperClass()
. According to the JavaDoc, a type that doesn't explicity extend anything (in other words, Object
is the super class) or that is an interface will return a NoType
with NONE
as TypeKind
.
Disregarding the fact that the whole model/mirror API thing seems to go out of its way to confuse you at every opportunity for a moment, how would I reliably check for this? Here's some code extracts:
//Cast is safe since TypeKind is checked before this
final TypeElement el = (TypeElement)annotatedElement;
...
final TypeMirror parent = el.getSuperclass();
//Checking whether "nothing" is extended
if(parent.getKind().equals(TypeKind.NONE) && parent instanceof NoType) {
...
}
Is this the proper manner? It seems rather clunky. Since the equals
method of TypeMirror should not be used for semantic equality checks, I'm wondering whether it is safe to use it for TypeKind
. My gut feeling says yes since it's an enum, but then I'm having doubts about that instanceof
.
Is there a better way of doing this? The whole javax.lang.model package and its children have cross-references all over the shop and it's never really clear to me what the best method for something is. There's useful utility methods for some stuff, and then there are seemingly simple tasks that require dubious acrobatics like the above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚做了一个测试,发现我误解了文档。对于 java.lang.Object 和接口,它返回类型为 NONE 的 NoType,而不是对于隐式扩展 Object 的东西。愚蠢的我。我不需要检查,因为之前的检查会检查带注释的元素是否是严格意义上的类(不是枚举或接口),如果是,则返回。换句话说,检查规范名称是否为 java.lang.Object 或者使用 Types.isSameType 应该可以解决问题。
对不起各位。我决定不删除,因为其他人可能会提出同样的问题。如果您认为合适,请随意投票删除。
编辑:这就是我最终选择的...
如果其中一些错误消息看起来很奇怪,那是因为我遗漏了一些特定于项目的内容。
I just did a test and realized I misinterpreted the documentation. It returns a NoType with kind NONE for java.lang.Object and interfaces, not for something implicitly extending Object. Silly me. I won't need the check since an earlier one checks if the annotated element is a class in the strict sense (not an enum or interface) and will return if so. In other words, checking if the canonical name is java.lang.Object or maybe using
Types.isSameType
should do the trick.Sorry folks. I've decided not to delete since others might arrive with the same question. Feel free to vote delete if you believe it's appropriate.
EDIT: Here's what I eventually went with...
If some of those error messages seem weird, it's because I left some project-specific stuff out.