List of usage examples for org.objectweb.asm MethodVisitor visitInsn
public void visitInsn(final int opcode)
From source file:de.javanarior.vo.generator.ByteCodeGenerator.java
License:Apache License
/** * Generate a implementation of a value object. * * @param valueType/*from w w w .j a va2 s . c om*/ * - value object type * @param technicalType * - to which the value object is mapped * @param wrapperClass * - abstract wrapper class, correspond to the technical type * @return class name and byte code in a container */ /* CHECKSTYLE:OFF */ public static ByteCodeContainer generate(Class<?> valueType, Class<? extends Comparable<?>> technicalType, @SuppressWarnings("rawtypes") Class<? extends AbstractValue> wrapperClass) { /* CHECKSTYLE:ON */ if (!isInterface(valueType)) { throw new IllegalArgumentException("Could not generate implementation for class " + valueType.getName() + ". Please provide interface"); } ClassWriter classWriter = new ClassWriter(0); MethodVisitor methodVisitor; classWriter.visit(Opcodes.V1_7, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, implementationClassName(valueType), "L" + parentClassName(wrapperClass) + "<" + addTypeDiscriptor(valueType) + ">;" + addTypeDiscriptor(valueType), parentClassName(wrapperClass), implementedInterfaces(valueType)); methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, CONSTRUCTOR, methodDescriptor(technicalType), null, null); methodVisitor.visitCode(); Label label0 = new Label(); methodVisitor.visitLabel(label0); /* CHECKSTYLE:OFF */ methodVisitor.visitLineNumber(8, label0); /* CHECKSTYLE:ON */ methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); methodVisitor.visitVarInsn(getILOADOpCode(technicalType), 1); methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, parentClassName(wrapperClass), CONSTRUCTOR, methodDescriptor(technicalType), false); Label label1 = new Label(); methodVisitor.visitLabel(label1); /* CHECKSTYLE:OFF */ methodVisitor.visitLineNumber(9, label1); /* CHECKSTYLE:ON */ methodVisitor.visitInsn(Opcodes.RETURN); Label label2 = new Label(); methodVisitor.visitLabel(label2); methodVisitor.visitLocalVariable("this", addTypeSignature(implementationClassName(valueType)), null, label0, label2, 0); methodVisitor.visitLocalVariable("value", getType(technicalType), null, label0, label2, 1); int stackSize = getStackSize(Type.getDescriptor(technicalType)); methodVisitor.visitMaxs(stackSize, stackSize); methodVisitor.visitEnd(); classWriter.visitEnd(); return new ByteCodeContainer(binaryClassName(valueType), classWriter.toByteArray()); }
From source file:de.kandid.model.Emitter.java
License:Apache License
/** * Assembles a class that implements the given interface by generating the byte code. * @param interfaze the interface to implement * @return the class//from www . j a v a 2s.co m */ private static Class<? extends Emitter<?>> makeClass(Class<?> interfaze) { String nameClass = _nameEmitter + '$' + interfaze.getName().replace('.', '$'); String nameInterface = Type.getInternalName(interfaze); // ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); ClassWriter cw = new ClassWriter(0); cw.visit(V1_4, ACC_PUBLIC + ACC_SUPER, nameClass, null, _nameEmitter, new String[] { name(interfaze) }); // Generate default construcotor MethodVisitor cv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); cv.visitVarInsn(ALOAD, 0); cv.visitMethodInsn(INVOKESPECIAL, _nameEmitter, "<init>", "()V"); cv.visitInsn(RETURN); cv.visitMaxs(1, 1); // Generate methods Method[] methods = interfaze.getMethods(); for (int i = 0; i < methods.length; ++i) { final Method m = methods[i]; if (m.getReturnType() != void.class) throw new IllegalArgumentException("Method " + m.toGenericString() + " must not return a value"); final String descMethod = Type.getMethodDescriptor(m); final MethodVisitor mw = cw.visitMethod(ACC_PUBLIC + ACC_SYNCHRONIZED, m.getName(), descMethod, null, null); final Type[] argTypes = Type.getArgumentTypes(m); // for (int i = 0; i < _end; i += 2) // ((Listener) _listeners[i]).send(...); int localStart = 1; // give one for "this" for (Type at : argTypes) localStart += at.getSize(); Label entry = new Label(); Label exit = new Label(); mw.visitLabel(entry); // _isFiring = true; mw.visitVarInsn(ALOAD, 0); mw.visitInsn(Opcodes.ICONST_1); mw.visitFieldInsn(Opcodes.PUTFIELD, nameClass, "_isFiring", Type.BOOLEAN_TYPE.getDescriptor()); // setup local variables: i, _listeners, _end mw.visitLocalVariable("i", Type.INT_TYPE.getDescriptor(), null, entry, exit, localStart); mw.visitInsn(Opcodes.ICONST_0); mw.visitIntInsn(Opcodes.ISTORE, localStart); mw.visitLocalVariable("listeners", _descObjectArray, null, entry, exit, localStart + 1); mw.visitVarInsn(ALOAD, 0); mw.visitFieldInsn(GETFIELD, nameClass, "_listeners", _descObjectArray); mw.visitIntInsn(Opcodes.ASTORE, localStart + 1); mw.visitLocalVariable("end", Type.INT_TYPE.getDescriptor(), null, entry, exit, localStart + 2); mw.visitVarInsn(ALOAD, 0); mw.visitFieldInsn(GETFIELD, nameClass, "_end", Type.INT_TYPE.getDescriptor()); mw.visitIntInsn(Opcodes.ISTORE, localStart + 2); final Label condition = new Label(); mw.visitJumpInsn(GOTO, condition); final Label loop = new Label(); mw.visitLabel(loop); //((Listener) _listeners[i]).doSomething() mw.visitIntInsn(Opcodes.ALOAD, localStart + 1); mw.visitIntInsn(Opcodes.ILOAD, localStart); mw.visitInsn(Opcodes.AALOAD); mw.visitTypeInsn(CHECKCAST, nameInterface); int offs = 1; // give one for "this" for (Type at : argTypes) { mw.visitVarInsn(at.getOpcode(ILOAD), offs); offs += at.getSize(); } mw.visitMethodInsn(INVOKEINTERFACE, nameInterface, m.getName(), descMethod); // i += 2 mw.visitIincInsn(localStart, 2); // if (i < end) goto loop mw.visitLabel(condition); mw.visitIntInsn(Opcodes.ILOAD, localStart); mw.visitIntInsn(Opcodes.ILOAD, localStart + 2); mw.visitJumpInsn(Opcodes.IF_ICMPLT, loop); // _isFiring = false; mw.visitVarInsn(ALOAD, 0); mw.visitInsn(Opcodes.ICONST_0); mw.visitFieldInsn(Opcodes.PUTFIELD, nameClass, "_isFiring", Type.BOOLEAN_TYPE.getDescriptor()); mw.visitLabel(exit); mw.visitInsn(RETURN); mw.visitMaxs(localStart + 2, localStart + 3); mw.visitEnd(); } cw.visitEnd(); return _loader.loadClass(cw, nameClass.replace('/', '.')); }
From source file:de.topicmapslab.aranuka.proxy.ProxyAdapter.java
License:Apache License
@Override public void visitEnd() { MethodVisitor visitMethod = super.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); visitMethod.visitCode();/*from w ww. j a va 2 s. c o m*/ visitMethod.visitVarInsn(ALOAD, 0); visitMethod.visitMethodInsn(INVOKESPECIAL, oldName, "<init>", "()V"); visitMethod.visitInsn(RETURN); visitMethod.visitMaxs(1, 1); visitMethod.visitEnd(); super.visitField(ACC_PRIVATE, "methodInterceptor", "Lde/topicmapslab/aranuka/proxy/IMethodInterceptor;", null, null).visitEnd(); visitMethod = super.visitMethod(ACC_PRIVATE, "setMethodInterceptor", "(Lde/topicmapslab/aranuka/proxy/IMethodInterceptor;)V", null, null); visitMethod.visitCode(); visitMethod.visitVarInsn(ALOAD, 0); visitMethod.visitVarInsn(ALOAD, 1); visitMethod.visitFieldInsn(PUTFIELD, newName, "methodInterceptor", "Lde/topicmapslab/aranuka/proxy/IMethodInterceptor;"); visitMethod.visitInsn(RETURN); visitMethod.visitMaxs(2, 2); visitMethod.visitEnd(); super.visitEnd(); }
From source file:de.unisb.cs.st.javalanche.mutation.bytecodeMutations.arithmetic.ArithmeticReplaceMethodAdapter.java
License:Open Source License
@Override protected void handleMutation(Mutation mutation, int opcode) { logger.debug("Querying mutation " + mutation); List<Mutation> mutations = QueryManager.getMutations(className.replace('/', '.'), methodName + desc, mutation.getLineNumber(), mutation.getMutationForLine(), MutationType.ARITHMETIC_REPLACE); MutationCode unMutated = new SingleInsnMutationCode(null, opcode); List<MutationCode> mutationCode = new ArrayList<MutationCode>(); for (Mutation m : mutations) { if (mutationManager.shouldApplyMutation(m)) { final int add = Integer.parseInt(m.getOperatorAddInfo()); MutationCode mutated = null; if (add == REMOVE_LEFT_VALUE_SINGLE || add == REMOVE_RIGHT_VALUE_SINGLE) { mutated = new MutationCode(m) { @Override// w w w. ja v a 2s .c o m public void insertCodeBlock(MethodVisitor mv) { if (add == REMOVE_LEFT_VALUE_SINGLE) { mv.visitInsn(Opcodes.SWAP); } mv.visitInsn(Opcodes.POP); } }; } else if (add == REMOVE_LEFT_VALUE_DOUBLE || add == REMOVE_RIGHT_VALUE_DOUBLE) { mutated = new MutationCode(m) { @Override public void insertCodeBlock(MethodVisitor mv) { if (add == REMOVE_LEFT_VALUE_DOUBLE) { mv.visitInsn(Opcodes.DUP2_X2); mv.visitInsn(Opcodes.POP2); mv.visitInsn(Opcodes.POP2); } else { mv.visitInsn(Opcodes.POP2); } } }; } else { mutated = new SingleInsnMutationCode(m, Integer.parseInt(m.getOperatorAddInfo())); } mutationCode.add(mutated); } } if (mutationCode.size() > 0) { BytecodeTasks.insertIfElse(mv, unMutated, mutationCode.toArray(new MutationCode[0])); } else { mv.visitInsn(opcode); } }
From source file:de.unisb.cs.st.javalanche.mutation.bytecodeMutations.arithmetic.SingleInsnMutationCode.java
License:Open Source License
@Override public void insertCodeBlock(MethodVisitor mv) { mv.visitInsn(opc); }
From source file:de.unisb.cs.st.javalanche.mutation.bytecodeMutations.negateJumps.NegateJumpsMethodAdapter.java
License:Open Source License
@Override protected void handleMutation(Mutation mutation, final Label label, final int opcode) { logger.debug("Applying mutation for line: " + getLineNumber()); MutationCode unMutated = new MutationCode(null) { @Override//from ww w . j a va2 s. c o m public void insertCodeBlock(MethodVisitor mv) { mv.visitJumpInsn(opcode, label); } }; List<Mutation> mutations = QueryManager.getMutations(mutation.getClassName(), mutation.getMethodName(), mutation.getLineNumber(), mutation.getMutationForLine(), MutationType.NEGATE_JUMP); List<MutationCode> mutationCode = new ArrayList<MutationCode>(); for (final Mutation m : mutations) { if (mutationManager.shouldApplyMutation(m)) { MutationCode mutated = new MutationCode(m) { @Override public void insertCodeBlock(MethodVisitor mv) { int insertOpcode = Integer.parseInt(m.getOperatorAddInfo()); if (insertOpcode == POP_ONCE_TRUE) { mv.visitInsn(Opcodes.POP); mv.visitJumpInsn(Opcodes.GOTO, label); } else if (insertOpcode == POP_ONCE_FALSE) { mv.visitInsn(Opcodes.POP); } else if (insertOpcode == POP_TWICE_TRUE) { mv.visitInsn(Opcodes.POP2); mv.visitJumpInsn(Opcodes.GOTO, label); } else if (insertOpcode == POP_TWICE_FALSE) { mv.visitInsn(Opcodes.POP2); } else { mv.visitJumpInsn(insertOpcode, label); } } }; mutationCode.add(mutated); } } if (mutationCode.size() > 0) { BytecodeTasks.insertIfElse(mv, unMutated, mutationCode.toArray(new MutationCode[0])); } else { mv.visitJumpInsn(opcode, label); } }
From source file:de.unisb.cs.st.javalanche.mutation.bytecodeMutations.unaryOperatorInsertion.UnaryOperatorMethodAdapter.java
License:Open Source License
@Override protected void handleMutation(Mutation mutation, final Integer type) { MutationCode unMutated = new MutationCode(null) { @Override/*from w w w . ja v a 2 s .c o m*/ public void insertCodeBlock(MethodVisitor mv) { } }; List<MutationCode> mutated = new ArrayList<MutationCode>(); mutation.setOperatorAddInfo(MINUS); if (mutationManager.shouldApplyMutation(mutation)) { Mutation dbMutation = QueryManager.getMutation(mutation); final int negOpcode = getOpcode(Opcodes.INEG, type); MutationCode mutateMinus = new MutationCode(dbMutation) { @Override public void insertCodeBlock(MethodVisitor mv) { mv.visitInsn(negOpcode); } }; mutated.add(mutateMinus); } mutation.setOperatorAddInfo(BITWISE_NEGATE); if (mutationManager.shouldApplyMutation(mutation)) { Mutation dbMutation = QueryManager.getMutation(mutation); MutationCode mutateNegate = new MutationCode(dbMutation) { @Override public void insertCodeBlock(MethodVisitor mv) { if (type == Opcodes.INTEGER) { mv.visitInsn(Opcodes.ICONST_M1); mv.visitInsn(Opcodes.IXOR); } else if (type == Opcodes.LONG) { mv.visitLdcInsn(Long.valueOf(-1l)); mv.visitInsn(Opcodes.LXOR); } } }; mutated.add(mutateNegate); } if (mutated.size() > 0) { BytecodeTasks.insertIfElse(mv, unMutated, mutated.toArray(new MutationCode[0])); } else { logger.debug("Not applying mutation"); } }
From source file:de.zib.sfs.instrument.AbstractSfsAdapter.java
License:BSD License
protected void wrapMethod(int access, String name, Type returnType, Type[] argumentTypes, String signature, String[] exceptions, String callbackName, Type additionalCallbackArgumentType, ResultPasser resultPasser) {//from ww w . ja va 2s . c o m argumentTypes = argumentTypes == null ? new Type[] {} : argumentTypes; String methodDescriptor = Type.getMethodDescriptor(returnType, argumentTypes); // <access> <returnType> <name>(<argumentTypes> arguments) throws // <exceptions> { MethodVisitor mv = this.cv.visitMethod(access, name, methodDescriptor, signature, exceptions); mv.visitCode(); // if (isInstrumentationActive()) { isInstrumentationActive(mv); Label instrumentationActiveLabel = new Label(); mv.visitJumpInsn(Opcodes.IFEQ, instrumentationActiveLabel); // return? methodPrefix<name>(arguments); mv.visitVarInsn(Opcodes.ALOAD, 0); int argumentIndex = 1; for (Type argument : argumentTypes) { mv.visitVarInsn(argument.getOpcode(Opcodes.ILOAD), argumentIndex); argumentIndex += argument.getSize(); } mv.visitMethodInsn((access & Opcodes.ACC_STATIC) == 0 ? Opcodes.INVOKESPECIAL : Opcodes.INVOKESTATIC, this.instrumentedTypeInternalName, this.methodPrefix + name, methodDescriptor, false); if (!Type.VOID_TYPE.equals(returnType)) { mv.visitInsn(returnType.getOpcode(Opcodes.IRETURN)); } else { mv.visitInsn(Opcodes.RETURN); } // } mv.visitLabel(instrumentationActiveLabel); // setInstrumentationActive(true); setInstrumentationActive(mv, true); // long startTime = System.nanoTime(); int startTimeIndex = 1; for (Type argument : argumentTypes) { startTimeIndex += argument.getSize(); } storeTime(mv, startTimeIndex); // <returnType> result =? methodPrefix<name>(arguments); mv.visitVarInsn(Opcodes.ALOAD, 0); argumentIndex = 1; for (Type argument : argumentTypes) { mv.visitVarInsn(argument.getOpcode(Opcodes.ILOAD), argumentIndex); argumentIndex += argument.getSize(); } mv.visitMethodInsn((access & Opcodes.ACC_STATIC) == 0 ? Opcodes.INVOKESPECIAL : Opcodes.INVOKESTATIC, this.instrumentedTypeInternalName, this.methodPrefix + name, methodDescriptor, false); int endTimeIndex = startTimeIndex + 2; if (!Type.VOID_TYPE.equals(returnType)) { mv.visitVarInsn(returnType.getOpcode(Opcodes.ISTORE), startTimeIndex + 2); endTimeIndex += returnType.getSize(); } // long endTime = System.nanoTime(); storeTime(mv, endTimeIndex); // callback.<callbackMethod>(startTime, endTime, result?); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, this.instrumentedTypeInternalName, "callback", this.callbackTypeDescriptor); mv.visitVarInsn(Opcodes.LLOAD, startTimeIndex); mv.visitVarInsn(Opcodes.LLOAD, endTimeIndex); // -1 indicates no result should be passed int resultIndex = resultPasser.getResultIndex(); if (resultIndex != -1) { // result of the actual operation requested if (resultIndex == 0) { mv.visitVarInsn(returnType.getOpcode(Opcodes.ILOAD), startTimeIndex + 2); resultPasser.passResult(mv); } else { // some parameter requested mv.visitVarInsn(argumentTypes[resultIndex - 1].getOpcode(Opcodes.ILOAD), resultIndex); resultPasser.passResult(mv); } } Type[] callbackArgumentTypes; if (additionalCallbackArgumentType == null) { callbackArgumentTypes = new Type[] { Type.LONG_TYPE, Type.LONG_TYPE }; } else { callbackArgumentTypes = new Type[] { Type.LONG_TYPE, Type.LONG_TYPE, additionalCallbackArgumentType }; } mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, this.callbackTypeInternalName, callbackName, Type.getMethodDescriptor(Type.VOID_TYPE, callbackArgumentTypes), false); // setInstrumentationActive(false); setInstrumentationActive(mv, false); // return result;? // } if (!Type.VOID_TYPE.equals(returnType)) { mv.visitVarInsn(returnType.getOpcode(Opcodes.ILOAD), startTimeIndex + 2); mv.visitInsn(returnType.getOpcode(Opcodes.IRETURN)); } else { mv.visitInsn(Opcodes.RETURN); } mv.visitMaxs(0, 0); mv.visitEnd(); }
From source file:de.zib.sfs.instrument.AbstractSfsAdapter.java
License:BSD License
@Override public void visitEnd() { // public void setInstrumentationActive(boolean instrumentationActive) { MethodVisitor setInstrumentationActiveMV = this.cv.visitMethod(Opcodes.ACC_PUBLIC, "setInstrumentationActive", Type.getMethodDescriptor(Type.VOID_TYPE, Type.BOOLEAN_TYPE), null, null);/*from ww w .j av a 2 s .co m*/ setInstrumentationActiveMV.visitCode(); // this.instrumentationActive.setInstrumentationActive(instrumentationActive); setInstrumentationActiveMV.visitVarInsn(Opcodes.ALOAD, 0); setInstrumentationActiveMV.visitFieldInsn(Opcodes.GETFIELD, this.instrumentedTypeInternalName, "instrumentationActive", Type.getDescriptor(InstrumentationActive.class)); setInstrumentationActiveMV.visitVarInsn(Opcodes.ILOAD, 1); setInstrumentationActiveMV.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(InstrumentationActive.class), "setInstrumentationActive", Type.getMethodDescriptor(Type.VOID_TYPE, Type.BOOLEAN_TYPE), false); // } setInstrumentationActiveMV.visitInsn(Opcodes.RETURN); setInstrumentationActiveMV.visitMaxs(0, 0); setInstrumentationActiveMV.visitEnd(); // public boolean isInstrumentationActive() { MethodVisitor isInstrumentationActiveMV = this.cv.visitMethod(Opcodes.ACC_PUBLIC, "isInstrumentationActive", Type.getMethodDescriptor(Type.BOOLEAN_TYPE), null, null); isInstrumentationActiveMV.visitCode(); // return instrumentationActive.isInstrumentationActive(); // } isInstrumentationActiveMV.visitVarInsn(Opcodes.ALOAD, 0); isInstrumentationActiveMV.visitFieldInsn(Opcodes.GETFIELD, this.instrumentedTypeInternalName, "instrumentationActive", Type.getDescriptor(InstrumentationActive.class)); isInstrumentationActiveMV.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(InstrumentationActive.class), "isInstrumentationActive", Type.getMethodDescriptor(Type.BOOLEAN_TYPE), false); isInstrumentationActiveMV.visitInsn(Opcodes.IRETURN); isInstrumentationActiveMV.visitMaxs(0, 0); isInstrumentationActiveMV.visitEnd(); appendWrappedMethods(this.cv); this.cv.visitEnd(); }
From source file:de.zib.sfs.instrument.AbstractSfsAdapter.java
License:BSD License
protected void setInstrumentationActive(MethodVisitor mv, boolean instrumentationActive) { // setInstrumentationActive(<instrumentationActive>); mv.visitVarInsn(Opcodes.ALOAD, 0);/*from w w w . j a v a2 s .c o m*/ mv.visitInsn(instrumentationActive ? Opcodes.ICONST_1 : Opcodes.ICONST_0); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, this.instrumentedTypeInternalName, "setInstrumentationActive", Type.getMethodDescriptor(Type.VOID_TYPE, Type.BOOLEAN_TYPE), false); }