Example usage for org.objectweb.asm.tree ClassNode accept

List of usage examples for org.objectweb.asm.tree ClassNode accept

Introduction

In this page you can find the example usage for org.objectweb.asm.tree ClassNode accept.

Prototype

public void accept(final ClassVisitor classVisitor) 

Source Link

Document

Makes the given class visitor visit this class.

Usage

From source file:org.coldswap.transformer.FloatMethodTransformer.java

License:Open Source License

@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
        ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
    if (className != null && !"".equals(className)) {
        for (String pack : ClassUtil.skipTransforming) {
            if (className.startsWith(pack)) {
                return classfileBuffer;
            }/*  www  .  j  a v  a2  s.  c o  m*/
        }

        ClassNode cn = new ClassNode(Opcodes.ASM5);
        ClassReader cr = new ClassReader(classfileBuffer);
        ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
        // create adapter for method insertion.
        cr.accept(cn, 0);
        // create a list of method name's so that we resolve method collision
        List<MethodNode> methodNodes = cn.methods;
        Iterator it = methodNodes.iterator();
        List<String> methodNames = new ArrayList<String>(methodNodes.size());
        while (it.hasNext()) {
            MethodNode methodNode = (MethodNode) it.next();
            methodNames.add(methodNode.name);
        }
        // insert helper methods if they do not exist.
        for (int i = 0; i < maxNumberOfMethods; i++) {
            String name = TransformerNameGenerator.getFloatMethodNameWithCounter(className, i);
            if (!methodNames.contains(name)) {
                MethodNode mn = MethodUtil.createFloatHelperMethod(className, i);
                cn.methods.add(mn);
            }
        }
        cn.accept(cw);
        byte[] toRet = cw.toByteArray();
        if (toRet != null) {
            logger.info("Successful transformation!");
            return toRet;
        } else {
            logger.severe("Could not transform class");
            return classfileBuffer;
        }
    }
    return classfileBuffer;

}

From source file:org.coldswap.transformer.IntMethodTransformer.java

License:Open Source License

@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
        ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
    if (className != null && !"".equals(className)) {
        for (String pack : ClassUtil.skipTransforming) {
            if (className.startsWith(pack)) {
                return classfileBuffer;
            }//from w w  w  . j  av a  2 s. c  o m
        }

        ClassNode cn = new ClassNode(Opcodes.ASM5);
        ClassReader cr = new ClassReader(classfileBuffer);
        ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
        // create adapter for method insertion.
        cr.accept(cn, 0);
        // create a list of method name's so that we resolve method collision
        List<MethodNode> methodNodes = cn.methods;
        Iterator it = methodNodes.iterator();
        List<String> methodNames = new ArrayList<String>(methodNodes.size());
        while (it.hasNext()) {
            MethodNode methodNode = (MethodNode) it.next();
            methodNames.add(methodNode.name);
        }
        // insert helper methods if they do not exist.
        for (int i = 0; i < maxNumberOfMethods; i++) {
            String name = TransformerNameGenerator.getIntMethodNameWithCounter(className, i);
            if (!methodNames.contains(name)) {
                MethodNode mn = MethodUtil.createIntHelperMethod(className, i);
                cn.methods.add(mn);
            }
        }
        cn.accept(cw);
        byte[] toRet = cw.toByteArray();
        if (toRet != null) {
            logger.info("Successful transformation!");
            return toRet;
        } else {
            logger.severe("Could not transform class");
            return classfileBuffer;
        }
    }
    return classfileBuffer;

}

From source file:org.coldswap.transformer.LongMethodTransformer.java

License:Open Source License

@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
        ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
    if (className != null && !"".equals(className)) {
        for (String pack : ClassUtil.skipTransforming) {
            if (className.startsWith(pack)) {
                return classfileBuffer;
            }/*  w ww . j  a  v a 2s. c  o  m*/
        }

        ClassNode cn = new ClassNode(Opcodes.ASM5);
        ClassReader cr = new ClassReader(classfileBuffer);
        ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
        // create adapter for method insertion.
        cr.accept(cn, 0);
        // create a list of method name's so that we resolve method collision
        List<MethodNode> methodNodes = cn.methods;
        Iterator it = methodNodes.iterator();
        List<String> methodNames = new ArrayList<String>(methodNodes.size());
        while (it.hasNext()) {
            MethodNode methodNode = (MethodNode) it.next();
            methodNames.add(methodNode.name);
        }
        // insert helper methods if they do not exist.
        for (int i = 0; i < maxNumberOfMethods; i++) {
            String name = TransformerNameGenerator.getLongMethodNameWithCounter(className, i);
            if (!methodNames.contains(name)) {
                MethodNode mn = MethodUtil.createLongHelperMethod(className, i);
                cn.methods.add(mn);
            }
        }
        cn.accept(cw);
        byte[] toRet = cw.toByteArray();
        if (toRet != null) {
            logger.info("Successful transformation!");
            return toRet;
        } else {
            logger.severe("Could not transform class");
            return classfileBuffer;
        }
    }
    return classfileBuffer;

}

From source file:org.coldswap.transformer.ObjectMethodTransformer.java

License:Open Source License

@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
        ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
    if (className != null && !"".equals(className)) {
        for (String pack : ClassUtil.skipTransforming) {
            if (className.startsWith(pack)) {
                return classfileBuffer;
            }/*from   w  w  w  .j a v a  2  s  . c  om*/
        }

        ClassNode cn = new ClassNode(Opcodes.ASM5);
        ClassReader cr = new ClassReader(classfileBuffer);
        ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
        // create adapter for method insertion.
        cr.accept(cn, 0);
        // create a list of method name's so that we resolve method collision
        List<MethodNode> methodNodes = cn.methods;
        Iterator it = methodNodes.iterator();
        List<String> methodNames = new ArrayList<String>(methodNodes.size());
        while (it.hasNext()) {
            MethodNode methodNode = (MethodNode) it.next();
            methodNames.add(methodNode.name);
        }
        // insert helper methods if they do not exist.
        for (int i = 0; i < maxNumberOfMethods; i++) {
            String name = TransformerNameGenerator.getObjectMethodNameWithCounter(className, i);
            if (!methodNames.contains(name)) {
                MethodNode mn = MethodUtil.createObjectHelperMethod(className, i);
                cn.methods.add(mn);
            }
        }
        cn.accept(cw);
        byte[] toRet = cw.toByteArray();
        if (toRet != null) {
            logger.info("Successful transformation!");
            return toRet;
        } else {
            logger.severe("Could not transform class");
            return classfileBuffer;
        }
    }
    return classfileBuffer;

}

From source file:org.coldswap.transformer.ReferenceReplacerManager.java

License:Open Source License

/**
 * Starts the process of find and replace for the given class node.
 *
 * @param clazz where to find and replace.
 * @return transformed class byte array.
 *///from  w  w  w .ja v a 2s.  c o m
public byte[] runReferenceReplacer(byte[] clazz) {
    ClassNode classNode = new ClassNode(Opcodes.ASM5);
    ClassReader classReader = new ClassReader(clazz);
    ClassWriter classWriter = new ClassWriter(classReader,
            ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    classReader.accept(classNode, 0);
    for (ReferenceReplacer replacer : fieldReferences) {
        replacer.findAndReplace(classNode);
    }

    classNode.accept(classWriter);
    return classWriter.toByteArray();
}

From source file:org.coldswap.transformer.StringMethodTransformer.java

License:Open Source License

@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
        ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
    if (className != null && !"".equals(className)) {
        for (String pack : ClassUtil.skipTransforming) {
            if (className.startsWith(pack)) {
                return classfileBuffer;
            }//w  ww  .  j  a v  a  2s.co  m
        }

        ClassNode cn = new ClassNode(Opcodes.ASM5);
        ClassReader cr = new ClassReader(classfileBuffer);
        ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
        // create adapter for method insertion.
        cr.accept(cn, 0);
        // create a list of method name's so that we resolve method collision
        List<MethodNode> methodNodes = cn.methods;
        Iterator it = methodNodes.iterator();
        List<String> methodNames = new ArrayList<String>(methodNodes.size());
        while (it.hasNext()) {
            MethodNode methodNode = (MethodNode) it.next();
            methodNames.add(methodNode.name);
        }
        // insert helper methods if they do not exist.
        for (int i = 0; i < maxNumberOfMethods; i++) {
            String name = TransformerNameGenerator.getStringMethodNameWithCounter(className, i);
            if (!methodNames.contains(name)) {
                MethodNode mn = MethodUtil.createStringHelperMethod(className, i);
                cn.methods.add(mn);
            }
        }
        cn.accept(cw);
        byte[] toRet = cw.toByteArray();
        if (toRet != null) {
            logger.info("Successful transformation!");
            return toRet;
        } else {
            logger.severe("Could not transform class");
            return classfileBuffer;
        }
    }
    return classfileBuffer;

}

From source file:org.coldswap.util.ByteCodeGenerator.java

License:Open Source License

/**
 * Creates a new class containing the new static field.
 *
 * @param classNode        containing the old class.
 * @param fieldNode        containing the old field.
 * @param initInstructions a list of instructions that goes into <clinit>.
 * @param className        the name of the new class to be generated.
 * @return an array of bytes which builds the new class.
 *///w  ww  .jav a 2  s  . co  m
@SuppressWarnings("unchecked")
public static byte[] newFieldClass(ClassNode classNode, FieldNode fieldNode, InsnList initInstructions,
        String className) {
    ClassNode newClass = new ClassNode();
    newClass.version = classNode.version;
    newClass.access = Opcodes.ACC_PUBLIC;
    newClass.signature = "L" + className + ";";
    newClass.name = className;
    newClass.superName = "java/lang/Object";
    newClass.fields.add(
            new FieldNode(fieldNode.access, fieldNode.name, fieldNode.desc, fieldNode.desc, fieldNode.value));
    if (initInstructions != null) {
        if (initInstructions.size() > 0) {
            MethodNode mn = new MethodNode(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
            InsnList il = mn.instructions;
            il.add(new LabelNode());
            il.add(initInstructions);
            il.add(new FieldInsnNode(Opcodes.PUTSTATIC, className, fieldNode.name, fieldNode.desc));
            il.add(new InsnNode(Opcodes.RETURN));
            newClass.methods.add(mn);
        }
    }

    ClassWriter newCWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    newClass.accept(newCWriter);
    return newCWriter.toByteArray();
}

From source file:org.copperengine.core.wfrepo.AbstractWorkflowRepository.java

License:Apache License

void instrumentWorkflows(File adaptedTargetDir, Map<String, Clazz> clazzMap, Map<String, ClassInfo> classInfos,
        File compileTargetDir) throws IOException {
    logger.info("Instrumenting classfiles");
    URLClassLoader tmpClassLoader = new URLClassLoader(new URL[] { compileTargetDir.toURI().toURL() },
            Thread.currentThread().getContextClassLoader());
    for (Clazz clazz : clazzMap.values()) {
        byte[] bytes;
        FileInputStream fis = new FileInputStream(clazz.classfile);
        try {/*from   w w w  .j  a v  a 2  s .c o  m*/
            ClassReader cr2 = new ClassReader(fis);
            ClassNode cn = new ClassNode();
            cr2.accept(cn, flags);

            // Now content of ClassNode can be modified and then serialized back into bytecode:
            new TryCatchBlockHandler().instrument(cn);

            ClassWriter cw2 = new ClassWriter(0);
            cn.accept(cw2);
            bytes = cw2.toByteArray();

            if (logger.isTraceEnabled()) {
                StringWriter sw = new StringWriter();
                new ClassReader(bytes).accept(new TraceClassVisitor(new PrintWriter(sw)), 0);
                logger.trace(sw.toString());
            }

            ClassReader cr = new ClassReader(bytes);
            ClassWriter cw = new ClassWriter(0);

            ScottyClassAdapter cv = new ScottyClassAdapter(cw, clazz.aggregatedInterruptableMethods);
            cr.accept(cv, flags);
            classInfos.put(clazz.classname, cv.getClassInfo());
            bytes = cw.toByteArray();

            // Recompute frames, etc.
            ClassReader cr3 = new ClassReader(bytes);
            ClassWriter cw3 = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
            cr3.accept(cw3, ClassReader.SKIP_FRAMES);
            bytes = cw3.toByteArray();

            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            CheckClassAdapter.verify(new ClassReader(cw.toByteArray()), tmpClassLoader, false, pw);
            if (sw.toString().length() != 0) {
                logger.error("CheckClassAdapter.verify failed for class " + cn.name + ":\n" + sw.toString());
            } else {
                logger.info("CheckClassAdapter.verify succeeded for class " + cn.name);
            }

        } finally {
            fis.close();
        }

        File adaptedClassfileName = new File(adaptedTargetDir, clazz.classname + ".class");
        adaptedClassfileName.getParentFile().mkdirs();
        FileOutputStream fos = new FileOutputStream(adaptedClassfileName);
        try {
            fos.write(bytes);
        } finally {
            fos.close();
        }
    }
}

From source file:org.devinprogress.YAIF.Transformer.ASMHelper.java

public byte[] transform(String obfClassName, String className, byte[] bytes) {
    if (!map.containsKey(className))
        return bytes;
    Map<String, Method> transMap = map.get(className);

    ClassReader cr = new ClassReader(bytes);
    ClassNode cn = new ClassNode();
    cr.accept(cn, 0);//from www  .  j  ava 2s .  co m

    for (MethodNode mn : cn.methods) {
        //System.out.println(String.format("Examing Method: %s%s",mn.name,mn.desc));
        String methodName = FMLDeobfuscatingRemapper.INSTANCE.mapMethodName(obfClassName, mn.name, mn.desc);
        String methodDesc = FMLDeobfuscatingRemapper.INSTANCE.mapMethodDesc(mn.desc);
        if (transMap.containsKey(methodName + methodDesc)) {
            try {
                //System.out.println(String.format("Invoking Method: %s%s",mn.name,mn.desc));
                transMap.get(methodName + methodDesc).invoke(obj, mn);
            } catch (Exception e) {
                e.printStackTrace();
                return bytes;
            }
        }
    }

    ClassWriter cw = new ClassWriter(0);
    cn.accept(cw);
    return cw.toByteArray();
}

From source file:org.eclipse.tycho.zipcomparator.internal.ClassfileComparator.java

License:Open Source License

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//from  ww w  . j a  v a  2  s. c o  m
            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();
}