Example usage for org.objectweb.asm MethodVisitor visitVarInsn

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

Introduction

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

Prototype

public void visitVarInsn(final int opcode, final int var) 

Source Link

Document

Visits a local variable instruction.

Usage

From source file:com.google.devtools.build.android.resources.RClassGenerator.java

License:Open Source License

private static void writeConstructor(ClassWriter classWriter) {
    MethodVisitor constructor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V",
            null, /* signature */
            null /* exceptions */);
    constructor.visitCode();/*  w  ww.j a va  2 s .  c  om*/
    constructor.visitVarInsn(Opcodes.ALOAD, 0);
    constructor.visitMethodInsn(Opcodes.INVOKESPECIAL, SUPER_CLASS, "<init>", "()V", false);
    constructor.visitInsn(Opcodes.RETURN);
    constructor.visitMaxs(1, 1);
    constructor.visitEnd();
}

From source file:com.google.devtools.build.android.resources.RClassWriter.java

License:Open Source License

private static void writeConstructor(ClassWriter classWriter) {
    MethodVisitor constructor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    constructor.visitCode();/*from   w  ww . j a  v  a2 s .c o  m*/
    constructor.visitVarInsn(Opcodes.ALOAD, 0);
    constructor.visitMethodInsn(Opcodes.INVOKESPECIAL, SUPER_CLASS, "<init>", "()V", false);
    constructor.visitInsn(Opcodes.RETURN);
    constructor.visitMaxs(1, 1);
    constructor.visitEnd();
}

From source file:com.google.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   w ww  . jav a 2  s.co m*/
 */
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 == 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 == 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.google.gwt.dev.shell.rewrite.RewriteSingleJsoImplDispatches.java

License:Apache License

/**
 * For regular Java objects that implement a SingleJsoImpl interface, write
 * instance trampoline dispatchers for mangled method names to the
 * implementing method.// w w  w.j  av a  2  s .c  om
 */
private void writeTrampoline(String stubIntr) {
    /*
     * This is almost the same kind of trampoline as the ones generated in
     * WriteJsoImpl, however there are enough small differences between the
     * semantics of the dispatches that would make a common implementation far
     * more awkward than the duplication of code.
     */
    for (String mangledName : toImplement(stubIntr)) {
        for (Method method : jsoData.getDeclarations(mangledName)) {

            Method toCall = new Method(method.getName(), method.getDescriptor());

            // Must not be final
            MethodVisitor mv = super.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, mangledName,
                    method.getDescriptor(), null, null);
            if (mv != null) {
                mv.visitCode();

                /*
                 * It just so happens that the stack and local variable sizes are the
                 * same, but they're kept distinct to aid in clarity should the
                 * dispatch logic change.
                 *
                 * These start at 1 because we need to load "this" onto the stack
                 */
                int var = 1;
                int size = 1;

                // load this
                mv.visitVarInsn(Opcodes.ALOAD, 0);

                // then the rest of the arguments
                for (Type t : toCall.getArgumentTypes()) {
                    size += t.getSize();
                    mv.visitVarInsn(t.getOpcode(Opcodes.ILOAD), var);
                    var += t.getSize();
                }

                // Make sure there's enough room for the return value
                size = Math.max(size, toCall.getReturnType().getSize());

                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, currentTypeName, toCall.getName(),
                        toCall.getDescriptor(), false);
                mv.visitInsn(toCall.getReturnType().getOpcode(Opcodes.IRETURN));
                mv.visitMaxs(size, var);
                mv.visitEnd();
            }
        }
    }
}

From source file:com.google.gwtorm.jdbc.AccessGen.java

License:Apache License

private void implementConstructor() {
    final String consName = "<init>";
    final String consDesc = Type.getMethodDescriptor(Type.VOID_TYPE,
            new Type[] { Type.getType(JdbcSchema.class) });
    final MethodVisitor mv;
    mv = cw.visitMethod(ACC_PUBLIC, consName, consDesc, null, null);
    mv.visitCode();//  w  ww .j a v a  2 s. c o  m
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKESPECIAL, superTypeName, consName, consDesc);
    mv.visitInsn(RETURN);
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}

From source file:com.google.gwtorm.jdbc.AccessGen.java

License:Apache License

private void implementPrimaryKey() {
    final KeyModel pk = model.getPrimaryKey();
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, "primaryKey",
            Type.getMethodDescriptor(Type.getType(Key.class), new Type[] { Type.getType(Object.class) }), null,
            null);/* ww w .ja v a  2  s  .c  o m*/
    mv.visitCode();
    if (pk != null && pk.getField().isNested()) {
        final ColumnModel pkf = pk.getField();
        mv.visitVarInsn(ALOAD, 1);
        mv.visitTypeInsn(CHECKCAST, entityType.getInternalName());
        mv.visitFieldInsn(GETFIELD, entityType.getInternalName(), pkf.getFieldName(),
                CodeGenSupport.toType(pkf).getDescriptor());
    } else {
        mv.visitInsn(ACONST_NULL);
    }
    mv.visitInsn(ARETURN);
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}

From source file:com.google.gwtorm.jdbc.AccessGen.java

License:Apache License

private void implementGetOne() {
    final KeyModel pk = model.getPrimaryKey();

    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, "get",
            Type.getMethodDescriptor(Type.getType(Object.class), new Type[] { Type.getType(Key.class) }), null,
            new String[] { Type.getType(OrmException.class).getInternalName() });
    mv.visitCode();/*from   w ww.j  a va 2s . co m*/
    if (pk != null && pk.getField().isNested()) {
        final Type keyType = CodeGenSupport.toType(pk.getField());
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitTypeInsn(CHECKCAST, keyType.getInternalName());
        mv.visitMethodInsn(INVOKEVIRTUAL, implTypeName, pk.getName(),
                Type.getMethodDescriptor(entityType, new Type[] { keyType }));
        mv.visitInsn(ARETURN);
    } else {
        throwUnsupported(mv, model.getMethodName() + " does not support get(Key)");
    }
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}

From source file:com.google.gwtorm.jdbc.AccessGen.java

License:Apache License

private void implementBindOne(final DmlType type) {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, type.methodName,
            Type.getMethodDescriptor(Type.VOID_TYPE,
                    new Type[] { Type.getType(PreparedStatement.class), Type.getType(Object.class) }),
            null, new String[] { Type.getType(SQLException.class).getInternalName() });
    mv.visitCode();/*from w w  w .j av a2  s . co  m*/

    if (type != DmlType.INSERT && model.getPrimaryKey() == null) {
        throwUnsupported(mv, model.getMethodName() + " has no primary key");
        mv.visitInsn(RETURN);
        mv.visitMaxs(-1, -1);
        mv.visitEnd();
    }

    mv.visitVarInsn(ALOAD, 2);
    mv.visitTypeInsn(CHECKCAST, entityType.getInternalName());
    mv.visitVarInsn(ASTORE, 2);

    final CodeGenSupport cgs = new CodeGenSupport(mv);
    cgs.setEntityType(entityType);

    for (final ColumnModel col : model.getRowVersionColumns()) {
        cgs.setFieldReference(col);
        cgs.fieldSetBegin();
        cgs.pushFieldValue();
        mv.visitInsn(ICONST_1);
        mv.visitInsn(IADD);
        cgs.fieldSetEnd();
    }
    cgs.resetColumnIndex(0);

    if (type != DmlType.DELETE) {
        final List<ColumnModel> cols = new ArrayList<ColumnModel>();
        cols.addAll(model.getDependentFields());
        cols.addAll(model.getRowVersionFields());
        for (final ColumnModel field : cols) {
            doBindOne(mv, cgs, field);
        }
    }

    for (final ColumnModel col : model.getPrimaryKeyColumns()) {
        cgs.setFieldReference(col);
        dialect.getSqlTypeInfo(col).generatePreparedStatementSet(cgs);
    }
    if (type != DmlType.INSERT) {
        for (final ColumnModel col : model.getRowVersionColumns()) {
            cgs.setFieldReference(col);
            cgs.pushSqlHandle();
            cgs.pushColumnIndex();
            cgs.pushFieldValue();
            mv.visitInsn(ICONST_1);
            mv.visitInsn(ISUB);
            cgs.invokePreparedStatementSet("Int");
        }
    }

    mv.visitInsn(RETURN);
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}

From source file:com.google.gwtorm.jdbc.AccessGen.java

License:Apache License

private void implementBindOneFetch() {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, "bindOneFetch",
            Type.getMethodDescriptor(Type.VOID_TYPE,
                    new Type[] { Type.getType(ResultSet.class), Type.getType(Object.class) }),
            null, new String[] { Type.getType(SQLException.class).getInternalName() });
    mv.visitCode();//from   w w w . ja  v a 2  s  .  c o  m

    mv.visitVarInsn(ALOAD, 2);
    mv.visitTypeInsn(CHECKCAST, entityType.getInternalName());
    mv.visitVarInsn(ASTORE, 2);

    final CodeGenSupport cgs = new CodeGenSupport(mv);
    cgs.setEntityType(entityType);

    if (model.getPrimaryKey() != null && model.getPrimaryKey().getField().isNested()) {
        final ColumnModel pkf = model.getPrimaryKey().getField();
        final Type vType = CodeGenSupport.toType(pkf);
        final int oldIdx = cgs.getColumnIndex();
        cgs.setFieldReference(pkf);
        cgs.fieldSetBegin();
        mv.visitTypeInsn(NEW, vType.getInternalName());
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, vType.getInternalName(), "<init>",
                Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}));
        cgs.fieldSetEnd();
        cgs.resetColumnIndex(oldIdx);
    }

    final List<ColumnModel> cols = new ArrayList<ColumnModel>();
    cols.addAll(model.getDependentFields());
    cols.addAll(model.getRowVersionFields());
    cols.addAll(model.getPrimaryKeyColumns());
    for (final ColumnModel field : cols) {
        doFetchOne(mv, cgs, field, -1);
    }

    mv.visitInsn(RETURN);
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}

From source file:com.google.gwtorm.jdbc.AccessGen.java

License:Apache License

private void doFetchOne(final MethodVisitor mv, final CodeGenSupport cgs, final ColumnModel field,
        final int reportLiveInto) {
    if (field.isNested()) {
        int oldIdx = cgs.getColumnIndex();
        final Type vType = CodeGenSupport.toType(field);
        final int livecnt;

        if (field.isNotNull()) {
            livecnt = -1;//from w  ww.ja  v  a2 s .  co  m
        } else {
            livecnt = cgs.newLocal();
            cgs.push(0);
            mv.visitVarInsn(ISTORE, livecnt);
        }

        cgs.setFieldReference(field);
        cgs.fieldSetBegin();
        mv.visitTypeInsn(NEW, vType.getInternalName());
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, vType.getInternalName(), "<init>",
                Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}));
        cgs.fieldSetEnd();

        cgs.resetColumnIndex(oldIdx);
        for (final ColumnModel c : field.getNestedColumns()) {
            doFetchOne(mv, cgs, c, livecnt);
        }

        if (livecnt >= 0) {
            oldIdx = cgs.getColumnIndex();

            final Label islive = new Label();
            mv.visitVarInsn(ILOAD, livecnt);
            mv.visitJumpInsn(IFNE, islive);
            cgs.setFieldReference(field);
            cgs.fieldSetBegin();
            mv.visitInsn(ACONST_NULL);
            cgs.fieldSetEnd();

            if (reportLiveInto >= 0) {
                final Label end = new Label();
                mv.visitJumpInsn(GOTO, end);
                mv.visitLabel(islive);
                mv.visitIincInsn(reportLiveInto, 1);
                mv.visitLabel(end);
            } else {
                mv.visitLabel(islive);
            }

            cgs.resetColumnIndex(oldIdx);
            cgs.freeLocal(livecnt);
        }

    } else {
        final int dupTo;
        if (reportLiveInto >= 0 && CodeGenSupport.toType(field).getSort() == Type.OBJECT) {
            dupTo = cgs.newLocal();
        } else {
            dupTo = -1;
        }

        cgs.setFieldReference(field);
        cgs.setDupOnFieldSetEnd(dupTo);
        dialect.getSqlTypeInfo(field).generateResultSetGet(cgs);

        if (reportLiveInto >= 0) {
            final Label wasnull = new Label();
            if (dupTo >= 0) {
                mv.visitVarInsn(ALOAD, dupTo);
                mv.visitJumpInsn(IFNULL, wasnull);
                cgs.freeLocal(dupTo);
            } else {
                cgs.pushSqlHandle();
                mv.visitMethodInsn(INVOKEINTERFACE, Type.getType(ResultSet.class).getInternalName(), "wasNull",
                        Type.getMethodDescriptor(Type.BOOLEAN_TYPE, new Type[] {}));
                mv.visitJumpInsn(IFNE, wasnull);
            }
            mv.visitIincInsn(reportLiveInto, 1);
            mv.visitLabel(wasnull);
        }
    }
}