JavaPoet - type of inner class type

I am trying to add an inner class (for example, the Listener {} interface) to TypeSpec. I also want to add a field of type Listener to my TypeSpec. How could I achieve something like this?

TypeSpec outerClass = ...;
TypeSpec innerClass = ...;
outerClass.addType(innerClass);
outerClass.addField(...); // How can i add a field of type innerClass?
+4
source share
1 answer

You need to calculate the full type name. It looks like this:

ClassName outerName = ClassName.get("com.example.project", "Outer");
ClassName innerName = outerName.nestedClass("Inner");

Then you can call outerClass.addField()by going over innerName.

+2
source

Source: https://habr.com/ru/post/1664520/


All Articles