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.nosql.IndexFunctionGen.java

License:Apache License

private void implementGetName() {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, "getName",
            Type.getMethodDescriptor(Type.getType(String.class), new Type[] {}), null, null);
    mv.visitCode();/*  w  w  w.  j  av a2s.co  m*/
    mv.visitLdcInsn(query.getName());
    mv.visitInsn(ARETURN);
    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();//from  w  ww .  j av a2  s.com
    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();
}

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

License:Apache License

private void implementEncode() throws OrmException {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "encode",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { indexKeyBuilder, object }), null, null);
    mv.visitCode();/* w  ww  . j a  v a  2s .  c  om*/
    final EncodeCGS cgs = new EncodeCGS(mv);
    cgs.setEntityType(pojoType);

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

    encodeFields(myFields, mv, cgs);

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

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

License:Apache License

private static void encodeScalar(QueryModel.OrderBy f, final MethodVisitor mv, final EncodeCGS cgs)
        throws OrmException {
    String method = f.descending ? "desc" : "add";
    ColumnModel c = f.column;/*ww w  . j a  v  a  2  s.  com*/
    cgs.setFieldReference(c);

    switch (Type.getType(c.getPrimitiveType()).getSort()) {
    case Type.BOOLEAN:
    case Type.BYTE:
    case Type.SHORT:
    case Type.CHAR:
    case Type.INT:
        cgs.pushBuilder();
        cgs.pushFieldValue();
        mv.visitInsn(I2L);
        mv.visitMethodInsn(INVOKEVIRTUAL, indexKeyBuilder.getInternalName(), method,
                Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.LONG_TYPE }));
        break;

    case Type.LONG:
        cgs.pushBuilder();
        cgs.pushFieldValue();
        mv.visitMethodInsn(INVOKEVIRTUAL, indexKeyBuilder.getInternalName(), method,
                Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.LONG_TYPE }));
        break;

    case Type.ARRAY:
    case Type.OBJECT: {
        if (c.getPrimitiveType() == byte[].class) {
            cgs.pushBuilder();
            cgs.pushFieldValue();
            mv.visitMethodInsn(INVOKEVIRTUAL, indexKeyBuilder.getInternalName(), method,
                    Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(byte[].class) }));

        } else if (c.getPrimitiveType() == String.class) {
            cgs.pushBuilder();
            cgs.pushFieldValue();
            mv.visitMethodInsn(INVOKEVIRTUAL, indexKeyBuilder.getInternalName(), method,
                    Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { string }));

        } else if (c.getPrimitiveType() == java.sql.Timestamp.class
                || c.getPrimitiveType() == java.util.Date.class
                || c.getPrimitiveType() == java.sql.Date.class) {
            cgs.pushBuilder();
            cgs.pushFieldValue();
            String tsType = Type.getType(c.getPrimitiveType()).getInternalName();
            mv.visitMethodInsn(INVOKEVIRTUAL, tsType, "getTime",
                    Type.getMethodDescriptor(Type.LONG_TYPE, new Type[] {}));
            mv.visitMethodInsn(INVOKEVIRTUAL, indexKeyBuilder.getInternalName(), method,
                    Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.LONG_TYPE }));
        } else {
            throw new OrmException(
                    "Type " + c.getPrimitiveType() + " not supported for field " + c.getPathToFieldName());
        }
        break;
    }

    default:
        throw new OrmException(
                "Type " + c.getPrimitiveType() + " not supported for field " + c.getPathToFieldName());
    }
}

From source file:com.google.gwtorm.protobuf.CodecGen.java

License:Apache License

private void implementSizeof() throws OrmException {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "sizeof",
            Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { object }), null, new String[] {});
    mv.visitCode();/*from w  w w  . ja v a  2  s . co  m*/
    final SizeofCGS cgs = new SizeofCGS(mv);
    cgs.setEntityType(pojoType);

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

    cgs.push(0);
    mv.visitVarInsn(ISTORE, cgs.sizeVar);
    sizeofMessage(myFields, mv, cgs);

    mv.visitVarInsn(ILOAD, cgs.sizeVar);
    mv.visitInsn(IRETURN);
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}

From source file:com.google.gwtorm.protobuf.CodecGen.java

License:Apache License

private static void sizeofScalar(final MethodVisitor mv, final SizeofCGS cgs, final JavaColumnModel f)
        throws OrmException {
    cgs.setFieldReference(f);/*from   w  w  w.  j  av  a 2s . c o m*/

    switch (Type.getType(f.getPrimitiveType()).getSort()) {
    case Type.BOOLEAN:
        cgs.push(f.getColumnID());
        cgs.pushFieldValue();
        cgs.inc("computeBoolSize", Type.INT_TYPE, Type.BOOLEAN_TYPE);
        break;

    case Type.CHAR:
        cgs.push(f.getColumnID());
        cgs.pushFieldValue();
        cgs.inc("computeUInt32Size", Type.INT_TYPE, Type.INT_TYPE);
        break;

    case Type.BYTE:
    case Type.SHORT:
    case Type.INT:
        cgs.push(f.getColumnID());
        cgs.pushFieldValue();
        cgs.inc("computeSInt32Size", Type.INT_TYPE, Type.INT_TYPE);
        break;

    case Type.FLOAT:
        cgs.push(f.getColumnID());
        cgs.pushFieldValue();
        cgs.inc("computeFloatSize", Type.INT_TYPE, Type.FLOAT_TYPE);
        break;

    case Type.DOUBLE:
        cgs.push(f.getColumnID());
        cgs.pushFieldValue();
        cgs.inc("computeDoubleSize", Type.INT_TYPE, Type.DOUBLE_TYPE);
        break;

    case Type.LONG:
        cgs.push(f.getColumnID());
        cgs.pushFieldValue();
        cgs.inc("computeSInt64", Type.INT_TYPE, Type.LONG_TYPE);
        break;

    case Type.ARRAY:
    case Type.OBJECT: {
        final Label end = new Label();
        cgs.pushFieldValue();
        mv.visitJumpInsn(IFNULL, end);

        if (f.getPrimitiveType() == byte[].class) {
            cgs.push(f.getColumnID());
            cgs.inc("computeTagSize", Type.INT_TYPE);

            cgs.pushFieldValue();
            mv.visitInsn(ARRAYLENGTH);
            cgs.inc("computeRawVarint32Size", Type.INT_TYPE);

            cgs.pushFieldValue();
            mv.visitInsn(ARRAYLENGTH);
            cgs.inc();

        } else if (f.getPrimitiveType() == String.class) {
            cgs.push(f.getColumnID());
            cgs.pushFieldValue();
            cgs.inc("computeStringSize", Type.INT_TYPE, string);

        } else if (f.getPrimitiveType() == java.sql.Timestamp.class
                || f.getPrimitiveType() == java.util.Date.class
                || f.getPrimitiveType() == java.sql.Date.class) {
            cgs.push(f.getColumnID());
            String tsType = Type.getType(f.getPrimitiveType()).getInternalName();
            mv.visitMethodInsn(INVOKEVIRTUAL, tsType, "getTime",
                    Type.getMethodDescriptor(Type.LONG_TYPE, new Type[] {}));
            cgs.inc("computeFixed64Size", Type.INT_TYPE, Type.LONG_TYPE);

        } else {
            throw new OrmException(
                    "Type " + f.getPrimitiveType() + " not supported for field " + f.getPathToFieldName());
        }
        mv.visitLabel(end);
        break;
    }

    default:
        throw new OrmException(
                "Type " + f.getPrimitiveType() + " not supported for field " + f.getPathToFieldName());
    }
}

From source file:com.google.gwtorm.protobuf.CodecGen.java

License:Apache License

private void implementEncode() throws OrmException {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "encode",
            Type.getMethodDescriptor(byteString, new Type[] { object }), null, new String[] {});
    mv.visitCode();/*  ww w.  j a v a 2s.com*/
    final EncodeCGS cgs = new EncodeCGS(mv);
    cgs.setEntityType(pojoType);

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

    encodeMessage(myFields, mv, cgs);

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

From source file:com.google.gwtorm.protobuf.CodecGen.java

License:Apache License

private static void encodeScalar(final MethodVisitor mv, final EncodeCGS cgs, final JavaColumnModel f)
        throws OrmException {
    cgs.setFieldReference(f);//  w  ww .j  a v  a  2 s .c  o m

    switch (Type.getType(f.getPrimitiveType()).getSort()) {
    case Type.BOOLEAN:
        cgs.pushCodedOutputStream();
        cgs.push(f.getColumnID());
        cgs.pushFieldValue();
        cgs.write("writeBool", Type.BOOLEAN_TYPE);
        break;

    case Type.CHAR:
        cgs.pushCodedOutputStream();
        cgs.push(f.getColumnID());
        cgs.pushFieldValue();
        cgs.write("writeUInt32", Type.INT_TYPE);
        break;

    case Type.BYTE:
    case Type.SHORT:
    case Type.INT:
        cgs.pushCodedOutputStream();
        cgs.push(f.getColumnID());
        cgs.pushFieldValue();
        cgs.write("writeSInt32", Type.INT_TYPE);
        break;

    case Type.FLOAT:
        cgs.pushCodedOutputStream();
        cgs.push(f.getColumnID());
        cgs.pushFieldValue();
        cgs.write("writeFloat", Type.FLOAT_TYPE);
        break;

    case Type.DOUBLE:
        cgs.pushCodedOutputStream();
        cgs.push(f.getColumnID());
        cgs.pushFieldValue();
        cgs.write("writeDouble", Type.DOUBLE_TYPE);
        break;

    case Type.LONG:
        cgs.pushCodedOutputStream();
        cgs.push(f.getColumnID());
        cgs.pushFieldValue();
        cgs.write("writeSInt64", Type.LONG_TYPE);
        break;

    case Type.ARRAY:
    case Type.OBJECT: {
        final Label end = new Label();
        cgs.pushFieldValue();
        mv.visitJumpInsn(IFNULL, end);

        if (f.getPrimitiveType() == byte[].class) {
            cgs.pushCodedOutputStream();
            cgs.push(f.getColumnID());
            cgs.push(WireFormat.FieldType.BYTES.getWireType());
            mv.visitMethodInsn(INVOKEVIRTUAL, codedOutputStream.getInternalName(), "writeTag",
                    Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.INT_TYPE, Type.INT_TYPE }));

            cgs.pushCodedOutputStream();
            cgs.pushFieldValue();
            mv.visitInsn(ARRAYLENGTH);
            mv.visitMethodInsn(INVOKEVIRTUAL, codedOutputStream.getInternalName(), "writeRawVarint32",
                    Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.INT_TYPE }));

            cgs.pushCodedOutputStream();
            cgs.pushFieldValue();
            mv.visitMethodInsn(INVOKEVIRTUAL, codedOutputStream.getInternalName(), "writeRawBytes",
                    Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(byte[].class) }));

        } else {
            cgs.pushCodedOutputStream();
            cgs.push(f.getColumnID());
            cgs.pushFieldValue();

            if (f.getPrimitiveType() == String.class) {
                cgs.write("writeString", string);

            } else if (f.getPrimitiveType() == java.sql.Timestamp.class
                    || f.getPrimitiveType() == java.util.Date.class
                    || f.getPrimitiveType() == java.sql.Date.class) {
                String tsType = Type.getType(f.getPrimitiveType()).getInternalName();
                mv.visitMethodInsn(INVOKEVIRTUAL, tsType, "getTime",
                        Type.getMethodDescriptor(Type.LONG_TYPE, new Type[] {}));
                cgs.write("writeFixed64", Type.LONG_TYPE);

            } else {
                throw new OrmException(
                        "Type " + f.getPrimitiveType() + " not supported for field " + f.getPathToFieldName());
            }
        }
        mv.visitLabel(end);
        break;
    }

    default:
        throw new OrmException(
                "Type " + f.getPrimitiveType() + " not supported for field " + f.getPathToFieldName());
    }
}

From source file:com.google.gwtorm.protobuf.CodecGen.java

License:Apache License

private void implementDecode() throws OrmException {
    final Type retType = object;
    final MethodVisitor mv = cw.visitMethod(ACC_PROTECTED, "decode",
            Type.getMethodDescriptor(retType, new Type[] { codedInputStream }), null, new String[] {});
    mv.visitCode();/*from  www  . j a  v a  2 s  . co m*/
    final DecodeCGS cgs = new DecodeCGS(mv);

    cgs.setEntityType(pojoType);

    mv.visitTypeInsn(NEW, pojoType.getInternalName());
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, pojoType.getInternalName(), "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}));
    mv.visitVarInsn(ASTORE, cgs.objVar);

    final int tagVar = cgs.newLocal();
    decodeMessage(myFields, mv, cgs);

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

From source file:com.google.gwtorm.protobuf.CodecGen.java

License:Apache License

private static void decodeMessage(final JavaColumnModel[] myFields, final MethodVisitor mv, final DecodeCGS cgs)
        throws OrmException {
    final Label nextField = new Label();
    final Label end = new Label();
    mv.visitLabel(nextField);/*from w w w. j  a v  a  2 s .  com*/

    // while (!ci.isAtEnd) { ...
    cgs.call("readTag", Type.INT_TYPE);
    mv.visitInsn(DUP);
    mv.visitVarInsn(ISTORE, cgs.tagVar);

    cgs.push(3);
    mv.visitInsn(IUSHR);

    final Label badField = new Label();
    final int[] caseTags = new int[1 + myFields.length];
    final Label[] caseLabels = new Label[caseTags.length];

    caseTags[0] = 0;
    caseLabels[0] = new Label();

    int gaps = 0;
    for (int i = 1; i < caseTags.length; i++) {
        caseTags[i] = myFields[i - 1].getColumnID();
        caseLabels[i] = new Label();
        gaps += caseTags[i] - (caseTags[i - 1] + 1);
    }

    if (2 * gaps / 3 <= myFields.length) {
        final int min = 0;
        final int max = caseTags[caseTags.length - 1];
        final Label[] table = new Label[max + 1];
        Arrays.fill(table, badField);
        for (int idx = 0; idx < caseTags.length; idx++) {
            table[caseTags[idx]] = caseLabels[idx];
        }
        mv.visitTableSwitchInsn(min, max, badField, table);
    } else {
        mv.visitLookupSwitchInsn(badField, caseTags, caseLabels);
    }

    mv.visitLabel(caseLabels[0]);
    mv.visitJumpInsn(GOTO, end);

    for (int idx = 1; idx < caseTags.length; idx++) {
        final JavaColumnModel f = myFields[idx - 1];
        mv.visitLabel(caseLabels[idx]);
        if (f.isNested()) {
            final Label load = new Label();
            cgs.setFieldReference(f);
            cgs.pushFieldValue();
            mv.visitJumpInsn(IFNONNULL, load);
            cgs.fieldSetBegin();
            mv.visitTypeInsn(NEW, Type.getType(f.getNestedClass()).getInternalName());
            mv.visitInsn(DUP);
            mv.visitMethodInsn(INVOKESPECIAL, Type.getType(f.getNestedClass()).getInternalName(), "<init>",
                    Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}));
            cgs.fieldSetEnd();

            // read the length, set a new limit, decode the message, validate
            // we stopped at the end of it as expected.
            //
            mv.visitLabel(load);
            final int limitVar = cgs.newLocal();
            cgs.pushCodedInputStream();
            cgs.call("readRawVarint32", Type.INT_TYPE);
            cgs.ncallInt("pushLimit", Type.INT_TYPE);
            mv.visitVarInsn(ISTORE, limitVar);

            decodeMessage(sort(f.getNestedColumns()), mv, cgs);

            cgs.pushCodedInputStream();
            mv.visitVarInsn(ILOAD, limitVar);
            cgs.ncallInt("popLimit", Type.VOID_TYPE);
            cgs.freeLocal(limitVar);

        } else {
            decodeScalar(mv, cgs, f);
        }
        mv.visitJumpInsn(GOTO, nextField);
    }

    // default:
    mv.visitLabel(badField);
    cgs.pushCodedInputStream();
    mv.visitVarInsn(ILOAD, cgs.tagVar);
    cgs.ncallInt("skipField", Type.BOOLEAN_TYPE);
    mv.visitInsn(POP);
    mv.visitJumpInsn(GOTO, nextField);

    mv.visitLabel(end);
    cgs.pushCodedInputStream();
    cgs.push(0);
    cgs.ncallInt("checkLastTagWas", Type.VOID_TYPE);
}