Search in sources :

Example 31 with TraceClassVisitor

use of org.objectweb.asm.util.TraceClassVisitor in project evosuite by EvoSuite.

the class TestStubbingClassLoader method instrumentClass.

private Class<?> instrumentClass(String fullyQualifiedTargetClass) throws ClassNotFoundException {
    try {
        String className = fullyQualifiedTargetClass.replace('.', '/');
        InputStream is = ClassLoader.getSystemResourceAsStream(className + ".class");
        if (is == null) {
            throw new ClassNotFoundException("Class '" + className + ".class" + "' should be in target project, but could not be found!");
        }
        ClassReader reader = new ClassReader(is);
        ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
        TraceClassVisitor tc = new TraceClassVisitor(writer, new PrintWriter(System.err));
        // CheckClassAdapter ca = new CheckClassAdapter(tc);
        StubClassVisitor visitor = new StubClassVisitor(tc, fullyQualifiedTargetClass);
        reader.accept(visitor, ClassReader.SKIP_FRAMES);
        byte[] byteBuffer = writer.toByteArray();
        Class<?> result = defineClass(fullyQualifiedTargetClass, byteBuffer, 0, byteBuffer.length);
        classes.put(fullyQualifiedTargetClass, result);
        return result;
    } catch (Throwable t) {
        throw new ClassNotFoundException(t.getMessage(), t);
    }
}
Also used : TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) InputStream(java.io.InputStream) ClassReader(org.objectweb.asm.ClassReader) ClassWriter(org.objectweb.asm.ClassWriter) PrintWriter(java.io.PrintWriter)

Example 32 with TraceClassVisitor

use of org.objectweb.asm.util.TraceClassVisitor in project evosuite by EvoSuite.

the class Instrumenter method transformClassNode.

@SuppressWarnings("unchecked")
public void transformClassNode(ClassNode cn, final String internalClassName) {
    if (!TransformerUtil.isClassConsideredForInstrumentation(internalClassName)) {
        logger.debug("Class {} has not been instrumented because its name is on the blacklist", internalClassName);
        return;
    }
    // consider only public and protected classes which are not interfaces
    if ((cn.access & Opcodes.ACC_INTERFACE) != 0) {
        return;
    }
    // No private
    if ((cn.access & Opcodes.ACC_PRIVATE) != 0) {
        // TODO: Why is this not detecting $DummyIntegrator?
        logger.debug("Ignoring private class {}", cn.name);
        return;
    }
    String packageName = internalClassName.replace('/', '.');
    if (packageName.contains("."))
        packageName = packageName.substring(0, packageName.lastIndexOf('.'));
    // ASM has some problem with the access of inner classes
    // so we check if the inner class name is the current class name
    // and if so, check if the inner class is actually accessible
    List<InnerClassNode> in = cn.innerClasses;
    for (InnerClassNode inc : in) {
        if (cn.name.equals(inc.name)) {
            logger.info("ASM Bug: Inner class equals class.");
            if ((inc.access & Opcodes.ACC_PROTECTED) == Opcodes.ACC_PROTECTED) {
                if (!Properties.CLASS_PREFIX.equals(packageName)) {
                    return;
                }
            }
            if ((inc.access & Opcodes.ACC_PRIVATE) == Opcodes.ACC_PRIVATE) {
                return;
            }
            logger.debug("Can use inner class {}", inc.name);
        }
    }
    logger.info("Checking package {} for class {}", packageName, cn.name);
    // Protected/default only if in same package
    if ((cn.access & Opcodes.ACC_PUBLIC) == 0) {
        if (!Properties.CLASS_PREFIX.equals(packageName)) {
            logger.info("Not using protected/default class because package name does not match");
            return;
        } else {
            logger.info("Using protected/default class because package name matches");
        }
    }
    /*
		if(	(cn.access & Opcodes.ACC_PUBLIC) == 0 && (cn.access & Opcodes.ACC_PROTECTED) == 0)
		{
			return;
		}
		*/
    final ArrayList<MethodNode> wrappedMethods = new ArrayList<MethodNode>();
    MethodNode methodNode;
    final Iterator<MethodNode> methodIter = cn.methods.iterator();
    while (methodIter.hasNext()) {
        methodNode = methodIter.next();
        // consider only public methods which are not abstract or native
        if (!TransformerUtil.isPrivate(methodNode.access) && !TransformerUtil.isAbstract(methodNode.access) && !TransformerUtil.isNative(methodNode.access) && !methodNode.name.equals("<clinit>")) {
            if (!TransformerUtil.isPublic(methodNode.access)) {
                // if(!Properties.CLASS_PREFIX.equals(packageName)) {
                transformWrapperCalls(methodNode);
                continue;
            // }
            }
            if (methodNode.name.equals("<init>")) {
                if (TransformerUtil.isAbstract(cn.access)) {
                    // We cannot invoke constructors of abstract classes directly
                    continue;
                }
                this.addFieldRegistryRegisterCall(methodNode);
            }
            this.instrumentPUTXXXFieldAccesses(cn, internalClassName, methodNode);
            this.instrumentGETXXXFieldAccesses(cn, internalClassName, methodNode);
            this.instrumentMethod(cn, internalClassName, methodNode, wrappedMethods);
        } else {
            transformWrapperCalls(methodNode);
        }
    }
    final int numWM = wrappedMethods.size();
    for (int i = 0; i < numWM; i++) {
        cn.methods.add(wrappedMethods.get(i));
    }
    TraceClassVisitor tcv = new TraceClassVisitor(new PrintWriter(System.err));
    cn.accept(tcv);
}
Also used : TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) MethodNode(org.objectweb.asm.tree.MethodNode) ArrayList(java.util.ArrayList) InnerClassNode(org.objectweb.asm.tree.InnerClassNode) PrintWriter(java.io.PrintWriter)

Example 33 with TraceClassVisitor

use of org.objectweb.asm.util.TraceClassVisitor in project groovy-core by groovy.

the class BytecodeDumper method processBytecode.

@Override
public byte[] processBytecode(final String name, final byte[] original) {
    PrintWriter pw = out instanceof PrintWriter ? (PrintWriter) out : new PrintWriter(out);
    TraceClassVisitor visitor = new TraceClassVisitor(pw);
    ClassReader reader = new ClassReader(original);
    reader.accept(visitor, 0);
    return original;
}
Also used : TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) ClassReader(org.objectweb.asm.ClassReader) PrintWriter(java.io.PrintWriter)

Example 34 with TraceClassVisitor

use of org.objectweb.asm.util.TraceClassVisitor in project gravel by gravel-st.

the class ASMClassWriter method printBytecode.

private void printBytecode(byte[] bytes) {
    ClassReader cr = new ClassReader(bytes);
    cr.accept(new TraceClassVisitor(new PrintWriter(System.out)), 0);
}
Also used : TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) ClassReader(org.objectweb.asm.ClassReader) PrintWriter(java.io.PrintWriter)

Example 35 with TraceClassVisitor

use of org.objectweb.asm.util.TraceClassVisitor in project pinpoint by naver.

the class ASMMethodNodeAdapterTestMain method addInterceptor.

private void addInterceptor(final String className) throws Exception {
    final boolean trace = false;
    final boolean verify = false;
    final String classInternalName = JavaAssistUtils.jvmNameToJavaName(className);
    ClassLoader classLoader = new ClassLoader() {

        @Override
        public Class<?> loadClass(String name) throws ClassNotFoundException {
            if (!name.startsWith("java") && !name.startsWith("sun") && super.findLoadedClass(name) == null) {
                try {
                    ClassNode classNode = ASMClassNodeLoader.get(JavaAssistUtils.javaNameToJvmName(name));
                    EngineComponent engineComponent = mock(DefaultEngineComponent.class);
                    ASMClass asmClass = new ASMClass(engineComponent, null, null, null, classNode);
                    if (asmClass.isInterceptable()) {
                        for (InstrumentMethod method : asmClass.getDeclaredMethods()) {
                            try {
                                method.addInterceptor(interceptorId);
                            } catch (Throwable t) {
                                t.printStackTrace();
                            }
                        }
                    }
                    byte[] bytes = asmClass.toBytecode();
                    if (trace) {
                        ClassReader classReader = new ClassReader(bytes);
                        ClassWriter cw = new ClassWriter(0);
                        TraceClassVisitor tcv = new TraceClassVisitor(cw, new PrintWriter(System.out));
                        classReader.accept(tcv, 0);
                    }
                    if (verify) {
                        CheckClassAdapter.verify(new ClassReader(bytes), false, new PrintWriter(System.out));
                    }
                    return super.defineClass(name, bytes, 0, bytes.length);
                } catch (Throwable ex) {
                    ex.printStackTrace();
                    return null;
                }
            } else {
                try {
                    return super.loadClass(name);
                } catch (Throwable t) {
                    t.printStackTrace();
                    return null;
                }
            }
        }
    };
    try {
        classLoader.loadClass(classInternalName);
    } catch (ClassNotFoundException ignore) {
    // ignore
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) InstrumentMethod(com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod) ClassWriter(org.objectweb.asm.ClassWriter) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) ClassReader(org.objectweb.asm.ClassReader) PrintWriter(java.io.PrintWriter)

Aggregations

TraceClassVisitor (org.objectweb.asm.util.TraceClassVisitor)39 PrintWriter (java.io.PrintWriter)31 ClassReader (org.objectweb.asm.ClassReader)24 ClassWriter (org.objectweb.asm.ClassWriter)16 StringWriter (java.io.StringWriter)14 ClassVisitor (org.objectweb.asm.ClassVisitor)10 Textifier (org.objectweb.asm.util.Textifier)10 IOException (java.io.IOException)6 File (java.io.File)5 ClassNode (org.objectweb.asm.tree.ClassNode)5 ArrayList (java.util.ArrayList)3 ASMifier (org.objectweb.asm.util.ASMifier)3 CheckClassAdapter (org.objectweb.asm.util.CheckClassAdapter)3 NeverNullArgAnalyzerAdapter (edu.columbia.cs.psl.phosphor.instrumenter.analyzer.NeverNullArgAnalyzerAdapter)2 FileInputStream (java.io.FileInputStream)2 URL (java.net.URL)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 DrillConfig (org.apache.drill.common.config.DrillConfig)2 QueryClassLoader (org.apache.drill.exec.compile.QueryClassLoader)2