Example usage for org.objectweb.asm MethodVisitor visitMethodInsn

List of usage examples for org.objectweb.asm MethodVisitor visitMethodInsn

Introduction

In this page you can find the example usage for org.objectweb.asm MethodVisitor visitMethodInsn.

Prototype

public void visitMethodInsn(final int opcode, final String owner, final String name, final String descriptor,
        final boolean isInterface) 

Source Link

Document

Visits a method instruction.

Usage

From source file:com.yahoo.yqlplus.engine.internal.plan.types.base.NullTestingSerializationAdapter.java

@Override
public BytecodeSequence serializeTo(final BytecodeExpression input, final BytecodeExpression generator) {
    return new BytecodeSequence() {
        @Override/*from  www. j av a2s.  c o m*/
        public void generate(CodeEmitter code) {
            Label isNull = new Label();
            Label done = new Label();
            BytecodeExpression source = code.evaluateOnce(input);
            code.gotoIfNull(source, isNull);
            MethodVisitor mv = code.getMethodVisitor();
            code.exec(targetAdapter.serializeTo(source, generator));
            mv.visitJumpInsn(Opcodes.GOTO, done);
            mv.visitLabel(isNull);
            switch (encoding) {
            case TBIN:
                code.exec(generator);
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(TBinEncoder.class), "writeNull",
                        Type.getMethodDescriptor(Type.VOID_TYPE), false);
                break;
            case JSON:
                code.exec(generator);
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(JsonGenerator.class),
                        "writeNull", Type.getMethodDescriptor(Type.VOID_TYPE), false);
                break;
            default:
                throw new UnsupportedOperationException("unknown NativeEncoding: " + encoding);
            }
            mv.visitLabel(done);
        }
    };
}

From source file:com.yahoo.yqlplus.engine.internal.source.MissingRequiredFieldExpr.java

@Override
public void generate(CodeEmitter code) {
    MethodVisitor mv = code.getMethodVisitor();
    mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(IllegalArgumentException.class));
    mv.visitInsn(Opcodes.DUP);/*from  w w w.  j a va  2  s  .  c  o  m*/
    mv.visitLdcInsn(String.format("%s::%s Missing required property '%s' (%s)", className, methodName,
            propertyName, type.getTypeName()));
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(IllegalArgumentException.class), "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(String.class)), false);
    mv.visitInsn(Opcodes.ATHROW);
}

From source file:de.javanarior.vo.generator.ByteCodeGenerator.java

License:Apache License

/**
 * Generate a implementation of a value object.
 *
 * @param valueType//  w w w.j  a v a  2  s. c  o m
 *            - 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.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  w  ww.  j  av  a2  s.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);// w ww.  j a v  a2  s.  c o  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 static void println(MethodVisitor mv, String string) {
    // System.err.println(<string>);
    mv.visitFieldInsn(Opcodes.GETSTATIC, Type.getInternalName(System.class), "err",
            Type.getDescriptor(PrintStream.class));
    mv.visitLdcInsn(string);/*  ww w  .  j av  a 2  s.  c o m*/
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(PrintStream.class), "println",
            Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(String.class)), false);
}

From source file:de.zib.sfs.instrument.AbstractSfsAdapter.java

License:BSD License

protected void storeTime(MethodVisitor mv, int index) {
    // long time = System.nanoTime();
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, this.systemInternalName, "nanoTime", this.nanoTimeDescriptor,
            false);/*from  w  w w  . ja  v a 2s.c o  m*/
    mv.visitVarInsn(Opcodes.LSTORE, index);
}

From source file:de.zib.sfs.instrument.AbstractSfsAdapter.java

License:BSD License

protected void isInstrumentationActive(MethodVisitor mv) {
    // isInstrumentationActive();
    mv.visitVarInsn(Opcodes.ALOAD, 0);/*from  ww w. j av a 2s . co m*/
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, this.instrumentedTypeInternalName, "isInstrumentationActive",
            Type.getMethodDescriptor(Type.BOOLEAN_TYPE), false);
}

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  ww  w.jav a2  s.  co 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);
}

From source file:de.zib.sfs.instrument.DirectByteBufferAdapter.java

License:BSD License

@Override
protected void initializeFields(MethodVisitor constructorMV, String constructorDesc) {
    if ("(Lsun/nio/ch/DirectBuffer;IIIII)V".equals(constructorDesc)) {
        // if we're constructed from another buffer, make sure we're from a
        // file too if the other buffer is too

        // if (db instanceof MappedByteBuffer) {
        constructorMV.visitVarInsn(Opcodes.ALOAD, 1);
        constructorMV.visitTypeInsn(Opcodes.INSTANCEOF, Type.getInternalName(MappedByteBuffer.class));
        Label memoryMappedBufferLabel = new Label();
        constructorMV.visitJumpInsn(Opcodes.IFEQ, memoryMappedBufferLabel);

        // setFromFileChannel(db.isFromFileChannel());
        constructorMV.visitVarInsn(Opcodes.ALOAD, 0);
        constructorMV.visitVarInsn(Opcodes.ALOAD, 1);
        constructorMV.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(MappedByteBuffer.class),
                "isFromFileChannel", Type.getMethodDescriptor(Type.BOOLEAN_TYPE), false);
        constructorMV.visitMethodInsn(Opcodes.INVOKESPECIAL, this.instrumentedTypeInternalName,
                "setFromFileChannel", Type.getMethodDescriptor(Type.VOID_TYPE, Type.BOOLEAN_TYPE), false);

        // callback.openCallback(db.fileDescriptor);
        constructorMV.visitVarInsn(Opcodes.ALOAD, 0);
        constructorMV.visitFieldInsn(Opcodes.GETFIELD, this.instrumentedTypeInternalName, "callback",
                this.callbackTypeDescriptor);
        constructorMV.visitVarInsn(Opcodes.ALOAD, 1);
        constructorMV.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(MappedByteBuffer.class),
                "fileDescriptor", Type.getDescriptor(FileDescriptor.class));
        constructorMV.visitMethodInsn(Opcodes.INVOKEVIRTUAL, this.callbackTypeInternalName, "openCallback",
                Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(FileDescriptor.class)), false);

        // }//from   www. ja  v  a  2  s.com
        constructorMV.visitLabel(memoryMappedBufferLabel);
    }
}