javax.lang.model:如何获取字段的类型?

发布于 2024-10-08 07:26:24 字数 719 浏览 9 评论 0原文

java.lang.reflect 中,人们会这样做:

Field someField = ...;
Class<?> fieldType = someField.getType();

但是我该如何处理 javax.lang.modelVariableElement (其中可能代表也可能不代表一个字段)?相应的返回值将是(我猜)TypeElement

VariableElement someField = ...;
TypeElement fieldType = someField.???;

那么,在javax.lang.model中,如何获取由VariableElement表示的字段的类型(或TypeElement) ?

顺便说一句,没有一个 Stackoverflow 标签适合 javax.lang.model ;)

In java.lang.reflect, one would do:

Field someField = ...;
Class<?> fieldType = someField.getType();

But what do I do with javax.lang.model's VariableElement (which may or may not represent a field)? A corresponding return value would be (I guess) TypeElement.

VariableElement someField = ...;
TypeElement fieldType = someField.???;

So, in javax.lang.model, how do I get the type (or TypeElement) of a field, represented by VariableElement?

BTW, there is not a single Stackoverflow-tag which would fit to javax.lang.model ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

逐鹿 2024-10-15 07:26:24

嗯,我不知道,这是正确的方法。
如果有人真正理解这个 API 告诉我,那就太好了。

但是,看起来不错。

public class SomeClass {
  private final ProcessingEnvironment pe = /* get it somewhere */;
  private final Types typeUtils = pe.getTypeUtils();

  public TypeElement getExtracted(VariableElement ve) {
    TypeMirror typeMirror = ve.asType();
    Element element = typeUtils.asElement(typeMirror);

    // instanceof implies null-ckeck
    return (element instanceof TypeElement)
        ? (TypeElement)element : null;
  }
}

看来 Types 类必须从当前的 ProcessingEnvironment 中获取,因为它的一些内部依赖于它,所以它不是一个常见的实用程序类。

Well, I don't know, it that's the right way to do this.
Would be nice if someone, who actually understands this API, told me.

But well, seams to work.

public class SomeClass {
  private final ProcessingEnvironment pe = /* get it somewhere */;
  private final Types typeUtils = pe.getTypeUtils();

  public TypeElement getExtracted(VariableElement ve) {
    TypeMirror typeMirror = ve.asType();
    Element element = typeUtils.asElement(typeMirror);

    // instanceof implies null-ckeck
    return (element instanceof TypeElement)
        ? (TypeElement)element : null;
  }
}

It seems that the class Types has to be got from current ProcessingEnvironment because some of its internals depend on it, so it's not a usual utility class.

请别遗忘我 2024-10-15 07:26:24

我在尝试理解新的 JDK Doclet 毛团时偶然发现了这一点。接受的答案对我不起作用,因为 Doclet 有各种无法使用的内部类。此外,这个答案更类似于“如何将 TypeMirror 转换为 TypeElement”;鉴于 java.lang.model 的缺乏,所以问题+答案,我想无论如何我都会发布这个。

这就是我所做的:

import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;

@Override
public boolean run(DocletEnvironment docEnv) {
    ...        
    elementUtils = docEnv.getElementUtils();
    typeUtils    = docEnv.getTypeUtils();
    ...
}

当您有一个 TypeElement t ,它是一个类/接口“种类”时,您可以执行类似的操作以从 TypeMirror 转到 TypeElement。

TypeMirror   p = t.getSuperclass();
TypeElement pe = (TypeElement) typeUtils.asElement(p);

我希望这对某人有帮助。

I stumbled across this trying to make sense out of the new JDK Doclet hairball. The accepted answer didn't work for me because, well, Doclet has various internal classes that cannot be used. Furthermore, this answer is more akin to "how do I convert TypeMirror to TypeElement"; given the paucity of java.lang.model SO questions+answers, I thought I'd post this anyway.

Here's what I did:

import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;

@Override
public boolean run(DocletEnvironment docEnv) {
    ...        
    elementUtils = docEnv.getElementUtils();
    typeUtils    = docEnv.getTypeUtils();
    ...
}

When you have a TypeElement t that is a class/interface "kind" you can do something like this to go from TypeMirror to TypeElement.

TypeMirror   p = t.getSuperclass();
TypeElement pe = (TypeElement) typeUtils.asElement(p);

I hope this helps someone.

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