I am using sun-codemodel to generate code. I have problem with generics. I know that to generate something like
LinkedList<String>,
I need to use
JType jtype = jCodeModel.ref("LinkedList").narrow(jCodeModel.ref("String"));
However, how do I create something more general, for more than one generic type?
HashMap<String,Integer>
I would like to do it in the loop so that it supports any number of arguments in custom classes, but for the code like:
for(String name: names()){
returnTypeClass = jCodeModel.ref(name).narrow(jCodeModel.ref(name));
}
the output is something like this:
JNarrowedClass(JCodeModel$JReferencedClass(HashMap)<Integer>)<String>
I'm unfamiliar with CodeModel, but looking at the API documentation it seems there's a varargs overload:
narrow(JClass...)
. Presumably this is used for resolving multiple type parameters. So in the case ofHashMap<String, Integer>
, you would do:And generalizing this to your loop: