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.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  ww .  ja va2s  . 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();//www  . j a  v  a  2s. c  o 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 ww. j a  v a  2 s .c  o m*/
        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 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  .com
 */
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 {//  w w  w  .  java 2  s .  c o m
            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);/*from ww w.  ja v  a  2 s  .  c o m*/
        mv.visitVarInsn(ASTORE, sequenceVar);
        int lengthVar = nextVar.next();
        Label endLabel = new Label();
        if (required) {
            mv.visitInsn(ARRAYLENGTH);
            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 generateEncodeEnumValue(TypeDef.Enum enumType, String genClassInternalName,
        String fieldIdentifier, LocalVariable nextVar, Class<?> javaClass, boolean required, MethodVisitor mv,
        String debugValueLabel) throws IllegalArgumentException {

    Label writeValueLabel = new Label();

    // Make sure that no unsupported javaClass is used for an enum
    if (javaClass != int.class && javaClass != Integer.class && (javaClass == null || !javaClass.isEnum())) {
        throw new IllegalArgumentException("Illegal enum javaClass: " + javaClass);
    }//w  ww.  j av a2 s  .co m

    // If null do not look it up
    if (!required) {
        Label lookupIdLabel = new Label();
        mv.visitInsn(DUP);
        mv.visitJumpInsn(IFNONNULL, lookupIdLabel);

        // null
        mv.visitInsn(POP);
        mv.visitInsn(ACONST_NULL);
        mv.visitJumpInsn(GOTO, writeValueLabel);

        // not null
        mv.visitLabel(lookupIdLabel);
    }

    // SymbolMapping
    if (required && !javaClass.isEnum()) {
        box(mv, int.class);
    }
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, genClassInternalName, "symbolMapping_" + fieldIdentifier,
            "Lcom/cinnober/msgcodec/SymbolMapping;");
    mv.visitInsn(SWAP);
    mv.visitMethodInsn(INVOKEINTERFACE, "com/cinnober/msgcodec/SymbolMapping", "getId",
            "(Ljava/lang/Object;)Ljava/lang/Integer;", true);

    // Write the value
    mv.visitLabel(writeValueLabel);
    if (required) {
        unbox(mv, Integer.class); // TODO: Should null check before this and throw exception since there was no mapping
    }
    generateEncodeInt32Value(required, mv);

    //        if (javaClass.isEnum()) {
    //            Label endLabel = new Label();
    //            if (!required) {
    //                Label nonNullLabel = new Label();
    //                mv.visitInsn(DUP);
    //                mv.visitJumpInsn(IFNONNULL, nonNullLabel);
    //                // null
    //                mv.visitInsn(POP);
    //                mv.visitInsn(ACONST_NULL);
    //                mv.visitMethodInsn(INVOKESTATIC, blinkOutputIName, "writeInt32Null",
    //                        "(Lcom/cinnober/msgcodec/io/ByteSink;Ljava/lang/Integer;)V", false);
    //                mv.visitJumpInsn(GOTO, endLabel);
    //                // not null
    //                mv.visitLabel(nonNullLabel);
    //            }
    //            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Enum", "ordinal", "()I", false);
    //            //switch
    //            EnumSymbols enumSymbols = new EnumSymbols(enumType, javaClass);
    //            Enum[] enumValues = ((Class<Enum>)javaClass).getEnumConstants();
    //            int[] ordinals = new int[enumValues.length];
    //            Label[] labels = new Label[enumValues.length];
    //            for (int i=0; i<enumValues.length; i++) {
    //                ordinals[i] = i;
    //                labels[i] = new Label();
    //            }
    //            Label defaultLabel = new Label();
    //            Label writeLabel = new Label();
    //            int symbolIdVar = nextVar.next();
    //            mv.visitLookupSwitchInsn(defaultLabel, ordinals, labels);
    //            for (int i=0; i<enumValues.length; i++) {
    //                mv.visitLabel(labels[i]);
    //                Symbol symbol = enumSymbols.getSymbol(enumValues[i]);
    //                if (symbol != null) {
    //                    mv.visitLdcInsn(symbol.getId());
    //                    mv.visitJumpInsn(GOTO, writeLabel);
    //                } else {
    //                    mv.visitLdcInsn(debugValueLabel);
    //                    mv.visitVarInsn(ALOAD, symbolIdVar);
    //                    mv.visitMethodInsn(INVOKESTATIC, baseclassIName, "unmappableEnumSymbolValue",
    //                            "(Ljava/lang/String;Ljava/lang/Enum;)Ljava/lang/IllegalArgumentException;", false);
    //                    mv.visitInsn(ATHROW);
    //                }
    //            }
    //            mv.visitLabel(defaultLabel);
    //            mv.visitTypeInsn(NEW, "java/lang/RuntimeException");
    //            mv.visitInsn(DUP);
    //            mv.visitLdcInsn("Should not happen: reached generated switch default case for value " + debugValueLabel);
    //            mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "(Ljava/lang/String;)V", false);
    //            mv.visitInsn(ATHROW);
    //
    //            // write
    //            mv.visitLabel(writeLabel);
    //            if (!required) {
    //                box(mv, int.class);
    //            }
    //            generateEncodeInt32Value(required, mv);
    //            // end
    //            mv.visitLabel(endLabel);
    //        } else if (javaClass == int.class || javaClass == Integer.class) {
    //            generateEncodeInt32Value(required, mv);
    //        } else {
    //            throw new IllegalArgumentException("Illegal enum javaClass: " + javaClass);
    //        }
}

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

License:Open Source License

/**
 * Generate value decoding using the blink input.
 *
 * <p>Defaults to <code>readBinary[Null].</code>
 *
 * @param required true if the field is required, otherwise false.
 * @param mv the method visitor, not null.
 * @see #generateDecodeValue/*ww  w. j a v  a2 s .  c  o  m*/
 */
protected void generateDecodeBinaryValue(TypeDef.Binary type, MethodVisitor mv, boolean required) {
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, baseclassIName, "getMaxBinarySize", "()I", false);
    if (required) {
        mv.visitMethodInsn(INVOKESTATIC, blinkInputIName, "readBinary",
                "(Lcom/cinnober/msgcodec/io/ByteSource;I)[B", false);
    } else {
        mv.visitMethodInsn(INVOKESTATIC, blinkInputIName, "readBinaryNull",
                "(Lcom/cinnober/msgcodec/io/ByteSource;I)[B", false);
    }
}

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

License:Open Source License

/**
 * Generate value decoding using the blink input.
 *
 * <p>Defaults to <code>readStringUTF8[Null].</code>
 *
 * @param required true if the field is required, otherwise false.
 * @param mv the method visitor, not null.
 * @see #generateDecodeValue/* w  ww. ja va2  s.c  om*/
 */
protected void generateDecodeStringValue(TypeDef.StringUnicode type, MethodVisitor mv, boolean required) {
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, baseclassIName, "getMaxBinarySize", "()I", false);
    if (required) {
        mv.visitMethodInsn(INVOKESTATIC, blinkInputIName, "readStringUTF8",
                "(Lcom/cinnober/msgcodec/io/ByteSource;I)Ljava/lang/String;", false);
    } else {
        mv.visitMethodInsn(INVOKESTATIC, blinkInputIName, "readStringUTF8Null",
                "(Lcom/cinnober/msgcodec/io/ByteSource;I)Ljava/lang/String;", false);
    }
}

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

License:Open Source License

protected void generateDecodeTimeValue(TypeDef.Time type, Class<?> javaClass, boolean required,
        MethodVisitor mv, LocalVariable nextVar) throws IllegalArgumentException {
    if (javaClass == long.class || javaClass == Long.class) {
        generateDecodeInt64Value(required, mv);
    } else if (javaClass == int.class || javaClass == Integer.class) {
        generateDecodeInt32Value(required, mv);
    } else if (javaClass == Date.class) {

        int timeVar = nextVar.next();
        nextVar.next(); // note: 2 variable slots
        Label endLabel = new Label();
        if (required) {
            generateDecodeInt64Value(true, mv);
        } else {/* w  ww  .j  av a  2s . c  om*/
            generateDecodeInt64Value(false, mv);
            mv.visitInsn(DUP);
            Label nonNullLabel = new Label();
            mv.visitJumpInsn(IFNONNULL, nonNullLabel);
            // null
            mv.visitTypeInsn(CHECKCAST, "java/util/Date");
            mv.visitJumpInsn(GOTO, endLabel);
            // not null
            mv.visitLabel(nonNullLabel);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Long", "longValue", "()J", false);
        }

        mv.visitVarInsn(LSTORE, timeVar);
        mv.visitTypeInsn(NEW, "java/util/Date");
        mv.visitInsn(DUP);
        mv.visitVarInsn(LLOAD, timeVar);
        // handle differences in UNIT and EPOCH
        long epochOffset = DateUtil.getEpochOffset(type.getEpoch());
        long timeInMillis = DateUtil.getTimeInMillis(type.getUnit());
        // dateTime = wireTime * timeUnitInMillis + epochOffset;
        if (timeInMillis != 1) {
            mv.visitLdcInsn(timeInMillis);
            mv.visitInsn(LMUL);
        }
        if (epochOffset != 0) {
            mv.visitLdcInsn(epochOffset);
            mv.visitInsn(LADD);
        }

        mv.visitMethodInsn(INVOKESPECIAL, "java/util/Date", "<init>", "(J)V", false);

        if (!required) {
            // end
            mv.visitLabel(endLabel);
        }
    } else {
        throw new IllegalArgumentException("Illegal time javaClass: " + javaClass);
    }
}