Using generics with com.sun.codemodel
Using com.sun.codemodel, I woul开发者_如何学Cd like to generate a generic LinkedList field. I can create a raw field using this code:
JDefinedClass masterClass;
String detailName;
JDefinedClass detailClass;
JFieldVar detailField = masterClass.field(JMod.PRIVATE, LinkedList.class, detailName);
But I could not find a way to create a LinkedList with a generic type of detailClass
.
JClass detailClass = codeModel.ref(String.class);
String detailName = "myLinkedListField";
JClass rawLLclazz = codeModel.ref(LinkedList.class);
JClass fieldClazz = rawLLclazz.narrow(detailClass);
JFieldVar detailField = def.field(JMod.PRIVATE, fieldClazz, detailName);
精彩评论