Class.forName(boolean.class.getName());
This doesn’t work in Java – the virtual machine slaps you with a ClassNotFoundException. I was in need for something like that because I wanted to reflect methods based on Strings that included the method signatures, like
public void doSomething(boolean yesWeCan, java.lang.String[] presidents);
At the end I came up with a custom ‘ClassFactory’ which translates the type Strings to class objects. This factory includes a lot of handlers for primitive and array type values.
The handler for array type objects is something like:
if (isArrayOfObjects) {
return Class.forName("L["+typeName.replace("[]", "")+";");
}
My question is – have I missed something in the Java 1.5+ API that might do the trick?
Edit
Thanks for your answers, it’s not a surprise that Class.forName works pretty well with the wrapper classes. But I’m looking for a solution for all java types, including arrays and primitives. Thus my first line is not a typo. Some method signatures do have java primitives as parameters, I can’t just use a wrapper class to reflect the method in a library, I really need the Class object for that type (like boolean).
Class<boolean> booleanClassObject = boolean.class;
works fine, as well as
String name = boolean.class.getName();
Perhaps you have a number of
ifs: