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:erjang.ETuple.java

License:Apache License

private static void create_cast2(ClassAdapter cw, int n) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "cast",
            "(L" + Type.getInternalName(EObject.class) + ";)L" + ETUPLE_NAME + n + ";", null, null);
    mv.visitCode();//from ww  w .j  a v  a2s  .  co m

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.DUP);
    mv.visitTypeInsn(Opcodes.INSTANCEOF, ETUPLE_NAME + n);

    Label fail = new Label();

    mv.visitJumpInsn(Opcodes.IFEQ, fail);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitTypeInsn(Opcodes.CHECKCAST, ETUPLE_NAME + n);
    mv.visitInsn(Opcodes.ARETURN);

    mv.visitLabel(fail);
    mv.visitInsn(Opcodes.ACONST_NULL);
    mv.visitInsn(Opcodes.ARETURN);

    mv.visitMaxs(2, 2);
    mv.visitEnd();
}

From source file:fr.insalyon.citi.golo.compiler.JavaBytecodeStructGenerator.java

License:Apache License

private void makeSetMethod(ClassWriter classWriter, Struct struct) {
    String owner = struct.getPackageAndClass().toJVMType();
    MethodVisitor visitor = classWriter.visitMethod(ACC_PUBLIC, "set",
            "(Ljava/lang/String;Ljava/lang/Object;)Lgololang/GoloStruct;", null, null);
    visitor.visitCode();//from  ww w.  j  a  v a2  s  .c  om
    insertPrivateElementCheck(struct, visitor);
    Label nextCase = new Label();
    for (String member : struct.getMembers()) {
        visitor.visitLdcInsn(member);
        visitor.visitVarInsn(ALOAD, 1);
        visitor.visitJumpInsn(IF_ACMPNE, nextCase);
        visitor.visitVarInsn(ALOAD, 0);
        visitor.visitVarInsn(ALOAD, 2);
        visitor.visitMethodInsn(INVOKEVIRTUAL, owner, member, "(Ljava/lang/Object;)Lgololang/GoloStruct;");
        visitor.visitInsn(ARETURN);
        visitor.visitLabel(nextCase);
        nextCase = new Label();
    }
    insertUnknowElementCode(struct, visitor);
    visitor.visitMaxs(0, 0);
    visitor.visitEnd();
}

From source file:fr.insalyon.citi.golo.compiler.JavaBytecodeStructGenerator.java

License:Apache License

private void makeGetMethod(ClassWriter classWriter, Struct struct) {
    String owner = struct.getPackageAndClass().toJVMType();
    MethodVisitor visitor = classWriter.visitMethod(ACC_PUBLIC, "get", "(Ljava/lang/String;)Ljava/lang/Object;",
            null, null);/*from w w w .java2s  .c  o  m*/
    visitor.visitCode();
    insertPrivateElementCheck(struct, visitor);
    Label nextCase = new Label();
    for (String member : struct.getMembers()) {
        visitor.visitLdcInsn(member);
        visitor.visitVarInsn(ALOAD, 1);
        visitor.visitJumpInsn(IF_ACMPNE, nextCase);
        visitor.visitVarInsn(ALOAD, 0);
        visitor.visitMethodInsn(INVOKEVIRTUAL, owner, member, "()Ljava/lang/Object;");
        visitor.visitInsn(ARETURN);
        visitor.visitLabel(nextCase);
        nextCase = new Label();
    }
    insertUnknowElementCode(struct, visitor);
    visitor.visitMaxs(0, 0);
    visitor.visitEnd();
}

From source file:fr.insalyon.citi.golo.compiler.JavaBytecodeStructGenerator.java

License:Apache License

private void insertPrivateElementCheck(Struct struct, MethodVisitor visitor) {
    Label afterPrivateCheck = new Label();
    visitor.visitVarInsn(ALOAD, 1);/*from   w  w w . ja  v a  2s . c  o m*/
    visitor.visitLdcInsn("_");
    visitor.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "startsWith", "(Ljava/lang/String;)Z");
    visitor.visitJumpInsn(IFEQ, afterPrivateCheck);
    visitor.visitTypeInsn(NEW, "java/lang/IllegalArgumentException");
    visitor.visitInsn(DUP);
    visitor.visitLdcInsn("Private member of " + struct.getPackageAndClass().toString());
    visitor.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalArgumentException", "<init>",
            "(Ljava/lang/String;)V");
    visitor.visitInsn(ATHROW);
    visitor.visitLabel(afterPrivateCheck);
}

From source file:fr.insalyon.citi.golo.compiler.JavaBytecodeStructGenerator.java

License:Apache License

private void makeEquals(ClassWriter classWriter, Struct struct) {
    String owner = struct.getPackageAndClass().toJVMType();
    MethodVisitor visitor = classWriter.visitMethod(ACC_PUBLIC, "equals", "(Ljava/lang/Object;)Z", null, null);
    Label notFrozenLabel = new Label();
    Label falseLabel = new Label();
    Label sameTypeLabel = new Label();
    visitor.visitCode();//  w w  w .j a  v  a2s  . c  om
    visitor.visitVarInsn(ALOAD, 0);
    visitor.visitFieldInsn(GETFIELD, owner, $_frozen, "Z");
    visitor.visitJumpInsn(IFNE, notFrozenLabel);
    // super.equals()
    visitor.visitVarInsn(ALOAD, 0);
    visitor.visitVarInsn(ALOAD, 1);
    visitor.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "equals", "(Ljava/lang/Object;)Z");
    visitor.visitInsn(IRETURN);
    // The receiver is frozen
    visitor.visitLabel(notFrozenLabel);
    visitor.visitVarInsn(ALOAD, 1);
    visitor.visitTypeInsn(INSTANCEOF, owner);
    visitor.visitJumpInsn(IFNE, sameTypeLabel);
    visitor.visitJumpInsn(GOTO, falseLabel);
    // The argument is of the same type, too
    visitor.visitLabel(sameTypeLabel);
    visitor.visitVarInsn(ALOAD, 1);
    visitor.visitTypeInsn(CHECKCAST, owner);
    visitor.visitFieldInsn(GETFIELD, owner, $_frozen, "Z");
    visitor.visitJumpInsn(IFEQ, falseLabel);
    // The argument is not frozen
    for (String member : struct.getMembers()) {
        visitor.visitVarInsn(ALOAD, 0);
        visitor.visitFieldInsn(GETFIELD, owner, member, "Ljava/lang/Object;");
        visitor.visitVarInsn(ALOAD, 1);
        visitor.visitTypeInsn(CHECKCAST, owner);
        visitor.visitFieldInsn(GETFIELD, owner, member, "Ljava/lang/Object;");
        visitor.visitMethodInsn(INVOKESTATIC, "java/util/Objects", "equals",
                "(Ljava/lang/Object;Ljava/lang/Object;)Z");
        visitor.visitJumpInsn(IFEQ, falseLabel);
    }
    visitor.visitInsn(ICONST_1);
    visitor.visitInsn(IRETURN);
    // False
    visitor.visitLabel(falseLabel);
    visitor.visitInsn(ICONST_0);
    visitor.visitInsn(IRETURN);
    visitor.visitMaxs(0, 0);
    visitor.visitEnd();
}

From source file:fr.insalyon.citi.golo.compiler.JavaBytecodeStructGenerator.java

License:Apache License

private void makeHashCode(ClassWriter classWriter, Struct struct) {
    String owner = struct.getPackageAndClass().toJVMType();
    MethodVisitor visitor = classWriter.visitMethod(ACC_PUBLIC, "hashCode", "()I", null, null);
    Label notFrozenLabel = new Label();
    visitor.visitCode();/* w w w .ja va2 s  .c om*/
    visitor.visitVarInsn(ALOAD, 0);
    visitor.visitFieldInsn(GETFIELD, owner, $_frozen, "Z");
    visitor.visitJumpInsn(IFNE, notFrozenLabel);
    // super.hashCode()
    visitor.visitVarInsn(ALOAD, 0);
    visitor.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "hashCode", "()I");
    visitor.visitInsn(IRETURN);
    // The receiver is frozen
    visitor.visitLabel(notFrozenLabel);
    loadInteger(visitor, struct.getMembers().size());
    visitor.visitTypeInsn(ANEWARRAY, "java/lang/Object");
    int i = 0;
    for (String member : struct.getMembers()) {
        visitor.visitInsn(DUP);
        loadInteger(visitor, i);
        visitor.visitVarInsn(ALOAD, 0);
        visitor.visitFieldInsn(GETFIELD, owner, member, "Ljava/lang/Object;");
        visitor.visitInsn(AASTORE);
        i = i + 1;
    }
    visitor.visitMethodInsn(INVOKESTATIC, "java/util/Objects", "hash", "([Ljava/lang/Object;)I");
    visitor.visitInsn(IRETURN);
    visitor.visitMaxs(0, 0);
    visitor.visitEnd();
}

From source file:fr.insalyon.citi.golo.compiler.JavaBytecodeStructGenerator.java

License:Apache License

private void makeSetter(ClassWriter classWriter, String owner, String name) {
    int accessFlag = name.startsWith("_") ? ACC_PRIVATE : ACC_PUBLIC;
    MethodVisitor visitor = classWriter.visitMethod(accessFlag, name,
            "(Ljava/lang/Object;)Lgololang/GoloStruct;", null, null);
    visitor.visitCode();//from   ww  w  .ja va  2s  .com
    visitor.visitVarInsn(ALOAD, 0);
    visitor.visitFieldInsn(GETFIELD, owner, $_frozen, "Z");
    Label setLabel = new Label();
    visitor.visitJumpInsn(IFEQ, setLabel);
    visitor.visitTypeInsn(NEW, "java/lang/IllegalStateException");
    visitor.visitInsn(DUP);
    visitor.visitLdcInsn("The struct instance is frozen");
    visitor.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalStateException", "<init>",
            "(Ljava/lang/String;)V");
    visitor.visitInsn(ATHROW);
    visitor.visitLabel(setLabel);
    visitor.visitVarInsn(ALOAD, 0);
    visitor.visitVarInsn(ALOAD, 1);
    visitor.visitFieldInsn(PUTFIELD, owner, name, "Ljava/lang/Object;");
    visitor.visitVarInsn(ALOAD, 0);
    visitor.visitInsn(ARETURN);
    visitor.visitMaxs(0, 0);
    visitor.visitEnd();
}

From source file:fr.insalyon.citi.golo.compiler.JavaBytecodeUnionGenerator.java

License:Apache License

private void makeEquals(ClassWriter cw, Union.Value value) {
    String target = value.getPackageAndClass().toJVMType();
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "equals", "(Ljava/lang/Object;)Z", null, null);
    Label notSameInstance = new Label();
    Label notNull = new Label();
    Label sameType = new Label();
    Label allAttrsEquals = new Label();
    Label attrNotEqual = new Label();
    mv.visitCode();/*from   ww w. ja va2 s  .c  o  m*/

    // if (other == this) { return true; }
    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitJumpInsn(IF_ACMPNE, notSameInstance);
    mv.visitInsn(ICONST_1);
    mv.visitInsn(IRETURN);
    mv.visitLabel(notSameInstance);

    // if (other == null) { return false; }
    mv.visitVarInsn(ALOAD, 1);
    mv.visitJumpInsn(IFNONNULL, notNull);
    mv.visitInsn(ICONST_0);
    mv.visitInsn(IRETURN);
    mv.visitLabel(notNull);

    // if (!(other instanceof <value>)) { return false; }
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(INSTANCEOF, target);
    mv.visitJumpInsn(IFNE, sameType);
    mv.visitInsn(ICONST_0);
    mv.visitInsn(IRETURN);
    mv.visitLabel(sameType);

    // cast other to <value>
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(CHECKCAST, target);
    mv.visitVarInsn(ASTORE, 2);

    // java.util.Objects.equals(<member>, other.<member>)
    for (String member : value.getMembers()) {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, target, member, "Ljava/lang/Object;");
        mv.visitVarInsn(ALOAD, 2);
        mv.visitFieldInsn(GETFIELD, target, member, "Ljava/lang/Object;");
        mv.visitMethodInsn(INVOKESTATIC, "java/util/Objects", "equals",
                "(Ljava/lang/Object;Ljava/lang/Object;)Z", false);
        mv.visitJumpInsn(IFEQ, attrNotEqual);
    }
    mv.visitInsn(ICONST_1);
    mv.visitJumpInsn(GOTO, allAttrsEquals);
    mv.visitLabel(attrNotEqual);
    mv.visitInsn(ICONST_0);
    mv.visitLabel(allAttrsEquals);
    mv.visitInsn(IRETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:fr.insalyon.citi.golo.compiler.JavaBytecodeUtils.java

License:Apache License

static void visitLine(GoloElement element, MethodVisitor visitor) {
    if (element.hasASTNode()) {
        Label label = new Label();
        visitor.visitLabel(label);
        visitor.visitLineNumber(element.getPositionInSourceCode().getLine(), label);
    }/*from   w  ww .  j a  v  a2s .  co m*/
}

From source file:fr.liglab.adele.cilia.dependency.ProxyGenerator.java

License:Apache License

/**
 * Generates a delegated method./*from  w  w w . j  a  v a2s .  c  om*/
 *
 * @param cw        the class writer
 * @param method    the method object to delegate
 * @param className the generated class name
 * @param itfName   the internal specification class name
 */
private static void generateDelegator(ClassWriter cw, Method method, String className, String itfName) {
    String methodName = method.getName();
    String desc = Type.getMethodDescriptor(method);
    String[] exceptions = getInternalClassNames(method.getExceptionTypes());
    int modifiers = method.getModifiers() & ~(Modifier.ABSTRACT | Modifier.NATIVE | Modifier.SYNCHRONIZED);
    Type[] types = Type.getArgumentTypes(method);

    int freeRoom = 1;
    for (int t = 0; t < types.length; t++) {
        freeRoom = freeRoom + types[t].getSize();
    }

    MethodVisitor mv = cw.visitMethod(modifiers, methodName, desc, null, exceptions);
    mv.visitCode();

    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, DEPENDENCY, DEPENDENCY_DESC); // The dependency is on the stack.
    mv.visitMethodInsn(INVOKEVIRTUAL, DEPENDENCY_INTERNAL_NAME, "getService", // Call getService
            "()Ljava/lang/Object;"); // The service object is on the stack.
    int varSvc = freeRoom;
    freeRoom = freeRoom + 1; // Object Reference.
    mv.visitVarInsn(ASTORE, varSvc); // Store the service object.

    Label notNull = new Label();
    Label isNull = new Label();
    mv.visitVarInsn(ALOAD, varSvc); // Load the service
    mv.visitJumpInsn(IFNONNULL, notNull); // If not null go to not null
    // Null branch - throw the exception
    mv.visitLabel(isNull);
    mv.visitTypeInsn(NEW, "java/lang/RuntimeException");
    mv.visitInsn(DUP);
    mv.visitLdcInsn("No service available");
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "(Ljava/lang/String;)V");
    mv.visitInsn(ATHROW);
    // End of the null branch

    // Not null, go one the execution
    mv.visitLabel(notNull);

    // Invoke the method on the service object.
    mv.visitVarInsn(ALOAD, varSvc);
    // Push argument on the stack.
    int i = 1; // Arguments. (non static method)
    for (int t = 0; t < types.length; t++) {
        mv.visitVarInsn(types[t].getOpcode(ILOAD), i);
        i = i + types[t].getSize();
    }
    // Invocation
    mv.visitMethodInsn(INVOKEINTERFACE, itfName, methodName, desc);

    // Return the result
    Type returnType = Type.getReturnType(desc);
    if (returnType.getSort() != Type.VOID) {
        mv.visitInsn(returnType.getOpcode(IRETURN));
    } else {
        mv.visitInsn(RETURN);
    }

    // End of the method.
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}