Example usage for org.objectweb.asm MethodVisitor visitJumpInsn

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

Introduction

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

Prototype

public void visitJumpInsn(final int opcode, final Label label) 

Source Link

Document

Visits a jump instruction.

Usage

From source file:com.github.stokito.gag.agent.ThisHadBetterGenerator.java

License:Apache License

private static void visitNullCheck(MethodVisitor mv, boolean be, LocalVarInfo param, Label okay) {
    switch (param.getType().getSort()) {
    case Type.ARRAY:
    case Type.OBJECT:
        mv.visitVarInsn(ALOAD, param.getIndex());
        break;/*w ww . jav a2  s  . c  om*/
    default:
        throw new AnnotationStateError("Unsupported type: " + param.getType());
    }

    mv.visitJumpInsn(be ? IFNULL : IFNONNULL, okay);
}

From source file:com.github.stokito.gag.agent.ThisHadBetterGenerator.java

License:Apache License

/**
 * The given Property needs to be either {@link Property#THE_BLUE_PILL} or
 * {@link Property#THE_RED_PILL}./*  www  . j av  a 2s . c om*/
 */
private static void visitPill(MethodVisitor mv, boolean be, Property property, LocalVarInfo param, Label okay) {

    if (param.getType().getSort() != Type.OBJECT) {
        throw new AnnotationStateError("Unsupported type: " + param.getType());
    }

    // TODO: Also handle if parameter is null.

    Label notOkay = new Label();

    // See if the param type matches a Pill type
    mv.visitVarInsn(ALOAD, param.getIndex());
    mv.visitMethodInsn(INVOKEVIRTUAL, param.getType().getInternalName(), "getClass", "()Ljava/lang/Class;");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;");
    mv.visitLdcInsn("Pill|ThePill|.*[\\.$]Pill|.*[\\.$]ThePill");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "matches", "(Ljava/lang/String;)Z");

    if (be) {
        // If the param type does not match a Pill type, that's not okay, go to exception.
        mv.visitJumpInsn(FALSE, notOkay);
    } else {
        // If the param type does not match a Pill type, then that's okay, skip exception.
        mv.visitJumpInsn(FALSE, okay);
    }

    // At this point, the param type matches a Pill type.
    // So check if the param type is an enum type.
    mv.visitVarInsn(ALOAD, param.getIndex());
    mv.visitTypeInsn(INSTANCEOF, "java/lang/Enum");

    if (be) {
        // If the param type is not an enum, that's not okay, go to exception.
        mv.visitJumpInsn(FALSE, notOkay);
    } else {
        // If the param type is not an enum, that's okay, skip exception.
        mv.visitJumpInsn(FALSE, okay);
    }

    // Check that the Pill type has the property specified in the annotation.
    // First try to match on "BLUE" (or "RED").
    mv.visitVarInsn(ALOAD, param.getIndex());
    mv.visitTypeInsn(CHECKCAST, "java/lang/Enum");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Enum", "name", "()Ljava/lang/String;");
    mv.visitLdcInsn(property == THE_BLUE_PILL ? "BLUE" : "RED");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z");
    mv.visitJumpInsn(TRUE, be ? okay : notOkay);

    // Then try to see if the value ends with "BLUE_PILL" (or "RED_PILL").
    mv.visitVarInsn(ALOAD, param.getIndex());
    mv.visitTypeInsn(CHECKCAST, "java/lang/Enum");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Enum", "name", "()Ljava/lang/String;");
    mv.visitLdcInsn(property == THE_BLUE_PILL ? "BLUE_PILL" : "RED_PILL");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "endsWith", "(Ljava/lang/String;)Z");
    mv.visitJumpInsn(be ? TRUE : FALSE, okay);

    mv.visitLabel(notOkay);
}

From source file:com.github.stokito.gag.agent.ThisHadBetterGenerator.java

License:Apache License

private static void visitDeathStarPlans(MethodVisitor mv, boolean be, LocalVarInfo param, Label okay) {

    if (param.getType().getSort() != Type.OBJECT) {
        throw new AnnotationStateError("Unsupported type: " + param.getType());
    }//from   w  w w.  ja v  a2s.  c  o  m

    Label notOkay = new Label();

    // See if the param type matches a DeathStarPlans type
    mv.visitVarInsn(ALOAD, param.getIndex());
    mv.visitMethodInsn(INVOKEVIRTUAL, param.getType().getInternalName(), "getClass", "()Ljava/lang/Class;");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;");
    mv.visitLdcInsn("DeathStarPlans|TheDeathStarPlans|.*[\\.$]DeathStarPlans|.*[\\.$]TheDeathStarPlans");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "matches", "(Ljava/lang/String;)Z");

    if (be) {
        // If the param type does not match a DeathStarPlans type, that's not okay, go to exception.
        mv.visitJumpInsn(FALSE, notOkay);
    } else {
        // If the param type does not match a DeathStarPlans type, then that's okay, skip exception.
        mv.visitJumpInsn(FALSE, okay);
    }

    // At this point, the param type matches a DeathStarPlans type.
    // So check if the param has an isStolen method.
    Label start = new Label();
    Label end = new Label();
    Label handler = new Label();
    mv.visitTryCatchBlock(start, end, handler, null);

    mv.visitLabel(start);
    mv.visitVarInsn(ALOAD, param.getIndex());
    mv.visitMethodInsn(INVOKEVIRTUAL, param.getType().getInternalName(), "getClass", "()Ljava/lang/Class;");
    mv.visitLdcInsn("isStolen");
    mv.visitInsn(ACONST_NULL);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getMethod",
            "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;");

    mv.visitVarInsn(ALOAD, param.getIndex());
    mv.visitInsn(ACONST_NULL);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Method", "invoke",
            "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;");
    mv.visitTypeInsn(CHECKCAST, "java/lang/Boolean");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z");

    mv.visitJumpInsn(be ? TRUE : FALSE, okay);

    mv.visitLabel(end);
    mv.visitJumpInsn(GOTO, notOkay);
    mv.visitLabel(handler);
    mv.visitInsn(POP);

    mv.visitLabel(notOkay);
}

From source file:com.github.wolf480pl.mias4j.MakeTestMH.java

License:Open Source License

public static byte[] dump() throws Exception {

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;

    cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, "TestMH", null, "java/lang/Object", null);

    cw.visitInnerClass("java/lang/invoke/MethodHandles$Lookup", "java/lang/invoke/MethodHandles", "Lookup",
            ACC_PUBLIC + ACC_FINAL + ACC_STATIC);

    {/*w ww  .  j  a va  2 s .c  om*/
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        Label l1 = new Label();
        Label l2 = new Label();
        mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Throwable");
        mv.visitLabel(l0);
        /*mv.visitMethodInsn(INVOKESTATIC, "java/lang/invoke/MethodHandles", "lookup", "()Ljava/lang/invoke/MethodHandles$Lookup;", false);
        mv.visitLdcInsn(Type.getType("Ljava/io/PrintStream;"));
        mv.visitLdcInsn("println");
        mv.visitFieldInsn(GETSTATIC, "java/lang/Void", "TYPE", "Ljava/lang/Class;");
        mv.visitLdcInsn(Type.getType("Ljava/lang/String;"));
        mv.visitMethodInsn(INVOKESTATIC, "java/lang/invoke/MethodType", "methodType", "(Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/invoke/MethodType;", false);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandles$Lookup", "findVirtual", "(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/MethodHandle;", false);*/
        mv.visitLdcInsn(new Handle(H_INVOKEVIRTUAL, "java/io/PrintStream", "println",
                Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(String.class))));
        mv.visitVarInsn(ASTORE, 1);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
        mv.visitLdcInsn("hey");
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "invoke",
                "(Ljava/io/PrintStream;Ljava/lang/String;)V", false);
        mv.visitLabel(l1);
        Label l3 = new Label();
        mv.visitJumpInsn(GOTO, l3);
        mv.visitLabel(l2);
        mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" });
        mv.visitVarInsn(ASTORE, 1);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Throwable", "printStackTrace", "()V", false);
        mv.visitLabel(l3);
        mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        mv.visitInsn(RETURN);
        mv.visitMaxs(5, 2);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();
}

From source file:com.gmail.socraticphoenix.nebula.event.wrappers.BytecodeEventListenerGeneration.java

License:Open Source License

public static void exclude(Exclude exclude, int event, MethodVisitor visitor) {
    visitor.visitVarInsn(ALOAD, event);//from   w  w w.ja v a2  s. c  o m
    visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.objectType.getInternalName(),
            "getClass", BytecodeEventListenerGeneration.getClassMethod.getDescriptor(), false);
    Label returnLabel = new Label();
    Label continueLabel = new Label();
    for (Class clazz : exclude.value()) {
        visitor.visitInsn(DUP);
        visitor.visitLdcInsn(Type.getType(clazz));
        visitor.visitInsn(SWAP);
        visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.objectType.getInternalName(),
                "equals", BytecodeEventListenerGeneration.equalsMethod.getDescriptor(), false);
        visitor.visitJumpInsn(IFNE, returnLabel);
    }
    visitor.visitJumpInsn(GOTO, continueLabel);
    visitor.visitLabel(returnLabel);
    visitor.visitInsn(RETURN);
    visitor.visitLabel(continueLabel);
}

From source file:com.gmail.socraticphoenix.nebula.event.wrappers.BytecodeEventListenerGeneration.java

License:Open Source License

public static void include(Include include, int event, MethodVisitor visitor) {
    visitor.visitVarInsn(ALOAD, event);//from   w ww  . j  av  a2  s. c  om
    visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.objectType.getInternalName(),
            "getClass", BytecodeEventListenerGeneration.getClassMethod.getDescriptor(), false);
    Label returnLabel = new Label();
    Label continueLabel = new Label();
    for (Class clazz : include.value()) {
        visitor.visitInsn(DUP);
        visitor.visitLdcInsn(Type.getType(clazz));
        visitor.visitInsn(SWAP);
        visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.objectType.getInternalName(),
                "equals", BytecodeEventListenerGeneration.equalsMethod.getDescriptor(), false);
        visitor.visitJumpInsn(IFEQ, returnLabel);
    }
    visitor.visitJumpInsn(GOTO, continueLabel);
    visitor.visitLabel(returnLabel);
    visitor.visitInsn(RETURN);
    visitor.visitLabel(continueLabel);
}

From source file:com.gmail.socraticphoenix.nebula.event.wrappers.BytecodeEventListenerGeneration.java

License:Open Source License

public static void cancelled(Cancelled cancelled, int event, Class eventClass, MethodVisitor visitor) {
    Label returnLabel = new Label();
    Label continueLabel = new Label();
    visitor.visitVarInsn(ALOAD, event);// w w  w .  j  a va 2s.  c  o m
    if (CancellableEvent.class.isAssignableFrom(eventClass)) { //Assume cancelled
        visitor.visitMethodInsn(INVOKEINTERFACE,
                BytecodeEventListenerGeneration.cancellableEventType.getInternalName(), "isCancelled",
                BytecodeEventListenerGeneration.isCancelledEventMethod.getDescriptor(), true);
        if (cancelled.value()) {
            visitor.visitJumpInsn(IFEQ, returnLabel);
        } else {
            visitor.visitJumpInsn(IFNE, returnLabel);
        }
    } else { //Check instance
        Label cleanLabel = new Label();
        Label continueLabel2 = new Label();
        visitor.visitInsn(DUP);
        visitor.visitTypeInsn(INSTANCEOF,
                BytecodeEventListenerGeneration.cancellableEventType.getInternalName());
        visitor.visitJumpInsn(IFEQ, cleanLabel);

        visitor.visitJumpInsn(GOTO, continueLabel2);

        visitor.visitLabel(cleanLabel);
        visitor.visitInsn(POP);
        visitor.visitJumpInsn(GOTO, continueLabel);

        visitor.visitLabel(continueLabel2);

        visitor.visitTypeInsn(CHECKCAST,
                BytecodeEventListenerGeneration.cancellableEventType.getInternalName());
        visitor.visitMethodInsn(INVOKEINTERFACE,
                BytecodeEventListenerGeneration.cancellableEventType.getInternalName(), "isCancelled",
                BytecodeEventListenerGeneration.isCancelledEventMethod.getDescriptor(), true);
        if (cancelled.value()) {
            visitor.visitJumpInsn(IFEQ, returnLabel);
        } else {
            visitor.visitJumpInsn(IFNE, returnLabel);
        }
    }
    visitor.visitJumpInsn(GOTO, continueLabel);
    visitor.visitLabel(returnLabel);
    visitor.visitInsn(RETURN);
    visitor.visitLabel(continueLabel);
}

From source file:com.gmail.socraticphoenix.nebula.event.wrappers.BytecodeEventListenerGeneration.java

License:Open Source License

public static void firstCause(FirstCause firstCause, Class type, int param, int event, MethodVisitor visitor) {
    Label returnLabel = new Label();
    Label continueLabel = new Label();

    visitor.visitVarInsn(ALOAD, event);/*from w  w w .  j ava2s.co m*/
    visitor.visitMethodInsn(INVOKEINTERFACE, BytecodeEventListenerGeneration.eventType.getInternalName(),
            "cause", BytecodeEventListenerGeneration.eventCauseMethod.getDescriptor(), true);
    visitor.visitLdcInsn(Type.getType(type));
    visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.causeType.getInternalName(), "first",
            BytecodeEventListenerGeneration.causeFirstMethod.getDescriptor(), false);
    visitor.visitInsn(DUP);
    visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.optionalType.getInternalName(),
            "isPresent", BytecodeEventListenerGeneration.isPresentMethod.getDescriptor(), false);
    visitor.visitJumpInsn(IFEQ, returnLabel);
    visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.optionalType.getInternalName(),
            "get", BytecodeEventListenerGeneration.getMethod.getDescriptor(), false);
    BytecodeEventListenerGeneration.checkCast(Type.getType(type), visitor);
    visitor.visitVarInsn(ASTORE, param);
    visitor.visitJumpInsn(GOTO, continueLabel);
    visitor.visitLabel(returnLabel);
    visitor.visitInsn(RETURN);
    visitor.visitLabel(continueLabel);
}

From source file:com.gmail.socraticphoenix.nebula.event.wrappers.BytecodeEventListenerGeneration.java

License:Open Source License

public static void lastCause(LastCause lastCause, Class type, int param, int event, MethodVisitor visitor) {
    Label returnLabel = new Label();
    Label continueLabel = new Label();

    visitor.visitVarInsn(ALOAD, event);//from   w w w.j  a va 2 s  .c  om
    visitor.visitMethodInsn(INVOKEINTERFACE, BytecodeEventListenerGeneration.eventType.getInternalName(),
            "cause", BytecodeEventListenerGeneration.eventCauseMethod.getDescriptor(), true);
    visitor.visitLdcInsn(Type.getType(type));
    visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.causeType.getInternalName(), "last",
            BytecodeEventListenerGeneration.causeLastMethod.getDescriptor(), false);
    visitor.visitInsn(DUP);
    visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.optionalType.getInternalName(),
            "isPresent", BytecodeEventListenerGeneration.isPresentMethod.getDescriptor(), false);
    visitor.visitJumpInsn(IFEQ, returnLabel);
    visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.optionalType.getInternalName(),
            "get", BytecodeEventListenerGeneration.getMethod.getDescriptor(), false);
    BytecodeEventListenerGeneration.checkCast(Type.getType(type), visitor);
    visitor.visitVarInsn(ASTORE, param);
    visitor.visitJumpInsn(GOTO, continueLabel);
    visitor.visitLabel(returnLabel);
    visitor.visitInsn(RETURN);
    visitor.visitLabel(continueLabel);
}

From source file:com.gmail.socraticphoenix.nebula.event.wrappers.BytecodeEventListenerGeneration.java

License:Open Source License

public static void getContext(GetContext getContext, String paramName, Class type, int param, int event,
        MethodVisitor visitor) {
    Label returnLabel = new Label();
    Label continueLabel = new Label();

    visitor.visitVarInsn(ALOAD, event);/*from www  .  ja  v  a2s .c o  m*/
    visitor.visitMethodInsn(INVOKEINTERFACE, BytecodeEventListenerGeneration.eventType.getInternalName(),
            "context", BytecodeEventListenerGeneration.eventContextMethod.getDescriptor(), true);
    visitor.visitLdcInsn(getContext.value().equals(GetContext.REFER_TO_PARAM) ? param : getContext.value());
    visitor.visitLdcInsn(Type.getType(type));
    visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.contextType.getInternalName(), "get",
            BytecodeEventListenerGeneration.contextGetMethod.getDescriptor(), false);
    visitor.visitInsn(DUP);
    visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.optionalType.getInternalName(),
            "isPresent", BytecodeEventListenerGeneration.isPresentMethod.getDescriptor(), false);
    visitor.visitJumpInsn(IFEQ, returnLabel);
    visitor.visitMethodInsn(INVOKEVIRTUAL, BytecodeEventListenerGeneration.optionalType.getInternalName(),
            "get", BytecodeEventListenerGeneration.getMethod.getDescriptor(), false);
    BytecodeEventListenerGeneration.checkCast(Type.getType(type), visitor);
    visitor.visitVarInsn(ASTORE, param);
    visitor.visitJumpInsn(GOTO, continueLabel);
    visitor.visitLabel(returnLabel);
    visitor.visitInsn(RETURN);
    visitor.visitLabel(continueLabel);
}