org.powermock.classloading
Class ClassloaderExecutor

java.lang.Object
  extended by org.powermock.classloading.ClassloaderExecutor

public class ClassloaderExecutor
extends Object

A ClassLoaderExecutor can run any code in any classloader. E.g. assume you have a classloader called myClassloader and you want to execute a (void) method called myMethod in myObject using this CL:

     ClassloaderExecutor cle = new ClassloaderExecutor(myClassloader);
     cle.execute(new Runnable() {
          public void run() {
             myObject.myMethod();
          }
     });
 
What happens is that the entire object graph of myObject is deep-cloned into the myClassloader classloader and then the myObject.myMethod() is executed.

You can also execute methods that return something:

     ClassloaderExecutor cle = new ClassloaderExecutor(myClassloader);
     MyResult result = cle.execute(new Callable() {
          public MyResult call() throws Exception {
             return myObject.myMethod();
          }
     });
 
Here we imagine that myObject.myMethod() returns an object of type MyResult. Again the entire state will be deep-cloned to myClassloader and then the myObject.myMethod() is executed. The result of the method call is deep-cloned back into the original classloader (the one that made the call to cle.execute(..)) and is ready for use.

Note that the ClassloaderExecutor requires a deep cloner implementing the DeepClonerSPI present in the class-path.


Constructor Summary
ClassloaderExecutor(ClassLoader classloader)
           
 
Method Summary
<T> T
execute(Callable<T> callable)
           
 void execute(Runnable runnable)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ClassloaderExecutor

public ClassloaderExecutor(ClassLoader classloader)
Method Detail

execute

public <T> T execute(Callable<T> callable)

execute

public void execute(Runnable runnable)


Copyright © 2007-2012. All Rights Reserved.