List of usage examples for org.objectweb.asm.tree InsnList remove
public void remove(final AbstractInsnNode insnNode)
From source file:edu.mit.streamjit.util.bytecode.MethodUnresolver.java
License:Open Source License
/** * Removes goto instructions that go to the label immediately following * them./*from w ww . j a va 2 s .c o m*/ * @return true iff changes were made */ private boolean removeUnnecessaryGotos() { InsnList insns = methodNode.instructions; for (int i = 0; i < insns.size() - 1; ++i) { AbstractInsnNode first = insns.get(i); if (first.getOpcode() != Opcodes.GOTO) continue; AbstractInsnNode second = insns.get(i + 1); if (!(second instanceof LabelNode)) continue; if (((JumpInsnNode) first).label != second) continue; insns.remove(first); return true; } return false; }
From source file:net.epoxide.surge.asm.ASMUtils.java
License:Creative Commons License
/** * Removes a specific set of instructions (the needle) from a much larger set of * instructions (the hay stack). Be cautious when using this method, as it is almost never * a good idea to remove instructions.//from w ww . ja v a 2 s . c o m * * @param haystack: A large list of instructions which is being searched through. * @param needle: A specific list of instructions which are to be removed from the larger * instruction list. */ public static void removeNeedleFromHaystack(InsnList haystack, InsnList needle) { final int firstInd = haystack.indexOf(findFirstNodeFromNeedle(haystack, needle)); final int lastInd = haystack.indexOf(findLastNodeFromNeedle(haystack, needle)); final List<AbstractInsnNode> realNeedle = new ArrayList<AbstractInsnNode>(); for (int i = firstInd; i <= lastInd; i++) realNeedle.add(haystack.get(i)); for (final AbstractInsnNode node : realNeedle) haystack.remove(node); }
From source file:nova.core.wrapper.mc.forge.v17.asm.lib.ASMHelper.java
License:Open Source License
public static void removeBlock(InsnList insns, InstructionComparator.InsnListSection block) { AbstractInsnNode insn = block.first; while (true) { AbstractInsnNode next = insn.getNext(); insns.remove(insn); if (insn == block.last) { break; }/*from w w w . j a va 2s . c om*/ insn = next; } }
From source file:org.coldswap.asm.field.PrivateStaticFieldReplacer.java
License:Open Source License
/** * Removes any initializing reference of the field. * * @param classNode containing the old class. * @param fieldNode containing the old field. * @return the initializing list of instructions. *//* www . j a v a 2s.c om*/ @SuppressWarnings("unchecked") private InsnList cleanClInit(ClassNode classNode, FieldNode fieldNode) { List<MethodNode> methodNodes = classNode.methods; AbstractInsnNode firstInst = null; int counter = 0; for (MethodNode methodNode : methodNodes) { if (methodNode.name.equals("<clinit>")) { // search for PUTSTATIC InsnList insnList = methodNode.instructions; Iterator iterator1 = insnList.iterator(); while (iterator1.hasNext()) { AbstractInsnNode ins2 = (AbstractInsnNode) iterator1.next(); // if a initializing has been found, then copy everything from // the corresponding label to the PUTSTATIC if (ins2.getOpcode() == Opcodes.PUTSTATIC) { final Boolean[] fieldFound = { false }; final FieldNode fNode = fieldNode; ins2.accept(new MethodVisitor(Opcodes.ASM5) { @Override public void visitFieldInsn(int i, String s, String s2, String s3) { if (s2.equals(fNode.name)) { fieldFound[0] = true; } super.visitFieldInsn(i, s, s2, s3); } }); if (fieldFound[0]) { // find the first PUTSTATIC before this one. boolean staticFound = false; while (!staticFound) { AbstractInsnNode tmpInst = ins2.getPrevious(); if (tmpInst != null) { if (tmpInst.getOpcode() != Opcodes.F_NEW) { if (tmpInst.getOpcode() == Opcodes.PUTSTATIC) { staticFound = true; } else { firstInst = tmpInst; counter++; } } } else { staticFound = true; } ins2 = tmpInst; } break; } } } if (firstInst != null) { InsnList iList = new InsnList(); iList.add(firstInst.clone(null)); counter--; while (counter > 0) { AbstractInsnNode ain = firstInst.getNext(); iList.add(ain.clone(null)); counter--; insnList.remove(firstInst); firstInst = ain; } // remove last instruction and the putstatic instruction AbstractInsnNode putStatic = firstInst.getNext(); insnList.remove(firstInst); insnList.remove(putStatic); return iList; } } } return null; }
From source file:org.coldswap.asm.field.ProtectedStaticFieldReplacer.java
License:Open Source License
/** * Removes any initializing reference of the field. * * @param classNode containing the old class. * @param fieldNode containing the old field. * @param canRemove <code>true</code> if this method should remove the initializing code and return it * or <code>false</code> if you only want to return the init code. * @return the initializing list of instructions. *//*from ww w . j a v a 2 s. co m*/ @SuppressWarnings("unchecked") private InsnList cleanClInit(ClassNode classNode, FieldNode fieldNode, boolean canRemove) { List<MethodNode> methodNodes = classNode.methods; AbstractInsnNode firstInst = null; int counter = 0; for (MethodNode methodNode : methodNodes) { if (methodNode.name.equals("<clinit>")) { // search for PUTSTATIC InsnList insnList = methodNode.instructions; Iterator iterator1 = insnList.iterator(); while (iterator1.hasNext()) { AbstractInsnNode ins2 = (AbstractInsnNode) iterator1.next(); // if a initializing has been found, then copy everything from // the coresponding label to the PUTSTATIC if (ins2.getOpcode() == Opcodes.PUTSTATIC) { final Boolean[] fieldFound = { false }; final FieldNode fNode = fieldNode; ins2.accept(new MethodVisitor(Opcodes.ASM5) { @Override public void visitFieldInsn(int i, String s, String s2, String s3) { if (s2.equals(fNode.name)) { fieldFound[0] = true; } super.visitFieldInsn(i, s, s2, s3); } }); if (fieldFound[0]) { // find the first PUTSTATIC before this one. boolean staticFound = false; while (!staticFound) { AbstractInsnNode tmpInst = ins2.getPrevious(); if (tmpInst != null) { if (tmpInst.getOpcode() != Opcodes.F_NEW) { if (tmpInst.getOpcode() == Opcodes.PUTSTATIC) { staticFound = true; } else { firstInst = tmpInst; counter++; } } } else { staticFound = true; } ins2 = tmpInst; } break; } } } if (firstInst != null) { InsnList iList = new InsnList(); iList.add(firstInst.clone(null)); counter--; while (counter > 0) { AbstractInsnNode ain = firstInst.getNext(); iList.add(ain.clone(null)); counter--; if (canRemove) { insnList.remove(firstInst); } firstInst = ain; } if (canRemove) { // remove last instruction and the putstatic instruction AbstractInsnNode putStatic = firstInst.getNext(); insnList.remove(firstInst); insnList.remove(putStatic); } return iList; } } } return null; }
From source file:org.coldswap.asm.method.PublicFloatMethodReplacer.java
License:Open Source License
private InsnList replaceReturn(InsnList insnList, Type retType) { final Type rretType = retType; int retOpcode = MethodUtil.getRetOpcodeToReplace(retType); for (int i = 0; i < insnList.size(); i++) { AbstractInsnNode absIns = insnList.get(i); int opcode = absIns.getOpcode(); if (opcode == retOpcode) { // if tries to return a Reference type into a primitive then // remove the unbox( we return an Object). If a primitive is returned // into a primitive then we must try to box from primitive to Object/Integer, etc.. // check if an unbox takes place before return final boolean[] isBoxUnbox = { false, false }; AbstractInsnNode valueOf = null; AbstractInsnNode primitiveValue = null; if (i > 1) { valueOf = insnList.get(i - 1); primitiveValue = insnList.get(i - 2); if (valueOf.getOpcode() == Opcodes.INVOKESTATIC) { valueOf.accept(new MethodVisitor(Opcodes.ASM5) { @Override public void visitMethodInsn(int i, String s, String s2, String s3) { if (AutoBoxing.isPrimitive(rretType.getDescriptor())) { if ((AutoBoxing.getBoxClassName(rretType) + ".valueOf").equals(s + s2)) { isBoxUnbox[0] = true; }/*from www . jav a 2 s. co m*/ } super.visitMethodInsn(i, s, s2, s3); } }); } if (isBoxUnbox[0] && primitiveValue.getOpcode() == Opcodes.INVOKEVIRTUAL) { primitiveValue.accept(new MethodVisitor(Opcodes.ASM5) { @Override public void visitMethodInsn(int i, String s, String s2, String s3) { if ((s + s2).equals(AutoBoxing.getUnBoxInvoke(rretType))) { isBoxUnbox[1] = true; } super.visitMethodInsn(i, s, s2, s3); } }); } } if (isBoxUnbox[0] && isBoxUnbox[1]) { // remove indexes insnList.remove(valueOf); insnList.remove(primitiveValue); } else { InsnList iList = new InsnList(); iList.add(AutoBoxing.box(retType)); iList.add(new InsnNode(Opcodes.ARETURN)); insnList.insertBefore(absIns, iList); insnList.remove(absIns); } } } return insnList; }
From source file:org.coldswap.asm.method.PublicObjectMethodReplacer.java
License:Open Source License
private InsnList replaceReturn(InsnList insnList, Type retType) { final Type rretType = retType; int retOpcode = MethodUtil.getRetOpcodeToReplace(retType); for (int i = 0; i < insnList.size(); i++) { AbstractInsnNode absIns = insnList.get(i); int opcode = absIns.getOpcode(); if (opcode == retOpcode) { // if tries to return a Reference type into a primitive then // remove the unbox( we return an Object). If a primitive is returned // into a primitive then we must try to box from primitive to Object/Integer, etc.. // check if an unbox takes place before return final boolean[] isBoxUnbox = { false, false }; AbstractInsnNode valueOf = null; AbstractInsnNode primitiveValue = null; if (i > 1) { valueOf = insnList.get(i - 1); primitiveValue = insnList.get(i - 2); if (valueOf.getOpcode() == Opcodes.INVOKESTATIC) { valueOf.accept(new MethodVisitor(Opcodes.ASM5) { @Override public void visitMethodInsn(int i, String s, String s2, String s3) { if (AutoBoxing.isPrimitive(rretType.getDescriptor())) { if ((AutoBoxing.getBoxClassName(rretType) + ".valueOf").equals(s + s2)) { isBoxUnbox[0] = true; }/*w w w. j a v a 2 s . c om*/ } super.visitMethodInsn(i, s, s2, s3); } }); } if (isBoxUnbox[0] && primitiveValue.getOpcode() == Opcodes.INVOKEVIRTUAL) { primitiveValue.accept(new MethodVisitor(Opcodes.ASM5) { @Override public void visitMethodInsn(int i, String s, String s2, String s3) { if ((s + s2).equals(AutoBoxing.getUnBoxInvoke(rretType))) { isBoxUnbox[1] = true; } super.visitMethodInsn(i, s, s2, s3); } }); } } if (isBoxUnbox[0] && isBoxUnbox[1]) { // remove indexes insnList.remove(valueOf); insnList.remove(primitiveValue); } else { InsnList iList = new InsnList(); iList.add(AutoBoxing.box(retType)); iList.add(new InsnNode(Opcodes.ARETURN)); insnList.insertBefore(absIns, iList); insnList.remove(absIns); } } } return insnList; }
From source file:org.diorite.inject.controller.TransformerFieldInjector.java
License:Open Source License
private void injectField(TransformerFieldPair fieldPair, TransformerInjectTracker result) { ControllerFieldData<?> fieldData = fieldPair.data; assert fieldData != null; MethodInsnNode injectionInvokeNode = result.getResult(); FieldInsnNode putfieldNode = result.getFieldInsnNode(); // insert invoke methods: MethodNode codeBefore = new MethodNode(); MethodNode codeAfter = new MethodNode(); this.injectTransformer.fillMethodInvokes(codeBefore, codeAfter, fieldData); // node list for PUTFIELD InsnList initNodeList = result.getInitNodeList(); // node list for invoke execution InsnList resultNodeList = result.getResultNodeList(); if (codeBefore.instructions.size() != 0) { // invoke before should be added before PUTFIELD and before INVOKE resultNodeList.insertBefore(injectionInvokeNode, codeBefore.instructions); }//from ww w .j a v a 2 s .c o m if (codeAfter.instructions.size() != 0) { // invoke after should be added after PUTFIELD initNodeList.insert(putfieldNode, codeAfter.instructions); } // and replace placeholder node with real injections: { MethodNode tempNode = new MethodNode(); TransformerInvokerGenerator.generateFieldInjection(this.injectTransformer.classData, fieldData, tempNode, -1); resultNodeList.insertBefore(injectionInvokeNode, tempNode.instructions); resultNodeList.remove(injectionInvokeNode); } }
From source file:org.diorite.inject.impl.controller.TransformerFieldInjector.java
License:Open Source License
private void injectField(TransformerFieldPair fieldPair, TransformerInjectTracker result) { ControllerFieldData<?> fieldData = fieldPair.data; assert fieldData != null; MethodInsnNode injectionInvokeNode = result.getResult(); FieldInsnNode putfieldNode = result.getFieldInsnNode(); // insert invoke methods: MethodNode codeBefore = new MethodNode(); MethodNode codeAfter = new MethodNode(); this.injectTransformer.fillMethodInvokes(codeBefore, codeAfter, fieldData); // node list for PUTFIELD InsnList initNodeList = result.getInitNodeList(); // node list for invoke execution InsnList resultNodeList = result.getResultNodeList(); if (codeBefore.instructions.size() != 0) { // invoke before should be added before PUTFIELD and before INVOKE resultNodeList.insertBefore(injectionInvokeNode, codeBefore.instructions); }// w w w . jav a2 s. co m if (codeAfter.instructions.size() != 0) { // invoke after should be added after PUTFIELD initNodeList.insert(putfieldNode, codeAfter.instructions); } // and replace placeholder node with real injections: { MethodNode tempNode = new MethodNode(); TransformerInvokerGenerator.generateFieldInjection(this.injectTransformer.classData, fieldData, tempNode, -1, fieldPair.placeholderType); resultNodeList.insertBefore(injectionInvokeNode, tempNode.instructions); resultNodeList.remove(injectionInvokeNode); } }
From source file:org.eclipse.objectteams.otredyn.bytecode.asm.AbstractTransformableClassNode.java
License:Open Source License
/** * Replace all return statements in the given instructions with new * statements that convert the real return value to {@link Object} * and return this new {@link Object}// w ww. j a va 2 s . c o m * * @param instructions * @param returnType */ protected void replaceReturn(InsnList instructions, Type returnType) { if (returnType.getSort() != Type.OBJECT && returnType.getSort() != Type.ARRAY && returnType.getSort() != Type.VOID) { ListIterator<AbstractInsnNode> orgMethodIter = instructions.iterator(); while (orgMethodIter.hasNext()) { AbstractInsnNode orgMethodNode = orgMethodIter.next(); if (orgMethodNode.getOpcode() == returnType.getOpcode(Opcodes.IRETURN)) { instructions.insertBefore(orgMethodNode, AsmTypeHelper.getBoxingInstructionForType(returnType)); instructions.insertBefore(orgMethodNode, new InsnNode(Opcodes.ARETURN)); instructions.remove(orgMethodNode); } } } else if (returnType.getSort() == Type.VOID) { ListIterator<AbstractInsnNode> orgMethodIter = instructions.iterator(); while (orgMethodIter.hasNext()) { AbstractInsnNode orgMethodNode = orgMethodIter.next(); if (orgMethodNode.getOpcode() == Opcodes.RETURN) { instructions.insertBefore(orgMethodNode, new InsnNode(Opcodes.ACONST_NULL)); instructions.insertBefore(orgMethodNode, new InsnNode(Opcodes.ARETURN)); instructions.remove(orgMethodNode); } } } }