如何从javax.lang.model.VariableElement获取类型和泛型类型?

huangapple go评论118阅读模式
英文:

How to get type and generic type from javax.lang.model.VariableElement?

问题

我正在使用注解处理和javapoet库来生成一些源代码。

比如说,我有一个

VariableElement fieldElement

如果

System.out.println("type:- " + fieldElement.asType().toString());

打印出

type:- java.util.Set<com.example.demo.test.model.User>

我要如何获取Set类和User类,以便我可以像这样在javapoet中使用?

ClassName user = ClassName.get("com.example.demo.test.model", "User");
ClassName set = ClassName.get("java.util", "Set");
TypeName setOfUsers = ParameterizedTypeName.get(set, user);

一些字符串比较可以完成任务,但似乎不是理想的方法。

英文:

I'm using annotation processing and javapoet library to generate some source code.

Say, I've a

VariableElement fieldElement

and if

System.out.println(&quot;type:- &quot; + fieldElement.asType().toString());

prints

> type:- java.util.Set<com.example.demo.test.model.User>

How do I get the Set class and User class so I can do something like this with javapoet?

ClassName user = ClassName.get(&quot;com.example.demo.test.model&quot;, &quot;User&quot;);
ClassName set = ClassName.get(&quot;java.util&quot;, &quot;Set&quot;);
TypeName setOfUsers = ParameterizedTypeName.get(set, user);

Some string comparison would get the job done, but doesn't seem like the ideal approach.

答案1

得分: 2

你可以尝试以下代码。

要获取那个User类,您可以使用**fieldElement.getEnclosingElement()**,它将为您提供带有完整包名称的类名。

现在,如果您只想要该类的名称,您可以使用**enclosingElement.getSimpleName()**。

要获取enclosedByElement,您可以使用TypeSymbol。只需将fieldElement.asType()强制转换为**Type,然后获取tsym**属性。

VariableElement fieldElement;

Symbol.TypeSymbol containerForEnclosingElement = ((Type) fieldElement.asType()).tsym;
Element enclosingElement = fieldElement.getEnclosingElement();

System.out.println("containerForEnclosingElement:- " + containerForEnclosingElement);
System.out.println("enclosingElement:- " + enclosingElement);
System.out.println("enclosingElement Name:- " + enclosingElement.getSimpleName());
System.out.println("fieldElement without root Type:- " + ((Type) fieldElement.asType()).getTypeArguments().get(0));

上述代码将输出如下结果。

containerForEnclosingElement:- java.util.Set
enclosingElement:- com.example.demo.test.model.User.
enclosingElement Name:- User
fieldElement without root Type:- com.example.demo.test.model.User

您还可以创建一个实用方法来获取这两个值。

这将对您有所帮助。

英文:

You can try below code.

For getting that User class you can use fieldElement.getEnclosingElement(), it will give you class name with full package name.

Now if you want only name of that class you can use enclosingElement.getSimpleName().

And to get enclosedByElement you can use TypeSymbol.Simply cast fieldElement.asType() to Type and get tsym attribute.

        VariableElement fieldElement;

        Symbol.TypeSymbol containerForEnclosingElement=((Type)fieldElement.asType()).tsym;
        Element enclosingElement=fieldElement.getEnclosingElement();

        System.out.println(&quot;containerForEnclosingElement:- &quot; +  containerForEnclosingElement);
        System.out.println(&quot;enclosingElement:- &quot; +  enclosingElement);
        System.out.println(&quot;enclosingElement Name:- &quot; +  enclosingElement.getSimpleName());
        System.out.println(&quot;fieldElement without root Type:- &quot;+((Type) fieldElement.asType()).getTypeArguments().get(0));

Above code will print output as below.

containerForEnclosingElement:- java.util.Set
enclosingElement:- com.example.demo.test.model.User.
enclosingElement Name:- User
fieldElement without root Type:- com.example.demo.test.model.User

You can also create one Utility method to get this two values.

This will help you.

huangapple
  • 本文由 发表于 2020年8月16日 22:11:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63437899.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定