开发者

Use a class from byte array without reflection?

开发者 https://www.devze.com 2023-01-28 04:06 出处:网络
I\'ve instrumented my class using ASM and processed it like this public class MyClassLoader extends ClassLoader {

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 us开发者_如何转开发ing wrong classloader or something?


Class.newInstance is a reflective call.

Typically what you would do in these situations is have the class loader be a child of the class loader that loaded the existing code. Have the implementation class implement an interface from your code's class loader. Use Class.asSubclass such that the Class is a known subtype of your interface. Construct an instance of the implementation class through Class.getConstructor.newInstance.

Using this technique you are not dependent upon using reflection to pick out a method, although it does require a no-args constructor.

0

精彩评论

暂无评论...
验证码 换一张
取 消