Reputation: 349
JavaPoet: Version 1.5.1
JDK: 1.7
I am using annotations to generate the code. Here is something that I am trying.
Following are available as part of the project:
@GenService
public abstract class BaseService {
...
}
@GenController
public abstract class BaseController {
...
}
Following are intended to be created through the above annotations:
public class AService extends BaseService {
...
}
public class AController extends BaseController {
@Autowired
private AService aService;
...
}
Until compiled AService
and AController
do not exist.
I can include @Autowired
annotation. But, how do I reference AService
as a type in AController
?
Upvotes: 1
Views: 1174
Reputation: 349
Though not direct, I found the answer through Hannes Dorfmann's blog:
The solution goes like this:
...
FieldSpec.Builder fsBuilder;
try {
ClassName clazz = ClassName.get("package.to.services", "AService");
fsBuilder = FieldSpec.builder(clazz, "aService")
.addModifiers(Modifier.PRIVATE)
.addAnnotation(autowired.build());
} catch (MirroredTypeException mte) {
DeclaredType classTypeMirror = (DeclaredType) mte.getTypeMirror();
fsBuilder = FieldSpec.builder(TypeName.get(classTypeMirror), "aService")
.addModifiers(Modifier.PRIVATE)
.addAnnotation(autowired.build());
}
typeBuilder.addField(fsBuilder.build());
It worked. But, please do let me know, if there is a better way.
Upvotes: 2