List of usage examples for org.objectweb.asm Opcodes PUTFIELD
int PUTFIELD
To view the source code for org.objectweb.asm Opcodes PUTFIELD.
Click Source Link
From source file:org.evosuite.instrumentation.PurityAnalysisMethodVisitor.java
License:Open Source License
/** {@inheritDoc} */ @Override// w ww. j a v a 2 s. c o m public void visitFieldInsn(int opcode, String owner, String name, String desc) { if (opcode == Opcodes.PUTSTATIC || opcode == Opcodes.PUTFIELD) { updatesField = true; } super.visitFieldInsn(opcode, owner, name, desc); }
From source file:org.evosuite.instrumentation.RemoveFinalMethodAdapter.java
License:Open Source License
/** {@inheritDoc} */ @Override//from ww w . j av a 2s. co m public void visitFieldInsn(int opcode, String owner, String name, String desc) { if ((opcode == Opcodes.PUTFIELD || opcode == Opcodes.PUTSTATIC) && owner.equals(className)) { if (!finalFields.contains(name)) { //System.out.println("Keeping non-final field " + name + " in class " // + owner); super.visitFieldInsn(opcode, owner, name, desc); } else { //System.out.println("Omitting final field " + name + " in class " + owner); Type type = Type.getType(desc); if (type.getSize() == 1) super.visitInsn(Opcodes.POP); else if (type.getSize() == 2) super.visitInsn(Opcodes.POP2); if (opcode == Opcodes.PUTFIELD) super.visitInsn(Opcodes.POP); } } else { //if (!owner.equals(className)) // System.out.println("Mismatch: " + className + " / " + owner); super.visitFieldInsn(opcode, owner, name, desc); } }
From source file:org.evosuite.instrumentation.testability.BooleanTestabilityTransformation.java
License:Open Source License
/** * This helper function determines whether the boolean on the stack at the * current position will be stored in a Boolean variable * //from w w w.j a va 2 s . co m * @param position * @param mn * @return */ public boolean isBooleanAssignment(AbstractInsnNode position, MethodNode mn) { AbstractInsnNode node = position.getNext(); logger.info("Checking for ISTORE after boolean"); boolean done = false; while (!done) { if (node.getOpcode() == Opcodes.PUTFIELD || node.getOpcode() == Opcodes.PUTSTATIC) { // TODO: Check whether field is static logger.info("Checking field assignment"); FieldInsnNode fn = (FieldInsnNode) node; if (Type.getType(DescriptorMapping.getInstance().getFieldDesc(fn.owner, fn.name, fn.desc)) == Type.BOOLEAN_TYPE) { return true; } else { return false; } } else if (node.getOpcode() == Opcodes.ISTORE) { logger.info("Found ISTORE after boolean"); VarInsnNode vn = (VarInsnNode) node; // TODO: Check whether variable at this position is a boolean if (isBooleanVariable(vn.var, mn)) { logger.info("Assigning boolean to variable "); return true; } else { logger.info("Variable is not a bool"); return false; } } else if (node.getOpcode() == Opcodes.IRETURN) { logger.info("Checking return value of method " + cn.name + "." + mn.name); if (DescriptorMapping.getInstance().isTransformedOrBooleanMethod(cn.name, mn.name, mn.desc)) { logger.info("Method returns a bool"); return true; } else { logger.info("Method does not return a bool"); return false; } } else if (node.getOpcode() == Opcodes.BASTORE) { // We remove all bytes, so BASTORE is only used for booleans AbstractInsnNode start = position.getNext(); boolean reassignment = false; while (start != node) { if (node instanceof InsnNode) { reassignment = true; } start = start.getNext(); } logger.info("Possible assignment to array?"); if (reassignment) return false; else return true; } else if (node instanceof MethodInsnNode) { // if it is a boolean parameter of a converted method, then it needs to be converted // Problem: How do we know which parameter it represents? MethodInsnNode methodNode = (MethodInsnNode) node; String desc = DescriptorMapping.getInstance().getMethodDesc(methodNode.owner, methodNode.name, methodNode.desc); Type[] types = Type.getArgumentTypes(desc); if (types.length > 0 && types[types.length - 1] == Type.BOOLEAN_TYPE) { return true; } else { return false; } } else if (node.getOpcode() == Opcodes.GOTO || node.getOpcode() == Opcodes.ICONST_0 || node.getOpcode() == Opcodes.ICONST_1 || node.getOpcode() == -1) { logger.info("Continuing search"); // continue search } else if (!(node instanceof LineNumberNode || node instanceof FrameNode)) { logger.info("Search ended with opcode " + node.getOpcode()); return false; } if (node != mn.instructions.getLast()) node = node.getNext(); else done = true; } return false; }
From source file:org.evosuite.instrumentation.testability.transformer.BooleanCallsTransformer.java
License:Open Source License
@Override protected AbstractInsnNode transformFieldInsnNode(MethodNode mn, FieldInsnNode fieldNode) { // TODO: If the field owner is not transformed, then convert this to a proper Boolean fieldNode.desc = this.booleanTestabilityTransformation.transformFieldDescriptor(fieldNode.owner, fieldNode.name, fieldNode.desc); // If after transformation the field is still Boolean, we need to convert if (Type.getType(fieldNode.desc).equals(Type.BOOLEAN_TYPE)) { if (fieldNode.getOpcode() == Opcodes.PUTFIELD || fieldNode.getOpcode() == Opcodes.PUTSTATIC) { MethodInsnNode n = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "intToBoolean", Type.getMethodDescriptor(Type.BOOLEAN_TYPE, new Type[] { Type.INT_TYPE })); TransformationStatistics.transformBackToBooleanField(); mn.instructions.insertBefore(fieldNode, n); } else {//from www . j ava 2s . c om MethodInsnNode n = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "booleanToInt", Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { Type.BOOLEAN_TYPE })); mn.instructions.insert(fieldNode, n); TransformationStatistics.transformBackToBooleanField(); return n; } } return fieldNode; }
From source file:org.evosuite.instrumentation.testability.transformer.BooleanDefinitionTransformer.java
License:Open Source License
@Override protected AbstractInsnNode transformFieldInsnNode(MethodNode mn, FieldInsnNode fieldNode) { // This handles the else branch for field assignments if (DescriptorMapping.getInstance().isTransformedOrBooleanField( this.booleanTestabilityTransformation.className, fieldNode.name, fieldNode.desc)) { if (fieldNode.getNext() instanceof FieldInsnNode) { FieldInsnNode other = (FieldInsnNode) fieldNode.getNext(); if (fieldNode.owner.equals(other.owner) && fieldNode.name.equals(other.name) && fieldNode.desc.equals(other.desc)) { if (fieldNode.getOpcode() == Opcodes.GETFIELD && other.getOpcode() == Opcodes.PUTFIELD) { this.booleanTestabilityTransformation.insertGetBefore(other, mn.instructions); } else if (fieldNode.getOpcode() == Opcodes.GETSTATIC && other.getOpcode() == Opcodes.PUTSTATIC) { this.booleanTestabilityTransformation.insertGetBefore(other, mn.instructions); }/*from ww w.j a v a 2 s .co m*/ } } } return fieldNode; }
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;//from ww w. j a va2s. 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.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
@SuppressWarnings("unchecked") @Override//from www . j a v a 2 s . com protected AbstractInsnNode transformFieldInsnNode(MethodNode mn, FieldInsnNode fieldNode) { if ((fieldNode.getOpcode() == Opcodes.PUTFIELD || fieldNode.getOpcode() == Opcodes.PUTSTATIC) && DescriptorMapping.getInstance().isTransformedOrBooleanField(fieldNode.owner, fieldNode.name, fieldNode.desc)) { if (addedInsns.contains(fieldNode)) return fieldNode; // Can only handle cases where the field owner is loaded directly before the field // TODO: We could pop the top of the stack and DUP the owner, but would need to take care // whether we need to pop one or two words if (fieldNode.getOpcode() == Opcodes.PUTFIELD) { AbstractInsnNode previous = fieldNode.getPrevious(); while (previous instanceof LineNumberNode || previous instanceof FrameNode || previous.getOpcode() == Opcodes.ICONST_0 || previous.getOpcode() == Opcodes.ICONST_1) previous = previous.getPrevious(); if (previous.getOpcode() != Opcodes.ALOAD) { BooleanTestabilityTransformation.logger.info("Can't handle case of " + previous); return fieldNode; } VarInsnNode varNode = (VarInsnNode) previous; if (varNode.var != 0) { BooleanTestabilityTransformation.logger.info("Can't handle case of " + previous); return fieldNode; } } BooleanTestabilityTransformation.logger.info("Handling PUTFIELD case!"); // Check if ICONST_0 or ICONST_1 are on the stack ControlDependenceGraph cdg = GraphPool.getInstance(this.booleanTestabilityTransformation.classLoader) .getCDG(this.booleanTestabilityTransformation.className.replace("/", "."), mn.name + mn.desc); int index = mn.instructions.indexOf(fieldNode); BooleanTestabilityTransformation.logger.info("Getting bytecode instruction for " + fieldNode.name + "/" + ((FieldInsnNode) mn.instructions.get(index)).name); InsnList nodes = mn.instructions; ListIterator<AbstractInsnNode> it = nodes.iterator(); while (it.hasNext()) { BytecodeInstruction in = new BytecodeInstruction(this.booleanTestabilityTransformation.classLoader, this.booleanTestabilityTransformation.className, mn.name, 0, 0, it.next()); BooleanTestabilityTransformation.logger.info(in.toString()); } BytecodeInstruction insn = BytecodeInstructionPool .getInstance(this.booleanTestabilityTransformation.classLoader) .getInstruction(this.booleanTestabilityTransformation.className.replace("/", "."), mn.name + mn.desc, index); if (insn == null) insn = BytecodeInstructionPool.getInstance(this.booleanTestabilityTransformation.classLoader) .getInstruction(this.booleanTestabilityTransformation.className.replace("/", "."), mn.name + mn.desc, fieldNode); //varNode); if (insn == null) { // TODO: Find out why BooleanTestabilityTransformation.logger.info("ERROR: Could not find node"); return fieldNode; } if (insn.getASMNode().getOpcode() != fieldNode.getOpcode()) { BooleanTestabilityTransformation.logger.info("Found wrong bytecode instruction at this index!"); BytecodeInstructionPool.getInstance(this.booleanTestabilityTransformation.classLoader) .getInstruction(this.booleanTestabilityTransformation.className, mn.name + mn.desc, fieldNode); } if (insn.getBasicBlock() == null) { BooleanTestabilityTransformation.logger.info("ERROR: Problematic node found"); return fieldNode; } Set<ControlDependency> dependencies = insn.getControlDependencies(); BooleanTestabilityTransformation.logger.info("Found flag assignment: " + insn + ", checking " + dependencies.size() + " control dependencies"); for (ControlDependency dep : dependencies) { if (!addedNodes.contains(dep)) handleDependency(dep, cdg, mn, fieldNode, insn); } } return fieldNode; }
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;//from w ww . j a v a 2 s.c o m 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.formulacompiler.compiler.internal.bytecode.ArrayAccessorForConstDataCompiler.java
License:Open Source License
@Override protected void compileBody() throws CompilerException { final GeneratorAdapter mv = mv(); final ArrayDescriptor dim = this.arrayNode.arrayDescriptor(); final int n = dim.numberOfElements(); final DataType eltDataType = this.arrayNode.getDataType(); final TypeCompiler eltCompiler = section().engineCompiler().typeCompiler(eltDataType); final Type eltType = eltCompiler.type(); // private double[] xy; final FieldVisitor fv = cw().visitField(Opcodes.ACC_PRIVATE, methodName(), arrayDescriptor(), null, null); fv.visitEnd();//from w w w .j a va 2 s . c o m // if (this.xy == null) { final Label skipInit = mv.newLabel(); mv.loadThis(); mv.visitFieldInsn(Opcodes.GETFIELD, section().classInternalName(), methodName(), arrayDescriptor()); mv.ifNonNull(skipInit); // ... new double[ n ] mv.loadThis(); mv.push(n); mv.newArray(eltType); // ... { c1, c2, ... } int i = 0; for (ExpressionNode elt : this.arrayNode.arguments()) { if (elt instanceof ExpressionNodeForConstantValue) { mv.visitInsn(Opcodes.DUP); mv.push(i); final ExpressionNodeForConstantValue constElt = (ExpressionNodeForConstantValue) elt; eltCompiler.compileConst(mv, constElt.value()); mv.arrayStore(eltType); } i++; } // this.xy *=* new double[] { ... } mv.visitFieldInsn(Opcodes.PUTFIELD, section().classInternalName(), methodName(), arrayDescriptor()); // } // return this.xy; mv.mark(skipInit); mv.loadThis(); mv.visitFieldInsn(Opcodes.GETFIELD, section().classInternalName(), methodName(), arrayDescriptor()); mv.visitInsn(Opcodes.ARETURN); }
From source file:org.formulacompiler.compiler.internal.bytecode.ArrayAccessorForFullDataCompiler.java
License:Open Source License
@Override protected void compileBody() throws CompilerException { final GeneratorAdapter mv = mv(); final DataType eltDataType = this.arrayNode.getDataType(); final ExpressionCompiler eltCompiler = expressionCompiler(eltDataType); final Type eltType = eltCompiler.type(); final String initName = methodName() + "$init"; final String initDesc = "Z"; // private boolean xy$init; final FieldVisitor fv = cw().visitField(Opcodes.ACC_PRIVATE, initName, initDesc, null, null); fv.visitEnd();//from w ww. j a v a 2s . c o m // if (!this.xy$init) { final Label skipInit = mv.newLabel(); mv.loadThis(); mv.visitFieldInsn(Opcodes.GETFIELD, section().classInternalName(), initName, initDesc); mv.visitJumpInsn(Opcodes.IFNE, skipInit); // this.xy$init = true; mv.loadThis(); mv.push(true); mv.visitFieldInsn(Opcodes.PUTFIELD, section().classInternalName(), initName, initDesc); // this.xy = { ?, c1, c2, ... } mv.loadThis(); section().getArrayAccessorForConstDataOnly(this.arrayNode).compileCall(mv); int i = 0; for (ExpressionNode elt : this.arrayNode.arguments()) { if (!(elt instanceof ExpressionNodeForConstantValue)) { mv.visitInsn(Opcodes.DUP); mv.visitIntInsn(Opcodes.BIPUSH, i); eltCompiler.compile(elt); mv.arrayStore(eltType); } i++; } // return this.xy; mv.visitInsn(Opcodes.ARETURN); // } else // return this.xy; mv.mark(skipInit); mv.loadThis(); section().getArrayAccessorForConstDataOnly(this.arrayNode).compileCall(mv); mv.visitInsn(Opcodes.ARETURN); if (section().hasReset()) { final GeneratorAdapter reset = section().resetter(); // this.xy$init = false; reset.loadThis(); reset.push(false); reset.visitFieldInsn(Opcodes.PUTFIELD, section().classInternalName(), initName, initDesc); } }