List of usage examples for org.objectweb.asm.tree InsnList getLast
public AbstractInsnNode getLast()
From source file:pxb.android.dex2jar.optimize.D.java
License:Apache License
private void cut(MethodNode method) { InsnList insns = method.instructions; method.instructions = null;// w w w . ja v a 2 s. c o m { AbstractInsnNode p = insns.getFirst(); if (!(p instanceof LabelNode)) { insns.insertBefore(p, new LabelNode()); } AbstractInsnNode q = insns.getLast(); if (!(q instanceof LabelNode)) { insns.insert(q, new LabelNode()); } } @SuppressWarnings("serial") Map<LabelNode, LabelNode> cloneMap = new HashMap<LabelNode, LabelNode>() { public LabelNode get(Object key) { LabelNode l = super.get(key); if (l == null) { l = new LabelNode(); put((LabelNode) key, l); } return l; } }; Map<LabelNode, Block> preBlockMap = new HashMap<LabelNode, Block>(); int i = 0; LabelNode label = null; Block block = null; List<AbstractInsnNode> currentInsnList = null; for (AbstractInsnNode p = insns.getFirst(); p != null; p = p.getNext()) { final AbstractInsnNode cp = p.clone(cloneMap); switch (cp.getType()) { case AbstractInsnNode.LABEL: { if (label != null) { block = new Block(i++, label); block.insns = currentInsnList; block.next = (LabelNode) cp; addToMap(block); } currentInsnList = new ArrayList<AbstractInsnNode>(); label = (LabelNode) cp; preBlockMap.put(label, block); break; } case AbstractInsnNode.JUMP_INSN: case AbstractInsnNode.LOOKUPSWITCH_INSN: case AbstractInsnNode.TABLESWITCH_INSN: { if (cp.getOpcode() == GOTO) { block = new Block(i++, label); block.next = (LabelNode) ((JumpInsnNode) cp).label; } else {// block = new BranchBlock(i++, label, cp); block.next = (LabelNode) getNextLabelNode(p, insns).clone(cloneMap); } block.insns = currentInsnList; addToMap(block); currentInsnList = null; label = null; break; } case AbstractInsnNode.FRAME: case AbstractInsnNode.LINE: // ignore break; default: switch (cp.getOpcode()) { case IRETURN: case LRETURN: case FRETURN: case DRETURN: case ARETURN: case RETURN: case ATHROW: block = new EndBlock(i++, label, cp); block.next = null; getNextLabelNode(p, insns); block.insns = currentInsnList; addToMap(block); currentInsnList = null; label = null; break; default: currentInsnList.add(cp); } } } for (Iterator<?> it = method.tryCatchBlocks.iterator(); it.hasNext();) { TryCatchBlockNode tcn = (TryCatchBlockNode) it.next(); Block s = map.get((LabelNode) tcn.start.clone(cloneMap)); Block e = map.get((LabelNode) tcn.end.clone(cloneMap)); Block handler = map.get(tcn.handler.clone(cloneMap)); TcbK key = new TcbK(s, e); Map<Block, String> handlers = tcbs.get(key); if (handlers == null) { handlers = new TreeMap<Block, String>(new Comparator<Block>() { @Override public int compare(Block o1, Block o2) { return o1.id - o2.id; } }); tcbs.put(key, handlers); } handlers.put(handler, tcn.type); tcn.start = s.label; tcn.end = e.label; tcn.handler = handler.label; } }