1
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.

kaan bobac
  • 729
  • 4
  • 8
Priya K
  • 23
  • 4
  • You don't need the actual class, but you do need its _fully qualified_ name (e.g. "org.package.OpCodeClass") as JavaPoet must also generate an import statement for the class. With the package and class names, you can create a `ClassName` object to pass to the `FieldSpec` builder. If you're sure you don't need the import (e.g. your generated class is in the same package as all of the opcode classes) you can write the field as a literal without using FieldSpec. Another possibility would be to [search the class via reflection](https://stackoverflow.com/q/520328/1110381) based on its name. – l4mpi Oct 12 '18 at 10:39

1 Answers1

0

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

Jesse Wilson
  • 39,078
  • 8
  • 121
  • 128