mercredi 27 mars 2019

Error getMethod(java.lang.String,java.lang.Class,java.lang.Class) not found in java.lang.Class

I am stuck in Javassisst. I want to put code in the method that is located in other class. I have "no method" exception. When I just start Test2 class by itself it starts ok, without any errors. I think problem in Classloader because I am trying to invoke methods from SUTTest class in order to conduct assertions and I am trying to do it with other class using the reflection too(Javassist). How I can fix this error?

Class1 - Javassist

    ClassPool pool = ClassPool.getDefault();

                        CtClass ctAgent = pool.get("Test2");

                        CtMethod method = ctAgent.getDeclaredMethod("runTestSamples");
                       method.insertAfter("targetClass.getMethod(\"setData\", int.class, int.class).invoke(targetInstance, 9, 2);");

                      //  method.insertAt(58, "memoryClassLoader.addDefinition(targetName, instrumented); memoryClassLoader = new MemoryClassLoader();targetClass = memoryClassLoader.loadClass(targetName); targetClass.getMethod(\"setData\", int.class, int.class).invoke(targetInstance, 9, 2);");
                        ctAgent.toClass();
                        new Test2().execute();

Class2 - Test2

    import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import org.jacoco.agent.AgentJar;
import org.jacoco.core.analysis.Analyzer;
import org.jacoco.core.analysis.CoverageBuilder;
import org.jacoco.core.analysis.IClassCoverage;
import org.jacoco.core.data.ExecutionDataStore;
import org.jacoco.core.data.SessionInfoStore;
import org.jacoco.core.instr.Instrumenter;
import org.jacoco.core.runtime.IRuntime;
import org.jacoco.core.runtime.LoggerRuntime;
import org.jacoco.core.runtime.RuntimeData;

public class Test2 {

    private Runnable targetInstance;
    public Class<? extends Runnable> targetClass;
    private static HashMap<Integer, String> testSamples;
    private static HashMap<String, Integer> coverageData;
    public String targetName;
    public IRuntime runtime;
    public Instrumenter instr;
    public InputStream original;
    public byte[] instrumented;
    public RuntimeData data;
    public MemoryClassLoader memoryClassLoader;

    static Test2 t2 = new Test2();
    int a;

    public static void main(String[] args) throws Exception {
        testSamples = new HashMap<Integer, String>();

        coverageData = new HashMap<String, Integer>();

        try {
            t2.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void execute() throws Exception {
        testSamples = new HashMap<Integer, String>();

        coverageData = new HashMap<String, Integer>();

        targetName = SUTClass.class.getName();
        runtime = new LoggerRuntime();
        instr = new Instrumenter(runtime);
        original = getTargetClass(targetName);
        instrumented = instr.instrument(original, targetName);
        original.close();
        data = new RuntimeData();
        runtime.startup(data);

        memoryClassLoader = new MemoryClassLoader();
        memoryClassLoader.addDefinition(targetName, instrumented);
        targetClass = (Class<? extends Runnable>) memoryClassLoader.loadClass(targetName);
        targetClass.getMethod("f", int.class, int.class).invoke(targetInstance, 2, 9);

        runTestSamples(targetClass);

        targetInstance = (Runnable) targetClass.newInstance();
        // Test samples

        targetInstance.run();
        final ExecutionDataStore executionData = new ExecutionDataStore();
        final SessionInfoStore sessionInfos = new SessionInfoStore();
        data.collect(executionData, sessionInfos, false);
        runtime.shutdown();
        final CoverageBuilder coverageBuilder = new CoverageBuilder();
        final Analyzer analyzer = new Analyzer(executionData, coverageBuilder);
        original = getTargetClass(targetName);
        analyzer.analyzeClass(original, targetName);
        original.close();

        for (final IClassCoverage cc : coverageBuilder.getClasses()) {
            coverageData.put("coveredInstructions", cc.getInstructionCounter().getCoveredCount());
        }

        System.out.println(coverageData.get("coveredInstructions"));
        System.out.println(a);

    }

    public static class MemoryClassLoader extends ClassLoader {
        private final Map<String, byte[]> definitions = new HashMap<String, byte[]>();

        public void addDefinition(final String name, final byte[] bytes) {
            definitions.put(name, bytes);
        }

        @Override
        protected Class<?> loadClass(final String name, final boolean resolve) throws ClassNotFoundException {
            final byte[] bytes = definitions.get(name);
            if (bytes != null) {
                return defineClass(name, bytes, 0, bytes.length);
            }
            return super.loadClass(name, resolve);
        }

    }

    private InputStream getTargetClass(final String name) {
        final String resource = '/' + name.replace('.', '/') + ".class";
        return getClass().getResourceAsStream(resource);
    }

    public void runTestSamples(Class<? extends Runnable> target)
            throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException,
            SecurityException, ClassNotFoundException {

        targetClass.getMethod("f", int.class, int.class).invoke(targetInstance, 2, 9);

        // testSamples.put(1, "targetClass.getMethod(\"f\", int.class,
        // int.class).invoke(targetInstance, 2, 9)");
        // testSamples.put(2, "targetClass.getMethod(\"d\", int.class,
        // int.class).invoke(targetInstance, 2, 9)");

    }

}

**Exception**
    javassist.CannotCompileException: [source error] getMethod(java.lang.String,java.lang.Class,java.lang.Class) not found in java.lang.Class
    at javassist.CtBehavior.insertAfter(CtBehavior.java:909)
    at javassist.CtBehavior.insertAfter(CtBehavior.java:824)
    at Agent3$IdleBehavior.action(Agent3.java:202)
    at jade.core.behaviours.Behaviour.actionWrapper(Behaviour.java:344)
    at jade.core.Agent$ActiveLifeCycle.execute(Agent.java:1585)
    at jade.core.Agent.run(Agent.java:1524)
    at java.lang.Thread.run(Unknown Source)
Caused by: compile error: getMethod(java.lang.String,java.lang.Class,java.lang.Class) not found in java.lang.Class
    at javassist.compiler.TypeChecker.atMethodCallCore(TypeChecker.java:777)
    at javassist.compiler.TypeChecker.atCallExpr(TypeChecker.java:723)
    at javassist.compiler.JvstTypeChecker.atCallExpr(JvstTypeChecker.java:170)
    at javassist.compiler.ast.CallExpr.accept(CallExpr.java:49)
    at javassist.compiler.TypeChecker.atCallExpr(TypeChecker.java:693)
    at javassist.compiler.JvstTypeChecker.atCallExpr(JvstTypeChecker.java:170)
    at javassist.compiler.ast.CallExpr.accept(CallExpr.java:49)
    at javassist.compiler.CodeGen.doTypeCheck(CodeGen.java:266)
    at javassist.compiler.CodeGen.atStmnt(CodeGen.java:360)
    at javassist.compiler.ast.Stmnt.accept(Stmnt.java:53)
    at javassist.compiler.Javac.compileStmnt(Javac.java:578)
    at javassist.CtBehavior.insertAfterAdvice(CtBehavior.java:924)
    at javassist.CtBehavior.insertAfter(CtBehavior.java:883)
    ... 6 more





Aucun commentaire:

Enregistrer un commentaire