Example usage for org.objectweb.asm Opcodes INVOKEVIRTUAL

List of usage examples for org.objectweb.asm Opcodes INVOKEVIRTUAL

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes INVOKEVIRTUAL.

Prototype

int INVOKEVIRTUAL

To view the source code for org.objectweb.asm Opcodes INVOKEVIRTUAL.

Click Source Link

Usage

From source file:org.boretti.drools.integration.drools5.DroolsAddConstructorMethodVisitor.java

License:Open Source License

@Override
protected void onMethodEnter() {
    this.visitVarInsn(Opcodes.ALOAD, 0);
    this.visitInsn(Opcodes.DUP);
    this.visitInsn(Opcodes.DUP);
    this.visitInsn(Opcodes.ICONST_0);
    this.visitFieldInsn(Opcodes.PUTFIELD, Type.getObjectType(parent).getInternalName(), droolsName,
            Type.BOOLEAN_TYPE.getDescriptor());
    this.visitTypeInsn(Opcodes.NEW, Type.getType(DroolsProvider.class).getInternalName());
    this.visitInsn(Opcodes.DUP);
    this.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getType(DroolsProvider.class).getInternalName(), "<init>",
            "()V");
    this.visitInsn(Opcodes.SWAP);
    this.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getObjectType(parent).getInternalName(), "getClass",
            "()Ljava/lang/Class;");
    if (overrideNameJVMIndex >= 0) {
        this.visitVarInsn(Opcodes.ALOAD, overrideNameJVMIndex);
    }//from w  w  w  .j a  va2 s.co m
    if (overrideTypeJVMIndex >= 0) {
        this.visitVarInsn(Opcodes.ALOAD, overrideTypeJVMIndex);
    }
    if (overrideNameJVMIndex < 0 && overrideTypeJVMIndex < 0) {
        this.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getType(DroolsProvider.class).getInternalName(),
                "getRuleBase", "(Ljava/lang/Class;)Lorg/drools/RuleBase;");
    } else if (overrideNameJVMIndex < 0 && overrideTypeJVMIndex >= 0) {
        this.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getType(DroolsProvider.class).getInternalName(),
                "getRuleBaseOverride",
                "(Ljava/lang/Class;Lorg/boretti/drools/integration/drools5/DroolsServiceType;)Lorg/drools/RuleBase;");
    } else if (overrideNameJVMIndex >= 0 && overrideTypeJVMIndex < 0) {
        this.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getType(DroolsProvider.class).getInternalName(),
                "getRuleBaseOverride", "(Ljava/lang/Class;Ljava/lang/String;)Lorg/drools/RuleBase;");
    } else {
        this.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getType(DroolsProvider.class).getInternalName(),
                "getRuleBaseOverride",
                "(Ljava/lang/Class;Ljava/lang/String;Lorg/boretti/drools/integration/drools5/DroolsServiceType;)Lorg/drools/RuleBase;");
    }
    this.visitFieldInsn(Opcodes.PUTFIELD, Type.getObjectType(parent).getInternalName(), droolsRule,
            Type.getType(RuleBase.class).getDescriptor());
}

From source file:org.boretti.drools.integration.drools5.DroolsAddGeneratedGetMethodVisitor.java

License:Open Source License

@Override
protected void onMethodEnter() {
    super.onMethodEnter();
    this.visitVarInsn(Opcodes.ALOAD, 0);
    this.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getObjectType(parent).getInternalName(), droolsRun, "()V");
}

From source file:org.boretti.drools.integration.drools5.DroolsClassVisitor.java

License:Open Source License

@Override
public void visitEnd() {
    FieldVisitor fv = null;/*from   w  ww. j a  v  a2  s  .  c o  m*/
    if (isNeedChangeForBoth()) {
        fv = super.visitField(Opcodes.ACC_PRIVATE, DROOLS_FIELD_NAME, Type.BOOLEAN_TYPE.getDescriptor(), null,
                null);
        if (fv != null) {
            AnnotationVisitor av = fv.visitAnnotation(Type.getType(Generated.class).getDescriptor(), true);
            AnnotationVisitor value = av.visitArray("value");
            value.visit("", "Generated by Drools5IntegrationHelper Maven plugin");
            value.visitEnd();
            av.visit("date", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz").format(current));
            av.visitEnd();
            fv.visitEnd();
        }
    }
    if (isNeedChangeForField()) {
        fv = super.visitField(Opcodes.ACC_PRIVATE, DROOLS_FIELD_RULE,
                Type.getType(RuleBase.class).getDescriptor(), null, null);
        if (fv != null) {
            AnnotationVisitor av = fv.visitAnnotation(Type.getType(Generated.class).getDescriptor(), true);
            AnnotationVisitor value = av.visitArray("value");
            value.visit("", "Generated by Drools5IntegrationHelper Maven plugin");
            value.visitEnd();
            av.visit("date", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz").format(current));
            av.visitEnd();
            fv.visitEnd();
        }
        MethodVisitor mv = super.visitMethod(Opcodes.ACC_PRIVATE, DROOLS_METHOD_RUN, "()V", null, null);
        AnnotationVisitor av = mv.visitAnnotation(Type.getType(Generated.class).getDescriptor(), true);
        AnnotationVisitor value = av.visitArray("value");
        value.visit("", "Generated by Drools5IntegrationHelper Maven plugin");
        value.visitEnd();
        av.visit("date", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz").format(current));
        av.visitEnd();
        mv.visitCode();
        Label start = new Label();
        mv.visitLabel(start);
        Label doIt = new Label();
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, Type.getObjectType(me).getInternalName(), DROOLS_FIELD_NAME,
                Type.BOOLEAN_TYPE.getDescriptor());
        mv.visitJumpInsn(Opcodes.IFEQ, doIt);
        mv.visitInsn(Opcodes.RETURN);
        mv.visitLabel(doIt);
        mv.visitFrame(Opcodes.F_SAME, 1, new Object[] { Type.getObjectType(me).getInternalName() }, 0,
                new Object[] {});
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, Type.getObjectType(me).getInternalName(), DROOLS_FIELD_RULE,
                Type.getType(RuleBase.class).getDescriptor());
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getType(RuleBase.class).getInternalName(),
                "newStatelessSession", "()Lorg/drools/StatelessSession;");
        mv.visitInsn(Opcodes.DUP);
        mv.visitLdcInsn(FIELD_NAME_LOGGER);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getType(Object.class).getInternalName(), "getClass",
                "()Ljava/lang/Class;");
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getType(org.apache.log4j.Logger.class).getInternalName(),
                "getLogger", "(Ljava/lang/Class;)Lorg/apache/log4j/Logger;");
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getType(StatelessSession.class).getInternalName(),
                "setGlobal", "(Ljava/lang/String;Ljava/lang/Object;)V");
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getType(StatelessSession.class).getInternalName(),
                "execute", "(Ljava/lang/Object;)V");
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitInsn(Opcodes.ICONST_1);
        mv.visitFieldInsn(Opcodes.PUTFIELD, Type.getObjectType(me).getInternalName(), DROOLS_FIELD_NAME,
                Type.BOOLEAN_TYPE.getDescriptor());
        mv.visitInsn(Opcodes.RETURN);
        Label end = new Label();
        mv.visitLabel(end);
        mv.visitLocalVariable("this", Type.getObjectType(me).getDescriptor(), null, start, end, 0);
        mv.visitMaxs(4, 1);
        mv.visitEnd();
    }
    super.visitEnd();
}

From source file:org.brutusin.instrumentation.Instrumentator.java

License:Apache License

private void addTraceStart() {
    InsnList il = new InsnList();
    int methodParametersIndex = addMethodParametersVariable(il);
    addGetMethodInvocation(il);/*from ww w  . j a  v a  2s .  c o  m*/
    addStoreMethod(il);
    addGetCallback(il);

    il.add(new VarInsnNode(Opcodes.ALOAD, this.methodVarIndex));
    il.add(new VarInsnNode(Opcodes.ALOAD, methodParametersIndex));
    il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "org/brutusin/instrumentation/Callback", "onStart",
            "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/String;", false));

    this.executionIdIndex = getFistAvailablePosition();
    il.add(new VarInsnNode(Opcodes.ASTORE, this.executionIdIndex));
    this.mn.maxLocals++;
    this.startNode = new LabelNode();
    this.mn.instructions.insert(startNode);
    this.mn.instructions.insert(il);
}

From source file:org.brutusin.instrumentation.Instrumentator.java

License:Apache License

private void addCatchBlock(LabelNode startNode, LabelNode endNode) {

    InsnList il = new InsnList();
    LabelNode handlerNode = new LabelNode();
    il.add(handlerNode);//from   w  w w . ja v  a2  s  .  c om

    int exceptionVariablePosition = getFistAvailablePosition();
    il.add(new VarInsnNode(Opcodes.ASTORE, exceptionVariablePosition));
    this.methodOffset++; // Actualizamos el offset
    addGetCallback(il);
    il.add(new VarInsnNode(Opcodes.ALOAD, this.methodVarIndex));
    il.add(new VarInsnNode(Opcodes.ALOAD, exceptionVariablePosition));
    il.add(new VarInsnNode(Opcodes.ALOAD, this.executionIdIndex));
    il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "org/brutusin/instrumentation/Callback",
            "onThrowableUncatched", "(Ljava/lang/Object;Ljava/lang/Throwable;Ljava/lang/String;)V", false));

    il.add(new VarInsnNode(Opcodes.ALOAD, exceptionVariablePosition));
    il.add(new InsnNode(Opcodes.ATHROW));

    TryCatchBlockNode blockNode = new TryCatchBlockNode(startNode, endNode, handlerNode, null);

    this.mn.tryCatchBlocks.add(blockNode);
    this.mn.instructions.add(il);
}

From source file:org.brutusin.instrumentation.Instrumentator.java

License:Apache License

private InsnList getVoidReturnTraceInstructions() {
    InsnList il = new InsnList();
    addGetCallback(il);//from  w  w w.j ava  2 s . com
    il.add(new VarInsnNode(Opcodes.ALOAD, this.methodVarIndex));
    il.add(new VarInsnNode(Opcodes.ALOAD, this.executionIdIndex));
    il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "org/brutusin/instrumentation/Callback", "onVoidFinish",
            "(Ljava/lang/Object;Ljava/lang/String;)V", false));

    return il;
}

From source file:org.brutusin.instrumentation.Instrumentator.java

License:Apache License

private InsnList getThrowTraceInstructions() {
    InsnList il = new InsnList();

    int exceptionVariablePosition = getFistAvailablePosition();
    il.add(new VarInsnNode(Opcodes.ASTORE, exceptionVariablePosition));

    this.methodOffset++; // Actualizamos el offset
    addGetCallback(il);//from www  .j a va  2 s .  c o m
    il.add(new VarInsnNode(Opcodes.ALOAD, this.methodVarIndex));
    il.add(new VarInsnNode(Opcodes.ALOAD, exceptionVariablePosition));
    il.add(new VarInsnNode(Opcodes.ALOAD, this.executionIdIndex));
    il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "org/brutusin/instrumentation/Callback",
            "onThrowableThrown", "(Ljava/lang/Object;Ljava/lang/Throwable;Ljava/lang/String;)V", false));

    il.add(new VarInsnNode(Opcodes.ALOAD, exceptionVariablePosition));

    return il;
}

From source file:org.brutusin.instrumentation.Instrumentator.java

License:Apache License

private InsnList getReturnTraceInstructions() {

    InsnList il = new InsnList();

    int retunedVariablePosition = getFistAvailablePosition();
    il.add(TreeInstructions.getStoreInst(this.methodReturnType, retunedVariablePosition));

    this.variableCreated(this.methodReturnType); // Actualizamos el offset
    addGetCallback(il);/*from w w  w .j av a 2  s  .co  m*/
    il.add(new VarInsnNode(Opcodes.ALOAD, this.methodVarIndex));
    il.add(TreeInstructions.getLoadInst(this.methodReturnType, retunedVariablePosition));
    MethodInsnNode mNode = TreeInstructions.getWrapperContructionInst(this.methodReturnType);
    if (mNode != null) {
        il.add(mNode);
    }
    il.add(new VarInsnNode(Opcodes.ALOAD, this.executionIdIndex));
    il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "org/brutusin/instrumentation/Callback", "onFinish",
            "(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/String;)V", false));

    il.add(TreeInstructions.getLoadInst(this.methodReturnType, retunedVariablePosition));

    return il;

}

From source file:org.cacheonix.impl.transformer.CacheonixMethodGenerator.java

License:LGPL

/**
 * Generated the byte instructions to write the message to System out stream
 *
 * @param mv      Method Visitor that writes byte instructions
 * @param message message that needs to be written to the out stream
 *//*from   w  w  w  . ja  v a 2 s.  com*/
public static void printingToSysout(final MethodVisitor mv, final String message) {

    mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
    mv.visitLdcInsn(message);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
}

From source file:org.codehaus.groovy.reflection.MethodHandleFactory.java

License:Apache License

@Deprecated
public static void genInvokeXxxWithArray(ClassWriter cw, Method method) {
    MethodVisitor mv;//from  w  w w .  j a v a  2s  . c  o  m
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "invoke",
            "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;", null, EXCEPTIONS);
    mv.visitCode();

    Class callClass = method.getDeclaringClass();
    boolean useInterface = callClass.isInterface();

    String type = BytecodeHelper.getClassInternalName(callClass.getName());
    String descriptor = BytecodeHelper.getMethodDescriptor(method.getReturnType(), method.getParameterTypes());

    // make call
    if (Modifier.isStatic(method.getModifiers())) {
        genLoadParameters(2, mv, method);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, type, method.getName(), descriptor);
    } else {
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        BytecodeHelper.doCast(mv, callClass);
        genLoadParameters(2, mv, method);
        mv.visitMethodInsn((useInterface) ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL, type,
                method.getName(), descriptor);
    }

    BytecodeHelper.box(mv, method.getReturnType());
    if (method.getReturnType() == void.class) {
        mv.visitInsn(Opcodes.ACONST_NULL);
    }

    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}