In my program, I deal with classes and primitive types.
If the program finds a class, it simply does one of the following calls :
Class.forName(classname)cc.toClass()whereccis an instance ofCtClass
However, if it finds a primitive type, things get worse :
Class.forNameis not usable, it cannot be used with primitive types.cc.toClass()returnsnull
It’s possible to call the TYPE field from primitive types wrapper class but how can I do it with reflection ?
Here is my code :
CtClass cc;//Obtained from caller code
Class<?> classParam;
if (cc.isprimitive()) {
classParam= ?? // How can I get TYPE field value with reflection ?
} else {
String nomClasseParam = cc.getName();
if (nomClasseParam.startsWith("java")) {
classeParam = Class.forName(nomClasseParam);
} else {
classeParam = cc.toClass();
}
}
Javassist 3.12.0.GA
EDIT:
I have posted the solution I chose in the anwsers below.
Anyway, I ticked Tom’s answer.
It looks to me like you can cast
ccto its subclass CtPrimitiveType.If you wanted a wrapper, you could then use the method getWrapperName to get the class name of the appropriate wrapper. You can use Class.forName as usual to turn that name into a
Classobject. However, i don’t think you do want a wrapper, so this doesn’t help.Instead, i think you want getDescriptor, followed by a laboriously handcoded switch statement:
Something like that really should be in Javassist. But as far as i can see, it isn’t.