List of usage examples for org.objectweb.asm Opcodes ASTORE
int ASTORE
To view the source code for org.objectweb.asm Opcodes ASTORE.
Click Source Link
From source file:org.brutusin.instrumentation.utils.TreeInstructions.java
License:Apache License
public static VarInsnNode getStoreInst(Type type, int position) { int opCode = -1; switch (type.getDescriptor().charAt(0)) { case 'B': opCode = Opcodes.ISTORE;//from w ww. ja v a2s. co m break; case 'C': opCode = Opcodes.ISTORE; break; case 'D': opCode = Opcodes.DSTORE; break; case 'F': opCode = Opcodes.FSTORE; break; case 'I': opCode = Opcodes.ISTORE; break; case 'J': opCode = Opcodes.LSTORE; break; case 'L': opCode = Opcodes.ASTORE; break; case '[': opCode = Opcodes.ASTORE; break; case 'Z': opCode = Opcodes.ISTORE; break; case 'S': opCode = Opcodes.ISTORE; break; default: throw new ClassFormatError("Invalid method signature: " + type.getDescriptor()); } return new VarInsnNode(opCode, position); }
From source file:org.decojer.cavaj.readers.asm.ReadMethodVisitor.java
License:Open Source License
@Override public void visitVarInsn(final int opcode, final int var) { T t = null;//from w w w. ja v a 2s .c o m switch (opcode) { /******** * LOAD * ********/ case Opcodes.ALOAD: t = T.REF; // fall through case Opcodes.DLOAD: if (t == null) { t = T.DOUBLE; } // fall through case Opcodes.FLOAD: if (t == null) { t = T.FLOAT; } // fall through case Opcodes.ILOAD: if (t == null) { t = T.AINT; } // fall through case Opcodes.LLOAD: if (t == null) { t = T.LONG; } add(new LOAD(this.ops.size(), opcode, this.line, t, var)); break; /********* * STORE * *********/ case Opcodes.ASTORE: t = T.AREF; // RET allowed too // fall through case Opcodes.DSTORE: if (t == null) { t = T.DOUBLE; } // fall through case Opcodes.FSTORE: if (t == null) { t = T.FLOAT; } // fall through case Opcodes.ISTORE: if (t == null) { t = T.AINT; } // fall through case Opcodes.LSTORE: if (t == null) { t = T.LONG; } add(new STORE(this.ops.size(), opcode, this.line, t, var)); break; /******* * RET * *******/ case Opcodes.RET: { add(new RET(this.ops.size(), opcode, this.line, var)); break; } default: log.warn(getM() + ": Unknown var insn opcode '" + opcode + "'!"); } }
From source file:org.eclipse.objectteams.otredyn.bytecode.asm.CreateAddRemoveRoleMethod.java
License:Open Source License
void genGetInitializedRoleSet(InsnList instructions, int targetLocal) { // x = this._OT$roleSet instructions.add(new IntInsnNode(Opcodes.ALOAD, 0)); instructions.add(new FieldInsnNode(Opcodes.GETFIELD, name, ConstantMembers.OT_ROLE_SET, ConstantMembers.HASH_SET_FIELD_TYPE)); instructions.add(new IntInsnNode(Opcodes.ASTORE, targetLocal)); instructions.add(new IntInsnNode(Opcodes.ALOAD, targetLocal)); // if (x == null) { LabelNode skipInstantiation = new LabelNode(); instructions.add(new JumpInsnNode(Opcodes.IFNONNULL, skipInstantiation)); // this._OT$roleSet = new HashSet(); instructions.add(new IntInsnNode(Opcodes.ALOAD, 0)); instructions.add(new TypeInsnNode(Opcodes.NEW, ClassNames.HASH_SET_SLASH)); instructions.add(new InsnNode(Opcodes.DUP)); instructions//from w w w .j a v a 2s . com .add(new MethodInsnNode(Opcodes.INVOKESPECIAL, ClassNames.HASH_SET_SLASH, "<init>", "()V", false)); instructions.add(new IntInsnNode(Opcodes.ASTORE, targetLocal)); instructions.add(new IntInsnNode(Opcodes.ALOAD, targetLocal)); instructions.add(new FieldInsnNode(Opcodes.PUTFIELD, name, ConstantMembers.OT_ROLE_SET, ConstantMembers.HASH_SET_FIELD_TYPE)); instructions.add(skipInstantiation); // } }
From source file:org.elasticsearch.painless.node.SSource.java
License:Apache License
@Override void write(MethodWriter writer, Globals globals) { if (reserved.usesScore()) { // if the _score value is used, we do this once: // final double _score = scorer.score(); Variable scorer = mainMethod.getVariable(null, Locals.SCORER); Variable score = mainMethod.getVariable(null, Locals.SCORE); writer.visitVarInsn(Opcodes.ALOAD, scorer.getSlot()); writer.invokeVirtual(WriterConstants.SCORER_TYPE, WriterConstants.SCORER_SCORE); writer.visitInsn(Opcodes.F2D);//from w w w. j a va 2s .co m writer.visitVarInsn(Opcodes.DSTORE, score.getSlot()); } if (reserved.usesCtx()) { // if the _ctx value is used, we do this once: // final Map<String,Object> ctx = input.get("ctx"); Variable input = mainMethod.getVariable(null, Locals.PARAMS); Variable ctx = mainMethod.getVariable(null, Locals.CTX); writer.visitVarInsn(Opcodes.ALOAD, input.getSlot()); writer.push(Locals.CTX); writer.invokeInterface(MAP_TYPE, MAP_GET); writer.visitVarInsn(Opcodes.ASTORE, ctx.getSlot()); } if (reserved.getMaxLoopCounter() > 0) { // if there is infinite loop protection, we do this once: // int #loop = settings.getMaxLoopCounter() Variable loop = mainMethod.getVariable(null, Locals.LOOP); writer.push(reserved.getMaxLoopCounter()); writer.visitVarInsn(Opcodes.ISTORE, loop.getSlot()); } for (AStatement statement : statements) { statement.write(writer, globals); } if (!methodEscape) { writer.visitInsn(Opcodes.ACONST_NULL); writer.returnValue(); } }
From source file:org.evosuite.graphs.cfg.ASMWrapper.java
License:Open Source License
/** * <p>//w w w. j a v a 2 s .c om * isLocalVarDefinition * </p> * * @return a boolean. */ public boolean isLocalVariableDefinition() { return asmNode.getOpcode() == Opcodes.ISTORE || asmNode.getOpcode() == Opcodes.LSTORE || asmNode.getOpcode() == Opcodes.FSTORE || asmNode.getOpcode() == Opcodes.DSTORE || asmNode.getOpcode() == Opcodes.ASTORE || asmNode.getOpcode() == Opcodes.IINC || isLocalArrayDefinition(); }
From source file:org.evosuite.graphs.cfg.BytecodeInstructionPool.java
License:Open Source License
/** * Determine how many bytes the current instruction occupies together with * its operands/*from w ww . j ava2 s . c o m*/ * * @return */ private int getBytecodeIncrement(AbstractInsnNode instructionNode) { int opcode = instructionNode.getOpcode(); switch (opcode) { case Opcodes.ALOAD: // index case Opcodes.ASTORE: // index case Opcodes.DLOAD: case Opcodes.DSTORE: case Opcodes.FLOAD: case Opcodes.FSTORE: case Opcodes.ILOAD: case Opcodes.ISTORE: case Opcodes.LLOAD: case Opcodes.LSTORE: VarInsnNode varNode = (VarInsnNode) instructionNode; if (varNode.var > 3) return 1; else return 0; case Opcodes.BIPUSH: // byte case Opcodes.NEWARRAY: case Opcodes.RET: return 1; case Opcodes.LDC: LdcInsnNode ldcNode = (LdcInsnNode) instructionNode; if (ldcNode.cst instanceof Double || ldcNode.cst instanceof Long) return 2; // LDC2_W else return 1; case 19: //LDC_W case 20: //LDC2_W return 2; case Opcodes.ANEWARRAY: // indexbyte1, indexbyte2 case Opcodes.CHECKCAST: // indexbyte1, indexbyte2 case Opcodes.GETFIELD: case Opcodes.GETSTATIC: case Opcodes.GOTO: case Opcodes.IF_ACMPEQ: case Opcodes.IF_ACMPNE: case Opcodes.IF_ICMPEQ: case Opcodes.IF_ICMPNE: case Opcodes.IF_ICMPGE: case Opcodes.IF_ICMPGT: case Opcodes.IF_ICMPLE: case Opcodes.IF_ICMPLT: case Opcodes.IFLE: case Opcodes.IFLT: case Opcodes.IFGE: case Opcodes.IFGT: case Opcodes.IFNE: case Opcodes.IFEQ: case Opcodes.IFNONNULL: case Opcodes.IFNULL: case Opcodes.IINC: case Opcodes.INSTANCEOF: case Opcodes.INVOKESPECIAL: case Opcodes.INVOKESTATIC: case Opcodes.INVOKEVIRTUAL: case Opcodes.JSR: case Opcodes.NEW: case Opcodes.PUTFIELD: case Opcodes.PUTSTATIC: case Opcodes.SIPUSH: // case Opcodes.LDC_W // case Opcodes.LDC2_W return 2; case Opcodes.MULTIANEWARRAY: return 3; case Opcodes.INVOKEDYNAMIC: case Opcodes.INVOKEINTERFACE: return 4; case Opcodes.LOOKUPSWITCH: case Opcodes.TABLESWITCH: // TODO: Could be more return 4; // case Opcodes.GOTO_W // case Opcodes.JSR_W } return 0; }
From source file:org.evosuite.instrumentation.coverage.DefUseInstrumentation.java
License:Open Source License
private void addObjectInstrumentation(BytecodeInstruction instruction, InsnList instrumentation, MethodNode mn) {// w ww. j av a2s . c o m if (instruction.isLocalVariableDefinition()) { if (instruction.getASMNode().getOpcode() == Opcodes.ALOAD) { instrumentation.add(new InsnNode(Opcodes.DUP)); } else { instrumentation.add(new InsnNode(Opcodes.ACONST_NULL)); } } else if (instruction.isLocalVariableUse()) { if (instruction.getASMNode().getOpcode() == Opcodes.ASTORE) { instrumentation.add(new InsnNode(Opcodes.DUP)); } else { instrumentation.add(new InsnNode(Opcodes.ACONST_NULL)); } } else if (instruction.isArrayStoreInstruction()) { // Object, index, value instrumentation.add(new InsnNode(Opcodes.DUP)); // } else if(instruction.isArrayLoadInstruction()) { // instrumentation.add(new InsnNode(Opcodes.DUP)); } else if (instruction.isFieldNodeDU()) { // TODO: FieldNodeDU takes care of ArrayStore - why? Type type = Type.getType(instruction.getFieldType()); if (type.getSort() == Type.OBJECT) { instrumentation.add(new InsnNode(Opcodes.DUP)); } else { instrumentation.add(new InsnNode(Opcodes.ACONST_NULL)); } } else if (instruction.isMethodCall()) { Type type = Type.getReturnType(instruction.getMethodCallDescriptor()); if (type.getSort() == Type.OBJECT) { instrumentation.add(new InsnNode(Opcodes.DUP)); } else { instrumentation.add(new InsnNode(Opcodes.ACONST_NULL)); } } }
From source file:org.evosuite.instrumentation.mutation.ReplaceArithmeticOperator.java
License:Open Source License
@SuppressWarnings("rawtypes") private static int getNextIndexFromLoad(MethodNode mn) { Iterator it = mn.instructions.iterator(); int index = 0; while (it.hasNext()) { AbstractInsnNode node = (AbstractInsnNode) it.next(); if (node instanceof VarInsnNode) { VarInsnNode varNode = (VarInsnNode) node; int varIndex = varNode.var; switch (varNode.getOpcode()) { case Opcodes.ALOAD: case Opcodes.ILOAD: case Opcodes.FLOAD: case Opcodes.IALOAD: case Opcodes.BALOAD: case Opcodes.CALOAD: case Opcodes.AALOAD: case Opcodes.ASTORE: case Opcodes.ISTORE: case Opcodes.FSTORE: case Opcodes.IASTORE: case Opcodes.BASTORE: case Opcodes.CASTORE: case Opcodes.AASTORE: index = Math.max(index, varIndex + 1); break; case Opcodes.DLOAD: case Opcodes.DSTORE: case Opcodes.LLOAD: case Opcodes.LSTORE: case Opcodes.DALOAD: case Opcodes.DASTORE: case Opcodes.LALOAD: case Opcodes.LASTORE: index = Math.max(index, varIndex + 2); break; }/*from www . jav a2 s.c om*/ } } return index; }
From source file:org.evosuite.testcarver.instrument.Instrumenter.java
License:Open Source License
/** * public int myMethod(int i)/*ww w. j a v a 2 s . co m*/ { try { return _sw_prototype_original_myMethod(i) } finally { Capturer.enable(); } } * @param classNode * @param className * @param methodNode */ @SuppressWarnings("unchecked") private MethodNode wrapMethod(final ClassNode classNode, final String className, final MethodNode methodNode) { methodNode.maxStack += 4; // create wrapper for original method final MethodNode wrappingMethodNode = new MethodNode(methodNode.access, methodNode.name, methodNode.desc, methodNode.signature, (String[]) methodNode.exceptions.toArray(new String[methodNode.exceptions.size()])); wrappingMethodNode.maxStack = methodNode.maxStack; // assign annotations to wrapping method wrappingMethodNode.visibleAnnotations = methodNode.visibleAnnotations; wrappingMethodNode.visibleParameterAnnotations = methodNode.visibleParameterAnnotations; // remove annotations from wrapped method to avoid wrong behavior controlled by annotations methodNode.visibleAnnotations = null; methodNode.visibleParameterAnnotations = null; // rename original method methodNode.access = TransformerUtil.modifyVisibility(methodNode.access, Opcodes.ACC_PRIVATE); final LabelNode l0 = new LabelNode(); final LabelNode l1 = new LabelNode(); final LabelNode l2 = new LabelNode(); final InsnList wInstructions = wrappingMethodNode.instructions; if ("<init>".equals(methodNode.name)) { // wrap a constructor methodNode.name = WRAP_NAME_PREFIX + "init" + WRAP_NAME_PREFIX; // move call to other constructors to new method AbstractInsnNode ins = null; ListIterator<AbstractInsnNode> iter = methodNode.instructions.iterator(); int numInvokeSpecials = 0; // number of invokespecial calls before actual constructor call while (iter.hasNext()) { ins = iter.next(); iter.remove(); wInstructions.add(ins); 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++; } } } } else { methodNode.name = WRAP_NAME_PREFIX + methodNode.name; } int varReturnValue = 0; final Type returnType = Type.getReturnType(methodNode.desc); if (returnType.equals(Type.VOID_TYPE)) { wrappingMethodNode.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l1, "java/lang/Throwable")); } else { wrappingMethodNode.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l2, "java/lang/Throwable")); //--- create "Object returnValue = null;" if (!TransformerUtil.isStatic(methodNode.access)) { // load "this" varReturnValue++; } // consider method arguments to find right variable index final Type[] argTypes = Type.getArgumentTypes(methodNode.desc); for (int i = 0; i < argTypes.length; i++) { varReturnValue++; // long/double take two registers if (argTypes[i].equals(Type.LONG_TYPE) || argTypes[i].equals(Type.DOUBLE_TYPE)) { varReturnValue++; } } // push NULL on the stack and initialize variable for return value for it wInstructions.add(new InsnNode(Opcodes.ACONST_NULL)); wInstructions.add(new VarInsnNode(Opcodes.ASTORE, varReturnValue)); } int var = 0; // --- L0 wInstructions.add(l0); wInstructions.add(this.addCaptureCall(TransformerUtil.isStatic(methodNode.access), className, wrappingMethodNode.name, wrappingMethodNode.desc, Type.getArgumentTypes(methodNode.desc))); // --- construct call to wrapped methode if (!TransformerUtil.isStatic(methodNode.access)) { // load "this" to call method wInstructions.add(new VarInsnNode(Opcodes.ALOAD, 0)); var++; } final Type[] argTypes = Type.getArgumentTypes(methodNode.desc); for (int i = 0; i < argTypes.length; i++) { this.addLoadInsn(wInstructions, argTypes[i], var++); // long/double take two registers if (argTypes[i].equals(Type.LONG_TYPE) || argTypes[i].equals(Type.DOUBLE_TYPE)) { var++; } } if (TransformerUtil.isStatic(methodNode.access)) { wInstructions.add( new MethodInsnNode(Opcodes.INVOKESTATIC, classNode.name, methodNode.name, methodNode.desc)); } else { wInstructions.add( new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classNode.name, methodNode.name, methodNode.desc)); } var++; if (returnType.equals(Type.VOID_TYPE)) { wInstructions.add(new JumpInsnNode(Opcodes.GOTO, l2)); // --- L1 wInstructions.add(l1); wInstructions.add(new FrameNode(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" })); wInstructions.add(new VarInsnNode(Opcodes.ASTORE, --var)); this.addCaptureEnableStatement(className, methodNode, wInstructions, -1); wInstructions.add(new VarInsnNode(Opcodes.ALOAD, var)); wInstructions.add(new InsnNode(Opcodes.ATHROW)); // FIXME <--- DUPLICATE CODE // --- L2 wInstructions.add(l2); wInstructions.add(new FrameNode(Opcodes.F_SAME, 0, null, 0, null)); this.addCaptureEnableStatement(className, methodNode, wInstructions, -1); wInstructions.add(new InsnNode(Opcodes.RETURN)); } else { // construct store of the wrapped method call's result this.addBoxingStmt(wInstructions, returnType); wInstructions.add(new VarInsnNode(Opcodes.ASTORE, varReturnValue)); wInstructions.add(new VarInsnNode(Opcodes.ALOAD, varReturnValue)); this.addUnBoxingStmt(wInstructions, returnType); final int storeOpcode = returnType.getOpcode(Opcodes.ISTORE); wInstructions.add(new VarInsnNode(storeOpcode, ++var)); // might be only var // --- L1 wInstructions.add(l1); this.addCaptureEnableStatement(className, methodNode, wInstructions, varReturnValue); // construct load of the wrapped method call's result int loadOpcode = returnType.getOpcode(Opcodes.ILOAD); wInstructions.add(new VarInsnNode(loadOpcode, var)); // construct return of the wrapped method call's result this.addReturnInsn(wInstructions, returnType); //---- L2 wInstructions.add(l2); wInstructions.add( new FrameNode(Opcodes.F_FULL, 2, new Object[] { className, this.getInternalName(returnType) }, 1, new Object[] { "java/lang/Throwable" })); wInstructions.add(new VarInsnNode(Opcodes.ASTORE, --var)); this.addCaptureEnableStatement(className, methodNode, wInstructions, varReturnValue); wInstructions.add(new VarInsnNode(Opcodes.ALOAD, var)); wInstructions.add(new InsnNode(Opcodes.ATHROW)); } transformWrapperCalls(methodNode); return wrappingMethodNode; }
From source file:org.fabric3.implementation.bytecode.reflection.BytecodeConsumerInvokerFactory.java
License:Open Source License
private void writeTargetInvoke(Method method, String internalTargetName, ClassWriter cw) { MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "invoke", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", null, EXCEPTIONS); mv.visitCode();//from w ww . j a v a 2 s . co m Label label1 = new Label(); mv.visitLabel(label1); mv.visitLineNumber(9, label1); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitTypeInsn(Opcodes.CHECKCAST, internalTargetName); if (method.getParameterTypes().length == 1) { // single argument method, load the parameter passes on to the stack Class<?> paramType = method.getParameterTypes()[0]; mv.visitVarInsn(Opcodes.ALOAD, 2); writeParam(paramType, mv); } else if (method.getParameterTypes().length > 1) { // multi-argument method: cast the parameter to an object array and then load each element on the stack to be passed as params mv.visitVarInsn(Opcodes.ALOAD, 2); mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;"); mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;"); mv.visitVarInsn(Opcodes.ASTORE, 3); int pos = 0; mv.visitVarInsn(Opcodes.ALOAD, 3); for (Class<?> paramType : method.getParameterTypes()) { mv.visitInsn(Opcodes.ICONST_0 + pos); mv.visitInsn(Opcodes.AALOAD); writeParam(paramType, mv); if (pos < method.getParameterTypes().length - 1) { mv.visitVarInsn(Opcodes.ALOAD, 3); } pos++; } } // invoke the instance String methodName = method.getName(); String methodDescriptor = Type.getMethodDescriptor(method); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, internalTargetName, methodName, methodDescriptor); Class<?> returnType = method.getReturnType(); writeReturn(returnType, mv); Label label2 = new Label(); mv.visitLabel(label2); String descriptor = Type.getDescriptor(ServiceInvoker.class); mv.visitLocalVariable("this", descriptor, null, label1, label2, 0); mv.visitLocalVariable("instance", "Ljava/lang/Object;", null, label1, label2, 1); mv.visitLocalVariable("arg", "Ljava/lang/Object;", null, label1, label2, 2); mv.visitMaxs(2, 3); mv.visitEnd(); }