private void encodeSimple ( GeneratorAdapter mg , TypeToken < ? > type , Schema schema , String encodeMethod , int value , int encoder ) { // encoder.writeXXX(value); TypeToken < ? > encodeType = type ; mg . loadArg ( encoder ) ; mg . loadArg ( value ) ; if ( Primitives . isWrapperType ( encodeType . getRawType ( ) ) ) { encodeType = TypeToken . of ( Primitives . unwrap ( encodeType . getRawType ( ) ) ) ; mg . unbox ( Type . getType ( encodeType . getRawType ( ) ) ) ; // A special case since INT type represents (byte, char, short and int). if ( schema . getType ( ) == Schema . Type . INT && ! int . class . equals ( encodeType . getRawType ( ) ) ) { encodeType = TypeToken . of ( int . class ) ; } } else if ( schema . getType ( ) == Schema . Type . STRING && ! String . class . equals ( encodeType . getRawType ( ) ) ) { // For non-string object that has a String schema, invoke toString(). mg . invokeVirtual ( Type . getType ( encodeType . getRawType ( ) ) , getMethod ( String . class , "toString" ) ) ; encodeType = TypeToken . of ( String . class ) ; } else if ( schema . getType ( ) == Schema . Type . BYTES && UUID . class . equals ( encodeType . getRawType ( ) ) ) { // Special case UUID, encode as byte array // ByteBuffer buf = ByteBuffer.allocate(Longs.BYTES * 2) // .putLong(uuid.getMostSignificantBits()) // .putLong(uuid.getLeastSignificantBits()); // encoder.writeBytes((ByteBuffer) buf.flip()); Type byteBufferType = Type . getType ( ByteBuffer . class ) ; Type uuidType = Type . getType ( UUID . class ) ; mg . push ( Longs . BYTES * 2 ) ; mg . invokeStatic ( byteBufferType , getMethod ( ByteBuffer . class , "allocate" , int . class ) ) ; mg . swap ( ) ; mg . invokeVirtual ( uuidType , getMethod ( long . class , "getMostSignificantBits" ) ) ; mg . invokeVirtual ( byteBufferType , getMethod ( ByteBuffer . class , "putLong" , long . class ) ) ; mg . loadArg ( value ) ; mg . invokeVirtual ( uuidType , getMethod ( long . class , "getLeastSignificantBits" ) ) ; mg . invokeVirtual ( byteBufferType , getMethod ( ByteBuffer . class , "putLong" , long . class ) ) ; mg . invokeVirtual ( Type . getType ( Buffer . class ) , getMethod ( Buffer . class , "flip" ) ) ; mg . checkCast ( byteBufferType ) ; encodeType = TypeToken . of ( ByteBuffer . class ) ; } mg . invokeInterface ( Type . getType ( Encoder . class ) , getMethod ( Encoder . class , encodeMethod , encodeType . getRawType ( ) ) ) ; mg . pop ( ) ; }
Generates method body for encoding simple schema type by calling corresponding write method in Encoder .