Package test

Source Code of test.MethodCallTest

package test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import alt.jiapi.*;
import alt.jiapi.reflect.Loader;
import alt.jiapi.instrumentor.*;
import alt.jiapi.util.InstrumentingClassLoader;

/**
* Used for testing method calls. Given without arguments
* static calls are made. With arguments, dynamic calls are made.
* This class implements Hook interface and sets hook method to
* be called based on the above.
*/
public class MethodCallTest implements Hook {
    private Class clazz;
    private static Object instance;
    private boolean callStatic;

    public static void main(String [] args) throws Exception {
        MethodCallTest mct = new MethodCallTest(args.length == 0);
        mct.run(args);
    }

    public MethodCallTest(boolean callStatic) throws Exception {
        this.callStatic = callStatic;

        String className = System.getProperty("class", "test.Foo");
        System.out.println("Loading class " + className);
        InstrumentationContext ctx = new InstrumentationContext();
        Instrumentor dispatcher = new MethodDispatcherInstrumentor();
        Instrumentor mei = new GrepInstrumentor(new MethodReturnStrategy());
        //        Instrumentor mei = new GrepInstrumentor(new MethodEntryStrategy());
        Instrumentor mcp = new MethodCallInstrumentor(this);

        InstrumentorChain chain = new InstrumentorChain();
        chain.add(dispatcher);
        chain.add(mei);
        chain.add(mcp);

        InstrumentationDescriptor id = new InstrumentationDescriptor();
        id.addInclusionRule("test.*");

        // Should we add
        // addExclusionRule(this.getClass().getName());
        // to implementation of InstrumentationDescriptor
        id.addExclusionRule("test.MethodCallTest");
        id.addChain(chain);

        ctx.addInstrumentationDescriptor(id);
        Loader loader = ctx.getLoader();
        ClassLoader cl = InstrumentingClassLoader.createClassLoader(ctx);

        this.clazz = cl.loadClass(className);
    }

    public void callMeDynamic(String s1, String s2) {
        System.out.println ("callMeDynamic : " + s1 + ", " + s2);
    }

    public static void callMeStatic(String s1, String s2) {
        System.out.println ("callMeStatic : " + s1 + ", " + s2);
    }


    // -- Interface Hook --
    public Method getHookMethod() {
        Method hookMethod = null;
        Class [] params = new Class [] {String.class, String.class};

        if (callStatic) {
            System.out.println("Instrumenting for static calls");
            try {
                hookMethod =
                    this.getClass().getMethod("callMeStatic", params);
            }
            catch(Exception e) {
                // not possible, but just in case
                throw new RuntimeException (e.toString ());
            }
        }
        else {
            System.out.println("Instrumenting for dynamic calls");
            try {
                hookMethod =
                    this.getClass().getMethod("callMeDynamic", params);
            }
            catch(Exception e) {
                // not possible, but just in case
                throw new RuntimeException(e.toString ());
            }
        }

        return hookMethod;
    }


    public void run(String[] args) throws Exception {
        java.lang.reflect.Method m =
            clazz.getMethod("main", new Class [] {String[].class});

        try {
            m.invoke(clazz, new Object [] {args});
        } catch (InvocationTargetException ite) {
            System.err.println("Target exception:");
            ite.printStackTrace();
            if (ite.getTargetException() != null) {
                ite.getTargetException().printStackTrace();
            }
        }
    }

    public Object getInstance() {
        return this;
    }
}
TOP

Related Classes of test.MethodCallTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.