Example usage for org.objectweb.asm MethodVisitor visitMaxs

List of usage examples for org.objectweb.asm MethodVisitor visitMaxs

Introduction

In this page you can find the example usage for org.objectweb.asm MethodVisitor visitMaxs.

Prototype

public void visitMaxs(final int maxStack, final int maxLocals) 

Source Link

Document

Visits the maximum stack size and the maximum number of local variables of the method.

Usage

From source file:blockphysics.asm.BPTransformer.java

License:Open Source License

private byte[] transformEntityBoat(byte[] bytes) {
    /*try//from w  ww . j a  v  a 2  s. c o  m
    {
        FileOutputStream fos = new FileOutputStream("d:/EntityBoat.orig.class");
        fos.write(bytes);
      fos.close();
    } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }*/

    System.out.print("[BlockPhysics] Patching EntityBoat.class ..............");

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

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

    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "al", "()V", null, null);
    mv.visitCode();
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 1);
    mv.visitEnd();

    cw.visitEnd();

    System.out.println("OK");

    /*try
    {
       FileOutputStream fos = new FileOutputStream("d:/EntityBoat.class");
       fos.write(cw.toByteArray());
     fos.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }*/

    return cw.toByteArray();
}

From source file:blockphysics.asm.BPTransformer.java

License:Open Source License

private byte[] transformEntityFallingSand(byte[] bytes) {
    /*try// ww  w .j a  va  2  s  .c  o m
    {
        FileOutputStream fos = new FileOutputStream("d:/EntityFallingSand.orig.class");
        fos.write(bytes);
      fos.close();
    } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }*/

    System.out.print("[BlockPhysics] Patching EntityFallingSand.class .......");
    boolean ok = false;
    boolean ok2 = false;
    boolean ok3 = false;
    boolean ok4 = false;
    boolean ok5 = false;
    boolean ok6 = false;
    boolean ok7 = false;
    boolean ok8 = false;
    boolean ok9 = false, ok10 = false;

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

    MethodNode m;
    Iterator<MethodNode> methods = classNode.methods.iterator();
    while (methods.hasNext()) {
        m = methods.next();

        if (m.name.equals("<init>") && m.desc.equals("(Labv;)V")) {
            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == RETURN) {
                    InsnList toInject = new InsnList();

                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(ICONST_1));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "m", "Z"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new LdcInsnNode(new Float("0.996")));
                    toInject.add(new LdcInsnNode(new Float("0.996")));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "sq", "a", "(FF)V"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "sq", "P", "F"));
                    toInject.add(new InsnNode(FCONST_2));
                    toInject.add(new InsnNode(FDIV));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "N", "F"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(DCONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "x", "D"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(DCONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "y", "D"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(DCONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "z", "D"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(DCONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationX", "D"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new LdcInsnNode(new Double("-0.024525")));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationY", "D"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(DCONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationZ", "D"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(ICONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "slideDir", "B"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(ICONST_1));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "Z", "Z"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new LdcInsnNode(new Float("0.8")));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "aa", "F"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(ICONST_4));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "dead", "B"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(ICONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "bpdata", "I"));

                    m.instructions.insertBefore(m.instructions.get(index), toInject);
                    ok10 = true;
                    break;
                }
            }
        } else if (m.name.equals("<init>") && m.desc.equals("(Labv;DDDII)V")) {
            for (int index = 0; index < m.instructions.size(); index++) {
                if (m.instructions.get(index).getOpcode() == INVOKEVIRTUAL
                        && m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN
                        && ((MethodInsnNode) m.instructions.get(index)).owner.equals("sq")
                        && ((MethodInsnNode) m.instructions.get(index)).name.equals("a")) {
                    m.instructions.set(m.instructions.get(index - 1), new LdcInsnNode(new Float("0.996")));
                    m.instructions.set(m.instructions.get(index - 2), new LdcInsnNode(new Float("0.996")));
                    ok = true;
                    break;
                }
            }

            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == RETURN) {
                    InsnList toInject = new InsnList();

                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(DCONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationX", "D"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new LdcInsnNode(new Double("-0.024525")));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationY", "D"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(DCONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationZ", "D"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(ICONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "slideDir", "B"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(ICONST_1));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "Z", "Z"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new LdcInsnNode(new Float("0.8")));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "aa", "F"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(ICONST_4));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "dead", "B"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(ICONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "bpdata", "I"));

                    m.instructions.insertBefore(m.instructions.get(index), toInject);
                    ok2 = true;
                    break;
                }
            }
        } else if (m.name.equals("K") && m.desc.equals("()Z")) {
            InsnList toInject = new InsnList();
            toInject.add(new InsnNode(ICONST_0));
            toInject.add(new InsnNode(IRETURN));

            m.instructions.clear();
            m.localVariables.clear();
            m.instructions.add(toInject);
            ok9 = true;
        } else if (m.name.equals("l_") && m.desc.equals("()V")) {
            InsnList toInject = new InsnList();
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new FieldInsnNode(GETFIELD, "sq", "q", "Labv;"));
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics", "fallingSandUpdate",
                    "(Labv;Lsq;)V"));
            toInject.add(new InsnNode(RETURN));

            m.instructions.clear();
            m.localVariables.clear();
            m.instructions.add(toInject);
            ok3 = true;
        } else if (m.name.equals("b") && m.desc.equals("(F)V")) {
            m.instructions.clear();
            m.localVariables.clear();
            m.instructions.add(new InsnNode(RETURN));
            ok4 = true;
        } else if (m.name.equals("b") && m.desc.equals("(Lbx;)V")) {
            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == RETURN) {
                    InsnList toInject = new InsnList();

                    toInject.add(new VarInsnNode(ALOAD, 1));
                    toInject.add(new LdcInsnNode("Acceleration"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(ICONST_3));
                    toInject.add(new IntInsnNode(NEWARRAY, T_DOUBLE));
                    toInject.add(new InsnNode(DUP));
                    toInject.add(new InsnNode(ICONST_0));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "sq", "accelerationX", "D"));
                    toInject.add(new InsnNode(DASTORE));
                    toInject.add(new InsnNode(DUP));
                    toInject.add(new InsnNode(ICONST_1));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "sq", "accelerationY", "D"));
                    toInject.add(new InsnNode(DASTORE));
                    toInject.add(new InsnNode(DUP));
                    toInject.add(new InsnNode(ICONST_2));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "sq", "accelerationZ", "D"));
                    toInject.add(new InsnNode(DASTORE));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "sq", "a", "([D)Lcf;"));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "bx", "a", "(Ljava/lang/String;Lck;)V"));
                    toInject.add(new VarInsnNode(ALOAD, 1));
                    toInject.add(new LdcInsnNode("BPData"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new FieldInsnNode(GETFIELD, "sq", "bpdata", "I"));
                    toInject.add(new InsnNode(I2B));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "bx", "a", "(Ljava/lang/String;B)V"));

                    m.instructions.insertBefore(m.instructions.get(index), toInject);
                    ok5 = true;
                    break;
                }
            }
        } else if (m.name.equals("a") && m.desc.equals("(Lbx;)V")) {

            while (!(m.instructions.get(0).getType() == AbstractInsnNode.LDC_INSN
                    && ((LdcInsnNode) m.instructions.get(0)).cst.equals("Data"))) {
                m.instructions.remove(m.instructions.get(0));
                ok6 = true;
            }

            InsnList toInject = new InsnList();
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new VarInsnNode(ALOAD, 1));
            toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics", "readFallingSandID",
                    "(Lbx;)I"));
            toInject.add(new FieldInsnNode(PUTFIELD, "sq", "a", "I"));
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new VarInsnNode(ALOAD, 1));

            m.instructions.insertBefore(m.instructions.get(0), toInject);

            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == RETURN) {
                    toInject = new InsnList();
                    toInject.add(new VarInsnNode(ALOAD, 1));
                    toInject.add(new LdcInsnNode("Acceleration"));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "bx", "b", "(Ljava/lang/String;)Z"));
                    LabelNode l4 = new LabelNode();
                    toInject.add(new JumpInsnNode(IFEQ, l4));
                    toInject.add(new VarInsnNode(ALOAD, 1));
                    toInject.add(new LdcInsnNode("Acceleration"));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "bx", "m", "(Ljava/lang/String;)Lcf;"));
                    toInject.add(new VarInsnNode(ASTORE, 2));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new VarInsnNode(ALOAD, 2));
                    toInject.add(new InsnNode(ICONST_0));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "cf", "b", "(I)Lck;"));
                    toInject.add(new TypeInsnNode(CHECKCAST, "ca"));
                    toInject.add(new FieldInsnNode(GETFIELD, "ca", "a", "D"));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationX", "D"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new VarInsnNode(ALOAD, 2));
                    toInject.add(new InsnNode(ICONST_1));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "cf", "b", "(I)Lck;"));
                    toInject.add(new TypeInsnNode(CHECKCAST, "ca"));
                    toInject.add(new FieldInsnNode(GETFIELD, "ca", "a", "D"));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationY", "D"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new VarInsnNode(ALOAD, 2));
                    toInject.add(new InsnNode(ICONST_2));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "cf", "b", "(I)Lck;"));
                    toInject.add(new TypeInsnNode(CHECKCAST, "ca"));
                    toInject.add(new FieldInsnNode(GETFIELD, "ca", "a", "D"));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationZ", "D"));
                    LabelNode l5 = new LabelNode();
                    toInject.add(new JumpInsnNode(GOTO, l5));
                    toInject.add(l4);
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(DCONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationX", "D"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(DCONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationY", "D"));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(DCONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "accelerationZ", "D"));
                    toInject.add(l5);
                    toInject.add(new VarInsnNode(ALOAD, 1));
                    toInject.add(new LdcInsnNode("BPData"));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "bx", "b", "(Ljava/lang/String;)Z"));
                    LabelNode l7 = new LabelNode();
                    toInject.add(new JumpInsnNode(IFEQ, l7));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new VarInsnNode(ALOAD, 1));
                    toInject.add(new LdcInsnNode("BPData"));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "bx", "c", "(Ljava/lang/String;)B"));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "bpdata", "I"));
                    LabelNode l8 = new LabelNode();
                    toInject.add(new JumpInsnNode(GOTO, l8));
                    toInject.add(l7);
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new InsnNode(ICONST_0));
                    toInject.add(new FieldInsnNode(PUTFIELD, "sq", "bpdata", "I"));
                    toInject.add(l8);

                    m.instructions.insertBefore(m.instructions.get(index), toInject);

                    ok7 = true;
                    break;
                }
            }
        } else if (m.name.equals("au") && m.desc.equals("()Z")) {
            InsnList toInject = new InsnList();
            toInject.add(new VarInsnNode(ALOAD, 0));
            toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "sq", "ae", "()Z"));
            toInject.add(new InsnNode(IRETURN));

            m.instructions.clear();
            m.localVariables.clear();
            m.instructions.add(toInject);
            ok8 = true;
        }

    }

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

    FieldVisitor fv;

    fv = cw.visitField(ACC_PUBLIC, "accelerationX", "D", null, null);
    fv.visitEnd();

    fv = cw.visitField(ACC_PUBLIC, "accelerationY", "D", null, null);
    fv.visitEnd();

    fv = cw.visitField(ACC_PUBLIC, "accelerationZ", "D", null, null);
    fv.visitEnd();

    fv = cw.visitField(ACC_PUBLIC, "bpdata", "I", null, null);
    fv.visitEnd();

    fv = cw.visitField(ACC_PUBLIC, "slideDir", "B", null, null);
    fv.visitEnd();

    fv = cw.visitField(ACC_PUBLIC, "media", "I", null, null);
    fv.visitEnd();

    fv = cw.visitField(ACC_PUBLIC, "dead", "B", null, null);
    fv.visitEnd();

    MethodVisitor mv;

    mv = cw.visitMethod(ACC_PUBLIC, "D", "()Lasu;", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, "sq", "E", "Lasu;");
    mv.visitInsn(ARETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();

    mv = cw.visitMethod(ACC_PUBLIC, "al", "()V", null, null);
    mv.visitCode();
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 1);
    mv.visitEnd();

    mv = cw.visitMethod(ACC_PUBLIC, "d", "(DDD)V", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, "sq", "q", "Labv;");
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(DLOAD, 1);
    mv.visitVarInsn(DLOAD, 3);
    mv.visitVarInsn(DLOAD, 5);
    mv.visitMethodInsn(INVOKESTATIC, "blockphysics/BlockPhysics", "moveEntity", "(Labv;Lsq;DDD)V");
    mv.visitInsn(RETURN);
    mv.visitMaxs(8, 7);
    mv.visitEnd();

    if (!ok8) {
        mv = cw.visitMethod(ACC_PUBLIC, "au", "()Z", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKEVIRTUAL, "sq", "ae", "()Z");
        mv.visitInsn(IRETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();

        ok8 = true;
    }

    cw.visitEnd();

    if (ok && ok2 && ok3 && ok4 && ok5 && ok6 && ok7 && ok8 && ok9 && ok10)
        System.out.println("OK");
    else
        System.out.println("Failed." + ok + ok2 + ok3 + ok4 + ok5 + ok6 + ok7 + ok8 + ok9 + ok10);

    /*try
    {
       FileOutputStream fos = new FileOutputStream("d:/EntityFallingSand.class");
       fos.write(cw.toByteArray());
     fos.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }*/

    return cw.toByteArray();
}

From source file:blue.lapis.pore.event.PoreListenerGenerator.java

License:Open Source License

private static byte[] generate(String name, Class<?> pore, Class<?> sponge, EventPriority priority,
        Order order) {/* w w  w  . j  a va 2 s. c  o  m*/
    name = name.replace('.', '/');
    String poreName = Type.getInternalName(pore);
    String spongeSignature = "(L" + Type.getInternalName(sponge) + ";)V";

    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;
    AnnotationVisitor av;

    cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, name, null, "java/lang/Object", null);

    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "on" + sponge.getSimpleName(), spongeSignature, null, null);
        {
            av = mv.visitAnnotation(ANNOTATION_DESCRIPTOR, true);
            av.visitEnum("order", ORDER_DESCRIPTOR, order.name());
            av.visitEnd();
        }
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESTATIC, EVENT_WRAPPER_CLASS, "get", GET_CACHE, false);
        mv.visitTypeInsn(CHECKCAST, poreName);
        mv.visitVarInsn(ASTORE, 2);
        mv.visitVarInsn(ALOAD, 2);
        Label l0 = new Label();
        mv.visitJumpInsn(IFNONNULL, l0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitTypeInsn(NEW, poreName);
        mv.visitInsn(DUP);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESPECIAL, poreName, "<init>", spongeSignature, false);
        mv.visitInsn(DUP);
        mv.visitVarInsn(ASTORE, 2);
        mv.visitMethodInsn(INVOKESTATIC, EVENT_WRAPPER_CLASS, "set", SET_CACHE, false);
        mv.visitLabel(l0);
        mv.visitFrame(F_APPEND, 1, new Object[] { poreName }, 0, null);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitFieldInsn(GETSTATIC, PRIORITY_CLASS, priority.name(), PRIORITY_DESCRIPTOR);
        mv.visitMethodInsn(INVOKESTATIC, EVENT_WRAPPER_CLASS, "call", CALL_EVENT, false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(4, 3);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();
}

From source file:bluejelly.PrimTransformer.java

License:BSD License

private void generate(PrimInfo pi) {
    for (int i = 0; i < pi.arity; i++) {
        Label start = new Label();
        Label end = new Label();
        String method = getMethodName(pi, i);
        String cont = getContName(pi, i);
        MethodVisitor v = cv.visitMethod(ACC_PUBLIC, method, DESC, null, null);
        generateAnn(v, pi, i);//from w  w  w . ja va 2 s.  c  om
        v.visitCode();
        v.visitLabel(start);
        if (i == 0) {
            v.visitIntInsn(ALOAD, 1);
            pushIntConst(v, pi.arity);
            v.visitMethodInsn(INVOKEVIRTUAL, CTX, "stackCheck", "(I)V");
        }
        v.visitIntInsn(ALOAD, 1);
        pushIntConst(v, i << 1);
        v.visitLdcInsn(cont);
        v.visitMethodInsn(INVOKEVIRTUAL, CTX, "evalVar", "(ILjava/lang/String;)V");
        v.visitLabel(end);
        v.visitInsn(RETURN);
        v.visitLocalVariable("ctx", Type.getDescriptor(ExecutionContext.class), null, start, end, 1);
        v.visitMaxs(3, 2);
        v.visitEnd();
    }
}

From source file:bluejelly.runtime.ModuleLoader.java

License:BSD License

/**
 * Generate a {@link Caf} subclass for the given method information.
 * /*w  w  w.java2s .co  m*/
 * @param module    module declaring the method to process
 * @param ci        code info for the method to process
 * 
 * @return
 *   a {@link Caf} subclass whose <code>fastEnter</code> will call
 *   module.ci.getMethod() with an {@link ExecutionContext} as argument.
 */
@SuppressWarnings("unchecked")
private Class<Caf> generateCaf(Module module, CodeInfo ci) {
    String nodeName = this.genNodeName(ci);
    String cafName = Type.getInternalName(Caf.class);
    String moduleName = Type.getInternalName(module.getClass());
    String methodName = ci.getMethod().getName();

    ClassWriter cw = new ClassWriter(0);
    cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL, nodeName, null, cafName, null);

    {
        String consDesc = Type.getMethodDescriptor(Type.VOID_TYPE,
                new Type[] { Type.getType(Module.class), Type.getType(String.class) });

        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", consDesc, null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKESPECIAL, cafName, "<init>", consDesc);
        mv.visitInsn(RETURN);
        mv.visitMaxs(3, 3);
        mv.visitEnd();
    }

    this.generateFastEnter(cw, cafName, moduleName, methodName);

    cw.visitEnd();

    byte[] b = cw.toByteArray();
    Class<?> clazz = this.loader.defineClass(nodeName, b);
    return (Class<Caf>) clazz;
}

From source file:bluejelly.runtime.ModuleLoader.java

License:BSD License

/**
 * Generate a {@link Code} subclass for the given method information.
 * /* w w  w.j a  v  a  2  s  .co  m*/
 * @param module    module declaring the method to process
 * @param ci        code info for the method to process
 * 
 * @return          
 *   a {@link Code} subclass whose <code>fastEnter</code> will call
 *   module.ci.getMethod() with an {@link ExecutionContext} as argument.
 */
@SuppressWarnings("unchecked")
private Class<Code> generateCode(Module module, CodeInfo ci) {
    String nodeName = this.genNodeName(ci);
    String codeName = Type.getInternalName(Code.class);
    String moduleName = Type.getInternalName(module.getClass());
    String methodName = ci.getMethod().getName();

    ClassWriter cw = new ClassWriter(0);
    cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL, nodeName, null, codeName, null);

    {
        String consDesc = Type.getMethodDescriptor(Type.VOID_TYPE,
                new Type[] { Type.getType(int.class), Type.getType(Module.class), Type.getType(String.class) });

        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", consDesc, null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ILOAD, 1);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitVarInsn(ALOAD, 3);
        mv.visitMethodInsn(INVOKESPECIAL, codeName, "<init>", consDesc);
        mv.visitInsn(RETURN);
        mv.visitMaxs(4, 4);
        mv.visitEnd();
    }

    this.generateFastEnter(cw, codeName, moduleName, methodName);

    cw.visitEnd();

    byte[] b = cw.toByteArray();
    Class<?> clazz = this.loader.defineClass(nodeName, b);
    return (Class<Code>) clazz;
}

From source file:bluejelly.runtime.ModuleLoader.java

License:BSD License

/**
 * Override <code>fastEnter</code> method in class being visited
 * by <code>cw</code>, subclass of <code>superName</code>. Method
 * will call <code>methodName</code>. declared in class 
 * <code>moduleName</code>. So , assuming class generated by
 * <code>cw</code> is named <code>X</code>, we will have:
 * /* w ww. j ava 2  s  .  co m*/
 * <p>
 * <code>
 * class X extends superName {
 *     public void fastEnter(ExecutionContext ctx) {
 *         ((moduleName)this.module).methodName(ctx);
 *     }
 * }
 * </code>
 * 
 * @param cw            class writer generating class code
 * @param superName     internal name of superclass: Code or Caf
 * @param moduleName    internal name of module class
 * @param methodName    name of method to invoke in module moduleName
 */
private void generateFastEnter(ClassWriter cw, String superName, String moduleName, String methodName) {

    String fieldDesc = Type.getDescriptor(Module.class);
    String methodDesc = "(" + Type.getDescriptor(ExecutionContext.class) + ")V";

    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "fastEnter", methodDesc, null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, superName, "module", fieldDesc);
    mv.visitTypeInsn(CHECKCAST, moduleName);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, moduleName, methodName, methodDesc);
    mv.visitInsn(RETURN);
    mv.visitMaxs(2, 2);
    mv.visitEnd();
}

From source file:blusunrize.immersiveengineering.common.util.compat.jei.arcfurnace.ArcFurnaceRecipeWrapper.java

private static Class<? extends ArcFurnaceRecipeWrapper> createSubWrapper(String subtype) throws Exception {
    String entitySuperClassName = Type.getInternalName(ArcFurnaceRecipeWrapper.class);
    String entityProxySubClassName = ArcFurnaceRecipeWrapper.class.getSimpleName().concat(subtype);
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, entityProxySubClassName, null,
            entitySuperClassName, null);
    cw.visitSource(entityProxySubClassName.concat(".java"), null);
    //create constructor
    String methodDescriptor = "(L" + Type.getInternalName(ArcFurnaceRecipe.class) + ";)V";
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", methodDescriptor, null, null);
    mv.visitCode();//from   w ww  . java 2 s . c o  m
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, entitySuperClassName, "<init>", methodDescriptor, false);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    cw.visitEnd();
    return (Class<? extends ArcFurnaceRecipeWrapper>) new ProxyClassLoader(
            Thread.currentThread().getContextClassLoader(), cw.toByteArray())
                    .loadClass(entityProxySubClassName.replaceAll("/", "."));
}

From source file:boilerplate.processor.adapters.EqualsAdapter.java

License:Open Source License

private void addEqualsMethod(List<FieldInfo> fields) {
    // CODE: public boolean equals(Object obj) {
    MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "equals", "(Ljava/lang/Object;)Z", null, null);
    mv.visitCode();//from   w  w w .  j  a  v  a 2 s.  co  m

    // CODE: if (this == obj) return true;
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    Label l0 = new Label();
    mv.visitJumpInsn(IF_ACMPNE, l0);
    mv.visitInsn(ICONST_1);
    mv.visitInsn(IRETURN);
    mv.visitLabel(l0);
    mv.visitFrame(F_SAME, 0, null, 0, null);

    // CODE: if (!(obj instanceof Foo)) return false;
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(INSTANCEOF, typeInfo.type);
    Label l1 = new Label();
    mv.visitJumpInsn(IFNE, l1);
    mv.visitInsn(ICONST_0);
    mv.visitInsn(IRETURN);
    mv.visitLabel(l1);
    mv.visitFrame(F_SAME, 0, null, 0, null);

    // CODE: final Foo other = (Foo) obj;
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(CHECKCAST, typeInfo.type);
    mv.visitVarInsn(ASTORE, 2);

    for (FieldInfo info : fields) {
        addTest(mv, info);
    }

    // CODE: return true; }
    mv.visitInsn(ICONST_1);
    mv.visitInsn(IRETURN);
    mv.visitMaxs(1, 2);
    mv.visitEnd();
}

From source file:boilerplate.processor.adapters.EqualsAdapter.java

License:Open Source License

private void addHashCodeMethod(List<FieldInfo> fields) {
    // CODE: public int hashCode() {
    MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "hashCode", "()I", null, null);
    mv.visitCode();/*from w ww . j  a  v  a  2s  .  co  m*/

    // CODE: final int prime = 31;
    mv.visitIntInsn(BIPUSH, 31);
    mv.visitVarInsn(ISTORE, 1);
    mv.visitInsn(ICONST_1);

    // CODE: int result = 1;
    mv.visitVarInsn(ISTORE, 2);

    for (FieldInfo fieldInfo : fields) {
        addCalculation(mv, fieldInfo);
    }

    // CODE: return result; }
    mv.visitVarInsn(ILOAD, 2);
    mv.visitInsn(IRETURN);
    mv.visitMaxs(1, 3);
    mv.visitEnd();
}