jeudi 26 mars 2015

Passing instance as object and getClass() returns java.lang.Class

I'm trying to get the class name of a passed Object. However the Object#getClass() just returns java.lang.Class. Here is an example where obj is an instance of net.minecraft.server.v1_8_R1.IChatBaseComponent:



System.out.println("logging x1: " + obj); // interface net.minecraft.server.v1_8_R1.IChatBaseComponent
System.out.println("logging x2: " + obj.getClass()); // class java.lang.Class


Why is this happening, and how can I get around it. No I cannot use newInstance or instanceof or isAssignableFrom because the entire method is used to determine the class names of passed objects.


The full method looks as so:



private static Class<?>[] toPrimitiveTypeArray(Object[] objects, boolean log) {
Class<?>[] types = new Class<?>[objects != null ? objects.length : 0];

for (int i = 0; i < types.length; i++) {
if (log) System.out.println("logging x1: " + objects[i]);
if (log) System.out.println("logging x2: " + objects[i].getClass());
types[i] = getPrimitiveType(objects[i].getClass());
}

return types;
}





Aucun commentaire:

Enregistrer un commentaire