Package org.objectweb.asm.tree

Examples of org.objectweb.asm.tree.LabelNode


  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));
View Full Code Here


    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) {
View Full Code Here

    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));
View Full Code Here

          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(),
View Full Code Here

      return al;
    }
 
    LabelNode getLabel(int intValue) {
      if (!_labels.containsKey(intValue)) {
        _labels.put(intValue, new LabelNode());
      }
      return _labels.get(intValue);
    }
View Full Code Here

        writer.write("\n\t\t\t\tinteger(" + n.getOpcode() + ", " + n.operand + ")");
      } else if (ai instanceof JumpInsnNode) {
        JumpInsnNode n = ((JumpInsnNode)ai);
        writer.write("\n\t\t\t\tjump(" + n.getOpcode() + ", " + getLabelIndex(n.label) + ")");
      } else if (ai instanceof LabelNode) {
        LabelNode n = ((LabelNode)ai);
        writer.write("\n\t\t\t\tlabel(" + getLabelIndex(n) + ")");
      } else if (ai instanceof LineNumberNode) {
        LineNumberNode n = ((LineNumberNode)ai);
        writer.write("\n\t\t\t\tlineNumber(" + n.line + ", " + getLabelIndex(n.start) + ")");
      } else if (ai instanceof VarInsnNode) {
        VarInsnNode n = ((VarInsnNode)ai);
        writer.write("\n\t\t\t\tlocalVariable(" + n.getOpcode() + ", " + n.var + ")");
      } else if (ai instanceof LdcInsnNode) {
        LdcInsnNode n = ((LdcInsnNode)ai);
        if (n.cst instanceof String) {
          writer.write("\n\t\t\t\tloadConstantString(\"" + escape((String)n.cst) + "\")");
        } else if (n.cst instanceof Integer) {
          writer.write("\n\t\t\t\tloadConstantInteger(" + n.cst + ")");
        } else if (n.cst instanceof Long) {
          writer.write("\n\t\t\t\tloadConstantLong(" + n.cst + ")");
        } else if (n.cst instanceof Float) {
          writer.write("\n\t\t\t\tloadConstantFloat(" + n.cst + ")");
        } else if (n.cst instanceof Double) {
          writer.write("\n\t\t\t\tloadConstantDouble(" + n.cst + ")");
        }
      } else if (ai instanceof LookupSwitchInsnNode) {
        LookupSwitchInsnNode n = ((LookupSwitchInsnNode)ai);
        writer.write("\n\t\t\t\tlookupSwitch(" + getLabelIndex(n.dflt) + ", [");
        boolean firstKey = true;
        for (Integer k : (List<Integer>)n.keys) {
          if (firstKey) { firstKey = false; } else { writer.write(", "); }
          writer.write(k);
        }
        writer.write("], [");
        boolean firstCase = true;
        for (LabelNode l : (List<LabelNode>)n.labels) {
          if (firstCase) { firstCase = false; } else { writer.write(", "); }
          writer.write("" + getLabelIndex(l));
        }
        writer.write("])");
      } else if (ai instanceof MethodInsnNode) {
        MethodInsnNode n = ((MethodInsnNode)ai);
        writer.write("\n\t\t\t\tmethod(" + n.getOpcode() + ", \"" + escape(n.owner) + "\", \"" + escape(n.name) + "\", \"" + escape(n.desc) + "\")");
      } else if (ai instanceof MultiANewArrayInsnNode) {
        MultiANewArrayInsnNode n = ((MultiANewArrayInsnNode)ai);
        writer.write("\n\t\t\t\tmultiANewArray(\"" + escape(n.desc) + "\", " + n.dims + ")");
      } else if (ai instanceof TableSwitchInsnNode) {
        TableSwitchInsnNode n = ((TableSwitchInsnNode)ai);
        writer.write("\n\t\t\t\ttableSwitch(" + n.min + ", " + n.max + ", " + getLabelIndex(n.dflt) + ", [");
        boolean firstCase = true;
        for (LabelNode l : (List<LabelNode>)n.labels) {
          if (firstCase) { firstCase = false; } else { writer.write(", "); }
          writer.write("" + getLabelIndex(l));
        }
        writer.write("])");
      } else if (ai instanceof TypeInsnNode) {
        TypeInsnNode n = ((TypeInsnNode)ai);
        writer.write("\n\t\t\t\t\\type(" + n.getOpcode() + ", \"" + escape(n.desc) + "\")");
      } else {
        if (!(ai instanceof FrameNode)) {
          throw new RuntimeException("Error: Unsupported instruction encountered (" + ai.getClass() + ").");
        }
        first = true;
View Full Code Here

        // now visit the instructions intercepting labels
        AbstractInsnNode insn = getFirst();
        while (insn != null) {
            insn.accept(mv);
            if (insn.getType() == AbstractInsnNode.LABEL) {
                LabelNode labelNode = (LabelNode) insn;
                Label label = labelNode.getLabel();
                List<LocalVariableNode> localStart = localStarts.get(label);
                List<LocalVariableNode> localEnd = localEnds.get(label);
                if (localStart != null) {
                    for (LocalVariableNode local : localStart) {
                        lsmv.visitLocalScopeStart(local.name, local.desc, local.signature, local.index, label.getOffset());
View Full Code Here

          logger.info("skipping node");
          continue;
        }
        labels.add(catchNode.handler.getLabel());
       
        LabelNode labelNode = catchNode.handler;
        AbstractInsnNode lineNumberNode = labelNode.getNext() instanceof LineNumberNode ? labelNode.getNext() : labelNode;
        FrameNode frameNode = (FrameNode) lineNumberNode.getNext();
        VarInsnNode varInsnNode = (VarInsnNode) frameNode.getNext();
        AbstractInsnNode insertPoint = varInsnNode;
       
        if (catchNode.type == null) {
          // this is probably a finally block;
          if (insertPoint.getNext() != null && insertPoint.getNext() instanceof LabelNode) {
            insertPoint = insertPoint.getNext();
          }
        }
       
        LabelNode labelNode4ifeg = new LabelNode();
        InsnList newCode = new InsnList();
        newCode.add(new VarInsnNode(Opcodes.ALOAD, varInsnNode.var));
        newCode.add(new TypeInsnNode(Opcodes.INSTANCEOF, INTERRUPT_EXCEPTION_NAME));
        newCode.add(new JumpInsnNode(Opcodes.IFEQ, labelNode4ifeg));
        newCode.add(new VarInsnNode(Opcodes.ALOAD, varInsnNode.var));
View Full Code Here

        boolean isStatic = (mn.access & ACC_STATIC) != 0;
        log("Instrumenting " + mn.name + " " + mn.desc + " "
                + (isStatic ? " static " : ""));
        Label fStartl = new Label();
        Label fEndl = new Label();
        LabelNode fStart = new LabelNode(fStartl);
        LabelNode fEnd = new LabelNode(fEndl);

        // Label vStartl = new Label();
        // Label vEndl = new Label();
        // LabelNode vStart = new LabelNode(vStartl);
        // LabelNode vEnd = new LabelNode(vEndl);

        LabelNode mStart = null;// (LabelNode) mn.instructions.getFirst();
        LabelNode mEnd = null; // (LabelNode) mn.instructions.getLast();

        for (ListIterator<AbstractInsnNode> i = mn.instructions.iterator(); i
                .hasNext();) {
            AbstractInsnNode node = i.next();
            if (node instanceof LabelNode) {
                if (mStart == null) {
                    mStart = (LabelNode) node;
                } else {
                    mEnd = (LabelNode) node;
                }
            } else {
                if (mStart == null) {
                    throw new RuntimeException(
                            "Expected instructions of " + cn.name + " : " + mn.name + " " + mn.desc + " to start with a label, got "
                                    + node.getClass());
                }
            }
        }

        Label mStartl = mStart.getLabel();
        if (mEnd == null) {
            mEnd = new LabelNode(new Label());
            mn.instructions.add(mEnd);
        }

        List<LocalVariableNode> paramsAndThis = new ArrayList<LocalVariableNode>();       
        for (Object o : mn.localVariables) {
            LocalVariableNode lvn = (LocalVariableNode) o;
            if (lvn.start.getLabel().equals(mStartl)) {
                paramsAndThis.add(lvn);
            }
        }
        int purrSecretIndex = 0;
        for (LocalVariableNode lvn : paramsAndThis) {
            purrSecretIndex += Type.getType(lvn.desc).getSize();
        }
        Set<String> lvNames = new HashSet<String>();
        for (Object o : mn.localVariables) {
            LocalVariableNode lvn = (LocalVariableNode) o;
            if (lvn.index >= purrSecretIndex) {
                lvn.index = lvn.index + 1;
            }
            lvNames.add(lvn.name);
        }
        String purrSecretUniqueName = makeUnique("_purrSecret", lvNames);

        mn.localVariables.add(paramsAndThis.size(), new LocalVariableNode(
                purrSecretUniqueName, OBJECT_DESC, null, mStart, mEnd,
                purrSecretIndex));
        mn.maxLocals += 1;

        for (Iterator<?> i = mn.instructions.iterator(); i.hasNext();) {
            Object current = i.next();
            if (current instanceof VarInsnNode) {
                VarInsnNode vin = (VarInsnNode) current;
                if (vin.var >= purrSecretIndex) {
                    vin.var = vin.var + 1;
                }
            }
            if (current instanceof IincInsnNode) {
                IincInsnNode iin = (IincInsnNode) current;
                iin.var = iin.var + 1;
                log("Incremented iinc var to " + iin.var);
            }
        }

        InsnList pre = new InsnList();
        // pre.add(vStart);
        if (isStatic) {
            pre.add(new LdcInsnNode(cn.name));
        } else {
            pre.add(new VarInsnNode(ALOAD, 0)); // "this" is index 0 locvar.
        }

        pre.add(new MethodInsnNode(INVOKESTATIC, classDesc, BEFORE,
                ONE_OBJECT_PARAM + OBJECT_DESC));
        pre.add(new VarInsnNode(ASTORE, purrSecretIndex));
        pre.add(fStart);
        mn.instructions.insert(mStart, pre);

        List<AbstractInsnNode> terminalNodes = new ArrayList<AbstractInsnNode>();
        for (int i = 0; i < mn.instructions.size(); i++) {
            AbstractInsnNode insn = mn.instructions.get(i);
            int opcode = insn.getOpcode();
            if (isReturnOpcode(opcode)) {
                terminalNodes.add(insn);
            }
        }

        LabelNode okReturnLabel = new LabelNode( new Label() );
//        LabelNode throwReturnLabel = new LabelNode( new Label() );
        Integer returnOpcode = null;
        for (AbstractInsnNode ain : terminalNodes) {
//            mn.instructions.insertBefore(ain,
//                    preExitInsns(cn, mn, purrSecretIndex));
View Full Code Here

        // find last return
        while (current.getOpcode() != ARETURN) {
            current = current.getPrevious();
        }

        LabelNode lastReturnLabel = new LabelNode();
        method.instructions.insertBefore(current, lastReturnLabel);

        // iterate backwards up to first instructions
        while ((current = current.getPrevious()) != null) {
View Full Code Here

TOP

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

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.