Consider these two lines:
return loadMethod.invoke(botScriptLoader, file.getName().replaceFirst("\\.class", "")).getClass();
return (Class<?>) loadMethod.invoke(botScriptLoader, file.getName().replaceFirst("\\.class", ""));
When I run my application using the first line, it does not work as desired. When I run my application using the second line, it does work as desired. I don’t feel that the code behind this is relevant because my point is that the behavior between the two lines is different. Why do they behave differently? getClass() returns Class and the typecast casts the object into a Class — so the end result should be the same. However, the two behave differently.
Your loadMethod appears to already return an object of type Class. So when you call
getClass()on that, it’s giving youjava.lang.Class.classback, not the thing you loaded. Casting the thing you loaded does not change the underlying object, which is the Class instance for your filename.That is, if you wanted to return the Class Object for the Type Integer, you would:
return Integer.class;Your first line is doing
return Integer.class.getClass();so you get the Class object that represents the Type Class itself, not the Type Integer.