package test;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import alt.jiapi.*;
import alt.jiapi.instrumentor.*;
import alt.jiapi.reflect.Loader;
import alt.jiapi.reflect.*;
import alt.jiapi.util.InstrumentingClassLoader;
/**
* Used to test coping of a method from one class to another.
*/
public class CopyMethodTest {
private Class clazz;
public static void main(String [] args) throws Exception {
CopyMethodTest cmt = new CopyMethodTest();
cmt.run(args);
}
public CopyMethodTest() throws Exception {
InstrumentationContext ctx = new InstrumentationContext();
Instrumentor createMethod =
new CreateMethodInstrumentor(Modifier.PUBLIC + Modifier.STATIC,
"bar");
Loader loader = ctx.getLoader();
JiapiClass sourceClass = loader.loadClass("test.Bar");
JiapiMethod sourceMethod =
sourceClass.getDeclaredMethod("bar", new String[0]);
Instrumentor copyMethod
= new CopyInstrumentor(sourceMethod.getInstructionList());
InstrumentationDescriptor id = new InstrumentationDescriptor();
InstrumentorChain chain = new InstrumentorChain();
chain.add(createMethod);
chain.add(copyMethod);
id.addChain(chain);
id.addInclusionRule("test.Foo");
ctx.addInstrumentationDescriptor(id);
ClassLoader cl = InstrumentingClassLoader.createClassLoader(ctx);
this.clazz = cl.loadClass("test.Foo");
}
public void run(String[] args) throws Exception {
java.lang.reflect.Method m =
clazz.getMethod("bar", new Class [] {});
System.out.println("trying to invoke the added method 'bar'...");
m.invoke(clazz, null);
System.out.println("OK.");
}
}