Search code examples
javareflectionjavassist

How to get class of primitive types with Javassist?


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() where cc is an instance of CtClass

However, if it finds a primitive type, things get worse :

  • Class.forName is not usable, it cannot be used with primitive types.
  • cc.toClass() returns null

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.


Solution

  • It looks to me like you can cast cc to 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 Class object. 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:

    switch(descriptor) {
        case 'I': classParam = int.class; break;
        // etc
    }
    

    Something like that really should be in Javassist. But as far as i can see, it isn't.