List of usage examples for org.objectweb.asm.tree InsnList InsnList
InsnList
From source file:org.evosuite.instrumentation.mutation.ReplaceVariable.java
License:Open Source License
/** * Generates the instructions to cast a numerical value from one type to * another./*from w ww . j a va2s. com*/ * * @param from * the type of the top stack value * @param to * the type into which this value must be cast. * @return a {@link org.objectweb.asm.tree.InsnList} object. */ public static InsnList cast(final Type from, final Type to) { InsnList list = new InsnList(); if (from != to) { if (from == Type.DOUBLE_TYPE) { if (to == Type.FLOAT_TYPE) { list.add(new InsnNode(Opcodes.D2F)); } else if (to == Type.LONG_TYPE) { list.add(new InsnNode(Opcodes.D2L)); } else { list.add(new InsnNode(Opcodes.D2I)); list.add(cast(Type.INT_TYPE, to)); } } else if (from == Type.FLOAT_TYPE) { if (to == Type.DOUBLE_TYPE) { list.add(new InsnNode(Opcodes.F2D)); } else if (to == Type.LONG_TYPE) { list.add(new InsnNode(Opcodes.F2L)); } else { list.add(new InsnNode(Opcodes.F2I)); list.add(cast(Type.INT_TYPE, to)); } } else if (from == Type.LONG_TYPE) { if (to == Type.DOUBLE_TYPE) { list.add(new InsnNode(Opcodes.L2D)); } else if (to == Type.FLOAT_TYPE) { list.add(new InsnNode(Opcodes.L2F)); } else { list.add(new InsnNode(Opcodes.L2I)); list.add(cast(Type.INT_TYPE, to)); } } else { if (to == Type.BYTE_TYPE) { list.add(new InsnNode(Opcodes.I2B)); } else if (to == Type.CHAR_TYPE) { list.add(new InsnNode(Opcodes.I2C)); } else if (to == Type.DOUBLE_TYPE) { list.add(new InsnNode(Opcodes.I2D)); } else if (to == Type.FLOAT_TYPE) { list.add(new InsnNode(Opcodes.I2F)); } else if (to == Type.LONG_TYPE) { list.add(new InsnNode(Opcodes.I2L)); } else if (to == Type.SHORT_TYPE) { list.add(new InsnNode(Opcodes.I2S)); } } } return list; }
From source file:org.evosuite.instrumentation.testability.transformer.BooleanCallsTransformer.java
License:Open Source License
@Override protected AbstractInsnNode transformMethodInsnNode(MethodNode mn, MethodInsnNode methodNode) { if (methodNode.owner.equals(Type.getInternalName(BooleanHelper.class))) return methodNode; methodNode.desc = this.booleanTestabilityTransformation.transformMethodDescriptor(methodNode.owner, methodNode.name, methodNode.desc); methodNode.name = DescriptorMapping.getInstance().getMethodName(methodNode.owner, methodNode.name, methodNode.desc);//from w w w .ja v a 2 s .co m if (DescriptorMapping.getInstance().isBooleanMethod(methodNode.desc)) { BooleanTestabilityTransformation.logger.info("Method needs value transformation: " + methodNode.name); if (DescriptorMapping.getInstance().hasBooleanParameters(methodNode.desc)) { BooleanTestabilityTransformation.logger .info("Method needs parameter transformation: " + methodNode.name); TransformationStatistics.transformBackToBooleanParameter(); int firstBooleanParameterIndex = -1; Type[] types = Type.getArgumentTypes(methodNode.desc); for (int i = 0; i < types.length; i++) { if (types[i].getDescriptor().equals("Z")) { if (firstBooleanParameterIndex == -1) { firstBooleanParameterIndex = i; break; } } } if (firstBooleanParameterIndex != -1) { int numOfPushs = types.length - 1 - firstBooleanParameterIndex; // int numOfPushs = types.length - firstBooleanParameterIndex; if (numOfPushs == 0) { if (!(methodNode.getPrevious().getOpcode() == Opcodes.ICONST_1 || methodNode.getPrevious().getOpcode() == Opcodes.ICONST_0)) { //the boolean parameter is the last parameter MethodInsnNode booleanHelperInvoke = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "intToBoolean", Type.getMethodDescriptor(Type.BOOLEAN_TYPE, new Type[] { Type.INT_TYPE })); mn.instructions.insertBefore(methodNode, booleanHelperInvoke); } } else { InsnList insnlist = new InsnList(); for (int i = 0; i < numOfPushs; i++) { MethodInsnNode booleanHelperPushParameter; if (types[types.length - 1 - i] == Type.BOOLEAN_TYPE || types[types.length - 1 - i] == Type.CHAR_TYPE || types[types.length - 1 - i] == Type.BYTE_TYPE || types[types.length - 1 - i] == Type.SHORT_TYPE || types[types.length - 1 - i] == Type.INT_TYPE || types[types.length - 1 - i] == Type.FLOAT_TYPE || types[types.length - 1 - i] == Type.LONG_TYPE || types[types.length - 1 - i] == Type.DOUBLE_TYPE) { if (types[types.length - 1 - i] == Type.BOOLEAN_TYPE) { booleanHelperPushParameter = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "pushParameter", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.INT_TYPE })); } else { booleanHelperPushParameter = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "pushParameter", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { types[types.length - 1 - i] })); } } else { booleanHelperPushParameter = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "pushParameter", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(Object.class) })); } insnlist.add(booleanHelperPushParameter); } for (int i = firstBooleanParameterIndex; i < types.length; i++) { if (i == firstBooleanParameterIndex) { MethodInsnNode booleanHelperInvoke = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "intToBoolean", Type.getMethodDescriptor(Type.BOOLEAN_TYPE, new Type[] { Type.INT_TYPE })); insnlist.add(booleanHelperInvoke); } else { MethodInsnNode booleanHelperPopParameter; boolean objectNeedCast = false; if (types[i] == Type.BOOLEAN_TYPE) { booleanHelperPopParameter = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "popParameterBooleanFromInt", Type.getMethodDescriptor(types[i], new Type[] {})); } else if (types[i] == Type.CHAR_TYPE) { booleanHelperPopParameter = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "popParameterChar", Type.getMethodDescriptor(types[i], new Type[] {})); } else if (types[i] == Type.BYTE_TYPE) { booleanHelperPopParameter = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "popParameterByte", Type.getMethodDescriptor(types[i], new Type[] {})); } else if (types[i] == Type.SHORT_TYPE) { booleanHelperPopParameter = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "popParameterShort", Type.getMethodDescriptor(types[i], new Type[] {})); } else if (types[i] == Type.INT_TYPE) { booleanHelperPopParameter = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "popParameterInt", Type.getMethodDescriptor(types[i], new Type[] {})); } else if (types[i] == Type.FLOAT_TYPE) { booleanHelperPopParameter = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "popParameterFloat", Type.getMethodDescriptor(types[i], new Type[] {})); } else if (types[i] == Type.LONG_TYPE) { booleanHelperPopParameter = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "popParameterLong", Type.getMethodDescriptor(types[i], new Type[] {})); } else if (types[i] == Type.DOUBLE_TYPE) { booleanHelperPopParameter = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "popParameterDouble", Type.getMethodDescriptor(types[i], new Type[] {})); } else { objectNeedCast = true; booleanHelperPopParameter = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "popParameterObject", Type.getMethodDescriptor(Type.getType(Object.class), new Type[] {})); } insnlist.add(booleanHelperPopParameter); if (objectNeedCast) { TypeInsnNode tin = new TypeInsnNode(Opcodes.CHECKCAST, types[i].getInternalName()); insnlist.add(tin); } } } mn.instructions.insertBefore(methodNode, insnlist); } } } if (Type.getReturnType(methodNode.desc).equals(Type.BOOLEAN_TYPE)) { BooleanTestabilityTransformation.logger .info("Method needs return transformation: " + methodNode.name); TransformationStatistics.transformBackToBooleanParameter(); MethodInsnNode n = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "booleanToInt", Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { Type.BOOLEAN_TYPE })); mn.instructions.insert(methodNode, n); return n; } } else { BooleanTestabilityTransformation.logger.info("Method needs no transformation: " + methodNode.name); } // TODO: If this is a method that is not transformed, and it requires a Boolean parameter // then we need to convert this boolean back to an int // For example, we could use flow analysis to determine the point where the value is added to the stack // and insert a conversion function there return methodNode; }
From source file:org.evosuite.instrumentation.testability.transformer.BooleanIfTransformer.java
License:Open Source License
@Override protected AbstractInsnNode transformJumpInsnNode(MethodNode mn, JumpInsnNode jumpNode) { if (jumpNode.getOpcode() == Opcodes.IFNE) { if (this.booleanTestabilityTransformation.isBooleanOnStack(mn, jumpNode, 0)) { TransformationStatistics.transformedBooleanComparison(); BooleanTestabilityTransformation.logger.info("Changing IFNE"); jumpNode.setOpcode(Opcodes.IFGT); } else {/*from ww w . jav a 2 s .com*/ BooleanTestabilityTransformation.logger.info("Not changing IFNE"); int insnPosition = mn.instructions.indexOf(jumpNode); Frame frame = this.booleanTestabilityTransformation.currentFrames[insnPosition]; AbstractInsnNode insn = mn.instructions.get(insnPosition - 1); BooleanTestabilityTransformation.logger.info("Current node: " + mn.instructions.get(insnPosition)); BooleanTestabilityTransformation.logger.info("Previous node: " + insn); if (insn instanceof MethodInsnNode) { MethodInsnNode mi = (MethodInsnNode) insn; if (Type.getReturnType(DescriptorMapping.getInstance().getMethodDesc(mi.owner, mi.name, mi.desc)) == Type.BOOLEAN_TYPE) { BooleanTestabilityTransformation.logger.info("Changing IFNE"); jumpNode.setOpcode(Opcodes.IFGT); } BooleanTestabilityTransformation.logger.info("Method: " + mi.name); } BooleanTestabilityTransformation.logger.info("Stack size: " + frame.getStackSize()); //logger.info("Top of stack: " + frame.getStack(0)); for (int i = 0; i < frame.getStackSize(); i++) { BooleanTestabilityTransformation.logger.info(i + " Stack: " + frame.getStack(i)); } } } else if (jumpNode.getOpcode() == Opcodes.IFEQ) { if (this.booleanTestabilityTransformation.isBooleanOnStack(mn, jumpNode, 0)) { TransformationStatistics.transformedBooleanComparison(); BooleanTestabilityTransformation.logger.info("Changing IFEQ"); jumpNode.setOpcode(Opcodes.IFLE); } else { BooleanTestabilityTransformation.logger.info("Not changing IFEQ"); int insnPosition = mn.instructions.indexOf(jumpNode); Frame frame = this.booleanTestabilityTransformation.currentFrames[insnPosition]; AbstractInsnNode insn = mn.instructions.get(insnPosition - 1); BooleanTestabilityTransformation.logger.info("Previous node: " + insn); if (insn instanceof MethodInsnNode) { MethodInsnNode mi = (MethodInsnNode) insn; BooleanTestabilityTransformation.logger.info("Method: " + mi.name); if (Type.getReturnType(BooleanTestabilityTransformation.getOriginalDesc(mi.owner, mi.name, mi.desc)) == Type.BOOLEAN_TYPE) { BooleanTestabilityTransformation.logger.info("Changing IFEQ"); jumpNode.setOpcode(Opcodes.IFLE); } else { BooleanTestabilityTransformation.logger.info("Return type: " + Type.getReturnType( BooleanTestabilityTransformation.getOriginalDesc(mi.owner, mi.name, mi.desc))); } } BooleanTestabilityTransformation.logger.info("Stack size: " + frame.getStackSize()); for (int i = 0; i < frame.getStackSize(); i++) { BooleanTestabilityTransformation.logger.info(i + " Stack: " + frame.getStack(i)); } } } else if (jumpNode.getOpcode() == Opcodes.IF_ICMPEQ) { if (this.booleanTestabilityTransformation.isBooleanOnStack(mn, jumpNode, 0)) { InsnList convert = new InsnList(); convert.add(new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "pushParameter", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.INT_TYPE }))); convert.add(new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "pushParameter", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.INT_TYPE }))); convert.add(new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "popParameterBooleanFromInt", Type.getMethodDescriptor(Type.BOOLEAN_TYPE, new Type[] {}))); convert.add(new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "popParameterBooleanFromInt", Type.getMethodDescriptor(Type.BOOLEAN_TYPE, new Type[] {}))); mn.instructions.insertBefore(jumpNode, convert); TransformationStatistics.transformedBooleanComparison(); } } else if (jumpNode.getOpcode() == Opcodes.IF_ICMPNE) { if (this.booleanTestabilityTransformation.isBooleanOnStack(mn, jumpNode, 0)) { InsnList convert = new InsnList(); convert.add(new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "pushParameter", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.INT_TYPE }))); convert.add(new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "pushParameter", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.INT_TYPE }))); convert.add(new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "popParameterBooleanFromInt", Type.getMethodDescriptor(Type.BOOLEAN_TYPE, new Type[] {}))); convert.add(new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "popParameterBooleanFromInt", Type.getMethodDescriptor(Type.BOOLEAN_TYPE, new Type[] {}))); mn.instructions.insertBefore(jumpNode, convert); TransformationStatistics.transformedBooleanComparison(); } } return jumpNode; }
From source file:org.evosuite.instrumentation.testability.transformer.ImplicitElseTransformer.java
License:Open Source License
private void handleDependency(ControlDependency dependency, ControlDependenceGraph cdg, MethodNode mn, FieldInsnNode varNode, BytecodeInstruction parentLevel) { if (addedNodes.contains(dependency)) return;//w w w .j a v a2 s . co m // Get the basic blocks reachable if the dependency would evaluate different Set<BasicBlock> blocks = cdg.getAlternativeBlocks(dependency); addedNodes.add(dependency); Set<ControlDependency> dependencies = dependency.getBranch().getInstruction().getControlDependencies(); //if (dependencies.size() == 1) { // ControlDependency dep = dependencies.iterator().next(); for (ControlDependency dep : dependencies) { if (!addedNodes.contains(dep) && dep != dependency) handleDependency(dep, cdg, mn, varNode, dependency.getBranch().getInstruction()); } // TODO: Need to check that there is an assignment in every alternative path through CDG boolean hasAssignment = false; for (BasicBlock block : blocks) { // If this block also assigns a value to the same variable for (BytecodeInstruction instruction : block) { if (instruction.getASMNode().getOpcode() == Opcodes.PUTFIELD || instruction.getASMNode().getOpcode() == Opcodes.PUTSTATIC) { FieldInsnNode otherFieldNode = (FieldInsnNode) instruction.getASMNode(); FieldInsnNode thisFieldNode = varNode; if (otherFieldNode.owner.equals(thisFieldNode.owner) && otherFieldNode.name.equals(thisFieldNode.name)) { hasAssignment = true; break; } } } if (hasAssignment) { break; } } // The Flag assignment is is the dependency evaluates to the given value // We thus need to insert the tautoligical assignment either directly after the IF (if the value is true) // or before the jump target (if the value is false) if (!hasAssignment) { if (dependency.getBranch().getInstruction().isSwitch()) { BooleanTestabilityTransformation.logger.warn("Don't know how to handle Switches yet"); return; } TransformationStatistics.transformedImplicitElse(); JumpInsnNode jumpNode = (JumpInsnNode) dependency.getBranch().getInstruction().getASMNode(); FieldInsnNode newLoad = new FieldInsnNode( varNode.getOpcode() == Opcodes.PUTSTATIC ? Opcodes.GETSTATIC : Opcodes.GETFIELD, varNode.owner, varNode.name, varNode.desc); FieldInsnNode newStore = new FieldInsnNode(varNode.getOpcode(), varNode.owner, varNode.name, varNode.desc); AbstractInsnNode newOwnerLoad1 = null; AbstractInsnNode newOwnerLoad2 = null; if (varNode.getOpcode() == Opcodes.PUTFIELD) { // Need to copy the bloody owner // Check for VarInsn //if (varNode.getPrevious().getOpcode() == Opcodes.ALOAD) { newOwnerLoad1 = new VarInsnNode(Opcodes.ALOAD, 0); newOwnerLoad2 = new VarInsnNode(Opcodes.ALOAD, 0); /* } else { // Else use helper function // Insert DUP and logger.info("Wargh"); System.exit(0); fieldOwnerId++; InsnNode dupNode = new InsnNode(Opcodes.DUP); mn.instructions.insertBefore(varNode, new LdcInsnNode( fieldOwnerId)); mn.instructions.insertBefore(varNode, dupNode); registerInstruction(mn, varNode, dupNode); MethodInsnNode storeOwner = new MethodInsnNode( Opcodes.INVOKESTATIC, "org/evosuite/instrumentation/BooleanHelper", "setFieldOwner", "(ILjava/lang/Object;)V"); mn.instructions.insertBefore(varNode, storeOwner); registerInstruction(mn, varNode, storeOwner); newOwnerLoad1 = new MethodInsnNode(Opcodes.INVOKESTATIC, "org/evosuite/instrumentation/BooleanHelper", "getFieldOwner", "(I)Ljava/lang/Object;"); newOwnerLoad2 = new MethodInsnNode(Opcodes.INVOKESTATIC, "org/evosuite/instrumentation/BooleanHelper", "getFieldOwner", "(I)Ljava/lang/Object;"); } */ } if (dependency.getBranchExpressionValue()) { BooleanTestabilityTransformation.logger.info("Inserting after if"); // Insert directly after if mn.instructions.insert(jumpNode, newStore); mn.instructions.insert(jumpNode, newLoad); if (newOwnerLoad1 != null) { mn.instructions.insert(jumpNode, newOwnerLoad1); registerInstruction(mn, varNode, newOwnerLoad1); } if (newOwnerLoad2 != null) { mn.instructions.insert(jumpNode, newOwnerLoad2); registerInstruction(mn, varNode, newOwnerLoad2); } registerInstruction(mn, varNode, newStore); registerInstruction(mn, varNode, newLoad); } else { BooleanTestabilityTransformation.logger.info("Inserting as jump target"); // Insert as jump target LabelNode target = jumpNode.label; LabelNode newTarget = new LabelNode(new Label()); registerInstruction(mn, target, newStore); registerInstruction(mn, target, newLoad); InsnList assignment = new InsnList(); assignment.add(new JumpInsnNode(Opcodes.GOTO, target)); assignment.add(newTarget); if (newOwnerLoad1 != null) { assignment.add(newOwnerLoad1); registerInstruction(mn, target, newOwnerLoad1); } if (newOwnerLoad2 != null) { assignment.add(newOwnerLoad2); registerInstruction(mn, target, newOwnerLoad2); } assignment.add(newLoad); assignment.add(newStore); jumpNode.label = newTarget; mn.instructions.insertBefore(target, assignment); } addedInsns.add(newStore); addedInsns.add(newLoad); } }
From source file:org.evosuite.instrumentation.testability.transformer.ImplicitElseTransformer.java
License:Open Source License
private void handleDependency(ControlDependency dependency, ControlDependenceGraph cdg, MethodNode mn, VarInsnNode varNode, BytecodeInstruction parentLevel) { if (addedNodes.contains(dependency)) return;/*from w w w. j a v a 2s.c o m*/ // Get the basic blocks reachable if the dependency would evaluate different Set<BasicBlock> blocks = cdg.getAlternativeBlocks(dependency); addedNodes.add(dependency); Set<ControlDependency> dependencies = dependency.getBranch().getInstruction().getControlDependencies(); //if (dependencies.size() == 1) { // ControlDependency dep = dependencies.iterator().next(); for (ControlDependency dep : dependencies) { if (!addedNodes.contains(dep) && dep != dependency) handleDependency(dep, cdg, mn, varNode, dependency.getBranch().getInstruction()); } // TODO: Need to check that there is an assignment in every alternative path through CDG boolean hasAssignment = false; for (BasicBlock block : blocks) { // If this block also assigns a value to the same variable for (BytecodeInstruction instruction : block) { if (instruction.getASMNode().getOpcode() == Opcodes.ISTORE) { VarInsnNode otherVarNode = (VarInsnNode) instruction.getASMNode(); VarInsnNode thisVarNode = varNode; if (otherVarNode.var == thisVarNode.var) { hasAssignment = true; break; } } } if (hasAssignment) { break; } } // The Flag assignment is is the dependency evaluates to the given value // We thus need to insert the tautoligical assignment either directly after the IF (if the value is true) // or before the jump target (if the value is false) if (!hasAssignment) { TransformationStatistics.transformedImplicitElse(); if (dependency.getBranch().getInstruction().isSwitch()) { BooleanTestabilityTransformation.logger.warn("Don't know how to handle Switches yet"); return; } JumpInsnNode jumpNode = (JumpInsnNode) dependency.getBranch().getInstruction().getASMNode(); VarInsnNode newStore = new VarInsnNode(Opcodes.ISTORE, varNode.var); VarInsnNode newLoad = new VarInsnNode(Opcodes.ILOAD, varNode.var); if (dependency.getBranchExpressionValue()) { BooleanTestabilityTransformation.logger.info("Inserting else branch directly after if"); // Insert directly after if if (isDefinedBefore(mn, varNode, jumpNode)) { mn.instructions.insert(jumpNode, newStore); mn.instructions.insert(jumpNode, newLoad); registerInstruction(mn, varNode, newStore); registerInstruction(mn, varNode, newLoad); } } else { BooleanTestabilityTransformation.logger.info("Inserting else branch as jump target"); // Insert as jump target if (isDefinedBefore(mn, varNode, jumpNode)) { LabelNode target = jumpNode.label; LabelNode newTarget = new LabelNode(new Label()); // jumpNode or target? registerInstruction(mn, jumpNode.getNext(), newStore); registerInstruction(mn, jumpNode.getNext(), newLoad); InsnList assignment = new InsnList(); assignment.add(new JumpInsnNode(Opcodes.GOTO, target)); assignment.add(newTarget); assignment.add(newLoad); assignment.add(newStore); jumpNode.label = newTarget; mn.instructions.insertBefore(target, assignment); } } } }
From source file:org.evosuite.testcarver.instrument.Instrumenter.java
License:Open Source License
@SuppressWarnings("unchecked") private void addFieldRegistryRegisterCall(final MethodNode methodNode) { AbstractInsnNode ins = null;// www.j av a 2s . c o m ListIterator<AbstractInsnNode> iter = methodNode.instructions.iterator(); int numInvokeSpecials = 0; // number of invokespecial calls before actual constructor call while (iter.hasNext()) { ins = iter.next(); if (ins instanceof MethodInsnNode) { MethodInsnNode mins = (MethodInsnNode) ins; if (ins.getOpcode() == Opcodes.INVOKESPECIAL) { if (mins.name.startsWith("<init>")) { if (numInvokeSpecials == 0) { break; } else { numInvokeSpecials--; } } } } else if (ins instanceof TypeInsnNode) { TypeInsnNode typeIns = (TypeInsnNode) ins; if (typeIns.getOpcode() == Opcodes.NEW || typeIns.getOpcode() == Opcodes.NEWARRAY) { numInvokeSpecials++; } } } final InsnList instructions = new InsnList(); instructions.add(new VarInsnNode(Opcodes.ALOAD, 0)); instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(FieldRegistry.class), "register", "(Ljava/lang/Object;)V")); methodNode.instructions.insert(ins, instructions); }
From source file:org.evosuite.testcarver.instrument.Instrumenter.java
License:Open Source License
private void instrumentGETXXXFieldAccesses(final ClassNode cn, final String internalClassName, final MethodNode methodNode) { final InsnList instructions = methodNode.instructions; AbstractInsnNode ins = null;/*from w w w .ja v a2 s . c o m*/ FieldInsnNode fieldIns = null; for (int i = 0; i < instructions.size(); i++) { ins = instructions.get(i); if (ins instanceof FieldInsnNode) { fieldIns = (FieldInsnNode) ins; /* * Is field referencing outermost instance? if yes, ignore it * http://tns-www.lcs.mit.edu/manuals/java-1.1.1/guide/innerclasses/spec/innerclasses.doc10.html */ if (fieldIns.name.endsWith("$0")) { continue; } final int opcode = ins.getOpcode(); if (opcode == Opcodes.GETFIELD || opcode == Opcodes.GETSTATIC) { final InsnList il = new InsnList(); if (opcode == Opcodes.GETFIELD) { Type fieldType = Type.getType(fieldIns.desc); if (fieldType.getSize() == 1) { instructions.insertBefore(fieldIns, new InsnNode(Opcodes.DUP)); il.add(new InsnNode(Opcodes.SWAP)); } else if (fieldType.getSize() == 2) { instructions.insertBefore(fieldIns, new InsnNode(Opcodes.DUP)); // v // GETFIELD // v, w il.add(new InsnNode(Opcodes.DUP2_X1)); // w, v, w il.add(new InsnNode(Opcodes.POP2)); // w, v // -> Call // w } } else il.add(new InsnNode(Opcodes.ACONST_NULL)); il.add(new LdcInsnNode(this.captureId)); il.add(new LdcInsnNode(fieldIns.owner)); il.add(new LdcInsnNode(fieldIns.name)); il.add(new LdcInsnNode(fieldIns.desc)); il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(org.evosuite.testcarver.capture.FieldRegistry.class), "notifyReadAccess", "(Ljava/lang/Object;ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V")); i += il.size(); instructions.insert(fieldIns, il); this.captureId++; } } } }
From source file:org.evosuite.testcarver.instrument.Instrumenter.java
License:Open Source License
private void instrumentPUTXXXFieldAccesses(final ClassNode cn, final String internalClassName, final MethodNode methodNode) { final InsnList instructions = methodNode.instructions; AbstractInsnNode ins = null;/* www. j av a 2 s . c om*/ FieldInsnNode fieldIns = null; // needed get right receiver var in case of PUTFIELD for (int i = 0; i < instructions.size(); i++) { ins = instructions.get(i); if (ins instanceof FieldInsnNode) { fieldIns = (FieldInsnNode) ins; /* * Is field referencing outermost instance? if yes, ignore it * http://tns-www.lcs.mit.edu/manuals/java-1.1.1/guide/innerclasses/spec/innerclasses.doc10.html */ if (fieldIns.name.endsWith("$0")) { continue; } final int opcode = ins.getOpcode(); if (opcode == Opcodes.PUTFIELD || opcode == Opcodes.PUTSTATIC) { // construction of // Capturer.capture(final Object receiver, final String methodName, final Object[] methodParams) // call final InsnList il = new InsnList(); if (opcode == Opcodes.PUTFIELD) { Type fieldType = Type.getType(fieldIns.desc); if (fieldType.getSize() == 1) { instructions.insertBefore(fieldIns, new InsnNode(Opcodes.DUP2)); il.add(new InsnNode(Opcodes.POP)); } else if (fieldType.getSize() == 2) { InsnList uglyList = new InsnList(); // v, w uglyList.add(new InsnNode(Opcodes.DUP2_X1)); // w, v, w uglyList.add(new InsnNode(Opcodes.POP2)); // w, v uglyList.add(new InsnNode(Opcodes.DUP)); // w, v, v uglyList.add(new InsnNode(Opcodes.DUP2_X2)); // v, v, w, v, v uglyList.add(new InsnNode(Opcodes.POP2)); // v, v, w instructions.insertBefore(fieldIns, uglyList); // PUTFIELD // v } } else il.add(new InsnNode(Opcodes.ACONST_NULL)); il.add(new LdcInsnNode(this.captureId)); il.add(new LdcInsnNode(fieldIns.owner)); il.add(new LdcInsnNode(fieldIns.name)); il.add(new LdcInsnNode(fieldIns.desc)); il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(FieldRegistry.class), "notifyModification", "(Ljava/lang/Object;ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V")); // PUTFIELDRegistry.notifyModification also adds corresponding GETFIELD capture instructions this.captureId++; i += il.size(); instructions.insert(fieldIns, il); this.captureId++; } } } }
From source file:org.evosuite.testcarver.instrument.Instrumenter.java
License:Open Source License
private InsnList addCaptureCall(final boolean isStatic, final String internalClassName, final String methodName, final String methodDesc, final Type[] argTypes) { // construction of // Capturer.capture(final Object receiver, final String methodName, final Object[] methodParams) // call//from w w w . j ava2 s .c om final InsnList il = new InsnList(); il.add(new LdcInsnNode(this.captureId)); // --- load receiver argument int varIndex; if (isStatic) { // static method invocation il.add(new LdcInsnNode(internalClassName)); il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(CaptureUtil.class), "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;")); varIndex = 0; } else { // non-static method call il.add(new VarInsnNode(Opcodes.ALOAD, 0)); varIndex = 1; } // --- load method name argument il.add(new LdcInsnNode(methodName)); // --- load method description argument il.add(new LdcInsnNode(methodDesc)); // --- load methodParams arguments // load methodParams length // TODO ICONST_1 to ICONST_5 would be more efficient il.add(new IntInsnNode(Opcodes.BIPUSH, argTypes.length)); // create array object il.add(new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Object")); // fill the array for (int i = 0; i < argTypes.length; i++) { il.add(new InsnNode(Opcodes.DUP)); // TODO ICONST_1 to ICONST_5 would be more efficient il.add(new IntInsnNode(Opcodes.BIPUSH, i)); //check for primitives this.loadAndConvertToObject(il, argTypes[i], varIndex++); il.add(new InsnNode(Opcodes.AASTORE)); // long/double take two registers if (argTypes[i].equals(Type.LONG_TYPE) || argTypes[i].equals(Type.DOUBLE_TYPE)) { varIndex++; } } // --- construct Capture.capture() call il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(org.evosuite.testcarver.capture.Capturer.class), "capture", "(ILjava/lang/Object;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V")); return il; }
From source file:org.jacoco.core.internal.analysis.MethodCoverageCalculatorTest.java
License:Open Source License
@Before public void setup() { instructions = new HashMap<AbstractInsnNode, Instruction>(); coverage = new MethodCoverageImpl("run", "()V", null); list = new InsnList(); }