javajavapoet

JavaPoet : How to add a field of type class , but I have only the class name, not the class?


String fieldName = (fieldchild.getAttribute("Name")); //gives field name : opcode

String fieldCustomID = (fieldchild.getAttribute("ID")); //gives id: 2022

String classTypeofField = IdNameMappingList.get(fieldCustomID).toString(); //give class name OpCodeClass

FieldSpec fieldSpec = FieldSpec.builder(**some code**, fieldName)
                                                        .build();

Should give something like :

OpCodeClass opcode;

Any other work around is also appreciated.


Solution

  • Try ClassName.get("com.company.app", classTypeofField), replacing the package name with that of the class.