I am working on an annotation processor and using JavaPoet to generate the output class from processing, but I can't seem to find a way to have a generated method return a properly typed object. For example, the output I would like to have is something like this...
public static final Map<String, Object> getObjects() {
return objects;
}
However I can only get it to do something like this...
public static final Map getObjects() {
return objects;
}
I am using the returns method in the MethodBuilder, but it requires a proper class as the return type, so how can you add modifiers like to the method as it's being built? Here is a simple version of what I have...
MethodSpec.methodBuilder("getObjects")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.returns(Map.class)
.addStatement("return objects").build()
I have tried searching everywhere and can't find an answer for this type of thing. I know all Maps are technically but I would like to avoid the darn unchecked cast highlighting in android studio, plus it feels wrong to not have the correct types on a method return. Is this possible, or should I just accept the highlighting and move on? Thank you.
Something like that should work:
.returns(ParameterizedTypeName.get(ClassName.get(Map.class),
ClassName.get(String.class),
ClassName.get(Object.class)))
Hope that helps.