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 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;//from w w  w . jav  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 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  a v  a  2s  . 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 implementGetString(final String methodName, final String returnValue) {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, methodName,
            Type.getMethodDescriptor(string, new Type[] {}), null, null);
    mv.visitCode();//w ww .j ava2 s  .c  om
    mv.visitLdcInsn(returnValue);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}

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

License:Apache License

private void implementGetObjectCodec() {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, "getObjectCodec",
            Type.getMethodDescriptor(protobufCodec, new Type[] {}), null, null);
    mv.visitCode();//  w ww  .  ja  v a 2  s  .c  o  m
    mv.visitFieldInsn(GETSTATIC, implTypeName, F_OBJECT_CODEC, protobufCodec.getDescriptor());
    mv.visitInsn(ARETURN);
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}

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

License:Apache License

private void implementGetIndexes() {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, "getIndexes",
            Type.getMethodDescriptor(Type.getType(IndexFunction[].class), new Type[] {}), null, null);
    mv.visitCode();/*from  w ww .j  a v a2s. c o  m*/
    mv.visitFieldInsn(GETSTATIC, implTypeName, F_INDEXES, Type.getType(IndexFunction[].class).getDescriptor());
    mv.visitInsn(ARETURN);
    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();/*from  ww  w  .  j av  a 2s .  c  o  m*/
    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();//from   www . j  av  a2 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  w  ww  . j  a  v a  2 s  .  co  m*/

    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;/*w  ww.  ja 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();/*ww  w.j  av 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();
}