Example usage for org.objectweb.asm.tree AbstractInsnNode LDC_INSN

List of usage examples for org.objectweb.asm.tree AbstractInsnNode LDC_INSN

Introduction

In this page you can find the example usage for org.objectweb.asm.tree AbstractInsnNode LDC_INSN.

Prototype

int LDC_INSN

To view the source code for org.objectweb.asm.tree AbstractInsnNode LDC_INSN.

Click Source Link

Document

The type of LdcInsnNode instructions.

Usage

From source file:blockphysics.asm.BPTransformer.java

License:Open Source License

private byte[] transformAnvilChunkLoader(byte[] bytes) {
    /*try/* www.ja v a  2  s.  c  om*/
     {
         FileOutputStream fos = new FileOutputStream("d:/AnvilChunkLoader.orig.class");
         fos.write(bytes);
       fos.close();
     } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     }*/

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

    boolean ok = false, ok2 = false, ok3 = false, ok4 = false, ok6 = false, ok7 = 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("a") && m.desc.equals("(Ladq;Labv;Lbx;)V")) {
            int var1 = 9;
            for (int index = 0; index < m.instructions.size(); index++) {
                if (ok3)
                    break;
                if (m.instructions.get(index).getOpcode() == INVOKESPECIAL
                        && m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN
                        && ((MethodInsnNode) m.instructions.get(index)).owner.equals("bx")
                        && ((MethodInsnNode) m.instructions.get(index)).name.equals("<init>")
                        && ((MethodInsnNode) m.instructions.get(index)).desc.equals("()V")) {
                    for (int index2 = index; index2 < m.instructions.size(); index2++) {
                        if (m.instructions.get(index2).getOpcode() == ASTORE) {
                            var1 = ((VarInsnNode) m.instructions.get(index2)).var;
                            ok3 = true;
                            break;
                        }
                    }
                }
            }

            int var2 = 11;
            for (int index = 0; index < m.instructions.size(); index++) {
                if (ok4)
                    break;
                if (m.instructions.get(index).getType() == AbstractInsnNode.LDC_INSN
                        && ((LdcInsnNode) m.instructions.get(index)).cst.equals("Data")) {
                    for (int index2 = index; index2 < m.instructions.size(); index2++) {
                        if (m.instructions.get(index2).getOpcode() == ALOAD) {
                            var2 = ((VarInsnNode) m.instructions.get(index2)).var;
                            ok4 = true;
                            break;
                        }
                    }
                }
            }

            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("cf")
                        && ((MethodInsnNode) m.instructions.get(index)).name.equals("a")
                        && ((MethodInsnNode) m.instructions.get(index)).desc.equals("(Lck;)V")) {
                    InsnList toInject = new InsnList();

                    toInject.add(new VarInsnNode(ALOAD, var1));
                    toInject.add(new LdcInsnNode("BPData"));
                    toInject.add(new VarInsnNode(ALOAD, var2));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "adr", "getBPdataArray", "()[B"));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "bx", "a", "(Ljava/lang/String;[B)V"));

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

                    ok = true;
                    break;
                }
            }
        } else if (m.name.equals("a") && m.desc.equals("(Labv;Lbx;)Ladq;")) {
            int var1 = 11;
            for (int index = 0; index < m.instructions.size(); index++) {

                if (ok6)
                    break;
                if (m.instructions.get(index).getOpcode() == INVOKEVIRTUAL
                        && m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN
                        && ((MethodInsnNode) m.instructions.get(index)).owner.equals("cf")
                        && ((MethodInsnNode) m.instructions.get(index)).name.equals("b")
                        && ((MethodInsnNode) m.instructions.get(index)).desc.equals("(I)Lck;")) {
                    for (int index2 = index; index2 < m.instructions.size(); index2++) {
                        if (m.instructions.get(index2).getOpcode() == ASTORE) {
                            var1 = ((VarInsnNode) m.instructions.get(index2)).var;
                            ok6 = true;
                            break;
                        }
                    }
                }
            }

            int var2 = 13;
            for (int index = 0; index < m.instructions.size(); index++) {
                if (ok7)
                    break;
                if (m.instructions.get(index).getOpcode() == INVOKESPECIAL
                        && m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN
                        && ((MethodInsnNode) m.instructions.get(index)).owner.equals("adr")
                        && ((MethodInsnNode) m.instructions.get(index)).name.equals("<init>")
                        && ((MethodInsnNode) m.instructions.get(index)).desc.equals("(IZ)V")) {
                    for (int index2 = index; index2 < m.instructions.size(); index2++) {
                        if (m.instructions.get(index2).getOpcode() == ASTORE) {
                            var2 = ((VarInsnNode) m.instructions.get(index2)).var;
                            ok7 = true;
                            break;
                        }
                    }
                }
            }

            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("adr")
                        && ((MethodInsnNode) m.instructions.get(index)).name.equals("e")
                        && ((MethodInsnNode) m.instructions.get(index)).desc.equals("()V")) {
                    InsnList toInject = new InsnList();

                    toInject.add(new VarInsnNode(ALOAD, var1));
                    toInject.add(new LdcInsnNode("BPData"));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "bx", "b", "(Ljava/lang/String;)Z"));
                    LabelNode l6 = new LabelNode();
                    toInject.add(new JumpInsnNode(IFEQ, l6));
                    toInject.add(new VarInsnNode(ALOAD, var2));
                    toInject.add(new VarInsnNode(ALOAD, var1));
                    toInject.add(new LdcInsnNode("BPData"));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "bx", "j", "(Ljava/lang/String;)[B"));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "adr", "setBPdataArray", "([B)V"));
                    LabelNode l7 = new LabelNode();
                    toInject.add(new JumpInsnNode(GOTO, l7));
                    toInject.add(l6);
                    toInject.add(new FrameNode(Opcodes.F_SAME, 0, null, 0, null));
                    toInject.add(new VarInsnNode(ALOAD, var2));
                    toInject.add(new IntInsnNode(SIPUSH, 4096));
                    toInject.add(new IntInsnNode(NEWARRAY, T_BYTE));
                    toInject.add(new MethodInsnNode(INVOKEVIRTUAL, "adr", "setBPdataArray", "([B)V"));
                    toInject.add(l7);

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

                    ok2 = true;
                    break;
                }
            }
        }
    }

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

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

    /*try
    {
       FileOutputStream fos = new FileOutputStream("d:/AnvilChunkLoader.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[] transformEntity(byte[] bytes) {
    /*try/*from   w w  w . j  a  va2s  .com*/
    {
        FileOutputStream fos = new FileOutputStream("d:/Entity.orig.class");
        fos.write(bytes);
      fos.close();
    } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }*/

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

    boolean ok = 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("C") && m.desc.equals("()V")) {
            for (int index = 0; index < m.instructions.size() - 1; index++) {
                if (m.instructions.get(index).getType() == AbstractInsnNode.LDC_INSN
                        && ((LdcInsnNode) m.instructions.get(index)).cst.equals(0.001D)) {
                    ok = true;
                    m.instructions.set(m.instructions.get(index), new LdcInsnNode(new Double("0.07")));
                }
            }
        }
    }

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

    if (ok)
        System.out.println("OK");
    else
        System.out.println("Failed." + ok);

    /*try
    {
       FileOutputStream fos = new FileOutputStream("d:/Entity.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//from   w  ww  .j  ava  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:cl.inria.stiq.instrumenter.BCIUtils.java

License:Open Source License

private static void printFrames(MethodNode aNode, Frame[] aFrames) {
    int bcIndex = 1;

    for (int i = 0; i < aFrames.length; i++) {
        Frame theFrame = aFrames[i];
        AbstractInsnNode theInsn = aNode.instructions.get(i);

        switch (theInsn.getType()) {
        case AbstractInsnNode.INSN:
        case AbstractInsnNode.INT_INSN:
        case AbstractInsnNode.VAR_INSN:
        case AbstractInsnNode.TYPE_INSN:
        case AbstractInsnNode.FIELD_INSN:
        case AbstractInsnNode.METHOD_INSN:
        case AbstractInsnNode.JUMP_INSN:
        case AbstractInsnNode.LDC_INSN:
        case AbstractInsnNode.IINC_INSN:
        case AbstractInsnNode.TABLESWITCH_INSN:
        case AbstractInsnNode.LOOKUPSWITCH_INSN:
        case AbstractInsnNode.MULTIANEWARRAY_INSN:
            TraceMethodVisitor theTraceVisitor = new TraceMethodVisitor();
            theInsn.accept(theTraceVisitor);
            StringWriter theWriter = new StringWriter();
            theTraceVisitor.print(new PrintWriter(theWriter));
            String theTraced = theWriter.toString().replace("\n", "");
            System.out.println(bcIndex + "\t" + frameString(theFrame) + " |\t" + theTraced);
            bcIndex++;//ww w .  j a  v a2s.  c o  m
            break;

        case AbstractInsnNode.FRAME:
        case AbstractInsnNode.LINE:
        case AbstractInsnNode.LABEL:
            break;
        }
    }
}

From source file:com.android.tools.lint.checks.ApiDetector.java

License:Apache License

@SuppressWarnings("rawtypes") // ASM API
@Override//  w ww .  j a v a 2 s  .  com
public void checkClass(@NonNull final ClassContext context, @NonNull ClassNode classNode) {
    if (mApiDatabase == null) {
        return;
    }

    if (AOSP_BUILD && classNode.name.startsWith("android/support/")) { //$NON-NLS-1$
        return;
    }

    // Requires util package (add prebuilts/tools/common/asm-tools/asm-debug-all-4.0.jar)
    //classNode.accept(new TraceClassVisitor(new PrintWriter(System.out)));

    int classMinSdk = getClassMinSdk(context, classNode);
    if (classMinSdk == -1) {
        classMinSdk = getMinSdk(context);
    }

    List methodList = classNode.methods;
    if (methodList.isEmpty()) {
        return;
    }

    boolean checkCalls = context.isEnabled(UNSUPPORTED) || context.isEnabled(INLINED);
    boolean checkMethods = context.isEnabled(OVERRIDE) && context.getMainProject().getBuildSdk() >= 1;
    String frameworkParent = null;
    if (checkMethods) {
        LintDriver driver = context.getDriver();
        String owner = classNode.superName;
        while (owner != null) {
            // For virtual dispatch, walk up the inheritance chain checking
            // each inherited method
            if ((owner.startsWith("android/") //$NON-NLS-1$
                    && !owner.startsWith("android/support/")) //$NON-NLS-1$
                    || owner.startsWith("java/") //$NON-NLS-1$
                    || owner.startsWith("javax/")) { //$NON-NLS-1$
                frameworkParent = owner;
                break;
            }
            owner = driver.getSuperClass(owner);
        }
        if (frameworkParent == null) {
            checkMethods = false;
        }
    }

    if (checkCalls) { // Check implements/extends
        if (classNode.superName != null) {
            String signature = classNode.superName;
            checkExtendsClass(context, classNode, classMinSdk, signature);
        }
        if (classNode.interfaces != null) {
            @SuppressWarnings("unchecked") // ASM API
            List<String> interfaceList = classNode.interfaces;
            for (String signature : interfaceList) {
                checkExtendsClass(context, classNode, classMinSdk, signature);
            }
        }
    }

    for (Object m : methodList) {
        MethodNode method = (MethodNode) m;

        int minSdk = getLocalMinSdk(method.invisibleAnnotations);
        if (minSdk == -1) {
            minSdk = classMinSdk;
        }

        InsnList nodes = method.instructions;

        if (checkMethods && Character.isJavaIdentifierStart(method.name.charAt(0))) {
            int buildSdk = context.getMainProject().getBuildSdk();
            String name = method.name;
            assert frameworkParent != null;
            int api = mApiDatabase.getCallVersion(frameworkParent, name, method.desc);
            if (api > buildSdk && buildSdk != -1) {
                // TODO: Don't complain if it's annotated with @Override; that means
                // somehow the build target isn't correct.
                String fqcn;
                String owner = classNode.name;
                if (CONSTRUCTOR_NAME.equals(name)) {
                    fqcn = "new " + ClassContext.getFqcn(owner); //$NON-NLS-1$
                } else {
                    fqcn = ClassContext.getFqcn(owner) + '#' + name;
                }
                String message = String.format(
                        "This method is not overriding anything with the current build "
                                + "target, but will in API level %1$d (current target is %2$d): %3$s",
                        api, buildSdk, fqcn);

                Location location = context.getLocation(method, classNode);
                context.report(OVERRIDE, method, null, location, message, null);
            }
        }

        if (!checkCalls) {
            continue;
        }

        if (CHECK_DECLARATIONS) {
            // Check types in parameter list and types of local variables
            List localVariables = method.localVariables;
            if (localVariables != null) {
                for (Object v : localVariables) {
                    LocalVariableNode var = (LocalVariableNode) v;
                    String desc = var.desc;
                    if (desc.charAt(0) == 'L') {
                        // "Lpackage/Class;" => "package/Bar"
                        String className = desc.substring(1, desc.length() - 1);
                        int api = mApiDatabase.getClassVersion(className);
                        if (api > minSdk) {
                            String fqcn = ClassContext.getFqcn(className);
                            String message = String.format(
                                    "Class requires API level %1$d (current min is %2$d): %3$s", api, minSdk,
                                    fqcn);
                            report(context, message, var.start, method,
                                    className.substring(className.lastIndexOf('/') + 1), null,
                                    SearchHints.create(NEAREST).matchJavaSymbol());
                        }
                    }
                }
            }

            // Check return type
            // The parameter types are already handled as local variables so we can skip
            // right to the return type.
            // Check types in parameter list
            String signature = method.desc;
            if (signature != null) {
                int args = signature.indexOf(')');
                if (args != -1 && signature.charAt(args + 1) == 'L') {
                    String type = signature.substring(args + 2, signature.length() - 1);
                    int api = mApiDatabase.getClassVersion(type);
                    if (api > minSdk) {
                        String fqcn = ClassContext.getFqcn(type);
                        String message = String.format(
                                "Class requires API level %1$d (current min is %2$d): %3$s", api, minSdk, fqcn);
                        AbstractInsnNode first = nodes.size() > 0 ? nodes.get(0) : null;
                        report(context, message, first, method, method.name, null,
                                SearchHints.create(BACKWARD).matchJavaSymbol());
                    }
                }
            }
        }

        for (int i = 0, n = nodes.size(); i < n; i++) {
            AbstractInsnNode instruction = nodes.get(i);
            int type = instruction.getType();
            if (type == AbstractInsnNode.METHOD_INSN) {
                MethodInsnNode node = (MethodInsnNode) instruction;
                String name = node.name;
                String owner = node.owner;
                String desc = node.desc;

                // No need to check methods in this local class; we know they
                // won't be an API match
                if (node.getOpcode() == Opcodes.INVOKEVIRTUAL && owner.equals(classNode.name)) {
                    owner = classNode.superName;
                }

                boolean checkingSuperClass = false;
                while (owner != null) {
                    int api = mApiDatabase.getCallVersion(owner, name, desc);
                    if (api > minSdk) {
                        if (method.name.startsWith(SWITCH_TABLE_PREFIX)) {
                            // We're in a compiler-generated method to generate an
                            // array indexed by enum ordinal values to enum values. The enum
                            // itself must be requiring a higher API number than is
                            // currently used, but the call site for the switch statement
                            // will also be referencing it, so no need to report these
                            // calls.
                            break;
                        }

                        if (!checkingSuperClass && node.getOpcode() == Opcodes.INVOKEVIRTUAL
                                && methodDefinedLocally(classNode, name, desc)) {
                            break;
                        }

                        String fqcn;
                        if (CONSTRUCTOR_NAME.equals(name)) {
                            fqcn = "new " + ClassContext.getFqcn(owner); //$NON-NLS-1$
                        } else {
                            fqcn = ClassContext.getFqcn(owner) + '#' + name;
                        }
                        String message = String.format(
                                "Call requires API level %1$d (current min is %2$d): %3$s", api, minSdk, fqcn);

                        if (name.equals(ORDINAL_METHOD) && instruction.getNext() != null
                                && instruction.getNext().getNext() != null
                                && instruction.getNext().getOpcode() == Opcodes.IALOAD
                                && instruction.getNext().getNext().getOpcode() == Opcodes.TABLESWITCH) {
                            message = String.format(
                                    "Enum for switch requires API level %1$d " + "(current min is %2$d): %3$s",
                                    api, minSdk, ClassContext.getFqcn(owner));
                        }

                        report(context, message, node, method, name, null,
                                SearchHints.create(FORWARD).matchJavaSymbol());
                    }

                    // For virtual dispatch, walk up the inheritance chain checking
                    // each inherited method
                    if (owner.startsWith("android/") //$NON-NLS-1$
                            || owner.startsWith("javax/")) { //$NON-NLS-1$
                        // The API map has already inlined all inherited methods
                        // so no need to keep checking up the chain
                        // -- unless it's the support library which is also in
                        // the android/ namespace:
                        if (owner.startsWith("android/support/")) { //$NON-NLS-1$
                            owner = context.getDriver().getSuperClass(owner);
                        } else {
                            owner = null;
                        }
                    } else if (owner.startsWith("java/")) { //$NON-NLS-1$
                        if (owner.equals(LocaleDetector.DATE_FORMAT_OWNER)) {
                            checkSimpleDateFormat(context, method, node, minSdk);
                        }
                        // Already inlined; see comment above
                        owner = null;
                    } else if (node.getOpcode() == Opcodes.INVOKEVIRTUAL) {
                        owner = context.getDriver().getSuperClass(owner);
                    } else if (node.getOpcode() == Opcodes.INVOKESTATIC && api == -1) {
                        // Inherit through static classes as well
                        owner = context.getDriver().getSuperClass(owner);
                    } else {
                        owner = null;
                    }

                    checkingSuperClass = true;
                }
            } else if (type == AbstractInsnNode.FIELD_INSN) {
                FieldInsnNode node = (FieldInsnNode) instruction;
                String name = node.name;
                String owner = node.owner;
                int api = mApiDatabase.getFieldVersion(owner, name);
                if (api > minSdk) {
                    if (method.name.startsWith(SWITCH_TABLE_PREFIX)) {
                        checkSwitchBlock(context, classNode, node, method, name, owner, api, minSdk);
                        continue;
                    }
                    String fqcn = ClassContext.getFqcn(owner) + '#' + name;
                    if (mPendingFields != null) {
                        mPendingFields.remove(fqcn);
                    }
                    String message = String.format("Field requires API level %1$d (current min is %2$d): %3$s",
                            api, minSdk, fqcn);
                    report(context, message, node, method, name, null,
                            SearchHints.create(FORWARD).matchJavaSymbol());
                }
            } else if (type == AbstractInsnNode.LDC_INSN) {
                LdcInsnNode node = (LdcInsnNode) instruction;
                if (node.cst instanceof Type) {
                    Type t = (Type) node.cst;
                    String className = t.getInternalName();

                    int api = mApiDatabase.getClassVersion(className);
                    if (api > minSdk) {
                        String fqcn = ClassContext.getFqcn(className);
                        String message = String.format(
                                "Class requires API level %1$d (current min is %2$d): %3$s", api, minSdk, fqcn);
                        report(context, message, node, method,
                                className.substring(className.lastIndexOf('/') + 1), null,
                                SearchHints.create(FORWARD).matchJavaSymbol());
                    }
                }
            }
        }
    }
}

From source file:com.android.tools.lint.checks.InvalidPackageDetector.java

License:Apache License

@SuppressWarnings("rawtypes") // ASM API
@Override//  w w w  . j a va2s . co  m
public void checkClass(@NonNull final ClassContext context, @NonNull ClassNode classNode) {
    if (!context.isFromClassLibrary() || shouldSkip(context.file)) {
        return;
    }

    if (mApiDatabase == null) {
        return;
    }

    if (classNode.name.startsWith(JAVAX_PKG_PREFIX)) {
        mJavaxLibraryClasses.add(classNode.name);
    }

    List methodList = classNode.methods;
    for (Object m : methodList) {
        MethodNode method = (MethodNode) m;

        InsnList nodes = method.instructions;

        // Check return type
        // The parameter types are already handled as local variables so we can skip
        // right to the return type.
        // Check types in parameter list
        String signature = method.desc;
        if (signature != null) {
            int args = signature.indexOf(')');
            if (args != -1 && signature.charAt(args + 1) == 'L') {
                String type = signature.substring(args + 2, signature.length() - 1);
                if (isInvalidPackage(type)) {
                    AbstractInsnNode first = nodes.size() > 0 ? nodes.get(0) : null;
                    record(context, method, first, type);
                }
            }
        }

        for (int i = 0, n = nodes.size(); i < n; i++) {
            AbstractInsnNode instruction = nodes.get(i);
            int type = instruction.getType();
            if (type == AbstractInsnNode.METHOD_INSN) {
                MethodInsnNode node = (MethodInsnNode) instruction;
                String owner = node.owner;

                // No need to check methods in this local class; we know they
                // won't be an API match
                if (node.getOpcode() == Opcodes.INVOKEVIRTUAL && owner.equals(classNode.name)) {
                    owner = classNode.superName;
                }

                while (owner != null) {
                    if (isInvalidPackage(owner)) {
                        record(context, method, instruction, owner);
                    }

                    // For virtual dispatch, walk up the inheritance chain checking
                    // each inherited method
                    if (owner.startsWith("android/") //$NON-NLS-1$
                            || owner.startsWith(JAVA_PKG_PREFIX) || owner.startsWith(JAVAX_PKG_PREFIX)) {
                        owner = null;
                    } else if (node.getOpcode() == Opcodes.INVOKEVIRTUAL) {
                        owner = context.getDriver().getSuperClass(owner);
                    } else if (node.getOpcode() == Opcodes.INVOKESTATIC) {
                        // Inherit through static classes as well
                        owner = context.getDriver().getSuperClass(owner);
                    } else {
                        owner = null;
                    }
                }
            } else if (type == AbstractInsnNode.FIELD_INSN) {
                FieldInsnNode node = (FieldInsnNode) instruction;
                String owner = node.owner;
                if (isInvalidPackage(owner)) {
                    record(context, method, instruction, owner);
                }
            } else if (type == AbstractInsnNode.LDC_INSN) {
                LdcInsnNode node = (LdcInsnNode) instruction;
                if (node.cst instanceof Type) {
                    Type t = (Type) node.cst;
                    String className = t.getInternalName();
                    if (isInvalidPackage(className)) {
                        record(context, method, instruction, className);
                    }
                }
            }
        }
    }
}

From source file:cz.vutbr.fit.xhriba01.bc.lib.AbstractNodeVisitor.java

License:Open Source License

/**
 * This method detects what type of instruction the NodeInstruction is
 * representing and calls appropriate before/visit/after methods.
 * Subclasses could rather override methods specific for particular
 * instruction type./*from ww w .j ava 2  s  .c o m*/
 * 
 * @param nodeInstruction the instruction node
 */
public void visitNodeInstruction(NodeInstruction nodeInstruction) {
    AbstractInsnNode asmInsn = nodeInstruction.getAsmInsnNode();
    switch (asmInsn.getType()) {
    case AbstractInsnNode.LINE:
        visitLineNumberNode((LineNumberNode) asmInsn, nodeInstruction);
        break;
    case AbstractInsnNode.LABEL:
        visitLabelNode((LabelNode) asmInsn, nodeInstruction);
        break;
    case AbstractInsnNode.FIELD_INSN:
        visitFieldInsn((FieldInsnNode) asmInsn, nodeInstruction);
        break;
    case AbstractInsnNode.IINC_INSN:
        visitIincInsn((IincInsnNode) asmInsn, nodeInstruction);
        break;
    case AbstractInsnNode.INSN:
        visitInsn((InsnNode) asmInsn, nodeInstruction);
        break;
    case AbstractInsnNode.INT_INSN:
        visitIntInsn((IntInsnNode) asmInsn, nodeInstruction);
        break;
    case AbstractInsnNode.INVOKE_DYNAMIC_INSN:
        visitInvokeDynamicInsn((InvokeDynamicInsnNode) asmInsn, nodeInstruction);
        break;
    case AbstractInsnNode.JUMP_INSN:
        visitJumpInsn((JumpInsnNode) asmInsn, nodeInstruction);
        break;
    case AbstractInsnNode.LDC_INSN:
        visitLdcInsn((LdcInsnNode) asmInsn, nodeInstruction);
        break;
    case AbstractInsnNode.LOOKUPSWITCH_INSN:
        visitLookupSwitchInsn((LookupSwitchInsnNode) asmInsn, nodeInstruction);
        break;
    case AbstractInsnNode.METHOD_INSN:
        visitMethodInsn((MethodInsnNode) asmInsn, nodeInstruction);
        break;
    case AbstractInsnNode.MULTIANEWARRAY_INSN:
        visitMultiANewArrayInsn((MultiANewArrayInsnNode) asmInsn, nodeInstruction);
        break;
    case AbstractInsnNode.TABLESWITCH_INSN:
        visitTableSwitchInsn((TableSwitchInsnNode) asmInsn, nodeInstruction);
        break;
    case AbstractInsnNode.TYPE_INSN:
        visitTypeInsn((TypeInsnNode) asmInsn, nodeInstruction);
        break;
    case AbstractInsnNode.VAR_INSN:
        visitVarInsn((VarInsnNode) asmInsn, nodeInstruction);
        break;
    default:
        // shouldn't happen
        break;
    }
}

From source file:cz.vutbr.fit.xhriba01.bc.lib.AbstractNodeVisitor.java

License:Open Source License

public void afterVisitNodeInstruction(NodeInstruction nodeInstruction) {

    AbstractInsnNode asmInsn = nodeInstruction.getAsmInsnNode();

    switch (asmInsn.getType()) {
    case AbstractInsnNode.LINE:
        afterVisitLineNumberNode((LineNumberNode) asmInsn, nodeInstruction);
        break;/* w w w. j av a 2s  . c o m*/
    case AbstractInsnNode.LABEL:
        afterVisitLabelNode((LabelNode) asmInsn, nodeInstruction);
        break;
    case AbstractInsnNode.FIELD_INSN:
        afterVisitFieldInsn((FieldInsnNode) asmInsn, nodeInstruction);
        break;
    case AbstractInsnNode.IINC_INSN:
        afterVisitIincInsn((IincInsnNode) asmInsn, nodeInstruction);
        break;
    case AbstractInsnNode.INSN:
        afterVisitInsn((InsnNode) asmInsn, nodeInstruction);
        break;
    case AbstractInsnNode.INT_INSN:
        afterVisitIntInsn((IntInsnNode) asmInsn, nodeInstruction);
        break;
    case AbstractInsnNode.INVOKE_DYNAMIC_INSN:
        afterVisitInvokeDynamicInsn((InvokeDynamicInsnNode) asmInsn, nodeInstruction);
        break;
    case AbstractInsnNode.JUMP_INSN:
        afterVisitJumpInsn((JumpInsnNode) asmInsn, nodeInstruction);
        break;
    case AbstractInsnNode.LDC_INSN:
        afterVisitLdcInsn((LdcInsnNode) asmInsn, nodeInstruction);
        break;
    case AbstractInsnNode.LOOKUPSWITCH_INSN:
        afterVisitLookupSwitchInsn((LookupSwitchInsnNode) asmInsn, nodeInstruction);
        break;
    case AbstractInsnNode.METHOD_INSN:
        afterVisitMethodInsn((MethodInsnNode) asmInsn, nodeInstruction);
        break;
    case AbstractInsnNode.MULTIANEWARRAY_INSN:
        afterVisitMultiANewArrayInsn((MultiANewArrayInsnNode) asmInsn, nodeInstruction);
        break;
    case AbstractInsnNode.TABLESWITCH_INSN:
        afterVisitTableSwitchInsn((TableSwitchInsnNode) asmInsn, nodeInstruction);
        break;
    case AbstractInsnNode.TYPE_INSN:
        afterVisitTypeInsn((TypeInsnNode) asmInsn, nodeInstruction);
        break;
    case AbstractInsnNode.VAR_INSN:
        afterVisitVarInsn((VarInsnNode) asmInsn, nodeInstruction);
        break;
    default:
        // shouldn't happen
        break;
    }

}

From source file:de.codesourcery.asm.util.Disassembler.java

License:Apache License

private static String disassemble(AbstractInsnNode node, MethodNode method) {
    final int opCode = node.getOpcode();
    String mnemonic = Printer.OPCODES[opCode];

    switch (node.getType()) {
    case AbstractInsnNode.FIELD_INSN: // GETSTATIC, PUTSTATIC, GETFIELD , PUTFIELD
        FieldInsnNode tmp = (FieldInsnNode) node;
        mnemonic += " " + (tmp.owner + "#" + tmp.name);
        break;//from   w w w  . j a va 2s . c  o  m
    case AbstractInsnNode.IINC_INSN: // IINC
        IincInsnNode tmp2 = (IincInsnNode) node;
        mnemonic += " " + (tmp2.var + " , " + tmp2.incr);
        break;
    case AbstractInsnNode.INSN: // regular opcodes
        break;
    case AbstractInsnNode.INT_INSN: // BIPUSH, SIPUSH or NEWARRAY
        IntInsnNode tmp3 = (IntInsnNode) node;
        mnemonic += " " + (tmp3.operand);
        break;
    case AbstractInsnNode.INVOKE_DYNAMIC_INSN: // INVOKEDYNAMIC
        break;
    case AbstractInsnNode.JUMP_INSN: // IFEQ, IFNE, IFLT, IFGE, IFGT, IFLE, IF_ICMPEQ,IF_ICMPNE, IF_ICMPLT, IF_ICMPGE, IF_ICMPGT, IF_ICMPLE,IF_ACMPEQ, IF_ACMPNE, GOTO, JSR, IFNULL or IFNONNULL
        JumpInsnNode tmp4 = (JumpInsnNode) node;
        int index = method.instructions.indexOf(tmp4.label);
        while (method.instructions.get(index).getOpcode() == -1) {
            index++;
        }
        mnemonic += " " + index;
        break;
    case AbstractInsnNode.LDC_INSN: // load constant
        LdcInsnNode tmp5 = (LdcInsnNode) node;
        Class<?> clazz = tmp5.cst.getClass();
        if (clazz == String.class) {
            mnemonic += " \"" + tmp5.cst + "\"";
        } else if (clazz == org.objectweb.asm.Type.class) {
            org.objectweb.asm.Type type = (org.objectweb.asm.Type) tmp5.cst;
            mnemonic += " (a " + type.getClassName() + ")";
        } else {
            mnemonic += " " + tmp5.cst + " (" + tmp5.cst.getClass().getName() + ")";
        }
        break;
    case AbstractInsnNode.LOOKUPSWITCH_INSN: // LOOKUPSWITCH
        break;
    case AbstractInsnNode.METHOD_INSN: // INVOKEVIRTUAL, INVOKESPECIAL, INVOKESTATIC , INVOKEINTERFACE
        MethodInsnNode tmp6 = (MethodInsnNode) node;
        mnemonic += " " + (tmp6.owner + "#" + tmp6.name + "()");
        break;
    case AbstractInsnNode.MULTIANEWARRAY_INSN: // MULTIANEWARRAY
        break;
    case AbstractInsnNode.TABLESWITCH_INSN: // TABLESWITCH
        break;
    case AbstractInsnNode.TYPE_INSN: // NEW, ANEWARRAY, CHECKCAST , INSTANCEOF
        TypeInsnNode tmp8 = (TypeInsnNode) node;
        mnemonic += " " + tmp8.desc;
        break;
    case AbstractInsnNode.VAR_INSN: // ILOAD, LLOAD, FLOAD, DLOAD, ALOAD, ISTORE, LSTORE, FSTORE, DSTORE, ASTORE , RET
        VarInsnNode tmp7 = (VarInsnNode) node;
        mnemonic += "_" + tmp7.var;
        break;
    // -- VIRTUAL --
    case AbstractInsnNode.FRAME: /* VIRTUAL */
    case AbstractInsnNode.LABEL: /* VIRTUAL */
    case AbstractInsnNode.LINE: /* VIRTUAL */
    default:
        throw new RuntimeException("Internal error, unhandled node type: " + node);
    }
    return mnemonic;
}

From source file:de.unisb.cs.st.javaslicer.tracer.instrumentation.TracingMethodInstrumenter.java

License:Open Source License

@SuppressWarnings("unchecked")
public void transform(final ListIterator<MethodNode> methodIt) {

    // do not modify abstract or native methods
    if ((this.methodNode.access & ACC_ABSTRACT) != 0 || (this.methodNode.access & ACC_NATIVE) != 0)
        return;/*from  w  ww.j  av a2 s.  c  o  m*/

    // check out what labels are jump targets (only these have to be traced)
    analyze(this.methodNode);

    this.instructionIterator = new FixedInstructionIterator(this.methodNode.instructions);
    // in the old method, initialize the new local variable for the threadtracer
    this.instructionIterator.add(new MethodInsnNode(INVOKESTATIC, Type.getInternalName(Tracer.class),
            "getInstance", "()L" + Type.getInternalName(Tracer.class) + ";", false));
    this.instructionIterator.add(new MethodInsnNode(INVOKEVIRTUAL, Type.getInternalName(Tracer.class),
            "getThreadTracer", "()L" + Type.getInternalName(ThreadTracer.class) + ";", false));
    this.instructionIterator.add(new InsnNode(DUP));
    this.instructionIterator.add(new VarInsnNode(ASTORE, this.tracerLocalVarIndex));

    this.instructionIterator.add(new MethodInsnNode(INVOKEINTERFACE, Type.getInternalName(ThreadTracer.class),
            "isPaused", "()Z", true));
    final LabelNode noTracingLabel = new LabelNode();
    this.instructionIterator.add(new JumpInsnNode(IFNE, noTracingLabel));
    // create a copy of the (uninstrumented) instructions (later, while iterating through the instructions)
    final InsnList oldInstructions = new InsnList();
    final Map<LabelNode, LabelNode> labelCopies = new LazyLabelMap();
    // copy the try-catch-blocks
    final Object[] oldTryCatchblockNodes = this.methodNode.tryCatchBlocks.toArray();
    for (final Object o : oldTryCatchblockNodes) {
        final TryCatchBlockNode tcb = (TryCatchBlockNode) o;
        final TryCatchBlockNode newTcb = new TryCatchBlockNode(labelCopies.get(tcb.start),
                labelCopies.get(tcb.end), labelCopies.get(tcb.handler), tcb.type);
        this.methodNode.tryCatchBlocks.add(newTcb);
    }

    // increment number of local variables by one (for the threadtracer)
    ++this.methodNode.maxLocals;

    // and increment all local variable indexes after the new one by one
    for (final Object o : this.methodNode.localVariables) {
        final LocalVariableNode localVar = (LocalVariableNode) o;
        if (localVar.index >= this.tracerLocalVarIndex)
            ++localVar.index;
    }

    // store information about local variables in the ReadMethod object
    List<LocalVariable> localVariables = new ArrayList<LocalVariable>();
    for (final Object o : this.methodNode.localVariables) {
        final LocalVariableNode localVar = (LocalVariableNode) o;
        while (localVariables.size() <= localVar.index)
            localVariables.add(null);
        localVariables.set(localVar.index, new LocalVariable(localVar.index, localVar.name, localVar.desc));
    }
    this.readMethod.setLocalVariables(localVariables.toArray(new LocalVariable[localVariables.size()]));
    localVariables = null;

    // each method must start with a (dedicated) label:
    assert this.readMethod.getInstructions().isEmpty();
    traceLabel(null, InstructionType.METHODENTRY);
    assert this.readMethod.getInstructions().size() == 1
            && this.readMethod.getInstructions().get(0) instanceof LabelMarker
            && ((LabelMarker) this.readMethod.getInstructions().get(0)).isAdditionalLabel();
    this.readMethod.setMethodEntryLabel((LabelMarker) this.readMethod.getInstructions().get(0));

    // needed later:
    final LabelNode l0 = new LabelNode();
    this.instructionIterator.add(l0);

    // then, visit the instructions that were in the method before
    while (this.instructionIterator.hasNext()) {

        final AbstractInsnNode insnNode = this.instructionIterator.next();
        switch (insnNode.getType()) {
        case AbstractInsnNode.INSN:
            transformInsn((InsnNode) insnNode);
            break;
        case AbstractInsnNode.INT_INSN:
            transformIntInsn((IntInsnNode) insnNode);
            break;
        case AbstractInsnNode.VAR_INSN:
            transformVarInsn((VarInsnNode) insnNode);
            break;
        case AbstractInsnNode.TYPE_INSN:
            transformTypeInsn((TypeInsnNode) insnNode);
            break;
        case AbstractInsnNode.FIELD_INSN:
            transformFieldInsn((FieldInsnNode) insnNode);
            break;
        case AbstractInsnNode.METHOD_INSN:
            transformMethodInsn((MethodInsnNode) insnNode);
            break;
        case AbstractInsnNode.JUMP_INSN:
            transformJumpInsn((JumpInsnNode) insnNode);
            break;
        case AbstractInsnNode.LABEL:
            transformLabel((LabelNode) insnNode);
            break;
        case AbstractInsnNode.LDC_INSN:
            transformLdcInsn((LdcInsnNode) insnNode);
            break;
        case AbstractInsnNode.IINC_INSN:
            transformIincInsn((IincInsnNode) insnNode);
            break;
        case AbstractInsnNode.TABLESWITCH_INSN:
            transformTableSwitchInsn((TableSwitchInsnNode) insnNode);
            break;
        case AbstractInsnNode.LOOKUPSWITCH_INSN:
            transformLookupSwitchInsn((LookupSwitchInsnNode) insnNode);
            break;
        case AbstractInsnNode.MULTIANEWARRAY_INSN:
            transformMultiANewArrayInsn((MultiANewArrayInsnNode) insnNode);
            break;
        case AbstractInsnNode.FRAME:
            // ignore
            break;
        case AbstractInsnNode.LINE:
            // ignore
            break;
        default:
            throw new RuntimeException("Unknown instruction type " + insnNode.getType() + " ("
                    + insnNode.getClass().getSimpleName() + ")");
        }
        oldInstructions.add(insnNode.clone(labelCopies));
    }

    assert this.outstandingInitializations == 0;

    // add the (old) try-catch blocks to the readMethods
    // (can only be done down here since we use the information in the
    // labels map)
    for (final Object o : oldTryCatchblockNodes) {
        final TryCatchBlockNode tcb = (TryCatchBlockNode) o;
        this.readMethod.addTryCatchBlock(new TryCatchBlock(this.labels.get(tcb.start), this.labels.get(tcb.end),
                this.labels.get(tcb.handler), tcb.type));
    }

    final LabelNode l1 = new LabelNode();
    this.instructionIterator.add(l1);
    final int newPos = this.readMethod.getInstructions().size();
    traceLabel(null, InstructionType.METHODEXIT);
    assert this.readMethod.getInstructions().size() == newPos + 1;
    final AbstractInstruction abnormalTerminationLabel = this.readMethod.getInstructions().get(newPos);
    assert abnormalTerminationLabel instanceof LabelMarker;
    this.readMethod.setAbnormalTerminationLabel((LabelMarker) abnormalTerminationLabel);
    this.methodNode.instructions.add(new InsnNode(ATHROW));

    // add a try catch block around the method so that we can trace when this method is left
    // by a thrown exception
    this.methodNode.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l1, null));

    // now add the code that is executed if no tracing should be performed
    this.methodNode.instructions.add(noTracingLabel);
    if (this.firstLine != -1)
        this.methodNode.instructions.add(new LineNumberNode(this.firstLine, noTracingLabel));
    this.methodNode.instructions.add(new InsnNode(ACONST_NULL));
    this.methodNode.instructions.add(new VarInsnNode(ASTORE, this.tracerLocalVarIndex));
    this.methodNode.instructions.add(oldInstructions);

    // finally: create a copy of the method that gets the ThreadTracer as argument
    // this is only necessary for private methods or "<init>"
    if (this.tracer.wasRedefined(this.readMethod.getReadClass().getName())
            && (this.methodNode.access & ACC_PRIVATE) != 0) {
        final Type[] oldMethodArguments = Type.getArgumentTypes(this.methodNode.desc);
        final Type[] newMethodArguments = Arrays.copyOf(oldMethodArguments, oldMethodArguments.length + 1);
        newMethodArguments[oldMethodArguments.length] = Type.getType(ThreadTracer.class);
        final String newMethodDesc = Type.getMethodDescriptor(Type.getReturnType(this.methodNode.desc),
                newMethodArguments);
        final MethodNode newMethod = new MethodNode(this.methodNode.access, this.methodNode.name, newMethodDesc,
                this.methodNode.signature,
                (String[]) this.methodNode.exceptions.toArray(new String[this.methodNode.exceptions.size()]));
        methodIt.add(newMethod);

        int threadTracerParamPos = ((this.readMethod.getAccess() & Opcodes.ACC_STATIC) == 0 ? 1 : 0);
        for (final Type t : oldMethodArguments)
            threadTracerParamPos += t.getSize();

        final Map<LabelNode, LabelNode> newMethodLabels = new LazyLabelMap();

        // copy the local variables information to the new method
        for (final Object o : this.methodNode.localVariables) {
            final LocalVariableNode lv = (LocalVariableNode) o;
            newMethod.localVariables.add(new LocalVariableNode(lv.name, lv.desc, lv.signature,
                    newMethodLabels.get(lv.start), newMethodLabels.get(lv.end), lv.index));
        }

        newMethod.maxLocals = this.methodNode.maxLocals;
        newMethod.maxStack = this.methodNode.maxStack;

        // copy the try-catch-blocks
        for (final Object o : this.methodNode.tryCatchBlocks) {
            final TryCatchBlockNode tcb = (TryCatchBlockNode) o;
            newMethod.tryCatchBlocks.add(new TryCatchBlockNode(newMethodLabels.get(tcb.start),
                    newMethodLabels.get(tcb.end), newMethodLabels.get(tcb.handler), tcb.type));
        }

        // skip the first 6 instructions, replace them with these:
        newMethod.instructions.add(new VarInsnNode(ALOAD, threadTracerParamPos));
        newMethod.instructions.add(new InsnNode(DUP));
        newMethod.instructions.add(new VarInsnNode(ASTORE, this.tracerLocalVarIndex));
        newMethod.instructions.add(new JumpInsnNode(IFNULL, newMethodLabels.get(noTracingLabel)));
        final Iterator<AbstractInsnNode> oldInsnIt = this.methodNode.instructions.iterator(6);
        // and add all the other instructions
        while (oldInsnIt.hasNext()) {
            final AbstractInsnNode insn = oldInsnIt.next();
            newMethod.instructions.add(insn.clone(newMethodLabels));
        }
    }

    ready();
}