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.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;//from  w  w w .  j ava2s .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);/*  w  ww  . j a v a 2  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();
}

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

License:Apache License

private void implementQuery(final QueryModel info) {
    final List<ColumnModel> pCols = info.getParameters();
    final boolean hasLimitParam = info.hasLimitParameter();
    final Type[] pTypes = new Type[pCols.size() + (hasLimitParam ? 1 : 0)];
    final int[] pVars = new int[pTypes.length];
    int nextVar = 1;
    for (int i = 0; i < pCols.size(); i++) {
        pTypes[i] = CodeGenSupport.toType(pCols.get(i));
        pVars[i] = nextVar;//  ww w.  jav a2s  . c  o  m
        nextVar += pTypes[i].getSize();
    }
    if (hasLimitParam) {
        pTypes[pTypes.length - 1] = Type.INT_TYPE;
        pVars[pTypes.length - 1] = nextVar;
        nextVar += Type.INT_TYPE.getSize();
    }

    final int psvar = nextVar++;
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, info.getName(),
            Type.getMethodDescriptor(Type.getType(com.google.gwtorm.server.ResultSet.class), pTypes), null,
            new String[] { Type.getType(OrmException.class).getInternalName() });
    mv.visitCode();

    mv.visitVarInsn(ALOAD, 0);
    mv.visitLdcInsn(info.getSelectSql(dialect, REL_ALIAS));
    mv.visitMethodInsn(INVOKEVIRTUAL, superTypeName, "prepareStatement", Type.getMethodDescriptor(
            Type.getType(PreparedStatement.class), new Type[] { Type.getType(String.class) }));
    mv.visitVarInsn(ASTORE, psvar);

    final int argIdx[] = new int[] { 0 };
    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) {
            final int n = argIdx[0];
            if (c == pCols.get(n)) {
                loadVar(pTypes[n], pVars[n]);
            } else {
                super.appendGetField(c);
            }
        }
    };
    for (final ColumnModel c : pCols) {
        if (c.isNested()) {
            for (final ColumnModel n : c.getAllLeafColumns()) {
                cgs.setFieldReference(n);
                dialect.getSqlTypeInfo(n).generatePreparedStatementSet(cgs);
            }
        } else {
            cgs.setFieldReference(c);
            dialect.getSqlTypeInfo(c).generatePreparedStatementSet(cgs);
        }
        argIdx[0]++;
    }

    if (info.hasLimit()) {
        if (hasLimitParam || !dialect.selectHasLimit()) {
            mv.visitVarInsn(ALOAD, psvar);
            if (hasLimitParam) {
                mv.visitVarInsn(ILOAD, pVars[pTypes.length - 1]);
            } else {
                cgs.push(info.getStaticLimit());
            }
            mv.visitMethodInsn(INVOKEINTERFACE, Type.getType(PreparedStatement.class).getInternalName(),
                    "setMaxRows", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.INT_TYPE }));
        }
    }

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

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

License:Apache License

private void implementConstructor() {
    final String consName = "<init>";
    final String consDesc = Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { schemaType });
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, consName, consDesc, null, null);
    mv.visitCode();//from  w w w. j  av  a2s  .  c  o  m
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKESPECIAL, accessType.getInternalName(), consName, consDesc);
    mv.visitInsn(RETURN);
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}

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

License:Apache License

private void implementPrimaryKey() {
    final ColumnModel f = key.getField();
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, "primaryKey",
            Type.getMethodDescriptor(ormKey, new Type[] { object }), null, null);
    mv.visitCode();/* w ww  . j  a va2  s . c om*/
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(CHECKCAST, entityType.getInternalName());
    mv.visitFieldInsn(GETFIELD, entityType.getInternalName(), f.getFieldName(),
            CodeGenSupport.toType(f).getDescriptor());
    mv.visitInsn(ARETURN);
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}

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

License:Apache License

private void implementEncodePrimaryKey() throws OrmException {
    final List<ColumnModel> pCols = Collections.singletonList(key.getField());
    final Type argType = CodeGenSupport.toType(key.getField());
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, "encodePrimaryKey",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { indexKeyBuilder, ormKey }), null, null);
    mv.visitCode();/*  w w  w.  j  a v  a 2 s .  c om*/

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

    final QueryCGS cgs = new QueryCGS(mv, new Type[] { argType }, pCols, new int[] { 2 }, 1);
    for (ColumnModel f : pCols) {
        IndexFunctionGen.encodeField(new QueryModel.OrderBy(f, false), mv, cgs);
    }

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

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

License:Apache License

private void implementKeyQuery(KeyModel key) {
    final Type keyType = CodeGenSupport.toType(key.getField());
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, key.getName(),
            Type.getMethodDescriptor(entityType, new Type[] { keyType }), null,
            new String[] { Type.getType(OrmException.class).getInternalName() });
    mv.visitCode();//from ww w  . j a  v a 2s.c  om

    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKESPECIAL, accessType.getInternalName(), "get",
            Type.getMethodDescriptor(object, new Type[] { ormKey }));
    mv.visitTypeInsn(CHECKCAST, entityType.getInternalName());

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

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

License:Apache License

private void implementQuery(final QueryModel info) throws OrmException {
    final List<ColumnModel> pCols = info.getParameters();
    final boolean hasLimitParam = info.hasLimitParameter();
    final Type[] pTypes = new Type[pCols.size() + (hasLimitParam ? 1 : 0)];
    final int[] pVars = new int[pTypes.length];
    int nextVar = 1;
    for (int i = 0; i < pCols.size(); i++) {
        pTypes[i] = CodeGenSupport.toType(pCols.get(i));
        pVars[i] = nextVar;/*from   w w w  .  j a v  a2  s.  c  o  m*/
        nextVar += pTypes[i].getSize();
    }
    if (hasLimitParam) {
        pTypes[pTypes.length - 1] = Type.INT_TYPE;
        pVars[pTypes.length - 1] = nextVar;
        nextVar += Type.INT_TYPE.getSize();
    }

    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, info.getName(),
            Type.getMethodDescriptor(resultSet, pTypes), null, new String[] { ormException.getInternalName() });
    mv.visitCode();

    final List<Tree> ops = compareOpsOnly(info.getParseTree());

    // Generate fromKey
    //
    final int fromBuf = nextVar++;
    mv.visitTypeInsn(NEW, indexKeyBuilder.getInternalName());
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, indexKeyBuilder.getInternalName(), "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}));
    mv.visitVarInsn(ASTORE, fromBuf);

    QueryCGS cgs = new QueryCGS(mv, pTypes, pCols, pVars, fromBuf);
    encodeFields(info, ops, mv, cgs, true /* fromKey */);

    // Generate toKey
    //
    final int toBuf = nextVar++;
    mv.visitTypeInsn(NEW, indexKeyBuilder.getInternalName());
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, indexKeyBuilder.getInternalName(), "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}));
    mv.visitVarInsn(ASTORE, toBuf);

    cgs = new QueryCGS(mv, pTypes, pCols, pVars, toBuf);
    encodeFields(info, ops, mv, cgs, false /* fromKey */);
    cgs.infinity();

    // Make the scan call
    //
    mv.visitVarInsn(ALOAD, 0);
    if (needsIndexFunction(info)) {
        mv.visitFieldInsn(GETSTATIC, implTypeName, "index_" + info.getName(), indexFunction.getDescriptor());
    }

    mv.visitVarInsn(ALOAD, fromBuf);
    mv.visitMethodInsn(INVOKEVIRTUAL, indexKeyBuilder.getInternalName(), "toByteArray",
            Type.getMethodDescriptor(byteArray, new Type[] {}));

    mv.visitVarInsn(ALOAD, toBuf);
    mv.visitMethodInsn(INVOKEVIRTUAL, indexKeyBuilder.getInternalName(), "toByteArray",
            Type.getMethodDescriptor(byteArray, new Type[] {}));

    // Set the limit on the number of results.
    //
    if (info.hasLimit()) {
        if (hasLimitParam) {
            mv.visitVarInsn(ILOAD, pVars[pTypes.length - 1]);
        } else {
            cgs.push(info.getStaticLimit());
        }
    } else {
        cgs.push(0);
    }

    // Only keep order if there is an order by clause present
    //
    cgs.push(info.hasOrderBy() ? 1 : 0);

    if (needsIndexFunction(info)) {
        mv.visitMethodInsn(INVOKEVIRTUAL, accessType.getInternalName(), "scanIndex",
                Type.getMethodDescriptor(resultSet,
                        new Type[] { indexFunction, byteArray, byteArray, Type.INT_TYPE, Type.BOOLEAN_TYPE }));
    } else {
        // No where and no order by clause? Use the primary key instead.
        //
        mv.visitMethodInsn(INVOKEVIRTUAL, accessType.getInternalName(), "scanPrimaryKey",
                Type.getMethodDescriptor(resultSet,
                        new Type[] { byteArray, byteArray, Type.INT_TYPE, Type.BOOLEAN_TYPE }));
    }

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

From source file:com.google.gwtorm.nosql.IndexFunctionGen.java

License:Apache License

private void implementConstructor() {
    final String consName = "<init>";
    final String consDesc = Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {});
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, consName, consDesc, null, null);
    mv.visitCode();//from w  ww .  ja  v  a 2 s  .  c o  m

    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, superTypeName, consName, consDesc);

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

From source file:com.google.gwtorm.nosql.IndexFunctionGen.java

License:Apache License

private void implementIncludes() throws OrmException {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "includes",
            Type.getMethodDescriptor(Type.BOOLEAN_TYPE, new Type[] { object }), null, null);
    mv.visitCode();/*w w w  .  j  a v  a  2s  .co m*/
    final IncludeCGS cgs = new IncludeCGS(mv);
    cgs.setEntityType(pojoType);

    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(CHECKCAST, pojoType.getInternalName());
    mv.visitVarInsn(ASTORE, 1);

    Set<ColumnModel> checked = new HashSet<ColumnModel>();
    for (QueryModel.OrderBy orderby : myFields) {
        checkNotNullFields(Collections.singleton(orderby.column), checked, mv, cgs);
    }

    final Tree parseTree = query.getParseTree();
    if (parseTree != null) {
        checkConstants(parseTree, mv, cgs);
    }

    cgs.push(1);
    mv.visitInsn(IRETURN);

    mv.visitLabel(cgs.no);
    cgs.push(0);
    mv.visitInsn(IRETURN);

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