Example usage for org.objectweb.asm MethodVisitor visitMethodInsn

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

Introduction

In this page you can find the example usage for org.objectweb.asm MethodVisitor 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:ataspectj.UnweavableTest.java

License:Open Source License

ISome getJit() {
    ClassWriter cw = new ClassWriter(true, true);
    cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "ataspectj/ISomeGen", null, "java/lang/Object",
            new String[] { "ataspectj/UnweavableTest$ISome" });
    AnnotationVisitor av = cw.visitAnnotation("Lataspectj/UnweavableTest$ASome;", true);
    av.visitEnd();/*from ww  w.j av  a 2 s. c  o  m*/

    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, new String[0]);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, 0);
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "giveOne", "()I", null, new String[0]);
    mv.visitInsn(Opcodes.ICONST_2);
    mv.visitInsn(Opcodes.IRETURN);
    mv.visitMaxs(0, 0);
    cw.visitEnd();

    try {
        ClassLoader loader = this.getClass().getClassLoader();
        Method def = ClassLoader.class.getDeclaredMethod("defineClass",
                new Class[] { String.class, byte[].class, int.class, int.class });
        def.setAccessible(true);
        Class gen = (Class) def.invoke(loader, "ataspectj.ISomeGen", cw.toByteArray(), 0,
                cw.toByteArray().length);
        return (ISome) gen.newInstance();
    } catch (Throwable t) {
        fail(t.toString());
        return null;
    }
}

From source file:ataspectj.UnweavableTest.java

License:Open Source License

Serializable getJitNoMatch() {
    ClassWriter cw = new ClassWriter(true, true);
    cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "ataspectj/unmatched/Gen", null, "java/lang/Object",
            new String[] { "java/io/Serializable" });

    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, new String[0]);
    mv.visitVarInsn(Opcodes.ALOAD, 0);//from   w ww.j a  v a  2  s .c  o m
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, 0);
    cw.visitEnd();

    try {
        ClassLoader loader = this.getClass().getClassLoader();
        Method def = ClassLoader.class.getDeclaredMethod("defineClass",
                new Class[] { String.class, byte[].class, int.class, int.class });
        def.setAccessible(true);
        Class gen = (Class) def.invoke(loader, "ataspectj.unmatched.Gen", cw.toByteArray(), 0,
                cw.toByteArray().length);
        return (Serializable) gen.newInstance();
    } catch (Throwable t) {
        fail(t.toString());
        return null;
    }
}

From source file:blockphysics.asm.BPTransformer.java

License:Open Source License

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

    System.out.print("[BlockPhysics] Patching Chunk.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, "getBlockBPdata", "(III)I", null, null);
    mv.visitCode();
    mv.visitVarInsn(ILOAD, 2);
    mv.visitInsn(ICONST_4);
    mv.visitInsn(ISHR);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, "adq", "r", "[Ladr;");
    mv.visitInsn(ARRAYLENGTH);
    Label l0 = new Label();
    mv.visitJumpInsn(IF_ICMPLT, l0);
    mv.visitInsn(ICONST_0);
    mv.visitInsn(IRETURN);
    mv.visitLabel(l0);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, "adq", "r", "[Ladr;");
    mv.visitVarInsn(ILOAD, 2);
    mv.visitInsn(ICONST_4);
    mv.visitInsn(ISHR);
    mv.visitInsn(AALOAD);
    mv.visitVarInsn(ASTORE, 4);
    mv.visitVarInsn(ALOAD, 4);
    Label l1 = new Label();
    mv.visitJumpInsn(IFNULL, l1);
    mv.visitVarInsn(ALOAD, 4);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitVarInsn(ILOAD, 2);
    mv.visitIntInsn(BIPUSH, 15);
    mv.visitInsn(IAND);
    mv.visitVarInsn(ILOAD, 3);
    mv.visitMethodInsn(INVOKEVIRTUAL, "adr", "getBlockBPdata", "(III)I");
    Label l2 = new Label();
    mv.visitJumpInsn(GOTO, l2);
    mv.visitLabel(l1);
    mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { "adr" }, 0, null);
    mv.visitInsn(ICONST_0);
    mv.visitLabel(l2);
    mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { Opcodes.INTEGER });
    mv.visitInsn(IRETURN);
    mv.visitMaxs(4, 5);
    mv.visitEnd();

    mv = cw.visitMethod(ACC_PUBLIC, "setBlockBPdata", "(IIII)Z", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, "adq", "r", "[Ladr;");
    mv.visitVarInsn(ILOAD, 2);
    mv.visitInsn(ICONST_4);
    mv.visitInsn(ISHR);
    mv.visitInsn(AALOAD);
    mv.visitVarInsn(ASTORE, 5);
    mv.visitVarInsn(ALOAD, 5);
    Label lab0 = new Label();
    mv.visitJumpInsn(IFNONNULL, lab0);
    mv.visitInsn(ICONST_0);
    mv.visitInsn(IRETURN);
    mv.visitLabel(lab0);
    mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { "adr" }, 0, null);
    mv.visitVarInsn(ALOAD, 5);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitVarInsn(ILOAD, 2);
    mv.visitIntInsn(BIPUSH, 15);
    mv.visitInsn(IAND);
    mv.visitVarInsn(ILOAD, 3);
    mv.visitMethodInsn(INVOKEVIRTUAL, "adr", "getBlockBPdata", "(III)I");
    mv.visitVarInsn(ISTORE, 6);
    mv.visitVarInsn(ILOAD, 6);
    mv.visitVarInsn(ILOAD, 4);
    Label lab1 = new Label();
    mv.visitJumpInsn(IF_ICMPNE, lab1);
    mv.visitInsn(ICONST_0);
    mv.visitInsn(IRETURN);
    mv.visitLabel(lab1);
    mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { Opcodes.INTEGER }, 0, null);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitInsn(ICONST_1);
    mv.visitFieldInsn(PUTFIELD, "adq", "l", "Z");
    mv.visitVarInsn(ALOAD, 5);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitVarInsn(ILOAD, 2);
    mv.visitIntInsn(BIPUSH, 15);
    mv.visitInsn(IAND);
    mv.visitVarInsn(ILOAD, 3);
    mv.visitVarInsn(ILOAD, 4);
    mv.visitMethodInsn(INVOKEVIRTUAL, "adr", "setBlockBPdata", "(IIII)V");
    mv.visitInsn(ICONST_1);
    mv.visitInsn(IRETURN);
    mv.visitMaxs(5, 7);
    mv.visitEnd();

    cw.visitEnd();

    System.out.println("OK");

    /*try
    {
       FileOutputStream fos = new FileOutputStream("d:/Chunk.mod.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/*  w w  w.j a  v a  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: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   ww w.j a va 2s .  c  o m
        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.
 * /*  ww  w . jav  a  2 s  .c  om*/
 * @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.
 * /*www .  j a  v a2s  .c  o  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:
 * //from w w w  .ja v a2  s .  c o 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:boilerplate.processor.adapters.ToStringAdapter.java

License:Open Source License

private void addMethod() {
    List<String> fieldNames = new ArrayList<String>(typeInfo.fields.size());
    Map<String, FieldInfo> fields = new HashMap<String, FieldInfo>(typeInfo.fields.size());
    boolean hasAnnotatedFields = typeInfo.hasAnnotatedFields();
    for (FieldInfo info : typeInfo.fields) {
        if (!(hasAnnotatedFields ^ info.mark == Mark.INCLUDE)) {
            fieldNames.add(info.name);/*ww  w  .  j a  va2s . c o  m*/
            fields.put(info.name, info);
        }
    }
    Collections.sort(fieldNames);

    MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null);
    mv.visitCode();
    mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V");
    mv.visitVarInsn(ASTORE, 1);
    for (String name : fieldNames) {
        FieldInfo info = fields.get(name);
        addField(mv, info);
    }
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;");
    mv.visitInsn(ARETURN);
    mv.visitMaxs(4, 2);
    mv.visitEnd();
}

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

License:Open Source License

public void generateCode(MethodVisitor mv, TypeInfo typeInfo, FieldInfo fieldInfo) {
    mv.visitVarInsn(ALOAD, 1);/*from   w  w  w .ja  va2 s.  co m*/
    mv.visitLdcInsn(fieldInfo.name);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, typeInfo.type, fieldInfo.name, fieldInfo.type.getDescriptor());
    mv.visitMethodInsn(INVOKESTATIC, "boilerplate/tostring/ToStringUtils", methodName, methodSignature);
}