static public Type resolveType ( Class fromClass , Class toClass , Type type ) { // Explicit type, eg String. if ( type instanceof Class ) return ( Class ) type ; // Type variable, eg T. if ( type instanceof TypeVariable ) return resolveTypeVariable ( fromClass , toClass , type , true ) ; // Type which has a type parameter, eg ArrayList<T> or ArrayList<ArrayList<T>>. if ( type instanceof ParameterizedType ) return ( Class ) ( ( ParameterizedType ) type ) . getRawType ( ) ; // Array which has a type variable, eg T[] or T[][], and also arrays with a type parameter, eg ArrayList<T>[]. if ( type instanceof GenericArrayType ) { int dimensions = 1 ; while ( true ) { type = ( ( GenericArrayType ) type ) . getGenericComponentType ( ) ; if ( ! ( type instanceof GenericArrayType ) ) break ; dimensions ++ ; } Type componentType = resolveType ( fromClass , toClass , type ) ; if ( ! ( componentType instanceof Class ) ) return type ; if ( dimensions == 1 ) return Array . newInstance ( ( Class ) componentType , 0 ) . getClass ( ) ; return Array . newInstance ( ( Class ) componentType , new int [ dimensions ] ) . getClass ( ) ; } // Type which has a wildcard, eg ArrayList<?>, ArrayList<? extends Number>, or ArrayList<? super Integer>. if ( type instanceof WildcardType ) { // Check upper bound first, it is more likely. There is always an upper bound. Type upperBound = ( ( WildcardType ) type ) . getUpperBounds ( ) [ 0 ] ; if ( upperBound != Object . class ) return resolveType ( fromClass , toClass , upperBound ) ; Type [ ] lowerBounds = ( ( WildcardType ) type ) . getLowerBounds ( ) ; if ( lowerBounds . length != 0 ) return resolveType ( fromClass , toClass , lowerBounds [ 0 ] ) ; return Object . class ; } // If this happens, there is a case we need to handle. throw new KryoException ( "Unable to resolve type: " + type ) ; }
Returns the class for the specified type after replacing any type variables using the class hierarchy between the specified classes .