I've instrumented my class using ASM and processed it like this
public class MyClassLoader extends ClassLoader {
...
byte[] classBytes = ... //the class is of type com.mypackage.Test
Class clazz = defineClass("com.mypackage.Test", classBytes, 0, classBytes.length);
resolveClass(clazz);
com.mypackage.Test test =(com.mypackage.Test) clazz.newInstance();
However I get ClassCastException on the last line:
java.lang.ClassCastException: com.mypackage.Test cannot be cast to com.mypackage.Test
The workaround is to use reflection:
Object test = clazz.newInstance();
test.getClass().getMethods()[0].invoke(test, null); //invoke some method - successfully
Am I using using wrong classloader or something?