List of usage examples for org.objectweb.asm Opcodes ALOAD
int ALOAD
To view the source code for org.objectweb.asm Opcodes ALOAD.
Click Source Link
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; }// w w w . j av a 2 s. com } } 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.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);// ww w . ja v a2 s.c o m 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:net.petercashel.jmsDd.util.ASMTransformer.java
License:Apache License
public static byte[] transform(String name, byte[] bytes) { if (debug)/* w w w . j a v a 2 s . c o m*/ System.out.println(bytes.length); ClassNode classNode = new ClassNode(); String classNameASM = name.replace('.', '/'); ClassReader classReader = new ClassReader(bytes); classReader.accept(classNode, 0); boolean DoModInit = false; boolean HasInit = false; String initDesc = "()V"; boolean lockDesc = false; try { try { for (int i = 0; i < classNode.visibleAnnotations.size(); i++) { AnnotationNode ann = (AnnotationNode) classNode.visibleAnnotations.get(i); if (ann.desc.equalsIgnoreCase("Lnet/petercashel/jmsDd/module/Module;")) { try { if (debug) System.out.println("ANNOTE!"); DoModInit = true; Map<String, Object> values = asmList2Map(ann.values); ModuleSystem.modulesToLoad.put( values.get("ModuleName").toString().replace("[", "").replace("]", ""), classNode.name.replace("/", ".")); } catch (Exception e) { e.printStackTrace(); } } } } catch (Exception e) { } try { if (DoModInit) { for (int i = 0; i < classNode.methods.size(); i++) { MethodNode m = (MethodNode) classNode.methods.get(i); if (m.name.contentEquals("<init>")) { initDesc = m.desc; if (m.desc.contentEquals("()V")) { HasInit = true; if (debug) System.out.println("Found <init>"); } } } } } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } if (debug) System.out.println("Still alive?"); try { // L0 // LINENUMBER 43 L0 // ALOAD 0 // INVOKESPECIAL // CLASSNAME FOR ASM // ()V ////// This is effectically a super() call but to the discovered constructor // L1 // LINENUMBER 44 L1 // INVOKESTATIC net/petercashel/jmsDd/API/API$Impl.getAPI ()Lnet/petercashel/jmsDd/API/API; // ALOAD 0 // INVOKEINTERFACE net/petercashel/jmsDd/API/API.registerEventBus (Ljava/lang/Object;)V // L2 // LINENUMBER 46 L2 // RETURN // L3 // LOCALVARIABLE this // "L" + CLASSNAME FOR ASM + ";" // L0 L3 0 // LOCALVARIABLE e Lnet/petercashel/jmsDd/event/module/DummyEvent; L0 L3 1 // MAXSTACK = 2 // MAXLOCALS = 2 if (DoModInit) { if (HasInit) { if (debug) System.out.println("Adding Extra Constructor to " + name); MethodNode constructor = new MethodNode(Opcodes.ACC_PUBLIC, "<init>", "(Lnet/petercashel/jmsDd/event/module/DummyEvent;)V", null, null); Label L0 = new Label(); constructor.visitLabel(L0); constructor.visitVarInsn(Opcodes.ALOAD, 0); constructor.visitMethodInsn(Opcodes.INVOKESPECIAL, classNameASM, "<init>", initDesc); Label L1 = new Label(); constructor.visitLabel(L1); constructor.visitMethodInsn(Opcodes.INVOKESTATIC, "net/petercashel/jmsDd/API/API$Impl", "getAPI", "()Lnet/petercashel/jmsDd/API/API;"); constructor.visitVarInsn(Opcodes.ALOAD, 0); constructor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "net/petercashel/jmsDd/API/API", "registerEventBus", "(Ljava/lang/Object;)V"); Label L2 = new Label(); constructor.visitLabel(L2); constructor.visitInsn(Opcodes.RETURN); Label L3 = new Label(); constructor.visitLabel(L3); constructor.visitLocalVariable("this", "L" + classNameASM + ";", null, L0, L3, 0); constructor.visitLocalVariable("e", "Lnet/petercashel/jmsDd/event/module/DummyEvent;", null, L0, L3, 1); constructor.visitMaxs(2, 2); constructor.visitEnd(); classNode.methods.add(constructor); } else { System.err.println("WARNING! " + name + " Doesn't have a default no-args constructor. Module loader cannot chain load the constructor. \n If you are recieving this error and your module has no constructors defined, or a no-args constructor defined, \n please report the bug to the author of JMSDd."); MethodNode constructor = new MethodNode(Opcodes.ACC_PUBLIC, "<init>", "(Lnet/petercashel/jmsDd/event/module/DummyEvent;)V", null, null); Label L0 = new Label(); constructor.visitLabel(L0); constructor.visitVarInsn(Opcodes.ALOAD, 0); //INVOKESPECIAL java/lang/Object.<init> ()V ////// There is no other constructor, call super() to Object. constructor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); Label L1 = new Label(); constructor.visitLabel(L1); constructor.visitMethodInsn(Opcodes.INVOKESTATIC, "net/petercashel/jmsDd/API/API$Impl", "getAPI", "()Lnet/petercashel/jmsDd/API/API;"); constructor.visitVarInsn(Opcodes.ALOAD, 0); constructor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "net/petercashel/jmsDd/API/API", "registerEventBus", "(Ljava/lang/Object;)V"); Label L2 = new Label(); constructor.visitLabel(L2); constructor.visitInsn(Opcodes.RETURN); Label L3 = new Label(); constructor.visitLabel(L3); constructor.visitLocalVariable("this", "L" + classNameASM + ";", null, L0, L3, 0); constructor.visitLocalVariable("e", "Lnet/petercashel/jmsDd/event/module/DummyEvent;", null, L0, L3, 1); constructor.visitMaxs(2, 2); constructor.visitEnd(); classNode.methods.add(constructor); } classNode.visitEnd(); ClassWriter wr = new ClassWriter(0); classNode.accept(wr); bytes = wr.toByteArray(); } } catch (Exception e) { e.printStackTrace(); } if (debug) System.out.println("Still alive."); if (debug) System.out.println(bytes.length); if (plugins.size() > 0) { for (ASMPlugin p : plugins) { try { bytes = p.transform(name, bytes); } catch (Exception e) { } } } return bytes; }
From source file:net.sf.cglib.proxy.TestEnhancer.java
License:Apache License
public void testBridgeParameterCheckcast() throws Exception { // If the compiler used for Z omits the bridge method, and X is compiled with javac, // javac will generate an invokespecial bridge in X. // public interface I<A, B> { // public A f(B b); // }// w w w . java 2s . c om // public abstract class Z<U extends Number> implements I<U, Long> { // public U f(Long id) { // return null; // } // } // public class X extends Z<Integer> {} final Map<String, byte[]> classes = new HashMap<String, byte[]>(); { ClassWriter cw = new ClassWriter(0); cw.visit(49, Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE, "I", "<A:Ljava/lang/Object;B:Ljava/lang/Object;>Ljava/lang/Object;", "java/lang/Object", null); { MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT, "f", "(Ljava/lang/Object;)Ljava/lang/Object;", "(TB;)TA;", null); mv.visitEnd(); } cw.visitEnd(); classes.put("I.class", cw.toByteArray()); } { ClassWriter cw = new ClassWriter(0); cw.visit(49, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER | Opcodes.ACC_ABSTRACT, "Z", "<U:Ljava/lang/Number;>Ljava/lang/Object;LI<TU;Ljava/lang/String;>;", "java/lang/Object", new String[] { "I" }); { MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); } { MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "f", "(Ljava/lang/String;)Ljava/lang/Number;", "(Ljava/lang/String;)TU;", null); mv.visitCode(); mv.visitInsn(Opcodes.ACONST_NULL); mv.visitInsn(Opcodes.ARETURN); mv.visitMaxs(1, 2); mv.visitEnd(); } cw.visitEnd(); classes.put("Z.class", cw.toByteArray()); } { ClassWriter cw = new ClassWriter(0); cw.visit(49, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, "X", "LZ<Ljava/lang/Integer;>;", "Z", null); { MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "Z", "<init>", "()V", false); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); } { MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_BRIDGE | Opcodes.ACC_SYNTHETIC, "f", "(Ljava/lang/Object;)Ljava/lang/Object;", null, null); mv.visitCode(); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/String"); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "Z", "f", "(Ljava/lang/String;)Ljava/lang/Number;", false); mv.visitInsn(Opcodes.ARETURN); mv.visitMaxs(2, 2); mv.visitEnd(); } cw.visitEnd(); classes.put("X.class", cw.toByteArray()); } ClassLoader classLoader = new ClassLoader(getClass().getClassLoader()) { @Override public InputStream getResourceAsStream(String name) { InputStream is = super.getResourceAsStream(name); if (is != null) { return is; } if (classes.containsKey(name)) { return new ByteArrayInputStream(classes.get(name)); } return null; } public Class findClass(String name) throws ClassNotFoundException { byte[] ba = classes.get(name.replace('.', '/') + ".class"); if (ba != null) { return defineClass(name, ba, 0, ba.length); } throw new ClassNotFoundException(name); } }; List<Class> retTypes = new ArrayList<Class>(); List<Class> paramTypes = new ArrayList<Class>(); Interceptor interceptor = new Interceptor(retTypes, paramTypes); Enhancer e = new Enhancer(); e.setClassLoader(classLoader); e.setSuperclass(classLoader.loadClass("X")); e.setCallback(interceptor); Object c = e.create(); for (Method m : c.getClass().getDeclaredMethods()) { if (m.getName().equals("f") && m.getReturnType().equals(Object.class)) { m.invoke(c, new Object[] { null }); } } // f(Object)Object should bridge to f(Number)String assertEquals(Arrays.asList(Object.class, Number.class), retTypes); assertEquals(Arrays.asList(Object.class, String.class), paramTypes); }
From source file:net.sourceforge.cobertura.instrument.FindTouchPointsMethodAdapter.java
License:GNU General Public License
/** * We try to detect such a last 2 instructions and extract the enum signature. *///from w w w . ja v a 2 s.c om private String tryToFindSignatureOfConditionEnum() { // mv.visitMethodInsn(INVOKESTATIC, "net/sourceforge/cobertura/instrument/FindTouchPointsMethodAdapter", "$SWITCH_TABLE$net$sourceforge$cobertura$instrument$FindTouchPointsMethodAdapter$Abc", "()[I"); // mv.visitVarInsn(ALOAD, 1); // mv.visitMethodInsn(INVOKEVIRTUAL, "net/sourceforge/cobertura/instrument/FindTouchPointsMethodAdapter$Abc", "ordinal", "()I"); // mv.visitInsn(IALOAD); if (backlog == null || backlog.size() < 4) return null; int last = backlog.size() - 1; if ((backlog.get(last) instanceof InsnNode) && (backlog.get(last - 1) instanceof MethodInsnNode) && (backlog.get(last - 2) instanceof VarInsnNode)) { VarInsnNode i2 = (VarInsnNode) backlog.get(last - 2); MethodInsnNode i3 = (MethodInsnNode) backlog.get(last - 1); InsnNode i4 = (InsnNode) backlog.get(last); if ((i2.getOpcode() == Opcodes.ALOAD) && (i3.getOpcode() == Opcodes.INVOKEVIRTUAL && i3.name.equals("ordinal")) && (i4.getOpcode() == Opcodes.IALOAD)) { return i3.owner; } } return null; }
From source file:net.sourceforge.cobertura.instrument.pass1.DetectIgnoredCodeMethodVisitor.java
License:GNU General Public License
public void visitVarInsn(int opcode, int i1) { super.visitVarInsn(opcode, i1); if (ignoredStatus.isTrivial() && opcode != Opcodes.ILOAD && opcode != Opcodes.LLOAD && opcode != Opcodes.FLOAD && opcode != Opcodes.DLOAD && opcode != Opcodes.ALOAD) { markNotTrivial();// w w w .ja va 2 s .co m } }
From source file:net.sourceforge.cobertura.instrument.pass3.AbstractCodeProvider.java
License:GNU General Public License
/** * {@inheritDoc}<br/><br/>/*from w w w . j a va2 s.com*/ * <p/> * Generates method (named {@link #COBERTURA_CLASSMAP_METHOD_NAME}) with such a signature: * __cobertura_classmap( {@link LightClassmapListener} listener).</br> * <p/> * The method informs the listener about all lines, jumps and switches found, and about all counters tracking * the constructions. */ public void generateCoberturaClassMapMethod(ClassVisitor cv, ClassMap classMap) { LinkedList<TouchPointDescriptor> touchPointDescriptors = new LinkedList<TouchPointDescriptor>( classMap.getTouchPointsInLineOrder()); int parts = 0; for (int j = 0; touchPointDescriptors.size() > 0; j++) { List<TouchPointDescriptor> bufor = new LinkedList<TouchPointDescriptor>(); for (int i = 0; i < 1000 && touchPointDescriptors.size() > 0; i++) { bufor.add(touchPointDescriptors.removeFirst()); } classMapContent(cv, j, bufor); parts++; } MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, COBERTURA_CLASSMAP_METHOD_NAME, "(" + Type.getType(LightClassmapListener.class).toString() + ")V", null, null); mv.visitCode(); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitInsn(Opcodes.DUP); mv.visitLdcInsn(classMap.getClassName()); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, CLASSMAP_LISTENER_INTERNALNAME, "setClazz", "(Ljava/lang/String;)V"); if (classMap.getSource() != null) { mv.visitInsn(Opcodes.DUP); mv.visitLdcInsn(classMap.getSource()); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, CLASSMAP_LISTENER_INTERNALNAME, "setSource", "(Ljava/lang/String;)V"); } for (int i = 0; i < parts; i++) { mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESTATIC, classMap.getClassName(), COBERTURA_CLASSMAP_METHOD_NAME + "_" + i, "(" + Type.getType(LightClassmapListener.class).toString() + ")V"); } mv.visitInsn(Opcodes.POP); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(0, 0);//will be recalculated by writer mv.visitEnd(); }
From source file:net.sourceforge.cobertura.instrument.pass3.AbstractCodeProvider.java
License:GNU General Public License
private void classMapContent(ClassVisitor cv, int nr, List<TouchPointDescriptor> touchPointDescriptors) { MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, COBERTURA_CLASSMAP_METHOD_NAME + "_" + nr, "(" + Type.getType(LightClassmapListener.class).toString() + ")V", null, null); mv.visitCode();/*from w w w.j a v a 2 s .c o m*/ mv.visitVarInsn(Opcodes.ALOAD, 0); for (TouchPointDescriptor tpd : touchPointDescriptors) { mv.visitInsn(Opcodes.DUP); mv.visitLdcInsn(tpd.getLineNumber()); if (tpd instanceof LineTouchPointDescriptor) { mv.visitLdcInsn(((LineTouchPointDescriptor) tpd).getCounterId()); mv.visitLdcInsn(((LineTouchPointDescriptor) tpd).getMethodName()); mv.visitLdcInsn(((LineTouchPointDescriptor) tpd).getMethodSignature()); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, CLASSMAP_LISTENER_INTERNALNAME, "putLineTouchPoint", "(IILjava/lang/String;Ljava/lang/String;)V"); } else if (tpd instanceof JumpTouchPointDescriptor) { mv.visitLdcInsn(((JumpTouchPointDescriptor) tpd).getCounterIdForTrue()); mv.visitLdcInsn(((JumpTouchPointDescriptor) tpd).getCounterIdForFalse()); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, CLASSMAP_LISTENER_INTERNALNAME, "putJumpTouchPoint", "(III)V"); } else if (tpd instanceof SwitchTouchPointDescriptor) { SwitchTouchPointDescriptor stpd = (SwitchTouchPointDescriptor) tpd; final String enum_sign = ((SwitchTouchPointDescriptor) tpd).getEnumType(); if (enum_sign == null) { mv.visitLdcInsn(Integer.MAX_VALUE); } else { mv.visitMethodInsn(Opcodes.INVOKESTATIC, enum_sign, "values", "()[L" + enum_sign + ";"); mv.visitInsn(Opcodes.ARRAYLENGTH); } Collection<Integer> ci = stpd.getCountersForLabels(); mv.visitLdcInsn(ci.size());//Size of a new table mv.visitIntInsn(Opcodes.NEWARRAY, Opcodes.T_INT); int i = 0; for (Integer counterId : ci) { mv.visitInsn(Opcodes.DUP); //First for addition of items, second ad putSwitchTouchPoint parameter (or next loop iteration) mv.visitLdcInsn(i); mv.visitLdcInsn(counterId); mv.visitInsn(Opcodes.IASTORE); i++; } mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, CLASSMAP_LISTENER_INTERNALNAME, "putSwitchTouchPoint", "(II[I)V"); } } mv.visitInsn(Opcodes.POP); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(0, 0);//will be recalculated by writer mv.visitEnd(); }
From source file:net.sourceforge.cobertura.instrument.pass3.AtomicArrayCodeProvider.java
License:GNU General Public License
/** * <pre>/*from ww w. ja va 2s.c om*/ * int[] __cobertura_get_and_reset_counters() { * int[] res = new int[counters.length()]; * for(int i=0; i<counters.length(); i++){ * res[i]=counters.getAndSet(i, 0); * } * return res; * } * </pre> */ public void generateCoberturaGetAndResetCountersMethod(ClassVisitor cv, String className) { MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, COBERTURA_GET_AND_RESET_COUNTERS_METHOD_NAME, "()[I", null, null); mv.visitCode(); mv.visitFieldInsn(Opcodes.GETSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME, COBERTURA_COUNTERS_FIELD_TYPE); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/concurrent/atomic/AtomicIntegerArray", "length", "()I"); mv.visitIntInsn(Opcodes.NEWARRAY, Opcodes.T_INT); mv.visitVarInsn(Opcodes.ASTORE, 0); mv.visitInsn(Opcodes.ICONST_0); mv.visitVarInsn(Opcodes.ISTORE, 1); Label l3 = new Label(); mv.visitJumpInsn(Opcodes.GOTO, l3); Label l4 = new Label(); mv.visitLabel(l4); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitVarInsn(Opcodes.ILOAD, 1); mv.visitFieldInsn(Opcodes.GETSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME, COBERTURA_COUNTERS_FIELD_TYPE); mv.visitVarInsn(Opcodes.ILOAD, 1); mv.visitInsn(Opcodes.ICONST_0); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/concurrent/atomic/AtomicIntegerArray", "getAndSet", "(II)I"); mv.visitInsn(Opcodes.IASTORE); mv.visitIincInsn(1, 1); mv.visitLabel(l3); mv.visitVarInsn(Opcodes.ILOAD, 1); mv.visitFieldInsn(Opcodes.GETSTATIC, className, COBERTURA_COUNTERS_FIELD_NAME, COBERTURA_COUNTERS_FIELD_TYPE); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/concurrent/atomic/AtomicIntegerArray", "length", "()I"); mv.visitJumpInsn(Opcodes.IF_ICMPLT, l4); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitInsn(Opcodes.ARETURN); mv.visitMaxs(0, 0);//will be recalculated by writer mv.visitEnd(); }