List of usage examples for org.objectweb.asm.tree AbstractInsnNode getNext
public AbstractInsnNode getNext()
From source file:jaspex.speculation.newspec.FlowFrame.java
License:Open Source License
/** Obtm label depois da instruco. Se j existir uma, reutilizada, cc uma nova criada. **/ public static LabelNode labelAfter(InsnList il, AbstractInsnNode insn) { if (!(insn.getNext() instanceof LabelNode)) { il.insert(insn, new LabelNode()); }/*from w w w . jav a2 s.co m*/ return (LabelNode) insn.getNext(); }
From source file:net.doubledoordev.inventorylock.asm.Transformer.java
License:Open Source License
@Override public byte[] transform(String name, String transformedName, byte[] basicClass) { ClassNode classNode = new ClassNode(); ClassReader classReader = new ClassReader(basicClass); classReader.accept(classNode, READER_FLAGS); boolean isPlayer = transformedName.equals(ENTITY_PLAYER_OWNER_NAME); if (isPlayer) LOGGER.info("Found EntityPlayer"); for (MethodNode method : classNode.methods) { InsnList list = method.instructions; if (isPlayer && INSTANCE.mapMethodDesc(method.desc).equals(ENTITY_PLAYER_DESC) && INSTANCE.mapMethodName(name, method.name, method.desc).equals(ENTITY_PLAYER_TARGET)) { final LabelNode newLabel = new LabelNode(); LOGGER.info("Found canOpen"); AbstractInsnNode node = list.getFirst(); while (node.getOpcode() != IRETURN && node != list.getLast()) { if (node.getOpcode() == IFEQ) ((JumpInsnNode) node).label = newLabel; node = node.getNext(); }/*from www . ja v a 2 s . c o m*/ if (node.getOpcode() != IRETURN) throw new RuntimeException("ASM failed. (return not found)"); final AbstractInsnNode target = node; while (node.getType() != LABEL && node != list.getLast()) node = node.getNext(); if (node.getType() != LABEL) throw new RuntimeException("ASM failed. (label not found)"); final LabelNode label = ((LabelNode) node); //Adding "else if (code instanceof BetterLockCode) return ((BetterLockCode) code).contains(this.getUniqueID());" InsnList inject = new InsnList(); inject.add(newLabel); inject.add(new VarInsnNode(ALOAD, 1)); inject.add(new TypeInsnNode(INSTANCEOF, BETTER_LOCK_TYPE)); inject.add(new JumpInsnNode(IFEQ, label)); inject.add(new VarInsnNode(ALOAD, 1)); inject.add(new TypeInsnNode(CHECKCAST, BETTER_LOCK_TYPE)); inject.add(new VarInsnNode(ALOAD, 0)); //inject.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, ENTITY_PLAYER_OWNER, ENTITY_PLAYER_GET_UUID, ENTITY_PLATER_GET_UUID_DESC, false)); inject.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, BETTER_LOCK_TYPE, BETTER_LOCK_CONTAINS, BETTER_LOCK_CONTAINS_DESC, false)); inject.add(new InsnNode(IRETURN)); list.insert(target, inject); LOGGER.info("Injected elseif into EntityPlayer's canOpen"); } for (AbstractInsnNode node = list.getFirst(); node != list.getLast(); node = node.getNext()) { if (node.getOpcode() != INVOKESTATIC) continue; MethodInsnNode methodInsnNode = ((MethodInsnNode) node); // if (transformedName.equals("net.minecraft.tileentity.TileEntityLockable")) // LOGGER.info("In {} ({}) Method {}.{}{} Translated {}.{}{}", name, transformedName, // methodInsnNode.owner, methodInsnNode.name, methodInsnNode.desc, // INSTANCE.map(methodInsnNode.owner), INSTANCE.mapMethodName(methodInsnNode.owner, methodInsnNode.name, methodInsnNode.desc), INSTANCE.mapMethodDesc(methodInsnNode.desc).equals(LOCK_CODE_DESC)); if (INSTANCE.map(methodInsnNode.owner).equals(LOCK_CODE_OWNER) && INSTANCE.mapMethodDesc(methodInsnNode.desc).equals(LOCK_CODE_DESC) && INSTANCE.mapMethodName(methodInsnNode.owner, methodInsnNode.name, methodInsnNode.desc) .equals(LOCK_CODE_TARGET)) { methodInsnNode.owner = LOCK_CODE_OWNER_REPLACE; methodInsnNode.name = LOCK_CODE_NAME; LOGGER.info("Replaced call in class {} ({}), method {}{}", name, transformedName, method.name, method.desc); } } } final ClassWriter writer = new ClassWriter(WRITER_FLAGS); classNode.accept(writer); return writer.toByteArray(); }
From source file:net.lyonlancer5.mcmp.karasu.asm.KarasuTransformer.java
License:Apache License
private static void transformEntityLivingBase(ClassNode classNode) { Constants.LOGGER.info("======== Project Karasu ~ Water Walking Enchantment Patch ========"); Constants.LOGGER.info("Patching net.minecraft.entity.EntityLivingBase"); for (MethodNode method : classNode.methods) { if (method.desc.equals("(FF)V")) { AbstractInsnNode movefNode = null; AbstractInsnNode motionxNode = null; AbstractInsnNode motionzNode = null; AbstractInsnNode motionyNode = null; AbstractInsnNode[] moveList = method.instructions.toArray(); int var11 = moveList.length; for (int var12 = 0; var12 < var11; ++var12) { AbstractInsnNode instruction = moveList[var12]; if (instruction.getOpcode() == Opcodes.ALOAD) { if (((VarInsnNode) instruction).var == 0 && instruction.getNext().getOpcode() == Opcodes.DUP && motionxNode == null) { motionxNode = instruction; } else if (((VarInsnNode) instruction).var == 0 && instruction.getNext().getOpcode() == Opcodes.DUP && motionyNode == null) { motionyNode = instruction; } else if (((VarInsnNode) instruction).var == 0 && instruction.getNext().getOpcode() == Opcodes.DUP && motionzNode == null) { motionzNode = instruction; } else if (movefNode == null && ((VarInsnNode) instruction).var == 0 && instruction.getNext().getOpcode() == Opcodes.FLOAD && ((VarInsnNode) instruction.getNext()).var == 1) { movefNode = instruction; }//from w w w . j a v a 2s . c o m } } int var14; InsnList var15; if (method.name.equals("e")) { Constants.LOGGER.info("Found matching method: " + method.name + " " + method.desc + " - (Obf)"); if (motionxNode != null) { for (var14 = 0; var14 < 6; ++var14) { motionxNode = motionxNode.getNext(); method.instructions.remove(motionxNode.getPrevious()); } Constants.LOGGER.info("Patching sv.w"); var15 = new InsnList(); var15.add(new VarInsnNode(Opcodes.ALOAD, 0)); var15.add(new InsnNode(Opcodes.DUP)); var15.add(new FieldInsnNode(Opcodes.GETFIELD, "net/minecraft/entity/EntityLivingBase", "w", "D")); var15.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "net/lyonlancer5/mcmp/karasu/asm/KarasuFeatherMetadata", "modifyHorizontalVel", "()D", false)); var15.add(new InsnNode(Opcodes.DMUL)); var15.add(new FieldInsnNode(Opcodes.PUTFIELD, "net/minecraft/entity/EntityLivingBase", "w", "D")); method.instructions.insertBefore(motionxNode, var15); Constants.LOGGER.info("Patched sv.w"); } if (motionzNode != null) { for (var14 = 0; var14 < 6; ++var14) { motionzNode = motionzNode.getNext(); method.instructions.remove(motionzNode.getPrevious()); } Constants.LOGGER.info("Patching sv.y"); var15 = new InsnList(); var15.add(new VarInsnNode(Opcodes.ALOAD, 0)); var15.add(new InsnNode(Opcodes.DUP)); var15.add(new FieldInsnNode(Opcodes.GETFIELD, "net/minecraft/entity/EntityLivingBase", "y", "D")); var15.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "net/lyonlancer5/mcmp/karasu/asm/KarasuFeatherMetadata", "modifyHorizontalVel", "()D", false)); var15.add(new InsnNode(Opcodes.DMUL)); var15.add(new FieldInsnNode(Opcodes.PUTFIELD, "net/minecraft/entity/EntityLivingBase", "y", "D")); method.instructions.insertBefore(motionzNode, var15); Constants.LOGGER.info("Patched sv.y"); } if (movefNode != null) { for (var14 = 0; var14 < 14; ++var14) { movefNode = movefNode.getNext(); method.instructions.remove(movefNode.getPrevious()); } var15 = new InsnList(); var15.add(new VarInsnNode(Opcodes.ALOAD, 0)); var15.add(new VarInsnNode(Opcodes.FLOAD, 1)); var15.add(new VarInsnNode(Opcodes.FLOAD, 2)); var15.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "net/lyonlancer5/mcmp/karasu/asm/KarasuFeatherMetadata", "modifyVerticalVel", "()F", false)); var15.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "net/minecraft/entity/EntityLivingBase", "a", "(FFF)V", false)); method.instructions.insertBefore(movefNode, var15); Constants.LOGGER.info("Patching method -- sv.a (FFF)V"); } Constants.LOGGER.info("Patch success!"); return; } else if (method.name.equals("moveEntityWithHeading")) { Constants.LOGGER .info("Found matching method: " + method.name + " " + method.desc + " - (Deobf)"); if (motionxNode != null) { for (var14 = 0; var14 < 6; ++var14) { motionxNode = motionxNode.getNext(); method.instructions.remove(motionxNode.getPrevious()); } Constants.LOGGER.info("Patching EntityLivingBase.motionX"); var15 = new InsnList(); var15.add(new VarInsnNode(Opcodes.ALOAD, 0)); var15.add(new InsnNode(Opcodes.DUP)); var15.add(new FieldInsnNode(Opcodes.GETFIELD, "net/minecraft/entity/EntityLivingBase", "motionX", "D")); var15.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "net/lyonlancer5/mcmp/karasu/asm/KarasuFeatherMetadata", "modifyHorizontalVel", "()D", false)); var15.add(new InsnNode(Opcodes.DMUL)); var15.add(new FieldInsnNode(Opcodes.PUTFIELD, "net/minecraft/entity/EntityLivingBase", "motionX", "D")); method.instructions.insertBefore(motionxNode, var15); Constants.LOGGER.info("Patched EntityLivingBase.motionX"); } if (motionzNode != null) { for (var14 = 0; var14 < 6; ++var14) { motionzNode = motionzNode.getNext(); method.instructions.remove(motionzNode.getPrevious()); } Constants.LOGGER.info("Patching EntityLivingBase.motionZ"); var15 = new InsnList(); var15.add(new VarInsnNode(Opcodes.ALOAD, 0)); var15.add(new InsnNode(Opcodes.DUP)); var15.add(new FieldInsnNode(Opcodes.GETFIELD, "net/minecraft/entity/EntityLivingBase", "motionZ", "D")); var15.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "net/lyonlancer5/mcmp/karasu/asm/KarasuFeatherMetadata", "modifyHorizontalVel", "()D", false)); var15.add(new InsnNode(Opcodes.DMUL)); var15.add(new FieldInsnNode(Opcodes.PUTFIELD, "net/minecraft/entity/EntityLivingBase", "motionZ", "D")); method.instructions.insertBefore(motionzNode, var15); Constants.LOGGER.info("Patched EntityLivingBase.motionZ"); } if (movefNode != null) { for (var14 = 0; var14 < 14; ++var14) { movefNode = movefNode.getNext(); method.instructions.remove(movefNode.getPrevious()); } var15 = new InsnList(); var15.add(new VarInsnNode(Opcodes.ALOAD, 0)); var15.add(new VarInsnNode(Opcodes.FLOAD, 1)); var15.add(new VarInsnNode(Opcodes.FLOAD, 2)); var15.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "net/lyonlancer5/mcmp/karasu/asm/KarasuFeatherMetadata", "modifyVerticalVel", "()F", false)); var15.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "net/minecraft/entity/EntityLivingBase", "moveFlying", "(FFF)V", false)); method.instructions.insertBefore(movefNode, var15); Constants.LOGGER.info("Patched method - EntityLivingBase.moveFlying (FFF)V"); } Constants.LOGGER.info("Patch success!"); return; } else if (method.name.equals("func_70612_e")) { Constants.LOGGER.info("Found matching method: " + method.name + " " + method.desc + " - (srg)"); if (motionxNode != null) { for (var14 = 0; var14 < 6; ++var14) { motionxNode = motionxNode.getNext(); method.instructions.remove(motionxNode.getPrevious()); } Constants.LOGGER.info("Patching EntityLivingBase.field_70159_w"); var15 = new InsnList(); var15.add(new VarInsnNode(Opcodes.ALOAD, 0)); var15.add(new InsnNode(Opcodes.DUP)); var15.add(new FieldInsnNode(Opcodes.GETFIELD, "net/minecraft/entity/EntityLivingBase", "field_70159_w", "D")); var15.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "net/lyonlancer5/mcmp/karasu/asm/KarasuFeatherMetadata", "modifyHorizontalVel", "()D", false)); var15.add(new InsnNode(Opcodes.DMUL)); var15.add(new FieldInsnNode(Opcodes.PUTFIELD, "net/minecraft/entity/EntityLivingBase", "field_70159_w", "D")); method.instructions.insertBefore(motionxNode, var15); Constants.LOGGER.info("Patched EntityLivingBase.field_70159_w"); } if (motionzNode != null) { for (var14 = 0; var14 < 6; ++var14) { motionzNode = motionzNode.getNext(); method.instructions.remove(motionzNode.getPrevious()); } Constants.LOGGER.info("Patching EntityLivingBase.field_70179_y"); var15 = new InsnList(); var15.add(new VarInsnNode(Opcodes.ALOAD, 0)); var15.add(new InsnNode(Opcodes.DUP)); var15.add(new FieldInsnNode(Opcodes.GETFIELD, "net/minecraft/entity/EntityLivingBase", "field_70179_y", "D")); var15.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "net/lyonlancer5/mcmp/karasu/asm/KarasuFeatherMetadata", "modifyHorizontalVel", "()D", false)); var15.add(new InsnNode(Opcodes.DMUL)); var15.add(new FieldInsnNode(Opcodes.PUTFIELD, "net/minecraft/entity/EntityLivingBase", "field_70179_y", "D")); method.instructions.insertBefore(motionzNode, var15); Constants.LOGGER.info("Patched EntityLivingBase.field_70179_y"); } if (movefNode != null) { for (var14 = 0; var14 < 14; ++var14) { movefNode = movefNode.getNext(); method.instructions.remove(movefNode.getPrevious()); } var15 = new InsnList(); var15.add(new VarInsnNode(Opcodes.ALOAD, 0)); var15.add(new VarInsnNode(Opcodes.FLOAD, 1)); var15.add(new VarInsnNode(Opcodes.FLOAD, 2)); var15.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "net/lyonlancer5/mcmp/karasu/asm/KarasuFeatherMetadata", "modifyVerticalVel", "()F", false)); var15.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "net/minecraft/entity/EntityLivingBase", "func_70060_a", "(FFF)V", false)); method.instructions.insertBefore(movefNode, var15); Constants.LOGGER.info("Patched method - EntityLivingBase.func_70060_a (FFF)V"); } Constants.LOGGER.info("Patch success!"); return; } } } // #OVERKILL throw new RuntimeException("Patch FAILED -- No matching method found"); }
From source file:net.malisis.core.asm.AsmUtils.java
License:Open Source License
public static AbstractInsnNode findInstruction(MethodNode method, InsnList matches) { AbstractInsnNode node = method.instructions.getFirst(); AbstractInsnNode match = matches.getFirst(); while (node != null) { if (insnEqual(node, match)) { AbstractInsnNode m = match.getNext(); AbstractInsnNode n = node.getNext(); while (m != null && n != null && insnEqual(m, n)) { m = m.getNext();//from w w w.j a v a 2s . c om n = n.getNext(); } if (m == null) return node; } node = node.getNext(); } return null; }
From source file:net.minecraftforge.fml.common.asm.transformers.SoundEngineFixTransformer.java
License:Open Source License
@Override public byte[] transform(String name, String transformedName, byte[] basicClass) { if (transformedName.equals("paulscode.sound.Source")) { ClassNode classNode = new ClassNode(); ClassReader classReader = new ClassReader(basicClass); classReader.accept(classNode, 0); classNode.fields.add(new FieldNode(Opcodes.ACC_PUBLIC, "removed", "Z", null, null)); // adding field 'public boolean removed;' ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS); classNode.accept(writer);/* w ww .j av a 2s .c om*/ return writer.toByteArray(); } else if (transformedName.equals("paulscode.sound.Library")) { ClassNode classNode = new ClassNode(); ClassReader classReader = new ClassReader(basicClass); classReader.accept(classNode, 0); MethodNode method = null; for (MethodNode m : classNode.methods) { if (m.name.equals("removeSource") && m.desc.equals("(Ljava/lang/String;)V")) // trying to find paulscode.sound.Library.removeSource(String) { method = m; break; } } if (method == null) throw new RuntimeException( "Error processing " + transformedName + " - no removeSource method found"); AbstractInsnNode referenceNode = null; for (Iterator<AbstractInsnNode> iterator = method.instructions.iterator(); iterator.hasNext();) { AbstractInsnNode insn = iterator.next(); if (insn instanceof MethodInsnNode && ((MethodInsnNode) insn).owner.equals("paulscode/sound/Source") // searching for mySource.cleanup() node (line 1086) && ((MethodInsnNode) insn).name.equals("cleanup")) { referenceNode = insn; break; } } if (referenceNode != null) { LabelNode after = (LabelNode) referenceNode.getNext(); AbstractInsnNode beginning = referenceNode.getPrevious(); int varIndex = ((VarInsnNode) beginning).var; method.instructions.insertBefore(beginning, new VarInsnNode(Opcodes.ALOAD, varIndex)); // adding extra if (mySource.toStream) method.instructions.insertBefore(beginning, new FieldInsnNode(Opcodes.GETFIELD, "paulscode/sound/Source", "toStream", "Z")); LabelNode elseNode = new LabelNode(); method.instructions.insertBefore(beginning, new JumpInsnNode(Opcodes.IFEQ, elseNode)); // if fails (else) -> go to mySource.cleanup(); method.instructions.insertBefore(beginning, new VarInsnNode(Opcodes.ALOAD, varIndex)); // if (mySource.toStream) { mySource.removed = true; } method.instructions.insertBefore(beginning, new InsnNode(Opcodes.ICONST_1)); method.instructions.insertBefore(beginning, new FieldInsnNode(Opcodes.PUTFIELD, "paulscode/sound/Source", "removed", "Z")); method.instructions.insertBefore(beginning, new JumpInsnNode(Opcodes.GOTO, after)); // still inside if -> jump to sourceMap.remove( sourcename ); method.instructions.insertBefore(beginning, elseNode); } ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS); classNode.accept(writer); return writer.toByteArray(); } else if (transformedName.equals("paulscode.sound.StreamThread")) { ClassNode classNode = new ClassNode(); ClassReader classReader = new ClassReader(basicClass); classReader.accept(classNode, 0); MethodNode method = null; for (MethodNode m : classNode.methods) { if (m.name.equals("run") && m.desc.equals("()V")) // trying to find paulscode.sound.StreamThread.run(); { method = m; break; } } if (method == null) throw new RuntimeException("Error processing " + transformedName + " - no run method found"); AbstractInsnNode referenceNode = null; for (Iterator<AbstractInsnNode> iterator = method.instructions.iterator(); iterator.hasNext();) { AbstractInsnNode insn = iterator.next(); if (insn instanceof MethodInsnNode && ((MethodInsnNode) insn).owner.equals("java/util/ListIterator") // searching for 'src = iter.next();' node (line 110) && ((MethodInsnNode) insn).name.equals("next")) { referenceNode = insn.getNext().getNext(); break; } } if (referenceNode != null) { int varIndex = ((VarInsnNode) referenceNode).var; LabelNode after = (LabelNode) referenceNode.getNext(); method.instructions.insertBefore(after, new VarInsnNode(Opcodes.ALOAD, varIndex)); // add if(removed) method.instructions.insertBefore(after, new FieldInsnNode(Opcodes.GETFIELD, "paulscode/sound/Source", "removed", "Z")); method.instructions.insertBefore(after, new JumpInsnNode(Opcodes.IFEQ, after)); // if the source has been marked as removed, clean it up and set the variable to null so it will be removed from the list method.instructions.insertBefore(after, new VarInsnNode(Opcodes.ALOAD, varIndex)); // src.cleanup(); method.instructions.insertBefore(after, new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "paulscode/sound/Source", "cleanup", "()V", false)); method.instructions.insertBefore(after, new InsnNode(Opcodes.ACONST_NULL)); // src = null; method.instructions.insertBefore(after, new VarInsnNode(Opcodes.ASTORE, varIndex)); } ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS); classNode.accept(writer); return writer.toByteArray(); } return basicClass; }
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);/*from www . j av a2 s .c o m*/ if (insn == block.last) { break; } insn = next; } }
From source file:nova.core.wrapper.mc.forge.v17.asm.lib.InsnListPrinter.java
License:Open Source License
public void visitInsnList(InstructionComparator.InsnListSection subsection) { text.clear();//w w w .j a va 2 s . c o m if (labelNames == null) { labelNames = new HashMap<Label, String>(); } else { labelNames.clear(); } buildingLabelMap = true; { AbstractInsnNode insn = subsection.first; while (true) { if (insn.getType() == 8) { visitLabel(((LabelNode) insn).getLabel()); } if (insn == subsection.last) { break; } insn = insn.getNext(); } } text.clear(); buildingLabelMap = false; { AbstractInsnNode insn = subsection.first; while (true) { _visitInsn(insn); if (insn == subsection.last) { break; } insn = insn.getNext(); } } }
From source file:org.apache.drill.exec.compile.bytecode.TrackingInstructionList.java
License:Apache License
@Override public void accept(MethodVisitor mv) { AbstractInsnNode insn = inner.getFirst(); while (insn != null) { currentFrame = frames[index];/*from www.j a v a 2 s .c o m*/ nextFrame = index + 1 < frames.length ? frames[index + 1] : null; insn.accept(mv); insn = insn.getNext(); index++; } }
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. *//*w w w. j a v a 2 s . c o m*/ @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. *///w ww .ja v a 2s . c o 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; }