英文:
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("type:- " + 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("com.example.demo.test.model", "User");
ClassName set = ClassName.get("java.util", "Set");
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("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));
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论