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.cinnober.msgcodec.blink.BaseByteCodeGenerator.java

License:Open Source License

protected void generateWriteStaticGroupForType(Schema schema, ClassVisitor cv, String genClassInternalName,
        boolean javaClassCodec) {
    for (GroupDef group : schema.getGroups()) {
        Object groupType = group.getGroupType();
        String groupDescriptor = getTypeDescriptor(groupType, javaClassCodec);
        MethodVisitor writemv = cv.visitMethod(ACC_PRIVATE, "writeStaticGroup_" + group.getName(),
                "(Lcom/cinnober/msgcodec/io/ByteSink;" + groupDescriptor + ")V", null,
                new String[] { "java/io/IOException" });
        writemv.visitCode();//  w w  w. java2s.  co  m
        LocalVariable nextWriteVar = new LocalVariable(3);

        // write fields of super group
        if (group.getSuperGroup() != null) {
            GroupDef superGroup = schema.getGroup(group.getSuperGroup());
            Object superGroupType = superGroup.getGroupType();
            String superGroupDescriptor = getTypeDescriptor(superGroupType, javaClassCodec);
            writemv.visitVarInsn(ALOAD, 0);
            writemv.visitVarInsn(ALOAD, 1);
            writemv.visitVarInsn(ALOAD, 2);
            writemv.visitMethodInsn(INVOKEVIRTUAL, genClassInternalName,
                    "writeStaticGroup_" + superGroup.getName(),
                    "(Lcom/cinnober/msgcodec/io/ByteSink;" + superGroupDescriptor + ")V", false);
        }

        // fields
        for (FieldDef field : group.getFields()) {

            writemv.visitVarInsn(ALOAD, 1); // output stream

            Class<?> javaClass = field.getJavaClass();
            Accessor<?, ?> accessor = field.getAccessor();

            if (accessor.getClass() == CreateAccessor.class) {
                if (field.getType().getType() == TypeDef.Type.REFERENCE) {
                    if (field.isRequired()) {
                        writemv.visitInsn(ICONST_0);
                        generateEncodeInt32Value(true, writemv);
                    } else {
                        writemv.visitInsn(ACONST_NULL);
                        generateEncodeInt32Value(false, writemv);
                    }
                    continue;
                }
            }

            if (isPublicFieldAccessor(accessor)) {
                Field f = ((FieldAccessor) accessor).getField();
                writemv.visitVarInsn(ALOAD, 2);
                writemv.visitFieldInsn(GETFIELD, Type.getInternalName(f.getDeclaringClass()), f.getName(),
                        Type.getDescriptor(f.getType()));
                if (!f.getType().equals(field.getJavaClass())) {
                    // this can happen when the field is a generic type variable in a super-class.
                    writemv.visitTypeInsn(CHECKCAST, Type.getInternalName(javaClass));
                }
            } else {
                writemv.visitVarInsn(ALOAD, 0);
                writemv.visitFieldInsn(GETFIELD, genClassInternalName,
                        "accessor_" + group.getName() + "_" + field.getName(),
                        "Lcom/cinnober/msgcodec/Accessor;");
                writemv.visitVarInsn(ALOAD, 2); // instance
                writemv.visitMethodInsn(INVOKEINTERFACE, "com/cinnober/msgcodec/Accessor", "getValue",
                        "(Ljava/lang/Object;)Ljava/lang/Object;", true);
                if (javaClass.isPrimitive()) {
                    writemv.visitTypeInsn(CHECKCAST, Type.getInternalName(box(javaClass)));
                    unbox(writemv, javaClass);
                } else {
                    writemv.visitTypeInsn(CHECKCAST, Type.getInternalName(javaClass));
                }
            }
            // the output stream and the value is now on the stack
            generateEncodeValue(writemv, 1, nextWriteVar, field.isRequired(), field.getType(), javaClass,
                    field.getComponentJavaClass(), schema, genClassInternalName,
                    group.getName() + "_" + field.getName(), group.getName() + "." + field.getName(),
                    javaClassCodec);
        }

        // end write
        writemv.visitInsn(RETURN);
        writemv.visitMaxs(4, nextWriteVar.get());
        writemv.visitEnd();
    }
}

From source file:com.cinnober.msgcodec.blink.BaseByteCodeGenerator.java

License:Open Source License

protected void generateReadStaticGroup(Schema schema, ClassVisitor cv, String genClassInternalName,
        boolean javaClassCodec) {
    MethodVisitor mv = cv.visitMethod(ACC_PROTECTED, "readStaticGroup",
            "(ILcom/cinnober/msgcodec/io/ByteSource;)Ljava/lang/Object;", null,
            new String[] { "java/io/IOException" });
    int nextVar = 3;
    mv.visitCode();//from w w  w . j av  a 2  s. c  o  m

    Map<Integer, Label> labelsByGroupId = new TreeMap<>();
    for (GroupDef group : schema.getGroups()) {
        if (group.getId() != -1) {
            labelsByGroupId.put(group.getId(), new Label());
        }
    }
    mv.visitVarInsn(ILOAD, 1); // group id
    Label unknownGroupIdLabel = new Label();
    {
        int[] caseValues = new int[labelsByGroupId.size()];
        int i = 0;
        for (int groupId : labelsByGroupId.keySet()) {
            caseValues[i++] = groupId;
        }
        Label[] caseLabels = labelsByGroupId.values().toArray(new Label[labelsByGroupId.size()]);
        mv.visitLookupSwitchInsn(unknownGroupIdLabel, caseValues, caseLabels);
    }

    for (Map.Entry<Integer, Label> caseEntry : labelsByGroupId.entrySet()) {
        GroupDef group = schema.getGroup(caseEntry.getKey().intValue());
        Object groupType = group.getGroupType();
        String groupDescriptor = getTypeDescriptor(groupType, javaClassCodec);

        mv.visitLabel(caseEntry.getValue());
        mv.visitFrame(F_SAME, 0, null, 0, null);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKEVIRTUAL, genClassInternalName, "readStaticGroup_" + group.getName(),
                "(Lcom/cinnober/msgcodec/io/ByteSource;)" + groupDescriptor, false);
        mv.visitInsn(ARETURN);
    }
    // default case
    mv.visitLabel(unknownGroupIdLabel);
    mv.visitFrame(F_SAME, 0, null, 0, null);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitMethodInsn(INVOKESTATIC, baseclassIName, "unknownGroupId",
            "(I)Lcom/cinnober/msgcodec/DecodeException;", false);
    mv.visitInsn(ATHROW);
    mv.visitMaxs(2, nextVar);
    mv.visitEnd();
}

From source file:com.cinnober.msgcodec.blink.BaseByteCodeGenerator.java

License:Open Source License

protected void generateReadStaticGroupForTypeAndCreate(Schema schema, ClassVisitor cv,
        String genClassInternalName, boolean javaClassCodec) {
    for (GroupDef group : schema.getGroups()) {
        Object groupType = group.getGroupType();
        String groupDescriptor = getTypeDescriptor(groupType, javaClassCodec);
        String groupInternalName = getTypeInternalName(groupType, javaClassCodec);
        MethodVisitor readmv = cv.visitMethod(ACC_PRIVATE, "readStaticGroup_" + group.getName(),
                "(Lcom/cinnober/msgcodec/io/ByteSource;)" + groupDescriptor, null,
                new String[] { "java/io/IOException" });
        readmv.visitCode();/*from ww w  .ja v a 2 s  .  co m*/
        int nextReadVar = 2;

        Factory<?> factory = group.getFactory();
        if (isPublicConstructorFactory(factory)) {
            // read, create instance
            readmv.visitTypeInsn(NEW, groupInternalName);
            readmv.visitInsn(DUP);
            readmv.visitMethodInsn(INVOKESPECIAL, groupInternalName, "<init>", "()V", false);
        } else {
            // read, create instance
            readmv.visitVarInsn(ALOAD, 0); // this
            readmv.visitFieldInsn(GETFIELD, genClassInternalName, "factory_" + group.getName(),
                    "Lcom/cinnober/msgcodec/Factory;");
            readmv.visitMethodInsn(INVOKEINTERFACE, "com/cinnober/msgcodec/Factory", "newInstance",
                    "()Ljava/lang/Object;", true);
            if (javaClassCodec) {
                readmv.visitTypeInsn(CHECKCAST, groupInternalName);
            }
        }
        final int readInstanceVar = nextReadVar++;
        readmv.visitVarInsn(ASTORE, readInstanceVar);

        readmv.visitVarInsn(ALOAD, 0);
        readmv.visitVarInsn(ALOAD, 1);
        readmv.visitVarInsn(ALOAD, readInstanceVar);
        readmv.visitMethodInsn(INVOKEVIRTUAL, genClassInternalName, "readStaticGroup_" + group.getName(),
                "(Lcom/cinnober/msgcodec/io/ByteSource;" + groupDescriptor + ")V", false);

        // end read
        readmv.visitVarInsn(ALOAD, readInstanceVar);
        readmv.visitInsn(ARETURN);
        readmv.visitMaxs(4, nextReadVar);
        readmv.visitEnd();
    }
}

From source file:com.cinnober.msgcodec.blink.BaseByteCodeGenerator.java

License:Open Source License

protected void generateReadStaticGroupForType(final Schema schema, ClassVisitor cv,
        final String genClassInternalName, final boolean javaClassCodec) {
    for (final GroupDef group : schema.getGroups()) {
        Object groupType = group.getGroupType();
        String groupDescriptor = getTypeDescriptor(groupType, javaClassCodec);
        final MethodVisitor mv = cv.visitMethod(ACC_PRIVATE, "readStaticGroup_" + group.getName(),
                "(Lcom/cinnober/msgcodec/io/ByteSource;" + groupDescriptor + ")V", null,
                new String[] { "java/io/IOException" });
        mv.visitCode();/*from  w  w  w .j a  va 2 s.com*/
        final LocalVariable nextVar = new LocalVariable(3);

        // read fields of super group
        if (group.getSuperGroup() != null) {
            GroupDef superGroup = schema.getGroup(group.getSuperGroup());
            Object superGroupType = superGroup.getGroupType();
            String superGroupDescriptor = getTypeDescriptor(superGroupType, javaClassCodec);
            mv.visitVarInsn(ALOAD, 0);
            mv.visitVarInsn(ALOAD, 1);
            mv.visitVarInsn(ALOAD, 2);
            mv.visitMethodInsn(INVOKEVIRTUAL, genClassInternalName, "readStaticGroup_" + superGroup.getName(),
                    "(Lcom/cinnober/msgcodec/io/ByteSource;" + superGroupDescriptor + ")V", false);
        }

        // fields
        for (final FieldDef field : group.getFields()) {
            final Class<?> javaClass = field.getJavaClass();

            Runnable readValue = () -> {
                Label tryStart = new Label();
                Label tryEnd = new Label();
                Label tryCatch = new Label();
                Label tryAfter = new Label();
                mv.visitTryCatchBlock(tryStart, tryEnd, tryCatch, "java/lang/Exception");

                mv.visitLabel(tryStart);
                mv.visitVarInsn(ALOAD, 1); // input stream
                generateDecodeValue(mv, 1, nextVar, field.isRequired(), field.getType(), javaClass,
                        field.getComponentJavaClass(), schema, genClassInternalName,
                        group.getName() + "_" + field.getName(), group.getName() + "." + field.getName(),
                        javaClassCodec);
                mv.visitLabel(tryEnd);
                mv.visitJumpInsn(GOTO, tryAfter);
                mv.visitLabel(tryCatch);
                int caughtExVar = nextVar.next();
                mv.visitVarInsn(ASTORE, caughtExVar);
                mv.visitTypeInsn(NEW, "com/cinnober/msgcodec/blink/FieldDecodeException");
                mv.visitInsn(DUP);
                mv.visitLdcInsn(field.getName());
                mv.visitVarInsn(ALOAD, caughtExVar);
                mv.visitMethodInsn(INVOKESPECIAL, "com/cinnober/msgcodec/blink/FieldDecodeException", "<init>",
                        "(Ljava/lang/String;Ljava/lang/Throwable;)V", false);
                mv.visitInsn(ATHROW);
                mv.visitLabel(tryAfter);
            };

            Accessor<?, ?> accessor = field.getAccessor();
            if (isPublicFieldAccessor(accessor)) {
                Field f = ((FieldAccessor) accessor).getField();
                mv.visitVarInsn(ALOAD, 2); // instance
                // value
                readValue.run();
                // store
                mv.visitFieldInsn(PUTFIELD, Type.getInternalName(f.getDeclaringClass()), f.getName(),
                        Type.getDescriptor(f.getType()));
            } else if (accessor.getClass() == CreateAccessor.class) {
                mv.visitVarInsn(ALOAD, 2); // instance

                Label tryStart = new Label();
                Label tryEnd = new Label();
                Label tryCatch = new Label();
                Label tryAfter = new Label();
                mv.visitTryCatchBlock(tryStart, tryEnd, tryCatch, "java/lang/Exception");

                mv.visitLabel(tryStart);
                mv.visitVarInsn(ALOAD, 1); // input stream

                generateDecodeDummy(mv, 1, nextVar, field.isRequired(), field.getType(), javaClass,
                        field.getComponentJavaClass(), schema, genClassInternalName,
                        group.getName() + "." + field.getName(), javaClassCodec);

                mv.visitInsn(POP);

                mv.visitLabel(tryEnd);
                mv.visitJumpInsn(GOTO, tryAfter);
                mv.visitLabel(tryCatch);
                int caughtExVar = nextVar.next();
                mv.visitVarInsn(ASTORE, caughtExVar);
                mv.visitTypeInsn(NEW, "com/cinnober/msgcodec/blink/FieldDecodeException");
                mv.visitInsn(DUP);
                mv.visitLdcInsn(field.getName());
                mv.visitVarInsn(ALOAD, caughtExVar);
                mv.visitMethodInsn(INVOKESPECIAL, "com/cinnober/msgcodec/blink/FieldDecodeException", "<init>",
                        "(Ljava/lang/String;Ljava/lang/Throwable;)V", false);
                mv.visitInsn(ATHROW);
                mv.visitLabel(tryAfter);

                mv.visitInsn(POP);
            } else {
                // accessor
                mv.visitVarInsn(ALOAD, 0);
                mv.visitFieldInsn(GETFIELD, genClassInternalName,
                        "accessor_" + group.getName() + "_" + field.getName(),
                        "Lcom/cinnober/msgcodec/Accessor;");
                // instance
                mv.visitVarInsn(ALOAD, 2); // instance
                // value
                readValue.run();
                if (javaClass.isPrimitive()) {
                    box(mv, javaClass);
                }
                // store
                mv.visitMethodInsn(INVOKEINTERFACE, "com/cinnober/msgcodec/Accessor", "setValue",
                        "(Ljava/lang/Object;Ljava/lang/Object;)V", true);
            }
        }

        mv.visitInsn(RETURN);
        mv.visitMaxs(6, nextVar.get());
        mv.visitEnd();
    }
}

From source file:com.cinnober.msgcodec.blink.BaseByteCodeGenerator.java

License:Open Source License

/**
 * Generate instructions to encode the specified value type.
 * The values on the stack are; the output stream and the value to be encoded.
 * After this call these values are expected to be consumed.
 *
 * @param mv the method visitor, open for code instructions.
 * @param outputStreamVar the variable instance that contains the output stream.
 * @param nextVar the next variable//from w ww  .j  a va2s  . c  om
 * @param required true if the field is required, otherwise false.
 * @param type the field type
 * @param javaClass the field java class
 * @param componentJavaClass the field component java class, or null if not a sequence type
 * @param schema the protocol schema, not null
 * @param genClassInternalName the internal name of the generated class
 */
protected void generateEncodeValue(MethodVisitor mv, int outputStreamVar, LocalVariable nextVar,
        boolean required, TypeDef type, Class<?> javaClass, Class<?> componentJavaClass, Schema schema,
        String genClassInternalName, String fieldIdentifier, String debugValueLabel, boolean javaClassCodec) {

    type = schema.resolveToType(type, false);
    GroupDef refGroup = schema.resolveToGroup(type);

    if (javaClass.isPrimitive() && !required) {
        box(mv, javaClass);
    } else if (!javaClass.isPrimitive() && required) {
        mv.visitInsn(DUP);
        Label notNullLabel = new Label();
        mv.visitJumpInsn(IFNONNULL, notNullLabel);
        mv.visitInsn(POP); // output stream
        mv.visitLdcInsn(debugValueLabel);
        mv.visitMethodInsn(INVOKESTATIC, baseclassIName, "missingRequiredValue",
                "(Ljava/lang/String;)Ljava/lang/IllegalArgumentException;", false);
        mv.visitInsn(ATHROW);
        mv.visitLabel(notNullLabel);
        unbox(mv, javaClass);
    }
    switch (type.getType()) {
    case INT8:
        generateEncodeInt8Value(required, mv);
        break;
    case UINT8:
        generateEncodeUInt8Value(required, mv);
        break;
    case CHAR:
        generateEncodeCharacterValue(required, mv);
        break;
    case INT16:
        generateEncodeInt16Value(required, mv);
        break;
    case UINT16:
        generateEncodeUInt16Value(required, mv);
        break;
    case INT32:
        generateEncodeInt32Value(required, mv);
        break;
    case UINT32:
        generateEncodeUInt32Value(required, mv);
        break;
    case INT64:
        generateEncodeInt64Value(required, mv);
        break;
    case UINT64:
        generateEncodeUInt64Value(required, mv);
        break;
    case FLOAT32:
        generateEncodeFloat32Value(required, mv);
        break;
    case FLOAT64:
        generateEncodeFloat64Value(required, mv);
        break;
    case BIGINT:
        generateEncodeBigIntValue(required, mv);
        break;
    case DECIMAL:
        generateEncodeDecimalValue(required, mv);
        break;
    case BIGDECIMAL:
        generateEncodeBigDecimalValue(required, mv);
        break;
    case STRING:
        generateEncodeStringValue((TypeDef.StringUnicode) type, required, mv);
        break;
    case BINARY:
        generateEncodeBinaryValue((TypeDef.Binary) type, required, mv);
        break;
    case BOOLEAN:
        generateEncodeBooleanValue(required, mv);
        break;
    case ENUM:
        generateEncodeEnumValue((TypeDef.Enum) type, genClassInternalName, fieldIdentifier, nextVar, javaClass,
                required, mv, debugValueLabel);
        break;
    case TIME:
        generateEncodeTimeValue((TypeDef.Time) type, javaClass, required, mv);
        break;
    case SEQUENCE:
        generateEncodeSequenceValue(javaClass, nextVar, mv, required, outputStreamVar, componentJavaClass, type,
                schema, genClassInternalName, fieldIdentifier, debugValueLabel, javaClassCodec);
        break;
    case REFERENCE:
        generateEncodeRefValue(refGroup, required, nextVar, mv, genClassInternalName, outputStreamVar,
                (TypeDef.Reference) type, javaClassCodec);
        break;
    case DYNAMIC_REFERENCE:
        generateEncodeDynRefValue(nextVar, mv, required);
        break;
    default:
        throw new RuntimeException("Unhandled case: " + type.getType());
    }
}

From source file:com.cinnober.msgcodec.blink.BaseByteCodeGenerator.java

License:Open Source License

/**
 * Generate value encoding using the blink output.
 *
 * <p>Defaults to <code>writeUInt16Null].</code>
 *
 * @param required true if the field is required, otherwise false.
 * @param mv the method visitor, not null.
 * @see #generateEncodeValue//from   w  w w.  jav a2 s  .c  o  m
 */
protected void generateEncodeCharacterValue(boolean required, MethodVisitor mv) {
    if (required) {
        mv.visitInsn(I2S);
        mv.visitMethodInsn(INVOKESTATIC, blinkOutputIName, "writeUInt16",
                "(Lcom/cinnober/msgcodec/io/ByteSink;S)V", false);
    } else {
        Label nullLabel = new Label();
        mv.visitInsn(DUP);
        mv.visitJumpInsn(IFNULL, nullLabel);
        unbox(mv, Character.class);
        mv.visitInsn(I2S);
        box(mv, Short.class);
        mv.visitLabel(nullLabel);
        mv.visitTypeInsn(CHECKCAST, Type.getInternalName(Short.class));
        mv.visitMethodInsn(INVOKESTATIC, blinkOutputIName, "writeUInt16Null",
                "(Lcom/cinnober/msgcodec/io/ByteSink;Ljava/lang/Short;)V", false);
    }
}

From source file:com.cinnober.msgcodec.blink.BaseByteCodeGenerator.java

License:Open Source License

/**
 * Generate value encoding using the blink output.
 *
 * <p>Defaults to <code>writeDynamicGroup[Null]</code> in the generated codec.
 *
 * @param required true if the field is required, otherwise false.
 * @param mv the method visitor, not null.
 * @see #generateEncodeValue//ww  w .ja v a2 s  .c  o m
 */
protected void generateEncodeDynRefValue(LocalVariable nextVar, MethodVisitor mv, boolean required) {
    // PENDING: validate that the instance class is a subclass of refGroup (unless null)
    int instanceVar = nextVar.next();
    mv.visitVarInsn(ASTORE, instanceVar);
    mv.visitVarInsn(ALOAD, 0); // this
    mv.visitInsn(SWAP); // this and out
    mv.visitVarInsn(ALOAD, instanceVar);
    if (required) {
        mv.visitMethodInsn(INVOKEVIRTUAL, baseclassIName, "writeDynamicGroup",
                "(Lcom/cinnober/msgcodec/io/ByteSink;Ljava/lang/Object;)V", false);
    } else {
        mv.visitMethodInsn(INVOKEVIRTUAL, baseclassIName, "writeDynamicGroupNull",
                "(Lcom/cinnober/msgcodec/io/ByteSink;Ljava/lang/Object;)V", false);
    }
}

From source file:com.cinnober.msgcodec.blink.BaseByteCodeGenerator.java

License:Open Source License

protected void generateEncodeRefValue(GroupDef refGroup, boolean required, LocalVariable nextVar,
        MethodVisitor mv, String genClassInternalName, int outputStreamVar, TypeDef.Reference type,
        boolean javaClassCodec) throws IllegalArgumentException {
    if (refGroup != null) {
        Object refGroupType = refGroup.getGroupType();
        String refGroupDescriptor = getTypeDescriptor(refGroupType, javaClassCodec);
        if (required) {
            int instanceVar = nextVar.next();
            mv.visitVarInsn(ASTORE, instanceVar);
            mv.visitVarInsn(ALOAD, 0); // this
            mv.visitInsn(SWAP); // this and out
            mv.visitVarInsn(ALOAD, instanceVar);
            mv.visitMethodInsn(INVOKEVIRTUAL, genClassInternalName, "writeStaticGroup_" + refGroup.getName(),
                    "(Lcom/cinnober/msgcodec/io/ByteSink;" + refGroupDescriptor + ")V", false);
        } else {/*  ww  w  . ja va  2 s .c  om*/
            int instanceVar = nextVar.next();
            mv.visitInsn(DUP);
            Label nonNullLabel = new Label();
            Label endLabel = new Label();
            mv.visitJumpInsn(IFNONNULL, nonNullLabel);
            // null
            mv.visitInsn(POP);
            mv.visitInsn(ICONST_0); // false
            mv.visitMethodInsn(INVOKESTATIC, blinkOutputIName, "writePresenceByte",
                    "(Lcom/cinnober/msgcodec/io/ByteSink;Z)V", false);
            mv.visitJumpInsn(GOTO, endLabel);

            // not null
            mv.visitLabel(nonNullLabel);
            // PENDING: mv.visitFrame?
            mv.visitVarInsn(ASTORE, instanceVar);
            mv.visitInsn(ICONST_1); // true
            mv.visitMethodInsn(INVOKESTATIC, blinkOutputIName, "writePresenceByte",
                    "(Lcom/cinnober/msgcodec/io/ByteSink;Z)V", false);
            mv.visitVarInsn(ALOAD, 0); // this
            mv.visitVarInsn(ALOAD, outputStreamVar);
            mv.visitVarInsn(ALOAD, instanceVar);
            mv.visitMethodInsn(INVOKEVIRTUAL, genClassInternalName, "writeStaticGroup_" + refGroup.getName(),
                    "(Lcom/cinnober/msgcodec/io/ByteSink;" + refGroupDescriptor + ")V", false);
            mv.visitLabel(endLabel);
            // PENDING: mv.visitFrame?
        }
    } else {
        throw new IllegalArgumentException("Illegal reference: " + type);
    }
}

From source file:com.cinnober.msgcodec.blink.BaseByteCodeGenerator.java

License:Open Source License

protected void generateEncodeSequenceValue(Class<?> javaClass, LocalVariable nextVar, MethodVisitor mv,
        boolean required, int outputStreamVar, Class<?> componentJavaClass, TypeDef type, Schema schema,
        String genClassInternalName, String fieldIdentifier, String debugValueLabel, boolean javaClassCodec)
        throws IllegalArgumentException {

    // PENDING: merge the two if-cases, and reuse common code blocks (see generateDecodeSquenceValue)
    if (javaClass.isArray()) {
        int sequenceVar = nextVar.next();
        mv.visitInsn(DUP);
        mv.visitVarInsn(ASTORE, sequenceVar);
        int lengthVar = nextVar.next();
        Label endLabel = new Label();
        if (required) {
            mv.visitInsn(ARRAYLENGTH);/*from  w w w.j a v  a2  s. c om*/
            mv.visitInsn(DUP);
            mv.visitVarInsn(ISTORE, lengthVar);
            mv.visitMethodInsn(INVOKESTATIC, blinkOutputIName, "writeUInt32",
                    "(Lcom/cinnober/msgcodec/io/ByteSink;I)V", false);
        } else {
            Label nonNullLabel = new Label();
            mv.visitInsn(DUP);
            mv.visitJumpInsn(IFNONNULL, nonNullLabel);
            // null
            mv.visitInsn(POP);
            mv.visitMethodInsn(INVOKESTATIC, blinkOutputIName, "writeNull",
                    "(Lcom/cinnober/msgcodec/io/ByteSink;)V", false);
            mv.visitJumpInsn(GOTO, endLabel);
            // not null
            mv.visitLabel(nonNullLabel);
            // PENDING: mv.visitFrame?
            mv.visitInsn(ARRAYLENGTH);
            mv.visitInsn(DUP);
            mv.visitVarInsn(ISTORE, lengthVar);
            mv.visitMethodInsn(INVOKESTATIC, blinkOutputIName, "writeUInt32",
                    "(Lcom/cinnober/msgcodec/io/ByteSink;I)V", false);
        }
        // for loop
        int loopVar = nextVar.next();
        mv.visitInsn(ICONST_0);
        mv.visitVarInsn(ISTORE, loopVar);
        Label loopLabel = new Label();
        mv.visitLabel(loopLabel);
        // PENDING: mv.visitFrame?
        mv.visitVarInsn(ILOAD, loopVar);
        mv.visitVarInsn(ILOAD, lengthVar);
        mv.visitJumpInsn(IF_ICMPGE, endLabel);
        mv.visitVarInsn(ALOAD, outputStreamVar);
        mv.visitVarInsn(ALOAD, sequenceVar);
        mv.visitVarInsn(ILOAD, loopVar);
        if (componentJavaClass == byte.class || componentJavaClass == boolean.class) {
            mv.visitInsn(BALOAD);
        } else if (componentJavaClass == short.class) {
            mv.visitInsn(SALOAD);
        } else if (componentJavaClass == int.class) {
            mv.visitInsn(IALOAD);
        } else if (componentJavaClass == long.class) {
            mv.visitInsn(LALOAD);
        } else if (componentJavaClass == float.class) {
            mv.visitInsn(FALOAD);
        } else if (componentJavaClass == double.class) {
            mv.visitInsn(DALOAD);
        } else if (componentJavaClass == char.class) {
            mv.visitInsn(CALOAD);
        } else {
            mv.visitInsn(AALOAD);
            mv.visitTypeInsn(CHECKCAST, Type.getInternalName(componentJavaClass));
        }

        // encode the element
        TypeDef.Sequence seqType = (TypeDef.Sequence) type;
        generateEncodeValue(mv, outputStreamVar, nextVar, true, seqType.getComponentType(), componentJavaClass,
                null, schema, genClassInternalName, fieldIdentifier, debugValueLabel + ".component",
                javaClassCodec);

        mv.visitIincInsn(loopVar, 1);
        mv.visitJumpInsn(GOTO, loopLabel);
        mv.visitLabel(endLabel);
        // PENDING: mv.visitFrame?

    } else if (javaClass == List.class) {
        int sequenceVar = nextVar.next();
        mv.visitInsn(DUP);
        mv.visitVarInsn(ASTORE, sequenceVar);
        int lengthVar = nextVar.next();
        Label endLabel = new Label();
        if (required) {
            mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "size", "()I", true);
            mv.visitInsn(DUP);
            mv.visitVarInsn(ISTORE, lengthVar);
            mv.visitMethodInsn(INVOKESTATIC, blinkOutputIName, "writeUInt32",
                    "(Lcom/cinnober/msgcodec/io/ByteSink;I)V", false);
        } else {
            Label nonNullLabel = new Label();
            mv.visitInsn(DUP);
            mv.visitJumpInsn(IFNONNULL, nonNullLabel);
            // null
            mv.visitInsn(POP);
            mv.visitMethodInsn(INVOKESTATIC, blinkOutputIName, "writeNull",
                    "(Lcom/cinnober/msgcodec/io/ByteSink;)V", false);
            mv.visitJumpInsn(GOTO, endLabel);
            // not null
            mv.visitLabel(nonNullLabel);
            // PENDING: mv.visitFrame?
            mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "size", "()I", true);
            mv.visitInsn(DUP);
            mv.visitVarInsn(ISTORE, lengthVar);
            mv.visitMethodInsn(INVOKESTATIC, blinkOutputIName, "writeUInt32",
                    "(Lcom/cinnober/msgcodec/io/ByteSink;I)V", false);
        }
        // for loop, using iterator
        int iteratorVar = nextVar.next();
        mv.visitVarInsn(ALOAD, sequenceVar);
        mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "iterator", "()Ljava/util/Iterator;", true);
        mv.visitVarInsn(ASTORE, iteratorVar);
        Label loopLabel = new Label();
        mv.visitLabel(loopLabel);
        // PENDING: mv.visitFrame?
        mv.visitVarInsn(ALOAD, iteratorVar);
        mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Iterator", "hasNext", "()Z", true);
        mv.visitJumpInsn(IFEQ, endLabel);
        mv.visitVarInsn(ALOAD, outputStreamVar);
        mv.visitVarInsn(ALOAD, iteratorVar);
        mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Iterator", "next", "()Ljava/lang/Object;", true);
        if (componentJavaClass.isPrimitive()) {
            mv.visitTypeInsn(CHECKCAST, Type.getInternalName(box(componentJavaClass)));
            unbox(mv, componentJavaClass);
        } else {
            mv.visitTypeInsn(CHECKCAST, Type.getInternalName(componentJavaClass));
        }

        // encode the element
        TypeDef.Sequence seqType = (TypeDef.Sequence) type;
        generateEncodeValue(mv, outputStreamVar, nextVar, true, seqType.getComponentType(), componentJavaClass,
                null, schema, genClassInternalName, fieldIdentifier, debugValueLabel + ".component",
                javaClassCodec);

        mv.visitJumpInsn(GOTO, loopLabel);
        mv.visitLabel(endLabel);
        // PENDING: mv.visitFrame?
    } else {
        throw new IllegalArgumentException("Illegal sequence javaClass: " + javaClass);
    }
}

From source file:com.cinnober.msgcodec.blink.BaseByteCodeGenerator.java

License:Open Source License

protected void generateEncodeTimeValue(TypeDef.Time type, Class<?> javaClass, boolean required,
        MethodVisitor mv) throws RuntimeException {
    if (javaClass == long.class || javaClass == Long.class) {
        generateEncodeInt64Value(required, mv);
    } else if (javaClass == int.class || javaClass == Integer.class) {
        generateEncodeInt32Value(required, mv);
    } else if (javaClass == Date.class) {

        Label nullLabel = new Label();
        Label endLabel = new Label();
        if (!required) {
            mv.visitInsn(DUP);
            mv.visitJumpInsn(IFNULL, nullLabel);
        }//from   www .j a v  a2s  . c om

        // not null
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/Date", "getTime", "()J", false);
        // handle differences in UNIT and EPOCH
        long epochOffset = DateUtil.getEpochOffset(type.getEpoch());
        long timeInMillis = DateUtil.getTimeInMillis(type.getUnit());
        // wireTime = (dateTime - epochOffset) / timeUnitInMillis);
        if (epochOffset != 0) {
            mv.visitLdcInsn(epochOffset);
            mv.visitInsn(LSUB);
        }
        if (timeInMillis != 1) {
            mv.visitLdcInsn(timeInMillis);
            mv.visitInsn(LDIV);
        }
        box(mv, long.class);
        mv.visitMethodInsn(INVOKESTATIC, blinkOutputIName, "writeInt64Null",
                "(Lcom/cinnober/msgcodec/io/ByteSink;Ljava/lang/Long;)V", false);

        if (!required) {
            mv.visitJumpInsn(GOTO, endLabel);
            mv.visitLabel(nullLabel);
            mv.visitInsn(POP);
            mv.visitInsn(ACONST_NULL);
            mv.visitMethodInsn(INVOKESTATIC, blinkOutputIName, "writeInt64Null",
                    "(Lcom/cinnober/msgcodec/io/ByteSink;Ljava/lang/Long;)V", false);
            mv.visitLabel(endLabel);
        }
    } else {
        throw new IllegalArgumentException("Illegal time javaClass: " + javaClass);
    }
}