Search in sources :

Example 26 with TraceClassVisitor

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

the class PrintBytecodeTransformer method transform.

/* (non-Javadoc)
     * @see java.lang.instrument.ClassFileTransformer#transform(java.lang.ClassLoader, java.lang.String, java.lang.Class, java.security.ProtectionDomain, byte[])
     */
/**
 * {@inheritDoc}
 */
@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
    if (className != null) {
        try {
            String classNameWithDots = ResourceList.getClassNameFromResourcePath(className);
            ClassReader reader = new ClassReader(classfileBuffer);
            ClassWriter writer = new ClassWriter(org.objectweb.asm.ClassWriter.COMPUTE_MAXS);
            ClassVisitor cv = writer;
            if (classNameWithDots.equals(target_class) || (classNameWithDots.startsWith(target_class + "$"))) {
                // if (classNameWithDots.startsWith(Properties.PROJECT_PREFIX)) {
                cv = new TraceClassVisitor(cv, new PrintWriter(System.out));
            }
            reader.accept(cv, ClassReader.SKIP_FRAMES);
            classfileBuffer = writer.toByteArray();
            return classfileBuffer;
        } catch (Throwable t) {
            throw new Error(t);
        }
    }
    return classfileBuffer;
}
Also used : TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) ClassReader(org.objectweb.asm.ClassReader) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) ClassVisitor(org.objectweb.asm.ClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter) PrintWriter(java.io.PrintWriter)

Example 27 with TraceClassVisitor

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

the class BytecodeInstrumentation method transformBytes.

/**
 * <p>
 * transformBytes
 * </p>
 *
 * @param className
 *            a {@link java.lang.String} object.
 * @param reader
 *            a {@link org.objectweb.asm.ClassReader} object.
 * @return an array of byte.
 */
public byte[] transformBytes(ClassLoader classLoader, String className, ClassReader reader) {
    int readFlags = ClassReader.SKIP_FRAMES;
    if (Properties.INSTRUMENTATION_SKIP_DEBUG)
        readFlags |= ClassReader.SKIP_DEBUG;
    String classNameWithDots = ResourceList.getClassNameFromResourcePath(className);
    if (!checkIfCanInstrument(classNameWithDots)) {
        throw new RuntimeException("Should not transform a shared class (" + classNameWithDots + ")! Load by parent (JVM) classloader.");
    }
    TransformationStatistics.reset();
    /*
		 * To use COMPUTE_FRAMES we need to remove JSR commands. Therefore, we
		 * have a JSRInlinerAdapter in NonTargetClassAdapter as well as
		 * CFGAdapter.
		 */
    int asmFlags = ClassWriter.COMPUTE_FRAMES;
    ClassWriter writer = new ComputeClassWriter(asmFlags);
    ClassVisitor cv = writer;
    if (logger.isDebugEnabled()) {
        cv = new TraceClassVisitor(cv, new PrintWriter(System.err));
    }
    if (Properties.RESET_STATIC_FIELDS) {
        cv = new StaticAccessClassAdapter(cv, className);
    }
    if (Properties.PURE_INSPECTORS) {
        CheapPurityAnalyzer purityAnalyzer = CheapPurityAnalyzer.getInstance();
        cv = new PurityAnalysisClassVisitor(cv, className, purityAnalyzer);
    }
    if (Properties.MAX_LOOP_ITERATIONS >= 0) {
        cv = new LoopCounterClassAdapter(cv);
    }
    // Apply transformations to class under test and its owned classes
    if (DependencyAnalysis.shouldAnalyze(classNameWithDots)) {
        logger.debug("Applying target transformation to class " + classNameWithDots);
        if (!Properties.TEST_CARVING && Properties.MAKE_ACCESSIBLE) {
            cv = new AccessibleClassAdapter(cv, className);
        }
        cv = new RemoveFinalClassAdapter(cv);
        cv = new ExecutionPathClassAdapter(cv, className);
        cv = new CFGClassAdapter(classLoader, cv, className);
        if (Properties.EXCEPTION_BRANCHES) {
            cv = new ExceptionTransformationClassAdapter(cv, className);
        }
        if (Properties.ERROR_BRANCHES) {
            cv = new ErrorConditionClassAdapter(cv, className);
        }
    } else {
        logger.debug("Not applying target transformation");
        cv = new NonTargetClassAdapter(cv, className);
        if (Properties.MAKE_ACCESSIBLE) {
            cv = new AccessibleClassAdapter(cv, className);
        }
        // to create the CFG first
        if (Properties.TT && classNameWithDots.startsWith(Properties.CLASS_PREFIX)) {
            cv = new CFGClassAdapter(classLoader, cv, className);
        }
    }
    // Collect constant values for the value pool
    cv = new PrimitiveClassAdapter(cv, className);
    if (Properties.RESET_STATIC_FIELDS) {
        cv = handleStaticReset(className, cv);
    }
    // Mock instrumentation (eg File and TCP).
    if (TestSuiteWriterUtils.needToUseAgent()) {
        cv = new MethodCallReplacementClassAdapter(cv, className);
        /*
			 * If the class is serializable, then doing any change (adding hashCode, static reset, etc)
			 * will change the serialVersionUID if it is not defined in the class.
			 * Hence, if it is not defined, we have to define it to
			 * avoid problems in serialising the class, as reading Master will not do instrumentation.
			 * The serialVersionUID HAS to be the same as the un-instrumented class
			 */
        if (RuntimeSettings.applyUIDTransformation)
            cv = new SerialVersionUIDAdder(cv);
    }
    // Testability Transformations
    if (classNameWithDots.startsWith(Properties.PROJECT_PREFIX) || (!Properties.TARGET_CLASS_PREFIX.isEmpty() && classNameWithDots.startsWith(Properties.TARGET_CLASS_PREFIX)) || shouldTransform(classNameWithDots)) {
        ClassNode cn = new AnnotatedClassNode();
        reader.accept(cn, readFlags);
        logger.info("Starting transformation of " + className);
        if (Properties.STRING_REPLACEMENT) {
            StringTransformation st = new StringTransformation(cn);
            if (isTargetClassName(classNameWithDots) || shouldTransform(classNameWithDots))
                cn = st.transform();
        }
        ComparisonTransformation cmp = new ComparisonTransformation(cn);
        if (isTargetClassName(classNameWithDots) || shouldTransform(classNameWithDots)) {
            cn = cmp.transform();
            ContainerTransformation ct = new ContainerTransformation(cn);
            cn = ct.transform();
        }
        if (shouldTransform(classNameWithDots)) {
            logger.info("Testability Transforming " + className);
            BooleanTestabilityTransformation tt = new BooleanTestabilityTransformation(cn, classLoader);
            try {
                cn = tt.transform();
            } catch (Throwable t) {
                throw new Error(t);
            }
            logger.info("Testability Transformation done: " + className);
        }
        // -----
        cn.accept(cv);
        if (Properties.TEST_CARVING && TransformerUtil.isClassConsideredForInstrumentation(className)) {
            return handleCarving(className, writer);
        }
    } else {
        reader.accept(cv, readFlags);
    }
    return writer.toByteArray();
}
Also used : ClassVisitor(org.objectweb.asm.ClassVisitor) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) SerialVersionUIDAdder(org.objectweb.asm.commons.SerialVersionUIDAdder) BooleanTestabilityTransformation(org.evosuite.instrumentation.testability.BooleanTestabilityTransformation) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) CFGClassAdapter(org.evosuite.graphs.cfg.CFGClassAdapter) ComparisonTransformation(org.evosuite.instrumentation.testability.ComparisonTransformation) PrintWriter(java.io.PrintWriter) ContainerTransformation(org.evosuite.instrumentation.testability.ContainerTransformation) ClassNode(org.objectweb.asm.tree.ClassNode) ErrorConditionClassAdapter(org.evosuite.instrumentation.error.ErrorConditionClassAdapter) StringTransformation(org.evosuite.instrumentation.testability.StringTransformation) ClassWriter(org.objectweb.asm.ClassWriter) ComputeClassWriter(org.evosuite.runtime.util.ComputeClassWriter) CheapPurityAnalyzer(org.evosuite.assertion.CheapPurityAnalyzer) ComputeClassWriter(org.evosuite.runtime.util.ComputeClassWriter) PrimitiveClassAdapter(org.evosuite.seeding.PrimitiveClassAdapter)

Example 28 with TraceClassVisitor

use of org.objectweb.asm.util.TraceClassVisitor in project quasar by puniverse.

the class Retransform method dumpClass.

public static void dumpClass(String className, byte[] data) {
    System.err.println("DUMP OF CLASS: " + className);
    ClassReader cr = new ClassReader(data);
    ClassVisitor cv = new TraceClassVisitor(null, new Textifier(), new PrintWriter(System.err));
    cr.accept(cv, ClassReader.SKIP_FRAMES);
    System.out.println("=================");
}
Also used : TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) ClassReader(org.objectweb.asm.ClassReader) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) ClassVisitor(org.objectweb.asm.ClassVisitor) Textifier(org.objectweb.asm.util.Textifier) PrintWriter(java.io.PrintWriter)

Example 29 with TraceClassVisitor

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

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 30 with TraceClassVisitor

use of org.objectweb.asm.util.TraceClassVisitor in project tycho by eclipse.

the class ClassfileComparator method disassemble.

private String disassemble(byte[] bytes) {
    ClassReader reader = new ClassReader(bytes);
    ClassNode clazz = new ClassNode();
    reader.accept(clazz, Opcodes.ASM5 | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
    // inner class list gets reordered during pack200 normalization
    if (clazz.innerClasses != null) {
        List<InnerClassNode> sorted = new ArrayList<>(clazz.innerClasses);
        Collections.sort(sorted, new Comparator<InnerClassNode>() {

            @Override
            public int compare(InnerClassNode o1, InnerClassNode o2) {
                return o1.name.compareTo(o2.name);
            }
        });
        clazz.innerClasses = sorted;
    }
    // rendering human-readable bytecode is an eyecandy, we can compare ClassNodes directly
    StringWriter buffer = new StringWriter();
    PrintWriter writer = new PrintWriter(buffer);
    clazz.accept(new TraceClassVisitor(writer));
    writer.flush();
    writer.close();
    return buffer.toString();
}
Also used : InnerClassNode(org.objectweb.asm.tree.InnerClassNode) ClassNode(org.objectweb.asm.tree.ClassNode) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) StringWriter(java.io.StringWriter) ArrayList(java.util.ArrayList) ClassReader(org.objectweb.asm.ClassReader) InnerClassNode(org.objectweb.asm.tree.InnerClassNode) 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