Search in sources :

Example 1 with Textifier

use of org.objectweb.asm.util.Textifier 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 Textifier

use of org.objectweb.asm.util.Textifier 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 3 with Textifier

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

the class RegressionClassDiff method getClassInstructions.

/*
   * Get bytecode instructions for the class based on the following format: Method -> List of
   * instructions
   */
private static Map<String, List<Integer>> getClassInstructions(InputStream classAsInputStream) {
    HashMap<String, List<Integer>> methodInstructionsMap = new HashMap<>();
    try {
        ClassReader reader = new ClassReader(classAsInputStream);
        ClassNode classNode = new ClassNode();
        reader.accept(classNode, 0);
        @SuppressWarnings("unchecked") final List<MethodNode> methods = classNode.methods;
        Printer printer = new Textifier();
        TraceMethodVisitor mp = new TraceMethodVisitor(printer);
        for (MethodNode m : methods) {
            List<Integer> instructions = new ArrayList<>();
            InsnList inList = m.instructions;
            String mathodID = m.name + ": " + m.desc;
            System.out.println(mathodID);
            int[] methodInstructions = new int[inList.size()];
            for (int i = 0; i < inList.size(); i++) {
                int op = inList.get(i).getOpcode();
                methodInstructions[i] = op;
                AbstractInsnNode insn = inList.get(i);
                insn.accept(mp);
                // logger.warn("{} -> {}", sw.toString(), op);
                if (op != -1)
                    instructions.add(op);
            }
            methodInstructionsMap.put(mathodID, instructions);
        }
    } catch (IOException e) {
        // Will fail if ClassReader fails
        e.printStackTrace();
    }
    return methodInstructionsMap;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Printer(org.objectweb.asm.util.Printer) Textifier(org.objectweb.asm.util.Textifier) InsnList(org.objectweb.asm.tree.InsnList) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) TraceMethodVisitor(org.objectweb.asm.util.TraceMethodVisitor) MethodNode(org.objectweb.asm.tree.MethodNode) ClassReader(org.objectweb.asm.ClassReader) ArrayList(java.util.ArrayList) InsnList(org.objectweb.asm.tree.InsnList) List(java.util.List) ResourceList(org.evosuite.classpath.ResourceList)

Example 4 with Textifier

use of org.objectweb.asm.util.Textifier in project Galacticraft by micdoodle8.

the class ASMHelper method dump.

public static void dump(Acceptor acceptor, File file, boolean filterImportant, boolean sortLocals, boolean textify) {
    try {
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        if (!file.exists()) {
            file.createNewFile();
        }
        PrintWriter pout = new PrintWriter(file);
        ClassVisitor cv = new TraceClassVisitor(null, textify ? new Textifier() : new ASMifier(), pout);
        if (filterImportant) {
            cv = new ImportantInsnVisitor(cv);
        }
        if (sortLocals) {
            cv = new LocalVariablesSorterVisitor(cv);
        }
        acceptor.accept(cv);
        pout.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : ASMifier(org.objectweb.asm.util.ASMifier) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) ClassVisitor(org.objectweb.asm.ClassVisitor) IOException(java.io.IOException) Textifier(org.objectweb.asm.util.Textifier) PrintWriter(java.io.PrintWriter)

Example 5 with Textifier

use of org.objectweb.asm.util.Textifier in project drill by apache.

the class ReplaceMethodInvoke method main.

// private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(ReplaceMethodInvoke.class);
public static void main(String[] args) throws Exception {
    final String k2 = "org/apache/drill/Pickle.class";
    final URL url = Resources.getResource(k2);
    final byte[] clazz = Resources.toByteArray(url);
    final ClassReader cr = new ClassReader(clazz);
    final ClassWriter cw = writer();
    final TraceClassVisitor visitor = new TraceClassVisitor(cw, new Textifier(), new PrintWriter(System.out));
    final ValueHolderReplacementVisitor v2 = new ValueHolderReplacementVisitor(visitor, true);
    // | ClassReader.SKIP_DEBUG);
    cr.accept(v2, ClassReader.EXPAND_FRAMES);
    final byte[] output = cw.toByteArray();
    Files.write(output, new File("/src/scratch/bytes/S.class"));
    check(output);
    final DrillConfig c = DrillConfig.forClient();
    final SystemOptionManager m = new SystemOptionManager(PhysicalPlanReaderTestFactory.defaultLogicalPlanPersistence(c), new LocalPersistentStoreProvider(c), c);
    m.init();
    try (QueryClassLoader ql = new QueryClassLoader(DrillConfig.create(), m)) {
        ql.injectByteCode("org.apache.drill.Pickle$OutgoingBatch", output);
        Class<?> clz = ql.loadClass("org.apache.drill.Pickle$OutgoingBatch");
        clz.getMethod("x").invoke(null);
    }
}
Also used : LocalPersistentStoreProvider(org.apache.drill.exec.store.sys.store.provider.LocalPersistentStoreProvider) SystemOptionManager(org.apache.drill.exec.server.options.SystemOptionManager) Textifier(org.objectweb.asm.util.Textifier) URL(java.net.URL) ClassWriter(org.objectweb.asm.ClassWriter) QueryClassLoader(org.apache.drill.exec.compile.QueryClassLoader) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) DrillConfig(org.apache.drill.common.config.DrillConfig) ClassReader(org.objectweb.asm.ClassReader) File(java.io.File) PrintWriter(java.io.PrintWriter)

Aggregations

Textifier (org.objectweb.asm.util.Textifier)32 PrintWriter (java.io.PrintWriter)15 StringWriter (java.io.StringWriter)10 TraceClassVisitor (org.objectweb.asm.util.TraceClassVisitor)10 ClassReader (org.objectweb.asm.ClassReader)8 TraceMethodVisitor (org.objectweb.asm.util.TraceMethodVisitor)8 IOException (java.io.IOException)5 ClassWriter (org.objectweb.asm.ClassWriter)4 File (java.io.File)3 ClassVisitor (org.objectweb.asm.ClassVisitor)3 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 DrillConfig (org.apache.drill.common.config.DrillConfig)2 QueryClassLoader (org.apache.drill.exec.compile.QueryClassLoader)2 SystemOptionManager (org.apache.drill.exec.server.options.SystemOptionManager)2 LocalPersistentStoreProvider (org.apache.drill.exec.store.sys.store.provider.LocalPersistentStoreProvider)2 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)2