Example usage for org.objectweb.asm MethodVisitor visitLabel

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

Introduction

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

Prototype

public void visitLabel(final Label label) 

Source Link

Document

Visits a label.

Usage

From source file:com.cinnober.msgcodec.blink.BaseByteCodeGenerator.java

License:Open Source License

protected void generateDecodeRefValue(GroupDef refGroup, boolean required, MethodVisitor mv, int byteSourceVar,
        String genClassInternalName, Class<?> javaClass, TypeDef type, boolean javaClassCodec)
        throws IllegalArgumentException {
    if (refGroup != null) {
        String groupDescriptor = getTypeDescriptor(javaClass, javaClassCodec);
        if (required) {
            mv.visitInsn(POP); // input stream
            mv.visitVarInsn(ALOAD, 0);//from  w  w w.  j a  va 2  s  . c  o  m
            mv.visitVarInsn(ALOAD, byteSourceVar);
            mv.visitMethodInsn(INVOKEVIRTUAL, genClassInternalName, "readStaticGroup_" + refGroup.getName(),
                    "(Lcom/cinnober/msgcodec/io/ByteSource;)" + groupDescriptor, false);
        } else {
            mv.visitMethodInsn(INVOKESTATIC, blinkInputIName, "readPresenceByte",
                    "(Lcom/cinnober/msgcodec/io/ByteSource;)Z", false);
            Label nonNullLabel = new Label();
            Label endLabel = new Label();
            mv.visitJumpInsn(IFNE, nonNullLabel); // not false, i.e. true
            // null
            mv.visitInsn(ACONST_NULL);
            mv.visitJumpInsn(GOTO, endLabel);

            // not null
            mv.visitLabel(nonNullLabel);
            mv.visitFrame(F_SAME, 0, null, 0, null);
            mv.visitVarInsn(ALOAD, 0);
            mv.visitVarInsn(ALOAD, byteSourceVar);
            mv.visitMethodInsn(INVOKEVIRTUAL, genClassInternalName, "readStaticGroup_" + refGroup.getName(),
                    "(Lcom/cinnober/msgcodec/io/ByteSource;)" + groupDescriptor, false);

            mv.visitLabel(endLabel);
            // PENDING: mv.visitFrame?
            mv.visitFrame(F_SAME, 0, null, 0, null);
        }
    } else {
        throw new IllegalArgumentException("Illegal reference: " + type);
    }
}

From source file:com.cinnober.msgcodec.blink.BaseByteCodeGenerator.java

License:Open Source License

protected void generateDecodeSequenceValue(Class<?> javaClass, LocalVariable nextVar, boolean required,
        MethodVisitor mv, Class<?> componentJavaClass, int byteSourceVar, TypeDef type, Schema schema,
        String genClassInternalName, String fieldIdentifier, String debugValueLabel, boolean javaClassCodec)
        throws IllegalArgumentException {
    if (!javaClass.isArray() && javaClass != List.class) {
        throw new IllegalArgumentException("Illegal sequence javaClass: " + javaClass);
    }/*from   w  ww .  j  ava2s. c o  m*/

    int lengthVar = nextVar.next();
    int sequenceVar = nextVar.next();
    Label finalEndLabel = new Label();
    if (required) {
        mv.visitMethodInsn(INVOKESTATIC, blinkInputIName, "readUInt32",
                "(Lcom/cinnober/msgcodec/io/ByteSource;)I", false);
        mv.visitVarInsn(ISTORE, lengthVar);
    } else {
        mv.visitMethodInsn(INVOKESTATIC, blinkInputIName, "readUInt32Null",
                "(Lcom/cinnober/msgcodec/io/ByteSource;)Ljava/lang/Integer;", false);
        mv.visitInsn(DUP);
        mv.visitJumpInsn(IFNULL, finalEndLabel);
        unbox(mv, Integer.class);
        mv.visitVarInsn(ISTORE, lengthVar);
    }

    if (javaClass.isArray()) {
        mv.visitVarInsn(ILOAD, lengthVar);
        generateNewArray(mv, componentJavaClass);
    } else {
        mv.visitTypeInsn(NEW, "java/util/ArrayList");
        mv.visitInsn(DUP);
        mv.visitVarInsn(ILOAD, lengthVar);
        mv.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "(I)V", false);
    }
    mv.visitVarInsn(ASTORE, sequenceVar);

    // for loop
    Label endLabel = new Label();
    int loopVar = nextVar.next();
    mv.visitInsn(ICONST_0);
    mv.visitVarInsn(ISTORE, loopVar);
    Label loopLabel = new Label();
    mv.visitLabel(loopLabel);
    // PENDING: mv.visitFrame?
    mv.visitFrame(F_SAME, 0, null, 0, null);
    mv.visitVarInsn(ILOAD, loopVar);
    mv.visitVarInsn(ILOAD, lengthVar);
    mv.visitJumpInsn(IF_ICMPGE, endLabel);

    mv.visitVarInsn(ALOAD, sequenceVar);
    mv.visitVarInsn(ILOAD, loopVar);
    mv.visitVarInsn(ALOAD, byteSourceVar);

    // decode the element
    TypeDef.Sequence seqType = (TypeDef.Sequence) type;
    generateDecodeValue(mv, byteSourceVar, nextVar, true, seqType.getComponentType(), componentJavaClass, null,
            schema, genClassInternalName, fieldIdentifier, debugValueLabel + ".component", javaClassCodec);

    // store the value
    if (javaClass.isArray()) {
        generateArrayStore(mv, componentJavaClass);
    } else {
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/ArrayList", "add", "(ILjava/lang/Object;)V", false);
    }

    mv.visitIincInsn(loopVar, 1);
    mv.visitJumpInsn(GOTO, loopLabel);
    mv.visitLabel(endLabel);
    mv.visitFrame(F_SAME, 0, null, 0, null);
    mv.visitVarInsn(ALOAD, sequenceVar);
    mv.visitLabel(finalEndLabel);
    mv.visitFrame(F_SAME, 0, null, 0, null);
    if (javaClass.isArray()) {
        mv.visitTypeInsn(CHECKCAST, Type.getInternalName(javaClass));
    }
}

From source file:com.facebook.presto.bytecode.instruction.LabelNode.java

License:Apache License

@Override
public void accept(MethodVisitor visitor, MethodGenerationContext generationContext) {
    visitor.visitLabel(label);
}

From source file:com.github.javalbert.bytecode.utils.AsmUtils.java

License:Apache License

public static void visitDefaultConstructor(ClassWriter cw, String classTypeDescriptor) {
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();/* w  w w  .  j  a va 2  s.  co m*/
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    mv.visitInsn(RETURN);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable("this", classTypeDescriptor, null, l0, l1, 0);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}

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

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String sig, String[] exceptions) {

    MethodVisitor mv = writer().visitMethod(access, name, desc, sig, exceptions);
    mv.visitCode();// www  .  ja  v a  2 s . c om

    MethodInfo method = classInfo().getMethod(name, desc);
    AnnoInfo anno = method.getAnnoFor(ROULETTE_TYPE);
    if (anno != null) {

        Double probability = (Double) anno.getValue("probability");
        if (probability < 0.0 || probability > 1.0) {
            throw new AnnotationStateError("Probability (" + probability + ") needs to be between 0 and 1");
        }

        Type exception = (Type) anno.getValue("exception");

        // TODO: Figure out how to get the default value from the annotation itself.
        if (exception == null) {
            exception = Type.getType(RuntimeException.class);
        }

        String message = (String) anno.getValue("message");

        Label okay = new Label();

        mv.visitTypeInsn(NEW, "java/util/Random");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "java/util/Random", "<init>", "()V");

        mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/Random", "nextDouble", "()D");
        mv.visitLdcInsn(probability);
        mv.visitInsn(DCMPG);
        mv.visitJumpInsn(IFGT, okay);

        mv.visitTypeInsn(NEW, exception.getInternalName());
        mv.visitInsn(DUP);
        mv.visitLdcInsn(message);
        mv.visitMethodInsn(INVOKESPECIAL, exception.getInternalName(), "<init>", "(Ljava/lang/String;)V");
        mv.visitInsn(ATHROW);

        mv.visitLabel(okay);

        setInstrumented(true);
    }

    mv.visitEnd();
    return mv;
}

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

License:Apache License

private static void visitThisHadBetter(MethodVisitor mv, LocalVarInfo param, AnnoInfo anno, boolean be) {

    Label okay = new Label();

    Property property = valueOf((String) anno.getValue("value"));
    switch (property) {
    case NEGATIVE:
    case POSITIVE:
    case ZERO://from w  w w .  j a  va  2 s. co m
        visitComparable(mv, be, property, param, okay);
        break;
    case NULL:
        visitNullCheck(mv, be, param, okay);
        break;
    case THE_BLUE_PILL:
    case THE_RED_PILL:
        visitPill(mv, be, property, param, okay);
        break;
    case THE_STOLEN_DEATH_STAR_PLANS:
        visitDeathStarPlans(mv, be, param, okay);
        break;
    default:
        throw new AnnotationStateError("Unsupported Property: " + property);
    }

    visitException(mv, param, " is" + (be ? " not " : " ") + format(property));
    mv.visitLabel(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}./*from   ww  w .  jav 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 www .  ja va 2  s.  c om*/

    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);

    {//from  w w  w .jav a 2  s.  co m
        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   ww w  .j  a  v  a2 s. com
    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);
}