protected static Streamer create ( Class < ? > target ) throws IOException { // validate that the class is really streamable boolean isInner = false , isStatic = Modifier . isStatic ( target . getModifiers ( ) ) ; try { isInner = ( target . getDeclaringClass ( ) != null ) ; } catch ( Throwable t ) { log . warning ( "Failure checking innerness of class" , "class" , target . getName ( ) , "error" , t ) ; } if ( isInner && ! isStatic ) { throw new IllegalArgumentException ( "Cannot stream non-static inner class: " + target . getName ( ) ) ; } // create streamers for array types if ( target . isArray ( ) ) { Class < ? > componentType = target . getComponentType ( ) ; if ( Modifier . isFinal ( componentType . getModifiers ( ) ) ) { Streamer delegate = Streamer . getStreamer ( componentType ) ; if ( delegate != null ) { return new FinalArrayStreamer ( componentType , delegate ) ; } // else: error, below } else if ( isStreamable ( componentType ) ) { return new ArrayStreamer ( componentType ) ; } String errmsg = "Aiya! Streamer created for array type but we have no registered " + "streamer for the element type [type=" + target . getName ( ) + "]" ; throw new RuntimeException ( errmsg ) ; } // create streamers for enum types if ( target . isEnum ( ) ) { switch ( ENUM_POLICY ) { case NAME_WITH_BYTE_ENUM : case ORDINAL_WITH_BYTE_ENUM : if ( ByteEnum . class . isAssignableFrom ( target ) ) { return new ByteEnumStreamer ( target ) ; } break ; default : // we do not care if it is a ByteEnum, we move on... break ; } switch ( ENUM_POLICY ) { case NAME_WITH_BYTE_ENUM : case NAME : return new NameEnumStreamer ( target ) ; default : List < ? > universe = ImmutableList . copyOf ( target . getEnumConstants ( ) ) ; int maxOrdinal = universe . size ( ) - 1 ; if ( maxOrdinal <= Byte . MAX_VALUE ) { return new ByteOrdEnumStreamer ( target , universe ) ; } else if ( maxOrdinal <= Short . MAX_VALUE ) { return new ShortOrdEnumStreamer ( target , universe ) ; } else { return new IntOrdEnumStreamer ( target , universe ) ; } } } // create Streamers for other types Method reader = null ; Method writer = null ; try { reader = target . getMethod ( READER_METHOD_NAME , READER_ARGS ) ; } catch ( NoSuchMethodException nsme ) { // nothing to worry about, we just don't have one } try { writer = target . getMethod ( WRITER_METHOD_NAME , WRITER_ARGS ) ; } catch ( NoSuchMethodException nsme ) { // nothing to worry about, we just don't have one } // if there is no reader and no writer, we can do a simpler thing if ( ( reader == null ) && ( writer == null ) ) { return new ClassStreamer ( target ) ; } else { return new CustomClassStreamer ( target , reader , writer ) ; } }
Create the appropriate Streamer for a newly - seen class .