Package org.objectweb.asm.tree

Examples of org.objectweb.asm.tree.InsnList


  public RemoveAddZeroTransformer(MethodTransformer mt) {
    super(mt);
  }

  public void transform(MethodNode mn) {
    InsnList insns = mn.instructions;
    Iterator i = insns.iterator();
    while (i.hasNext()) {
      AbstractInsnNode i1 = (AbstractInsnNode) i.next();
      if (i1.getOpcode() == ICONST_0) {
        AbstractInsnNode i2 = getNext(i);
        if (i2 != null && i2.getOpcode() == IADD) {
          insns.remove(i1);
          insns.remove(i2);
        }
      }
    }
    super.transform(mn);
  }
View Full Code Here


  public RemoveGetFieldPutFieldTransformer2(MethodTransformer mt) {
    super(mt);
  }

  public void transform(MethodNode mn) {
    InsnList insns = mn.instructions;
    Iterator i = insns.iterator();
    while (i.hasNext()) {
      AbstractInsnNode i1 = (AbstractInsnNode) i.next();
      if (isALOAD0(i1)) {
        AbstractInsnNode i2 = getNext(i);
        if (i2 != null && isALOAD0(i2)) {
          AbstractInsnNode i3 = getNext(i);
          while (i3 != null && isALOAD0(i3)) {
            i1 = i2;
            i2 = i3;
            i3 = getNext(i);
          }
          if (i3 != null && i3.getOpcode() == GETFIELD) {
            AbstractInsnNode i4 = getNext(i);
            if (i4 != null && i4.getOpcode() == PUTFIELD) {
              if (sameField(i3, i4)) {
                insns.remove(i1);
                insns.remove(i2);
                insns.remove(i3);
                insns.remove(i4);
              }
            }
          }
        }
      }
View Full Code Here

public class CyclomaticComplexityTest extends AbstractTestCase {

  public void test() throws AnalyzerException {
    MethodNode mn = new MethodNode(ACC_PUBLIC, "checkAndSetF", "(I)V",
        null, null);
    InsnList il = mn.instructions;
    il.add(new VarInsnNode(ILOAD, 1));
    LabelNode label = new LabelNode();
    il.add(new JumpInsnNode(IFLT, label));
    il.add(new VarInsnNode(ALOAD, 0));
    il.add(new VarInsnNode(ILOAD, 1));
    il.add(new FieldInsnNode(PUTFIELD, "pkg/Bean", "f", "I"));
    LabelNode end = new LabelNode();
    il.add(new JumpInsnNode(GOTO, end));
    il.add(label);
    il.add(new FrameNode(F_SAME, 0, null, 0, null));
    il.add(new TypeInsnNode(NEW, "java/lang/IllegalArgumentException"));
    il.add(new InsnNode(DUP));
    il.add(new MethodInsnNode(INVOKESPECIAL,
        "java/lang/IllegalArgumentException", "<init>", "()V"));
    il.add(new InsnNode(ATHROW));
    il.add(end);
    il.add(new FrameNode(F_SAME, 0, null, 0, null));
    il.add(new InsnNode(RETURN));
    mn.maxStack = 2;
    mn.maxLocals = 2;
    CyclomaticComplexity cc = new CyclomaticComplexity();
    assert (cc.getCyclomaticComplexity("pkg/Bean", mn) == 1);
  }
View Full Code Here

    if ((cn.access & ACC_INTERFACE) == 0) {
      for (MethodNode mn : (List<MethodNode>) cn.methods) {
        if ("<init>".equals(mn.name) || "<clinit>".equals(mn.name)) {
          continue;
        }
        InsnList insns = mn.instructions;
        if (insns.size() == 0) {
          continue;
        }
        Iterator j = insns.iterator();
        while (j.hasNext()) {
          AbstractInsnNode in = (AbstractInsnNode) j.next();
          int op = in.getOpcode();
          if ((op >= IRETURN && op <= RETURN) || op == ATHROW) {
            InsnList il = new InsnList();
            il.add(new FieldInsnNode(GETSTATIC, cn.name, "timer", "J"));
            il.add(new MethodInsnNode(INVOKESTATIC, "java/lang/System",
                "currentTimeMillis", "()J"));
            il.add(new InsnNode(LADD));
            il.add(new FieldInsnNode(PUTSTATIC, cn.name, "timer", "J"));
            if (in.getPrevious() == null) {
              continue;
            }
            insns.insert(in.getPrevious(), il);
          }
        }
        InsnList il = new InsnList();
        il.add(new FieldInsnNode(GETSTATIC, cn.name, "timer", "J"));
        il.add(new MethodInsnNode(INVOKESTATIC, "java/lang/System",
            "currentTimeMillis", "()J"));
        il.add(new InsnNode(LSUB));
        il.add(new FieldInsnNode(PUTSTATIC, cn.name, "timer", "J"));
        insns.insert(il);

        mn.maxStack += 4;
      }
      int acc = ACC_PUBLIC + ACC_STATIC;
View Full Code Here

  public OptimizeJumpTransformer(MethodTransformer mt) {
    super(mt);
  }

  public void transform(MethodNode mn) {
    InsnList insns = mn.instructions;
    Iterator i = insns.iterator();
    while (i.hasNext()) {
      AbstractInsnNode in = (AbstractInsnNode) i.next();
      if (in instanceof JumpInsnNode) {
        LabelNode label = ((JumpInsnNode) in).label;
        AbstractInsnNode target;
        // while target == goto l, replace label with l
        while (true) {
          target = label;
          while (target != null && target.getOpcode() < 0) {
            target = target.getNext();
          }
          if (target != null && target.getOpcode() == GOTO) {
            label = ((JumpInsnNode) target).label;
          } else {
            break;
          }
        }
        // update target
        ((JumpInsnNode) in).label = label;
        // if possible, replace jump with target instruction
        if (in.getOpcode() == GOTO && target != null) {
          int op = target.getOpcode();
          if ((op >= IRETURN && op <= RETURN) || op == ATHROW) {
            // replace 'in' with clone of 'target'
            insns.set(in, target.clone(null));
          }
        }
      }
    }
    super.transform(mn);
View Full Code Here

  public RemoveLoadStoreTransformer(MethodTransformer mt) {
    super(mt);
  }

  public void transform(MethodNode mn) {
    InsnList insns = mn.instructions;
    Iterator i = insns.iterator();
    while (i.hasNext()) {
      AbstractInsnNode i1 = (AbstractInsnNode) i.next();
      if (i1 instanceof VarInsnNode) {
        VarInsnNode v1 = (VarInsnNode) i1;
        int op1 = v1.getOpcode();
        if (op1 >= ILOAD && op1 <= ALOAD) {
          AbstractInsnNode i2 = getNext(i);
          if (i2 instanceof VarInsnNode) {
            VarInsnNode v2 = (VarInsnNode) i2;
            int op2 = v2.getOpcode();
            if (op2 - ISTORE == op1 - ILOAD && v2.var == v1.var) {
              insns.remove(i1);
              insns.remove(i2);
            }
          }
        }
      }
    }
View Full Code Here

  public RemoveGetFieldPutFieldTransformer(MethodTransformer mt) {
    super(mt);
  }

  public void transform(MethodNode mn) {
    InsnList insns = mn.instructions;
    Iterator i = insns.iterator();
    while (i.hasNext()) {
      AbstractInsnNode i1 = (AbstractInsnNode) i.next();
      if (isALOAD0(i1)) {
        AbstractInsnNode i2 = getNext(i1);
        if (i2 != null && isALOAD0(i2)) {
          AbstractInsnNode i3 = getNext(i2);
          if (i3 != null && i3.getOpcode() == GETFIELD) {
            AbstractInsnNode i4 = getNext(i3);
            if (i4 != null && i4.getOpcode() == PUTFIELD) {
              if (sameField(i3, i4)) {
                while (i.next() != i4) {
                }
                insns.remove(i1);
                insns.remove(i2);
                insns.remove(i3);
                insns.remove(i4);
              }
            }
          }
        }
      }
View Full Code Here

    cn.fields.add(new FieldNode(ACC_PRIVATE, "f", "I", null, null));
    {
      MethodNode mn = new MethodNode(ACC_PUBLIC, "<init>", "()V", null,
          null);
      cn.methods.add(mn);
      InsnList il = mn.instructions;
      il.add(new VarInsnNode(ALOAD, 0));
      il.add(new MethodInsnNode(INVOKESPECIAL, "java/lang/Object",
          "<init>", "()V"));
      il.add(new InsnNode(RETURN));
      mn.maxStack = 1;
      mn.maxLocals = 1;
    }
    {
      MethodNode mn = new MethodNode(ACC_PUBLIC, "getF", "()I", null,
          null);
      cn.methods.add(mn);
      InsnList il = mn.instructions;
      il.add(new VarInsnNode(ALOAD, 0));
      il.add(new FieldInsnNode(GETFIELD, "pkg/Bean", "f", "I"));
      il.add(new InsnNode(IRETURN));
      mn.maxStack = 1;
      mn.maxLocals = 1;
    }
    {
      MethodNode mn = new MethodNode(ACC_PUBLIC, "setF", "(I)V", null,
          null);
      cn.methods.add(mn);
      InsnList il = mn.instructions;
      il.add(new VarInsnNode(ALOAD, 0));
      il.add(new VarInsnNode(ILOAD, 1));
      il.add(new FieldInsnNode(PUTFIELD, "pkg/Bean", "f", "I"));
      il.add(new InsnNode(RETURN));
      mn.maxStack = 2;
      mn.maxLocals = 2;
    }
    MethodNode mn = new MethodNode(ACC_PUBLIC, "checkAndSetF", "(I)V",
        null, null);
    cn.methods.add(mn);
    InsnList il = mn.instructions;
    il.add(new VarInsnNode(ILOAD, 1));
    LabelNode label = new LabelNode();
    il.add(new JumpInsnNode(IFLT, label));
    il.add(new VarInsnNode(ALOAD, 0));
    il.add(new VarInsnNode(ILOAD, 1));
    il.add(new FieldInsnNode(PUTFIELD, "pkg/Bean", "f", "I"));
    LabelNode end = new LabelNode();
    il.add(new JumpInsnNode(GOTO, end));
    il.add(label);
    il.add(new FrameNode(F_SAME, 0, null, 0, null));
    il.add(new TypeInsnNode(NEW, "java/lang/IllegalArgumentException"));
    il.add(new InsnNode(DUP));
    il.add(new MethodInsnNode(INVOKESPECIAL,
        "java/lang/IllegalArgumentException", "<init>", "()V"));
    il.add(new InsnNode(ATHROW));
    il.add(end);
    il.add(new FrameNode(F_SAME, 0, null, 0, null));
    il.add(new InsnNode(RETURN));
    mn.maxStack = 2;
    mn.maxLocals = 2;
    ClassWriter cw = new ClassWriter(0);
    cn.accept(cw);
    return cw.toByteArray();
View Full Code Here

      }
      return ml;
    }
 
    InsnList buildInstructions(IList iList) {
      InsnList il = new InsnList();
      for (IValue v : iList) {
        if (((IConstructor)v).getName().equals("fieldRef")) {
          il.add(new FieldInsnNode(((IInteger)((IConstructor)v).get(0)).intValue(),
                       ((IString)((IConstructor)v).get(1)).getValue(),
                       ((IString)((IConstructor)v).get(2)).getValue(),
                       ((IString)((IConstructor)v).get(3)).getValue()));
        } else if (((IConstructor)v).getName().equals("increment")) {
          il.add(new IincInsnNode(((IInteger)((IConstructor)v).get(0)).intValue(),
                      ((IInteger)((IConstructor)v).get(1)).intValue()));
        } else if (((IConstructor)v).getName().equals("instruction")) {
          il.add(new InsnNode(((IInteger)((IConstructor)v).get(0)).intValue()));
        } else if (((IConstructor)v).getName().equals("integer")) {
          il.add(new IntInsnNode(((IInteger)((IConstructor)v).get(0)).intValue(),
                       ((IInteger)((IConstructor)v).get(1)).intValue()));
        } else if (((IConstructor)v).getName().equals("jump")) {
          il.add(new JumpInsnNode(((IInteger)((IConstructor)v).get(0)).intValue(),
                      getLabel(((IInteger)((IConstructor)v).get(1)).intValue())));
        } else if (((IConstructor)v).getName().equals("label")) {
          il.add(getLabel(((IInteger)((IConstructor)v).get(0)).intValue()));
        } else if (((IConstructor)v).getName().equals("lineNumber")) {
          il.add(new LineNumberNode(((IInteger)((IConstructor)v).get(0)).intValue(),
                        getLabel(((IInteger)((IConstructor)v).get(1)).intValue())));
        } else if (((IConstructor)v).getName().equals("localVariable")) {
          il.add(new VarInsnNode(((IInteger)((IConstructor)v).get(0)).intValue(),
                       ((IInteger)((IConstructor)v).get(1)).intValue()));
        } else if (((IConstructor)v).getName().equals("loadConstantString")) {
          il.add(new LdcInsnNode(((IString)((IConstructor)v).get(0)).getValue()));
        } else if (((IConstructor)v).getName().equals("loadConstantInteger")) {
          il.add(new LdcInsnNode(((IInteger)((IConstructor)v).get(0)).intValue()));
        } else if (((IConstructor)v).getName().equals("loadConstantLong")) {
          il.add(new LdcInsnNode(((IInteger)((IConstructor)v).get(0)).longValue()));
        } else if (((IConstructor)v).getName().equals("loadConstantFloat")) {
          il.add(new LdcInsnNode(((IReal)((IConstructor)v).get(0)).floatValue()));
        } else if (((IConstructor)v).getName().equals("loadConstantDouble")) {
          il.add(new LdcInsnNode(((IReal)((IConstructor)v).get(0)).doubleValue()));
        } else if (((IConstructor)v).getName().equals("lookupSwitch")) {
          IList kl = (IList)((IConstructor)v).get(1);
          int ka[] = new int[kl.length()];
          for (int i = 0; i < kl.length(); i++) {
            ka[i] = ((IInteger)kl.get(i)).intValue();
          }
          IList ll = (IList)((IConstructor)v).get(2);
          LabelNode la[] = new LabelNode[ll.length()];
          for (int i = 0; i < ll.length(); i++) {
            la[i] = getLabel(((IInteger)ll.get(i)).intValue());
          }
          il.add(new LookupSwitchInsnNode(getLabel(((IInteger)((IConstructor)v).get(0)).intValue()),
                          ka,
                          la));
        } else if (((IConstructor)v).getName().equals("method")) {
          il.add(new MethodInsnNode(((IInteger)((IConstructor)v).get(0)).intValue(),
                        ((IString)((IConstructor)v).get(1)).getValue(),
                        ((IString)((IConstructor)v).get(2)).getValue(),
                        ((IString)((IConstructor)v).get(3)).getValue()));
        } else if (((IConstructor)v).getName().equals("multiANewArray")) {
          il.add(new MultiANewArrayInsnNode(((IString)((IConstructor)v).get(0)).getValue(),
                            ((IInteger)((IConstructor)v).get(1)).intValue()));
        } else if (((IConstructor)v).getName().equals("tableSwitch")) {
          IList ll = (IList)((IConstructor)v).get(3);
          LabelNode la[] = new LabelNode[ll.length()];
          for (int i = 0; i < ll.length(); i++) {
            la[i] = getLabel(((IInteger)ll.get(i)).intValue());
          }
          il.add(new TableSwitchInsnNode(((IInteger)((IConstructor)v).get(0)).intValue(),
                           ((IInteger)((IConstructor)v).get(1)).intValue(),
                           getLabel(((IInteger)((IConstructor)v).get(2)).intValue()),
                           la));
        } else if (((IConstructor)v).getName().equals("type")) {
          il.add(new TypeInsnNode(((IInteger)((IConstructor)v).get(0)).intValue(),
                          ((IString)((IConstructor)v).get(1)).getValue()));
        }
      }
      return il;
    }
View Full Code Here

    fieldDesc = f.desc;
  }
 
  @Override
  public boolean transform(MethodNode mn) {
    InsnList instructions = mn.instructions;
    String owner = meta.type.getInternalName();
   
    if (LOG) System.out.println("OWNER: " + owner + " " + mn.name);
   
    ByteBufferHelper bufferHelper = new ByteBufferHelper(meta);
   
    boolean shouldDoSetter = true;
    for (int i = 0; instructions.size() > i; i++) {
      AbstractInsnNode node = instructions.get(i);
      switch(node.getType()) {
        case AbstractInsnNode.FIELD_INSN:
          FieldInsnNode f = (FieldInsnNode)node;
          if (shouldDoSetter && isSettingFieldWithPrimitive(f)) {
            if (LOG) System.out.println(">> SETTING FIELD index=" + i);
View Full Code Here

TOP

Related Classes of org.objectweb.asm.tree.InsnList

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.