Package org.powermock.classloading
Class SingleClassloaderExecutor
- java.lang.Object
-
- org.powermock.classloading.AbstractClassloaderExecutor
-
- org.powermock.classloading.SingleClassloaderExecutor
-
- All Implemented Interfaces:
ClassloaderExecutor
public class SingleClassloaderExecutor extends AbstractClassloaderExecutor
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:SingleClassloaderExecutor cle = new SingleClassloaderExecutor(myClassloader); cle.execute(new Runnable() { public void run() { myObject.myMethod(); } });
What happens is that the entire object graph of myObject is deep-cloned into themyClassloader
classloader and then themyObject.myMethod()
is executed.You can also execute methods that return something:
SingleClassloaderExecutor cle = new SingleClassloaderExecutor(myClassloader); MyResult result = cle.execute(new Callable
Here we imagine that() { public MyResult call() throws Exception { return myObject.myMethod(); } }); myObject.myMethod()
returns an object of typeMyResult
. Again the entire state will be deep-cloned tomyClassloader
and then themyObject.myMethod()
is executed. The result of the method call is deep-cloned back into the original classloader (the one that made the call tocle.execute(..)
) and is ready for use.Note that the SingleClassloaderExecutor requires a deep cloner implementing the
DeepClonerSPI
present in the class-path.
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.ClassLoader
classloader
-
Constructor Summary
Constructors Constructor Description SingleClassloaderExecutor(java.lang.ClassLoader classloader)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.lang.Object
execute(java.lang.Object instance, java.lang.reflect.Method method, java.lang.Object... arguments)
-
Methods inherited from class org.powermock.classloading.AbstractClassloaderExecutor
execute, execute, executeWithClassLoader
-
-
-
-
Method Detail
-
execute
protected java.lang.Object execute(java.lang.Object instance, java.lang.reflect.Method method, java.lang.Object... arguments)
- Specified by:
execute
in classAbstractClassloaderExecutor
-
-