List of usage examples for org.objectweb.asm MethodVisitor visitInsn
public void visitInsn(final int opcode)
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); }/*from ww w . j ava 2s .c o 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>readUInt16[Null].</code> * * @param required true if the field is required, otherwise false. * @param mv the method visitor, not null. * @see #generateDecodeValue//from ww w . j a v a2 s . c o m */ protected void generateDecodeCharacterValue(boolean required, MethodVisitor mv) { if (required) { mv.visitMethodInsn(INVOKESTATIC, blinkInputIName, "readUInt16", "(Lcom/cinnober/msgcodec/io/ByteSource;)S", false); mv.visitInsn(I2C); } else { mv.visitMethodInsn(INVOKESTATIC, blinkInputIName, "readUInt16Null", "(Lcom/cinnober/msgcodec/io/ByteSource;)Ljava/lang/Short;", false); Label nullLabel = new Label(); mv.visitInsn(DUP); mv.visitJumpInsn(IFNULL, nullLabel); unbox(mv, Short.class); mv.visitInsn(I2C); box(mv, Character.class); mv.visitLabel(nullLabel); mv.visitTypeInsn(CHECKCAST, Type.getInternalName(Character.class)); } }
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 {/*from w w w .j a va2 s .co m*/ 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); } }
From source file:com.cinnober.msgcodec.blink.BaseByteCodeGenerator.java
License:Open Source License
/** * @see #generateDecodeInt32Value(boolean, org.objectweb.asm.MethodVisitor) */// w ww . j a va 2 s .co m protected void generateDecodeEnumValue(boolean required, MethodVisitor mv, TypeDef type, String genClassInternalName, String fieldIdentifier, LocalVariable nextVar, Class<?> javaClass, String debugValueLabel) throws IllegalArgumentException { Label endLabel = new Label(); if (required) { generateDecodeInt32Value(true, mv); box(mv, Integer.class); } else { generateDecodeInt32Value(false, mv); mv.visitInsn(DUP); mv.visitJumpInsn(IFNULL, endLabel); // Label nonNullLabel = new Label(); // mv.visitJumpInsn(IFNONNULL, nonNullLabel); // mv.visitTypeInsn(CHECKCAST, Type.getInternalName(javaClass)); // mv.visitJumpInsn(GOTO, endLabel); // not null // mv.visitLabel(nonNullLabel); // mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Integer", "intValue", "()I", false); // unbox(mv, Integer.class); } // SymbolMapping mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, genClassInternalName, "symbolMapping_" + fieldIdentifier, "Lcom/cinnober/msgcodec/SymbolMapping;"); mv.visitInsn(SWAP); mv.visitMethodInsn(INVOKEINTERFACE, "com/cinnober/msgcodec/SymbolMapping", "lookup", "(Ljava/lang/Integer;)Ljava/lang/Object;", true); // // switch // TypeDef.Enum enumType = (TypeDef.Enum) type; // List<Symbol> symbols = enumType.getSymbols(); // int numSymbols = symbols.size(); // int[] ids = new int[numSymbols]; // Label[] labels = new Label[numSymbols]; // for (int i=0; i<numSymbols; i++) { // ids[i] = symbols.get(i).getId(); // labels[i] = new Label(); // } // Label defaultLabel = new Label(); // int symbolIdVar = nextVar.next(); // mv.visitInsn(DUP); // mv.visitVarInsn(ISTORE, symbolIdVar); // EnumSymbols enumSymbols = null; // if (javaClass.isEnum()) { // enumSymbols = new EnumSymbols(enumType, javaClass); // } // mv.visitLookupSwitchInsn(defaultLabel, ids, labels); // for (int i=0; i<numSymbols; i++) { // boolean addGotoEnd = true; // mv.visitLabel(labels[i]); // if (javaClass.isEnum()) { // Enum enumValue = enumSymbols.getEnum(ids[i]); // if (enumValue != null) { // //mv.visitLdcInsn(Type.getType(javaClass)); // mv.visitFieldInsn(GETSTATIC, Type.getInternalName(javaClass), // enumValue.name(), // Type.getDescriptor(javaClass)); // } else { // mv.visitLdcInsn(debugValueLabel); // mv.visitLdcInsn(ids[i]); // mv.visitLdcInsn(Type.getType(javaClass)); // mv.visitMethodInsn(INVOKESTATIC, baseclassIName, "unmappableEnumSymbolId", // "(Ljava/lang/String;ILjava/lang/Class;)Lcom/cinnober/msgcodec/DecodeException;", false); // mv.visitInsn(ATHROW); // addGotoEnd = false; // } // } else if (javaClass == int.class || javaClass == Integer.class) { // mv.visitLdcInsn(ids[i]); // if (!required) { // box(mv, Integer.class); // } // } else { // throw new IllegalArgumentException("Illegal enum javaClass: " + javaClass); // } // if (addGotoEnd) { // mv.visitJumpInsn(GOTO, endLabel); // } // } // mv.visitLabel(defaultLabel); // mv.visitLdcInsn(debugValueLabel); // mv.visitVarInsn(ILOAD, symbolIdVar); // mv.visitMethodInsn(INVOKESTATIC, baseclassIName, "unknownEnumSymbol", // "(Ljava/lang/String;I)Lcom/cinnober/msgcodec/DecodeException;", false); // mv.visitInsn(ATHROW); // end mv.visitLabel(endLabel); if (!javaClass.isEnum()) { mv.visitTypeInsn(CHECKCAST, Type.getInternalName(Integer.class)); if (required) { unbox(mv, Integer.class); } } else { mv.visitTypeInsn(CHECKCAST, Type.getInternalName(javaClass)); } }
From source file:com.cinnober.msgcodec.blink.BaseByteCodeGenerator.java
License:Open Source License
protected void generateDecodeDynRefValue(MethodVisitor mv, boolean required, GroupDef refGroup, Class<?> javaClass) { mv.visitVarInsn(ALOAD, 0); // this mv.visitInsn(SWAP); // this and in if (required) { mv.visitMethodInsn(INVOKEVIRTUAL, baseclassIName, "readDynamicGroup", "(Lcom/cinnober/msgcodec/io/ByteSource;)Ljava/lang/Object;", false); } else {/*from w ww . j a va 2 s. c om*/ mv.visitMethodInsn(INVOKEVIRTUAL, baseclassIName, "readDynamicGroupNull", "(Lcom/cinnober/msgcodec/io/ByteSource;)Ljava/lang/Object;", false); } if (refGroup != null) { mv.visitTypeInsn(CHECKCAST, Type.getInternalName(javaClass)); } }
From source file:com.cinnober.msgcodec.blink.BaseByteCodeGenerator.java
License:Open Source License
protected void generateDecodeRefValue(GroupDef refGroup, boolean required, MethodVisitor mv, int byteSourceVar, String genClassInternalName, Class<?> javaClass, TypeDef type, boolean javaClassCodec) throws IllegalArgumentException { if (refGroup != null) { String groupDescriptor = getTypeDescriptor(javaClass, javaClassCodec); if (required) { mv.visitInsn(POP); // input stream mv.visitVarInsn(ALOAD, 0);//from w ww . j ava 2 s .co m mv.visitVarInsn(ALOAD, byteSourceVar); mv.visitMethodInsn(INVOKEVIRTUAL, genClassInternalName, "readStaticGroup_" + refGroup.getName(), "(Lcom/cinnober/msgcodec/io/ByteSource;)" + groupDescriptor, false); } else { mv.visitMethodInsn(INVOKESTATIC, blinkInputIName, "readPresenceByte", "(Lcom/cinnober/msgcodec/io/ByteSource;)Z", false); Label nonNullLabel = new Label(); Label endLabel = new Label(); mv.visitJumpInsn(IFNE, nonNullLabel); // not false, i.e. true // null mv.visitInsn(ACONST_NULL); mv.visitJumpInsn(GOTO, endLabel); // not null mv.visitLabel(nonNullLabel); mv.visitFrame(F_SAME, 0, null, 0, null); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, byteSourceVar); mv.visitMethodInsn(INVOKEVIRTUAL, genClassInternalName, "readStaticGroup_" + refGroup.getName(), "(Lcom/cinnober/msgcodec/io/ByteSource;)" + groupDescriptor, false); mv.visitLabel(endLabel); // PENDING: mv.visitFrame? mv.visitFrame(F_SAME, 0, null, 0, null); } } else { throw new IllegalArgumentException("Illegal reference: " + type); } }
From source file:com.cinnober.msgcodec.blink.BaseByteCodeGenerator.java
License:Open Source License
protected void generateDecodeSequenceValue(Class<?> javaClass, LocalVariable nextVar, boolean required, MethodVisitor mv, Class<?> componentJavaClass, int byteSourceVar, TypeDef type, Schema schema, String genClassInternalName, String fieldIdentifier, String debugValueLabel, boolean javaClassCodec) throws IllegalArgumentException { if (!javaClass.isArray() && javaClass != List.class) { throw new IllegalArgumentException("Illegal sequence javaClass: " + javaClass); }/*from w w w. j ava2 s .c o m*/ int lengthVar = nextVar.next(); int sequenceVar = nextVar.next(); Label finalEndLabel = new Label(); if (required) { mv.visitMethodInsn(INVOKESTATIC, blinkInputIName, "readUInt32", "(Lcom/cinnober/msgcodec/io/ByteSource;)I", false); mv.visitVarInsn(ISTORE, lengthVar); } else { mv.visitMethodInsn(INVOKESTATIC, blinkInputIName, "readUInt32Null", "(Lcom/cinnober/msgcodec/io/ByteSource;)Ljava/lang/Integer;", false); mv.visitInsn(DUP); mv.visitJumpInsn(IFNULL, finalEndLabel); unbox(mv, Integer.class); mv.visitVarInsn(ISTORE, lengthVar); } if (javaClass.isArray()) { mv.visitVarInsn(ILOAD, lengthVar); generateNewArray(mv, componentJavaClass); } else { mv.visitTypeInsn(NEW, "java/util/ArrayList"); mv.visitInsn(DUP); mv.visitVarInsn(ILOAD, lengthVar); mv.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "(I)V", false); } mv.visitVarInsn(ASTORE, sequenceVar); // for loop Label endLabel = new Label(); int loopVar = nextVar.next(); mv.visitInsn(ICONST_0); mv.visitVarInsn(ISTORE, loopVar); Label loopLabel = new Label(); mv.visitLabel(loopLabel); // PENDING: mv.visitFrame? mv.visitFrame(F_SAME, 0, null, 0, null); mv.visitVarInsn(ILOAD, loopVar); mv.visitVarInsn(ILOAD, lengthVar); mv.visitJumpInsn(IF_ICMPGE, endLabel); mv.visitVarInsn(ALOAD, sequenceVar); mv.visitVarInsn(ILOAD, loopVar); mv.visitVarInsn(ALOAD, byteSourceVar); // decode the element TypeDef.Sequence seqType = (TypeDef.Sequence) type; generateDecodeValue(mv, byteSourceVar, nextVar, true, seqType.getComponentType(), componentJavaClass, null, schema, genClassInternalName, fieldIdentifier, debugValueLabel + ".component", javaClassCodec); // store the value if (javaClass.isArray()) { generateArrayStore(mv, componentJavaClass); } else { mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/ArrayList", "add", "(ILjava/lang/Object;)V", false); } mv.visitIincInsn(loopVar, 1); mv.visitJumpInsn(GOTO, loopLabel); mv.visitLabel(endLabel); mv.visitFrame(F_SAME, 0, null, 0, null); mv.visitVarInsn(ALOAD, sequenceVar); mv.visitLabel(finalEndLabel); mv.visitFrame(F_SAME, 0, null, 0, null); if (javaClass.isArray()) { mv.visitTypeInsn(CHECKCAST, Type.getInternalName(javaClass)); } }
From source file:com.cinnober.msgcodec.blink.BaseByteCodeGenerator.java
License:Open Source License
public static void generateArrayStore(MethodVisitor mv, Class<?> componentJavaClass) { if (componentJavaClass == byte.class || componentJavaClass == boolean.class) { mv.visitInsn(BASTORE); } else if (componentJavaClass == short.class) { mv.visitInsn(SASTORE);/*from w ww . jav a 2 s . com*/ } else if (componentJavaClass == int.class) { mv.visitInsn(IASTORE); } else if (componentJavaClass == long.class) { mv.visitInsn(LASTORE); } else if (componentJavaClass == float.class) { mv.visitInsn(FASTORE); } else if (componentJavaClass == double.class) { mv.visitInsn(DASTORE); } else if (componentJavaClass == char.class) { mv.visitInsn(CASTORE); } else { mv.visitInsn(AASTORE); } }
From source file:com.cinnober.msgcodec.blink.NativeByteCodeGenerator.java
License:Open Source License
@Override protected void generateWriteStaticGroupForTypeWithId(Schema schema, ClassVisitor cv, String genClassInternalName, boolean javaClassCodec) { for (GroupDef group : schema.getGroups()) { Object groupType = group.getGroupType(); String groupDescriptor = getTypeDescriptor(groupType, javaClassCodec); MethodVisitor mv = cv.visitMethod(ACC_PRIVATE, "writeStaticGroupWithId_" + group.getName(), "(Lcom/cinnober/msgcodec/io/ByteSink;" + groupDescriptor + ")V", null, new String[] { "java/io/IOException" }); mv.visitCode();// w ww. j a v a 2 s.co m int nextWriteidVar = 3; if (group.getId() != -1) { // write with id mv.visitVarInsn(ALOAD, 1); // out mv.visitLdcInsn(group.getId() & 0xffffffffL); mv.visitMethodInsn(INVOKESTATIC, blinkOutputIName, "writeUInt64", "(Lcom/cinnober/msgcodec/io/ByteSink;J)V", false); // add extension offset (four zero bytes) mv.visitVarInsn(ALOAD, 1); // out mv.visitInsn(ICONST_0); generateEncodeUInt32Value(true, mv); mv.visitVarInsn(ALOAD, 0); // this mv.visitVarInsn(ALOAD, 1); // out mv.visitVarInsn(ALOAD, 2); // obj mv.visitMethodInsn(INVOKEVIRTUAL, genClassInternalName, "writeStaticGroup_" + group.getName(), "(Lcom/cinnober/msgcodec/io/ByteSink;" + groupDescriptor + ")V", false); mv.visitInsn(RETURN); } else { // write with id mv.visitTypeInsn(NEW, "java/lang/IllegalArgumentException"); mv.visitInsn(DUP); mv.visitLdcInsn("No group id"); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalArgumentException", "<init>", "(Ljava/lang/String;)V", false); mv.visitInsn(ATHROW); } // end mv.visitMaxs(3, nextWriteidVar); mv.visitEnd(); } }
From source file:com.e2info.helloasm.HelloAsmApp.java
License:Open Source License
public static void main(String[] args) throws IOException { String name = "HelloAsm"; int flag = ClassWriter.COMPUTE_MAXS; ClassWriter cw = new ClassWriter(flag); cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, name, null, "java/lang/Object", null); cw.visitSource(name + ".java", null); {/*from w w w . j a v a 2 s.co m*/ MethodVisitor mv; mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); mv.visitInsn(Opcodes.RETURN); // we need this call to take effect ClassWriter.COMPUTE_MAXS flag. mv.visitMaxs(0, 0); mv.visitEnd(); } { MethodVisitor mv; mv = cw.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null); mv.visitCode(); mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("hello ASM"); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V"); mv.visitInsn(Opcodes.RETURN); // we need this call to take effect ClassWriter.COMPUTE_MAXS flag. mv.visitMaxs(0, 0); mv.visitEnd(); } cw.visitEnd(); // build binary byte[] bin = cw.toByteArray(); // save asm trace for human readable { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); new ClassReader(bin).accept(new TraceClassVisitor(pw), 0); File f = new File(name + ".txt"); FileUtils.writeStringToFile(f, sw.toString()); } // save as calss file { File f = new File(name + ".class"); FileUtils.writeByteArrayToFile(f, bin); } }