mardi 3 mai 2016

AbstractMethodError with dynamic java compilation and class loading with reflection

I am trying the following wrt Refleciton, let me know if this is possible

public class A {
    void start(){
       execute();
     }

}

public class B extends A {
    void execute()
    {
       doWork();
    }
    abstract void doWork();
}

I have the above classes packaged in a jar and have it running on a JVM. Now I am trying to create another class at run time, compile it at run time and trying to use reflection to invoke Class B's execute function().

public class C extends B {
     @Override
     public void doWork()
     {
       //Implementation
     }
}

Reflection code:

Classloader = urls of application jars and url of C.class, compiled at run time. Parent loader - Thread.currentThread().getContextClassLoader() I am also setting the current thread's context class loader to the classloader created above.

Class<? extends B> cls = (Class<? extends B>) Class.forName("className", true, clsLoader);

Object obj = cls.newInstance();
Method method = obj.getClass().getSuperclass().getMethod("start", new Class[0]);
method.invoke(obj, new Object[0]);   

I am able to get the method and the invoke also gets called. However when class B's execute is called, it is trying to call the doWork() and then I run into an AbstractMethodError. Upon looking up on the exception, I see that the exception happens with incorrect classloaders/jars. But I am not sure how do I go about fixing it in my case.

Can anyone assist?





Aucun commentaire:

Enregistrer un commentaire