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.jvnet.jax_ws_commons.beans_generator.ambassador.impl.asm.ASMRequestBeanGenerator.java

License:Open Source License

public byte[] generate() {
    ClassNode tree = new ClassNode();

    tree.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, methodInfo.getRequestBeanClassName().replace(".", "/"), null,
            Type.getInternalName(Object.class), new String[0]);

    generateNoArgsConstructor();/*from  w w w . j a  v  a 2  s  .co  m*/
    generateAnnotations();
    generateFields();

    tree.visitEnd();
    tree.accept(writer);
    return writer.toByteArray();
}

From source file:org.jvnet.jax_ws_commons.beans_generator.ambassador.impl.asm.ASMResponseBeanGenerator.java

License:Open Source License

public byte[] generate() {
    ClassNode tree = new ClassNode();

    tree.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, methodInfo.getResponseBeanClassName().replace(".", "/"), null,
            Type.getInternalName(Object.class), new String[0]);

    generateNoArgsConstructor();//from w  ww.j a v a  2  s .  c om
    generateAnnotations();
    generateFields();

    tree.visitEnd();
    tree.accept(writer);
    return writer.toByteArray();
}

From source file:org.lenskit.util.reflect.DynamicClassLoader.java

License:Open Source License

public Class<?> defineClass(ClassNode def) {
    ClassWriter cw = new ClassWriter(0);
    def.accept(cw);
    byte[] bytes = cw.toByteArray();
    if (DEBUG_DIR != null) {
        logger.debug("writing class {}", def.name);
        try {/*from   w w  w  . j  av  a 2s  .  c om*/
            Path fn = DEBUG_DIR.resolve(def.name + ".class");
            Files.createDirectories(fn.getParent());
            Files.write(fn, bytes);
        } catch (IOException e) {
            logger.error("error writing class file " + def.name, e);
        }
    }
    String name = def.name.replace('/', '.');
    logger.debug("defining class {} (internal name {})", name, def.name);
    try {
        Class<?> cls = defineClass(name, bytes, 0, bytes.length);
        logger.debug("defined class {}", cls);
        return cls;
    } catch (ClassFormatError e) {
        throw new IllegalArgumentException("Format error in class " + name, e);
    }
}

From source file:org.neptunepowered.vanilla.launch.transformer.AccessTransformer.java

License:MIT License

@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
    if (bytes == null || !this.modifiers.containsKey(transformedName)) {
        return bytes;
    }//  ww  w  .  j  av a2  s .co m

    ClassNode classNode = new ClassNode();
    ClassReader reader = new ClassReader(bytes);
    reader.accept(classNode, 0);

    for (Modifier m : this.modifiers.get(transformedName)) {
        if (m.isClass) { // Class
            classNode.access = m.transform(classNode.access);
        } else if (m.desc == null) { // Field
            for (FieldNode fieldNode : classNode.fields) {
                if (m.wildcard || fieldNode.name.equals(m.name)) {
                    fieldNode.access = m.transform(fieldNode.access);
                    if (!m.wildcard) {
                        break;
                    }
                }
            }
        } else {
            List<MethodNode> overridable = null;

            for (MethodNode methodNode : classNode.methods) {
                if (m.wildcard || (methodNode.name.equals(m.name) && methodNode.desc.equals(m.desc))) {
                    boolean wasPrivate = (methodNode.access & ACC_PRIVATE) != 0;
                    methodNode.access = m.transform(methodNode.access);

                    // Constructors always use INVOKESPECIAL
                    // if we changed from private to something else we need to replace all INVOKESPECIAL calls to
                    // this method with INVOKEVIRTUAL
                    // so that overridden methods will be called. Only need to scan this class, because obviously
                    // the method was private.
                    if (!methodNode.name.equals("<init>") && wasPrivate
                            && (methodNode.access & ACC_PRIVATE) == 0) {
                        if (overridable == null) {
                            overridable = Lists.newArrayListWithExpectedSize(3);
                        }

                        overridable.add(methodNode);
                    }

                    if (!m.wildcard) {
                        break;
                    }
                }
            }

            if (overridable != null) {
                for (MethodNode methodNode : classNode.methods) {
                    for (Iterator<AbstractInsnNode> itr = methodNode.instructions.iterator(); itr.hasNext();) {
                        AbstractInsnNode insn = itr.next();
                        if (insn.getOpcode() == INVOKESPECIAL) {
                            MethodInsnNode mInsn = (MethodInsnNode) insn;
                            for (MethodNode replace : overridable) {
                                if (replace.name.equals(mInsn.name) && replace.desc.equals(mInsn.desc)) {
                                    mInsn.setOpcode(INVOKEVIRTUAL);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    ClassWriter writer = new ClassWriter(0);
    classNode.accept(writer);
    return writer.toByteArray();
}

From source file:org.qkit.core.asm.adapters.AddCustomGetterAdapter.java

License:Open Source License

@Override
public void visitEnd() {
    ClassNode cn = (ClassNode) cv;

    for (FieldNode f : cn.fields) {
        if (fieldName.equals(f.name) && fieldDescriptor.equals(f.desc)) {
            isFieldPresent = true;/*from  w  ww  . j a  va  2 s .c  om*/
            signature = f.signature;
            if ((f.access & ACC_STATIC) != 0) {
                isStatic = true;
            }
            break;
        }
    }

    for (MethodNode mv : cn.methods) {
        if (getterName.equals(cn.name) && fieldDescriptor.equals(mv.desc)) {
            isMethodPresent = true;
            break;
        }
    }

    if (isFieldPresent && !isMethodPresent) {
        MethodNode mn = new MethodNode(ACC_PUBLIC, getterName, "()" + getterDesc, signature, null);

        mn.instructions.add(new VarInsnNode(ALOAD, 0));
        mn.instructions
                .add(new FieldInsnNode(isStatic ? GETSTATIC : GETFIELD, cn.name, fieldName, fieldDescriptor));
        mn.instructions.add(new InsnNode(retInsn));

        mn.visitMaxs(3, 3);
        mn.visitEnd();
        cn.methods.add(mn);

        System.out.println("          [+M] " + fieldDescriptor + " " + getterName + "() identified as "
                + cn.name + "." + fieldName);
    }

    try {
        cn.accept(next);
    } catch (Exception ez) {
        ez.printStackTrace();
    }

}

From source file:org.qkit.core.asm.adapters.AddCustomGraphicsAdapter.java

License:Open Source License

@Override
public void visitEnd() {
    ClassNode cn = (ClassNode) cv;

    String imgVar = null;//from  w w w.  j  a v  a2s  .c o  m
    for (FieldNode f : cn.fields) {
        if (f.desc.contains(Image.class.getName().replace('.', '/')))
            imgVar = f.name;
    }

    MethodNode mm = null, mv = null;

    for (MethodNode m : cn.methods) {
        if (m.name.equals("drawGraphics")) {
            mm = m;
            mv = new MethodNode(ACC_PUBLIC, "drawGraphics", "(ILjava/awt/Graphics;I)V", null, null);
            mv.visitCode();
            mv.visitVarInsn(ALOAD, 0);
            mv.visitMethodInsn(INVOKESPECIAL, "RSImageProducer", "method239", "()V");
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, "RSImageProducer", "anImage320", "Ljava/awt/Image;");
            mv.visitVarInsn(ILOAD, 1);
            mv.visitVarInsn(ILOAD, 3);
            mv.visitMethodInsn(INVOKESTATIC, "RSImageProducer", "render", "(Ljava/awt/Image;II)Z");
            Label l0 = new Label();
            mv.visitJumpInsn(IFEQ, l0);
            mv.visitInsn(RETURN);
            mv.visitLabel(l0);
            mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
            mv.visitVarInsn(ALOAD, 2);
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, "RSImageProducer", "anImage320", "Ljava/awt/Image;");
            mv.visitVarInsn(ILOAD, 3);
            mv.visitVarInsn(ILOAD, 1);
            mv.visitVarInsn(ALOAD, 0);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/awt/Graphics", "drawImage",
                    "(Ljava/awt/Image;IILjava/awt/image/ImageObserver;)Z");
            mv.visitInsn(POP);
            mv.visitInsn(RETURN);
            mv.visitMaxs(5, 4);
            mv.visitEnd();
        }
    }

    cn.methods.remove(mm);
    cn.methods.add(mv);

    System.out.println("          [+M] Added gfx method");

    try {
        cn.accept(next);
    } catch (Exception ez) {
        ez.printStackTrace();
    }

}

From source file:org.qkit.core.asm.adapters.AddGetterAdapter.java

License:Open Source License

@Override
public void visitEnd() {
    ClassNode cn = (ClassNode) cv;

    for (FieldNode f : cn.fields) {
        if (fieldName.equals(f.name) && fieldDescriptor.equals(f.desc)) {
            isFieldPresent = true;//from w w  w .  java2  s  .c  o m
            signature = f.signature;
            if ((f.access & ACC_STATIC) != 0) {
                isStatic = true;
            }
            break;
        }
    }

    for (MethodNode mv : cn.methods) {
        if (getterName.equals(cn.name) && fieldDescriptor.equals(mv.desc)) {
            isMethodPresent = true;
            break;
        }
    }

    if (isFieldPresent && !isMethodPresent) {
        MethodNode mn = new MethodNode(ACC_PUBLIC, getterName, "()" + fieldDescriptor, signature, null);

        mn.instructions.add(new VarInsnNode(ALOAD, 0));
        mn.instructions
                .add(new FieldInsnNode(isStatic ? GETSTATIC : GETFIELD, cn.name, fieldName, fieldDescriptor));
        mn.instructions.add(new InsnNode(retInsn));

        mn.visitMaxs(3, 3);
        mn.visitEnd();
        cn.methods.add(mn);

        System.out.println("          [+M] " + fieldDescriptor + " " + getterName + "() identified as "
                + cn.name + "." + fieldName);
    }

    try {
        cn.accept(next);
    } catch (Exception ez) {
        ez.printStackTrace();
    }

}

From source file:org.qkit.core.asm.adapters.AddInterfaceAdapter.java

License:Open Source License

@Override
public void visitEnd() {
    ClassNode cn = (ClassNode) cv;
    for (String i : interfacesToAdd) {
        cn.interfaces.add(i);/*from   w  ww  . j  a  v  a2s  . co m*/
        System.out.println("[+Interface Addition] " + cn.name + " implements " + i);
    }

    cn.accept(next);

}

From source file:org.qkit.core.asm.adapters.AddMethodAdapter.java

License:Open Source License

@Override
public void visitEnd() {
    ClassNode cn = (ClassNode) cv;

    for (FieldNode f : cn.fields) {
        if (fieldName.equals(f.name) && fieldDescriptor.equals(f.desc)) {
            isFieldPresent = true;//from w  w w  . java  2  s . co m
            signature = f.signature;
            break;
        }
    }

    for (MethodNode mv : cn.methods) {
        if (getterName.equals(cn.name) && fieldDescriptor.equals(mv.desc)) {
            isMethodPresent = true;
            break;
        }
    }

    if (isFieldPresent && !isMethodPresent) {
        MethodNode mn = new MethodNode(ACC_PUBLIC, getterName, "()" + fieldDescriptor, signature, null);

        mn.instructions.add(new VarInsnNode(varInsn, 0));
        mn.instructions.add(new FieldInsnNode(GETFIELD, cn.name, fieldName, fieldDescriptor));
        mn.instructions.add(new InsnNode(retInsn));

        mn.visitMaxs(0, 0);

        cn.methods.add(mn);
        System.out.println(
                "[+Method Addition] " + fieldDescriptor + " " + getterName + "() returns " + fieldName);
    }

    try {
        cn.accept(next);
    } catch (Exception ez) {
        ez.printStackTrace();
    }

}

From source file:org.qkit.core.asm.adapters.ChangeMethodNameAdapter.java

License:Open Source License

@Override
public void visitEnd() {
    ClassNode cn = (ClassNode) cv;

    for (MethodNode mn : cn.methods) {
        if (mn.name.equals(targetMethodName)) {
            mn.name = wantedMethodName;/*from  ww  w . j  av a2  s  .  co m*/
            System.out
                    .println("    [!!M] Changed Method name: " + targetMethodName + " -> " + wantedMethodName);
        }
    }

    try {
        cn.accept(next);
    } catch (Exception ez) {
        ez.printStackTrace();
    }

}