Search in sources :

Example 6 with ClassReader

use of jdk.internal.org.objectweb.asm.ClassReader in project openj9 by eclipse.

the class Transformer method transform.

public byte[] transform(ClassLoader loader, String className, Class<?> classToTransform, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
    try {
        if (flag == MODIFY_EXISTING_METHOD) {
            // for fast HCR
            AgentMain.logger.debug("Instrumenting " + className + " by modifying existing method.");
            ClassReader reader = new ClassReader(classToTransform.getCanonicalName());
            ClassWriter cw = new ClassWriter(reader, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
            ClassVisitor visitor = new CustomClassVisitor(cw, classToTransform.getCanonicalName());
            reader.accept(visitor, ClassReader.EXPAND_FRAMES);
            return cw.toByteArray();
        } else if (flag == ADD_NEW_METHOD) {
            // for extended HCR
            AgentMain.logger.debug("Instrumenting " + className + " by adding a new method.");
            ClassReader reader = new ClassReader(classToTransform.getCanonicalName());
            ClassWriter cw = new ClassWriter(reader, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
            ClassVisitor cv = new MethodAddAdapter(new CheckClassAdapter(cw));
            reader.accept(cv, ClassReader.SKIP_FRAMES);
            return cw.toByteArray();
        } else if (flag == ADD_NEW_FIELD) {
            // for GCRetransform test
            AgentMain.logger.debug("Instrumenting " + className + " by adding a new field.");
            ClassReader reader = new ClassReader(classToTransform.getCanonicalName());
            ClassWriter cw = new ClassWriter(reader, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
            FieldNode newField = new FieldNode(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "new_static_int_array", "[I", null, null);
            ClassVisitor cv = new FieldAdder(new CheckClassAdapter(cw), newField, className, _methodName);
            reader.accept(cv, ClassReader.SKIP_FRAMES);
            return cw.toByteArray();
        }
        return null;
    } catch (IOException e) {
        return null;
    }
}
Also used : FieldNode(jdk.internal.org.objectweb.asm.tree.FieldNode) CheckClassAdapter(jdk.internal.org.objectweb.asm.util.CheckClassAdapter) ClassReader(jdk.internal.org.objectweb.asm.ClassReader) ClassVisitor(jdk.internal.org.objectweb.asm.ClassVisitor) IOException(java.io.IOException) ClassWriter(jdk.internal.org.objectweb.asm.ClassWriter)

Example 7 with ClassReader

use of jdk.internal.org.objectweb.asm.ClassReader in project Bytecoder by mirkosertic.

the class ASMifier method main.

/**
 * Prints the ASM source code to generate the given class to the standard
 * output.
 * <p>
 * Usage: ASMifier [-debug] &lt;binary class name or class file name&gt;
 *
 * @param args
 *            the command line arguments.
 *
 * @throws Exception
 *             if the class cannot be found, or if an IO exception occurs.
 */
public static void main(final String[] args) throws Exception {
    int i = 0;
    int flags = ClassReader.SKIP_DEBUG;
    boolean ok = true;
    if (args.length < 1 || args.length > 2) {
        ok = false;
    }
    if (ok && "-debug".equals(args[0])) {
        i = 1;
        flags = 0;
        if (args.length != 2) {
            ok = false;
        }
    }
    if (!ok) {
        System.err.println("Prints the ASM code to generate the given class.");
        System.err.println("Usage: ASMifier [-debug] " + "<fully qualified class name or class file name>");
        return;
    }
    ClassReader cr;
    if (args[i].endsWith(".class") || args[i].indexOf('\\') > -1 || args[i].indexOf('/') > -1) {
        cr = new ClassReader(new FileInputStream(args[i]));
    } else {
        cr = new ClassReader(args[i]);
    }
    cr.accept(new TraceClassVisitor(null, new ASMifier(), new PrintWriter(System.out)), flags);
}
Also used : ClassReader(jdk.internal.org.objectweb.asm.ClassReader) FileInputStream(java.io.FileInputStream) PrintWriter(java.io.PrintWriter)

Example 8 with ClassReader

use of jdk.internal.org.objectweb.asm.ClassReader in project Bytecoder by mirkosertic.

the class CheckClassAdapter method main.

/**
 * Checks a given class.
 * <p>
 * Usage: CheckClassAdapter &lt;binary class name or class file name&gt;
 *
 * @param args
 *            the command line arguments.
 *
 * @throws Exception
 *             if the class cannot be found, or if an IO exception occurs.
 */
public static void main(final String[] args) throws Exception {
    if (args.length != 1) {
        System.err.println("Verifies the given class.");
        System.err.println("Usage: CheckClassAdapter " + "<fully qualified class name or class file name>");
        return;
    }
    ClassReader cr;
    if (args[0].endsWith(".class")) {
        cr = new ClassReader(new FileInputStream(args[0]));
    } else {
        cr = new ClassReader(args[0]);
    }
    verify(cr, false, new PrintWriter(System.err));
}
Also used : ClassReader(jdk.internal.org.objectweb.asm.ClassReader) FileInputStream(java.io.FileInputStream) PrintWriter(java.io.PrintWriter)

Example 9 with ClassReader

use of jdk.internal.org.objectweb.asm.ClassReader in project Bytecoder by mirkosertic.

the class ModuleInfoExtender method toByteArray.

/**
 * Returns the bytes of the modified module-info.class.
 * Once this method has been called then the Extender object should
 * be discarded.
 */
public byte[] toByteArray() throws IOException {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
    AttributeAddingClassVisitor cv = new AttributeAddingClassVisitor(Opcodes.ASM5, cw);
    ClassReader cr = new ClassReader(in);
    if (packages != null)
        cv.addAttribute(new ModulePackagesAttribute(packages));
    if (mainClass != null)
        cv.addAttribute(new ModuleMainClassAttribute(mainClass));
    if (targetPlatform != null)
        cv.addAttribute(new ModuleTargetAttribute(targetPlatform));
    if (hashes != null)
        cv.addAttribute(new ModuleHashesAttribute(hashes));
    if (moduleResolution != null)
        cv.addAttribute(new ModuleResolutionAttribute(moduleResolution.value()));
    List<Attribute> attrs = new ArrayList<>();
    // prototypes of attributes that should be parsed
    attrs.add(new ModuleAttribute(version));
    attrs.add(new ModulePackagesAttribute());
    attrs.add(new ModuleMainClassAttribute());
    attrs.add(new ModuleTargetAttribute());
    attrs.add(new ModuleHashesAttribute());
    cr.accept(cv, attrs.toArray(new Attribute[0]), 0);
    // add any attributes that didn't replace previous attributes
    cv.finish();
    return cw.toByteArray();
}
Also used : Attribute(jdk.internal.org.objectweb.asm.Attribute) ArrayList(java.util.ArrayList) ClassReader(jdk.internal.org.objectweb.asm.ClassReader) ClassWriter(jdk.internal.org.objectweb.asm.ClassWriter)

Example 10 with ClassReader

use of jdk.internal.org.objectweb.asm.ClassReader in project CorfuDB by CorfuDB.

the class Utils method printByteCode.

public static String printByteCode(byte[] bytes) {
    ClassReader cr = new ClassReader(bytes);
    ClassNode cn = new ClassNode();
    cr.accept(cn, 0);
    final List<MethodNode> methods = cn.methods;
    StringBuilder sb = new StringBuilder();
    for (MethodNode m : methods) {
        InsnList inList = m.instructions;
        sb.append(m.name);
        for (int i = 0; i < inList.size(); i++) {
            sb.append(insnToString(inList.get(i)));
        }
    }
    return sb.toString();
}
Also used : ClassNode(jdk.internal.org.objectweb.asm.tree.ClassNode) MethodNode(jdk.internal.org.objectweb.asm.tree.MethodNode) ClassReader(jdk.internal.org.objectweb.asm.ClassReader) InsnList(jdk.internal.org.objectweb.asm.tree.InsnList)

Aggregations

ClassReader (jdk.internal.org.objectweb.asm.ClassReader)20 ClassWriter (jdk.internal.org.objectweb.asm.ClassWriter)9 PrintWriter (java.io.PrintWriter)7 FileInputStream (java.io.FileInputStream)6 ClassVisitor (jdk.internal.org.objectweb.asm.ClassVisitor)5 ClassNode (jdk.internal.org.objectweb.asm.tree.ClassNode)3 Attribute (jdk.internal.org.objectweb.asm.Attribute)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 URL (java.net.URL)1 ReadableByteChannel (java.nio.channels.ReadableByteChannel)1 ArrayList (java.util.ArrayList)1 JarEntry (java.util.jar.JarEntry)1 JarFile (java.util.jar.JarFile)1 BuiltinClassLoader (jdk.internal.loader.BuiltinClassLoader)1 AnnotationVisitor (jdk.internal.org.objectweb.asm.AnnotationVisitor)1 FieldVisitor (jdk.internal.org.objectweb.asm.FieldVisitor)1 MethodVisitor (jdk.internal.org.objectweb.asm.MethodVisitor)1