2005-06-11

Class.getName is criminally insane

Examples from the JavaDoc for Class.getName:

String.class.getName()
returns "java.lang.String"
byte.class.getName()
returns "byte"
(new Object[3]).getClass().getName()
returns "[Ljava.lang.Object;"
(new int[3][4][5][6][7][8][9]).getClass().getName()
returns "[[[[[[[I"

So if you've got an array, you get the encoded name for the element type (such as "Z", "J", or "Ljava.lang.Class;"). But if you've just got a primitive, you get the source name (such as "boolean", "long", "java.lang.Class"). And, as far as I can see, there's no method to get the encoded name. And the depraved mind responsible for this mess isn't even prepared to face the world:

* @author unascribed

Java has too many names for any given type. Take java.lang.String, for example, which might be "String", "java.lang.String", "java/lang/String", "Ljava/lang/String;", or "class java.lang.String", depending on what you're doing.

Oh well. Back to writing a method to give me the encoded name for a Class...