Example usage for org.objectweb.asm.tree MethodNode visitMethodInsn

List of usage examples for org.objectweb.asm.tree MethodNode visitMethodInsn

Introduction

In this page you can find the example usage for org.objectweb.asm.tree MethodNode visitMethodInsn.

Prototype

@Deprecated
public void visitMethodInsn(final int opcode, final String owner, final String name, final String descriptor) 

Source Link

Document

Visits a method instruction.

Usage

From source file:com.seovic.pof.PortableTypeGenerator.java

License:Apache License

private void implementDefaultConstructor() {
    MethodNode ctor = findMethod("<init>", "()V");
    if (ctor == null) {
        ctor = new MethodNode(ACC_PUBLIC, "<init>", "()V", null, null);

        ctor.visitCode();/*ww w .j a  va2  s . co  m*/
        ctor.visitVarInsn(ALOAD, 0);
        ctor.visitMethodInsn(INVOKESPECIAL, cn.superName, "<init>", "()V");
        ctor.visitInsn(RETURN);
        ctor.visitMaxs(1, 1);
        ctor.visitEnd();

        cn.methods.add(ctor);
    } else if ((ctor.access & ACC_PUBLIC) == 0) {
        LOG.info("Class " + cn.name + " has a non-public default constructor. Making it public.");
        ctor.access = ACC_PUBLIC;
    }
}

From source file:com.seovic.pof.PortableTypeGenerator.java

License:Apache License

private void implementReadExternal() {
    int index = 0;

    MethodNode mn = new MethodNode(ACC_PRIVATE, "readExternal", "(Lcom/tangosol/io/pof/PofReader;)V", null,
            new String[] { "java/io/IOException" });
    mn.visitCode();//from w w w. j av a2 s .com

    for (int version : properties.keySet()) {
        mn.visitVarInsn(ALOAD, 1);
        mn.visitMethodInsn(INVOKEINTERFACE, "com/tangosol/io/pof/PofReader", "getVersionId", "()I");
        mn.visitIntInsn(BIPUSH, version);
        Label l = new Label();
        mn.visitJumpInsn(IF_ICMPLT, l);

        SortedSet<FieldNode> fields = properties.get(version);
        for (FieldNode fn : fields) {
            Type type = Type.getType(fn.desc);

            if (isDebugEnabled()) {
                mn.visitLdcInsn("reading attribute " + index + " (" + fn.name + ") from POF stream");
                mn.visitIntInsn(BIPUSH, 7);
                mn.visitMethodInsn(INVOKESTATIC, "com/tangosol/net/CacheFactory", "log",
                        "(Ljava/lang/String;I)V");
            }
            mn.visitVarInsn(ALOAD, 0);
            mn.visitVarInsn(ALOAD, 1);
            mn.visitIntInsn(BIPUSH, index++);

            ReadMethod readMethod = getReadMethod(fn, type);
            readMethod.createTemplate(mn, fn, type);
            mn.visitMethodInsn(INVOKEINTERFACE, "com/tangosol/io/pof/PofReader", readMethod.getName(),
                    readMethod.getDescriptor());
            if (type.getSort() == Type.OBJECT || "readObjectArray".equals(readMethod.getName())) {
                mn.visitTypeInsn(CHECKCAST, type.getInternalName());
            }
            mn.visitFieldInsn(PUTFIELD, cn.name, fn.name, fn.desc);
        }

        mn.visitLabel(l);
        mn.visitFrame(F_SAME, 0, null, 0, null);
    }

    mn.visitInsn(RETURN);
    mn.visitMaxs(0, 0);
    mn.visitEnd();

    if (!hasMethod(mn)) {
        cn.methods.add(mn);
    }
    LOG.debug("Implemented method: " + mn.name);
}

From source file:com.seovic.pof.PortableTypeGenerator.java

License:Apache License

private void implementWriteExternal() {
    int index = 0;

    MethodNode mn = new MethodNode(ACC_PRIVATE, "writeExternal", "(Lcom/tangosol/io/pof/PofWriter;)V", null,
            new String[] { "java/io/IOException" });
    mn.visitCode();//from   ww  w .  j a  va2 s .  c o m

    for (int version : properties.keySet()) {
        SortedSet<FieldNode> fields = properties.get(version);
        for (FieldNode fn : fields) {
            addPofIndex(fn, index);
            Type type = Type.getType(fn.desc);

            if (isDebugEnabled()) {
                mn.visitLdcInsn("writing attribute " + index + " (" + fn.name + ") to POF stream");
                mn.visitIntInsn(BIPUSH, 7);
                mn.visitMethodInsn(INVOKESTATIC, "com/tangosol/net/CacheFactory", "log",
                        "(Ljava/lang/String;I)V");
            }
            mn.visitVarInsn(ALOAD, 1);
            mn.visitIntInsn(BIPUSH, index++);
            mn.visitVarInsn(ALOAD, 0);
            mn.visitFieldInsn(GETFIELD, cn.name, fn.name, fn.desc);

            WriteMethod writeMethod = getWriteMethod(fn, type);
            writeMethod.pushUniformTypes(mn);
            mn.visitMethodInsn(INVOKEINTERFACE, "com/tangosol/io/pof/PofWriter", writeMethod.getName(),
                    writeMethod.getDescriptor());
        }
    }

    mn.visitInsn(RETURN);
    mn.visitMaxs(0, 0);
    mn.visitEnd();

    if (!hasMethod(mn)) {
        cn.methods.add(mn);
    }
    LOG.debug("Implemented method: " + mn.name);
}

From source file:naftoreiclag.dontdigleft.transformer.ResizePlayerBoudingBox.java

License:Open Source License

@Override
public byte[] transform(String name, String transformedName, byte[] basicClass) {
    if (name.equals("net.minecraft.entity.player.EntityPlayer") || name.equals("xl")) {
        ClassNode classNode = new ClassNode();
        ClassReader classReader = new ClassReader(basicClass);

        classReader.accept(classNode, 0);

        MethodNode mv = new MethodNode(ASM4, ACC_PROTECTED, "setSize", "(FF)V", null, null);

        mv.visitCode();// w w  w .  java2 s  .  co  m
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitVarInsn(FLOAD, 1);
        mv.visitLdcInsn(new Float("0.6"));
        mv.visitInsn(FCMPL);
        Label l1 = new Label();
        mv.visitJumpInsn(IFNE, l1);
        mv.visitVarInsn(FLOAD, 2);
        mv.visitLdcInsn(new Float("1.8"));
        mv.visitInsn(FCMPL);
        mv.visitJumpInsn(IFNE, l1);
        Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLdcInsn(new Float("0.6"));
        mv.visitVarInsn(FSTORE, 1);
        Label l3 = new Label();
        mv.visitLabel(l3);
        mv.visitLdcInsn(new Float("0.6"));
        mv.visitVarInsn(FSTORE, 2);
        mv.visitLabel(l1);
        mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(FLOAD, 1);
        mv.visitVarInsn(FLOAD, 2);
        mv.visitMethodInsn(INVOKESPECIAL, "net/minecraft/entity/Entity", "setSize", "(FF)V");
        Label l4 = new Label();
        mv.visitLabel(l4);
        mv.visitInsn(RETURN);
        Label l5 = new Label();
        mv.visitLabel(l5);
        mv.visitLocalVariable("this", "Lnet/minecraft/entity/player/EntityPlayer;", null, l0, l5, 0);
        mv.visitLocalVariable("par1", "F", null, l0, l5, 1);
        mv.visitLocalVariable("par2", "F", null, l0, l5, 2);
        mv.visitMaxs(3, 3);
        mv.visitEnd();

        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        classNode.accept(cw);
        mv.accept(cw);

        return cw.toByteArray();
    } else {
        return basicClass;
    }
}

From source file:net.petercashel.jmsDd.util.ASMTransformer.java

License:Apache License

public static byte[] transform(String name, byte[] bytes) {
    if (debug)/*from   w  w  w  . j  a v  a 2 s . c o m*/
        System.out.println(bytes.length);
    ClassNode classNode = new ClassNode();
    String classNameASM = name.replace('.', '/');
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);
    boolean DoModInit = false;
    boolean HasInit = false;
    String initDesc = "()V";
    boolean lockDesc = false;

    try {
        try {
            for (int i = 0; i < classNode.visibleAnnotations.size(); i++) {
                AnnotationNode ann = (AnnotationNode) classNode.visibleAnnotations.get(i);
                if (ann.desc.equalsIgnoreCase("Lnet/petercashel/jmsDd/module/Module;")) {
                    try {
                        if (debug)
                            System.out.println("ANNOTE!");
                        DoModInit = true;
                        Map<String, Object> values = asmList2Map(ann.values);
                        ModuleSystem.modulesToLoad.put(
                                values.get("ModuleName").toString().replace("[", "").replace("]", ""),
                                classNode.name.replace("/", "."));
                    } catch (Exception e) {
                        e.printStackTrace();

                    }
                }
            }
        } catch (Exception e) {
        }

        try {
            if (DoModInit) {

                for (int i = 0; i < classNode.methods.size(); i++) {
                    MethodNode m = (MethodNode) classNode.methods.get(i);
                    if (m.name.contentEquals("<init>")) {
                        initDesc = m.desc;
                        if (m.desc.contentEquals("()V")) {
                            HasInit = true;
                            if (debug)
                                System.out.println("Found <init>");
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (debug)
        System.out.println("Still alive?");
    try {

        //          L0
        //             LINENUMBER 43 L0
        //             ALOAD 0
        //             INVOKESPECIAL // CLASSNAME FOR ASM  // ()V    ////// This is effectically a super() call but to the discovered constructor
        //            L1
        //             LINENUMBER 44 L1
        //             INVOKESTATIC net/petercashel/jmsDd/API/API$Impl.getAPI ()Lnet/petercashel/jmsDd/API/API;
        //             ALOAD 0
        //             INVOKEINTERFACE net/petercashel/jmsDd/API/API.registerEventBus (Ljava/lang/Object;)V
        //            L2
        //             LINENUMBER 46 L2
        //             RETURN
        //            L3
        //             LOCALVARIABLE this // "L" + CLASSNAME FOR ASM + ";" // L0 L3 0
        //             LOCALVARIABLE e Lnet/petercashel/jmsDd/event/module/DummyEvent; L0 L3 1
        //             MAXSTACK = 2
        //             MAXLOCALS = 2
        if (DoModInit) {
            if (HasInit) {
                if (debug)
                    System.out.println("Adding Extra Constructor to " + name);
                MethodNode constructor = new MethodNode(Opcodes.ACC_PUBLIC, "<init>",
                        "(Lnet/petercashel/jmsDd/event/module/DummyEvent;)V", null, null);

                Label L0 = new Label();
                constructor.visitLabel(L0);
                constructor.visitVarInsn(Opcodes.ALOAD, 0);
                constructor.visitMethodInsn(Opcodes.INVOKESPECIAL, classNameASM, "<init>", initDesc);

                Label L1 = new Label();
                constructor.visitLabel(L1);
                constructor.visitMethodInsn(Opcodes.INVOKESTATIC, "net/petercashel/jmsDd/API/API$Impl",
                        "getAPI", "()Lnet/petercashel/jmsDd/API/API;");
                constructor.visitVarInsn(Opcodes.ALOAD, 0);
                constructor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "net/petercashel/jmsDd/API/API",
                        "registerEventBus", "(Ljava/lang/Object;)V");

                Label L2 = new Label();
                constructor.visitLabel(L2);
                constructor.visitInsn(Opcodes.RETURN);

                Label L3 = new Label();
                constructor.visitLabel(L3);
                constructor.visitLocalVariable("this", "L" + classNameASM + ";", null, L0, L3, 0);
                constructor.visitLocalVariable("e", "Lnet/petercashel/jmsDd/event/module/DummyEvent;", null, L0,
                        L3, 1);
                constructor.visitMaxs(2, 2);
                constructor.visitEnd();
                classNode.methods.add(constructor);

            } else {
                System.err.println("WARNING! " + name
                        + " Doesn't have a default no-args constructor.  Module loader cannot chain load the constructor. \n If you are recieving this error and your module has no constructors defined, or a no-args constructor defined, \n please report the bug to the author of JMSDd.");
                MethodNode constructor = new MethodNode(Opcodes.ACC_PUBLIC, "<init>",
                        "(Lnet/petercashel/jmsDd/event/module/DummyEvent;)V", null, null);

                Label L0 = new Label();
                constructor.visitLabel(L0);
                constructor.visitVarInsn(Opcodes.ALOAD, 0);
                //INVOKESPECIAL java/lang/Object.<init> ()V ////// There is no other constructor, call super() to Object.
                constructor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");

                Label L1 = new Label();
                constructor.visitLabel(L1);
                constructor.visitMethodInsn(Opcodes.INVOKESTATIC, "net/petercashel/jmsDd/API/API$Impl",
                        "getAPI", "()Lnet/petercashel/jmsDd/API/API;");
                constructor.visitVarInsn(Opcodes.ALOAD, 0);
                constructor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "net/petercashel/jmsDd/API/API",
                        "registerEventBus", "(Ljava/lang/Object;)V");

                Label L2 = new Label();
                constructor.visitLabel(L2);
                constructor.visitInsn(Opcodes.RETURN);

                Label L3 = new Label();
                constructor.visitLabel(L3);
                constructor.visitLocalVariable("this", "L" + classNameASM + ";", null, L0, L3, 0);
                constructor.visitLocalVariable("e", "Lnet/petercashel/jmsDd/event/module/DummyEvent;", null, L0,
                        L3, 1);
                constructor.visitMaxs(2, 2);
                constructor.visitEnd();
                classNode.methods.add(constructor);
            }
            classNode.visitEnd();
            ClassWriter wr = new ClassWriter(0);
            classNode.accept(wr);

            bytes = wr.toByteArray();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (debug)
        System.out.println("Still alive.");
    if (debug)
        System.out.println(bytes.length);
    if (plugins.size() > 0) {
        for (ASMPlugin p : plugins) {
            try {
                bytes = p.transform(name, bytes);
            } catch (Exception e) {

            }
        }
    }

    return bytes;
}