Java – on COM sun. Using generics in codemodel
•
Java
Use COM sun. Codemodel, I want to generate a generic LinkedList field I can create an original field using the following code:
JDefinedClass masterClass; String detailName; JDefinedClass detailClass; JFieldVar detailField = masterClass.field(JMod.PRIVATE,LinkedList.class,detailName);
But I can't find a method to create a LinkedList using the generic type detailclass
Solution
JClass detailClass = codeModel.ref(String.class);
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);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码