Example usage for org.objectweb.asm MethodVisitor visitInsn

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

Introduction

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

Prototype

public void visitInsn(final int opcode) 

Source Link

Document

Visits a zero operand instruction.

Usage

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

License:Apache License

private void implementMissingGetString(final String methodName, final String why) {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, methodName,
            Type.getMethodDescriptor(Type.getType(String.class), new Type[] {}), null, null);
    mv.visitCode();//from   ww  w.  jav  a  2s.c  o m
    throwUnsupported(mv, model.getMethodName() + " does not support " + why);
    mv.visitInsn(RETURN);
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}

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

License:Apache License

private void throwUnsupported(final MethodVisitor mv, final String message) {
    final Type eType = Type.getType(UnsupportedOperationException.class);
    mv.visitTypeInsn(NEW, eType.getInternalName());
    mv.visitInsn(DUP);
    mv.visitLdcInsn(message);/*  w w w . j  a  v a  2 s.  c o m*/
    mv.visitMethodInsn(INVOKESPECIAL, eType.getInternalName(), "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(String.class) }));
    mv.visitInsn(ATHROW);
}

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);/*from  w  w  w .  j  a  v a 2s. co 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  ww  w  . ja v a  2s.c  o 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 implementNewEntityInstance() {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, "newEntityInstance",
            Type.getMethodDescriptor(Type.getType(Object.class), new Type[] {}), null, null);
    mv.visitCode();// ww w . j  a v  a 2 s . co m
    mv.visitTypeInsn(NEW, entityType.getInternalName());
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, entityType.getInternalName(), "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}));
    mv.visitInsn(ARETURN);
    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   www  .j  a  v a  2s  . c  o  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  om*/

    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  www.java 2 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);
        }
    }
}

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

License:Apache License

private void implementKeyQuery(final KeyModel info) {
    final Type keyType = CodeGenSupport.toType(info.getField());
    final StringBuilder query = new StringBuilder();
    query.append(model.getSelectSql(dialect, REL_ALIAS));
    query.append(" WHERE ");
    int nth = 1;/*  ww w  .j ava 2s.co  m*/
    for (final Iterator<ColumnModel> i = info.getAllLeafColumns().iterator(); i.hasNext();) {
        final ColumnModel c = i.next();
        query.append(REL_ALIAS);
        query.append('.');
        query.append(c.getColumnName());
        query.append('=');
        query.append(dialect.getParameterPlaceHolder(nth++));
        if (i.hasNext()) {
            query.append(" AND ");
        }
    }

    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, info.getName(),
            Type.getMethodDescriptor(entityType, new Type[] { keyType }), null,
            new String[] { Type.getType(OrmException.class).getInternalName() });
    mv.visitCode();

    final int keyvar = 1, psvar = keyvar + keyType.getSize();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitLdcInsn(query.toString());
    mv.visitMethodInsn(INVOKEVIRTUAL, superTypeName, "prepareStatement", Type.getMethodDescriptor(
            Type.getType(PreparedStatement.class), new Type[] { Type.getType(String.class) }));
    mv.visitVarInsn(ASTORE, psvar);

    final CodeGenSupport cgs = new CodeGenSupport(mv) {
        @Override
        public void pushSqlHandle() {
            mv.visitVarInsn(ALOAD, psvar);
        }

        @Override
        public void pushFieldValue() {
            appendGetField(getFieldReference());
        }

        @Override
        protected void appendGetField(final ColumnModel c) {
            if (c.getParent() == null) {
                loadVar(keyType, keyvar);
            } else {
                super.appendGetField(c);
            }
        }
    };
    for (final ColumnModel c : info.getAllLeafColumns()) {
        cgs.setFieldReference(c);
        dialect.getSqlTypeInfo(c).generatePreparedStatementSet(cgs);
    }

    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, psvar);
    mv.visitMethodInsn(INVOKEVIRTUAL, superTypeName, "queryOne", Type.getMethodDescriptor(
            Type.getType(Object.class), new Type[] { Type.getType(PreparedStatement.class) }));
    mv.visitTypeInsn(CHECKCAST, entityType.getInternalName());
    mv.visitInsn(ARETURN);
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}

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

License:Apache License

private void overrideGetMany() {
    final KeyModel pk = model.getPrimaryKey();
    final StringBuilder query = new StringBuilder();
    query.append(model.getSelectSql(dialect, REL_ALIAS));
    query.append(" WHERE ");
    final ColumnModel pkcol = pk.getAllLeafColumns().iterator().next();
    query.append(REL_ALIAS);//from  ww  w .jav a2  s.c o  m
    query.append('.');
    query.append(pkcol.getColumnName());
    query.append(" IN");

    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, "getBySqlIn",
            Type.getMethodDescriptor(Type.getType(com.google.gwtorm.server.ResultSet.class),
                    new Type[] { Type.getType(Collection.class) }),
            null, new String[] { Type.getType(OrmException.class).getInternalName() });
    mv.visitCode();

    final int keyset = 1;
    final int psvar = 2;
    final int itrvar = 3;
    final int colvar = 4;
    final int keyvar = 5;

    mv.visitVarInsn(ALOAD, 0);
    mv.visitLdcInsn(query.toString());
    mv.visitVarInsn(ALOAD, keyset);
    mv.visitMethodInsn(INVOKEVIRTUAL, superTypeName, "prepareBySqlIn",
            Type.getMethodDescriptor(Type.getType(PreparedStatement.class),
                    new Type[] { Type.getType(String.class), Type.getType(Collection.class) }));
    mv.visitVarInsn(ASTORE, psvar);

    mv.visitVarInsn(ALOAD, keyset);
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Collection.class), "iterator",
            Type.getMethodDescriptor(Type.getType(Iterator.class), new Type[] {}));
    mv.visitVarInsn(ASTORE, itrvar);

    mv.visitInsn(ICONST_1);
    mv.visitVarInsn(ISTORE, colvar);

    final Label endbind = new Label();
    final Label again = new Label();
    mv.visitLabel(again);
    mv.visitVarInsn(ALOAD, itrvar);
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Iterator.class), "hasNext",
            Type.getMethodDescriptor(Type.BOOLEAN_TYPE, new Type[] {}));
    mv.visitJumpInsn(IFEQ, endbind);

    mv.visitVarInsn(ALOAD, itrvar);
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Iterator.class), "next",
            Type.getMethodDescriptor(Type.getType(Object.class), new Type[] {}));
    mv.visitTypeInsn(CHECKCAST, CodeGenSupport.toType(pk.getField()).getInternalName());
    mv.visitVarInsn(ASTORE, keyvar);

    final CodeGenSupport cgs = new CodeGenSupport(mv) {
        @Override
        public void pushSqlHandle() {
            mv.visitVarInsn(ALOAD, psvar);
        }

        @Override
        public void pushFieldValue() {
            appendGetField(getFieldReference());
        }

        @Override
        public void pushColumnIndex() {
            mv.visitVarInsn(ILOAD, colvar);
        }

        @Override
        protected void appendGetField(final ColumnModel c) {
            if (c.getParent() == null) {
                mv.visitVarInsn(ALOAD, keyvar);
            } else {
                super.appendGetField(c);
            }
        }
    };

    cgs.setFieldReference(pkcol);
    dialect.getSqlTypeInfo(pkcol).generatePreparedStatementSet(cgs);
    mv.visitIincInsn(colvar, 1);
    mv.visitJumpInsn(GOTO, again);

    mv.visitLabel(endbind);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, psvar);
    mv.visitMethodInsn(INVOKEVIRTUAL, superTypeName, "queryList",
            Type.getMethodDescriptor(Type.getType(com.google.gwtorm.server.ResultSet.class),
                    new Type[] { Type.getType(PreparedStatement.class) }));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}