Search in sources :

Example 1 with TraceClassVisitor

use of org.objectweb.asm.util.TraceClassVisitor in project presto by prestodb.

the class CompilerUtils method defineClasses.

private static Map<String, Class<?>> defineClasses(List<ClassDefinition> classDefinitions, DynamicClassLoader classLoader) {
    ClassInfoLoader classInfoLoader = ClassInfoLoader.createClassInfoLoader(classDefinitions, classLoader);
    if (DUMP_BYTE_CODE_TREE) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        DumpBytecodeVisitor dumpBytecode = new DumpBytecodeVisitor(new PrintStream(out));
        for (ClassDefinition classDefinition : classDefinitions) {
            dumpBytecode.visitClass(classDefinition);
        }
        System.out.println(new String(out.toByteArray(), StandardCharsets.UTF_8));
    }
    Map<String, byte[]> bytecodes = new LinkedHashMap<>();
    for (ClassDefinition classDefinition : classDefinitions) {
        ClassWriter cw = new SmartClassWriter(classInfoLoader);
        try {
            classDefinition.visit(ADD_FAKE_LINE_NUMBER ? new AddFakeLineNumberClassVisitor(cw) : cw);
        } catch (IndexOutOfBoundsException | NegativeArraySizeException e) {
            Printer printer = new Textifier();
            StringWriter stringWriter = new StringWriter();
            TraceClassVisitor tcv = new TraceClassVisitor(null, printer, new PrintWriter(stringWriter));
            classDefinition.visit(tcv);
            throw new IllegalArgumentException("An error occurred while processing classDefinition:" + System.lineSeparator() + stringWriter.toString(), e);
        }
        try {
            byte[] bytecode = cw.toByteArray();
            if (RUN_ASM_VERIFIER) {
                ClassReader reader = new ClassReader(bytecode);
                CheckClassAdapter.verify(reader, classLoader, true, new PrintWriter(System.out));
            }
            bytecodes.put(classDefinition.getType().getJavaClassName(), bytecode);
        } catch (RuntimeException e) {
            throw new CompilationException("Error compiling class " + classDefinition.getName(), e);
        }
    }
    String dumpClassPath = DUMP_CLASS_FILES_TO.get();
    if (dumpClassPath != null) {
        for (Map.Entry<String, byte[]> entry : bytecodes.entrySet()) {
            File file = new File(dumpClassPath, ParameterizedType.typeFromJavaClassName(entry.getKey()).getClassName() + ".class");
            try {
                log.debug("ClassFile: " + file.getAbsolutePath());
                Files.createParentDirs(file);
                Files.write(entry.getValue(), file);
            } catch (IOException e) {
                log.error(e, "Failed to write generated class file to: %s" + file.getAbsolutePath());
            }
        }
    }
    if (DUMP_BYTE_CODE_RAW) {
        for (byte[] bytecode : bytecodes.values()) {
            ClassReader classReader = new ClassReader(bytecode);
            classReader.accept(new TraceClassVisitor(new PrintWriter(System.err)), ClassReader.EXPAND_FRAMES);
        }
    }
    Map<String, Class<?>> classes = classLoader.defineClasses(bytecodes);
    try {
        for (Class<?> clazz : classes.values()) {
            Reflection.initialize(clazz);
        }
    } catch (VerifyError e) {
        throw new RuntimeException(e);
    }
    return classes;
}
Also used : Textifier(org.objectweb.asm.util.Textifier) Printer(org.objectweb.asm.util.Printer) LinkedHashMap(java.util.LinkedHashMap) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter) PrintStream(java.io.PrintStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ClassWriter(org.objectweb.asm.ClassWriter) ClassReader(org.objectweb.asm.ClassReader) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) File(java.io.File)

Example 2 with TraceClassVisitor

use of org.objectweb.asm.util.TraceClassVisitor in project jacoco by jacoco.

the class FramesTest method dump.

private String dump(byte[] bytes) {
    final StringWriter buffer = new StringWriter();
    final PrintWriter writer = new PrintWriter(buffer);
    new ClassReader(bytes).accept(new MaxStackEliminator(new TraceClassVisitor(writer)), ClassReader.EXPAND_FRAMES);
    return buffer.toString();
}
Also used : TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) StringWriter(java.io.StringWriter) ClassReader(org.objectweb.asm.ClassReader) PrintWriter(java.io.PrintWriter)

Example 3 with TraceClassVisitor

use of org.objectweb.asm.util.TraceClassVisitor in project jphp by jphp-compiler.

the class OpcodePrinter method toWriter.

public void toWriter(Writer writer, int flags) {
    PrintWriter printWriter = new PrintWriter(writer);
    TraceClassVisitor traceClassVisitor = new TraceClassVisitor(null, printWriter);
    classReader.accept(traceClassVisitor, flags);
}
Also used : TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor)

Example 4 with TraceClassVisitor

use of org.objectweb.asm.util.TraceClassVisitor in project closure-templates by google.

the class ClassData method toString.

@Override
public String toString() {
    StringWriter sw = new StringWriter();
    new ClassReader(data).accept(new TraceClassVisitor(null, new Textifier(), new PrintWriter(sw)), 0);
    return sw.toString();
}
Also used : TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) StringWriter(java.io.StringWriter) ClassReader(org.objectweb.asm.ClassReader) Textifier(org.objectweb.asm.util.Textifier) PrintWriter(java.io.PrintWriter)

Example 5 with TraceClassVisitor

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

the class BytecodeInstrumentation method handleCarving.

private byte[] handleCarving(String className, ClassWriter writer) {
    ClassReader cr = new ClassReader(writer.toByteArray());
    ClassNode cn2 = new ClassNode();
    cr.accept(cn2, ClassReader.EXPAND_FRAMES);
    this.testCarvingInstrumenter.transformClassNode(cn2, className);
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    cn2.accept(cw);
    if (logger.isDebugEnabled()) {
        final StringWriter sw = new StringWriter();
        cn2.accept(new TraceClassVisitor(new PrintWriter(sw)));
        logger.debug("test carving instrumentation result:\n{}", sw);
    }
    return cw.toByteArray();
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) StringWriter(java.io.StringWriter) ClassReader(org.objectweb.asm.ClassReader) ClassWriter(org.objectweb.asm.ClassWriter) ComputeClassWriter(org.evosuite.runtime.util.ComputeClassWriter) 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