mirror of
https://github.com/fankes/moshi.git
synced 2025-10-20 00:19:21 +08:00
Merge pull request #74 from square/jwilson_0820_make_types_public
Make four methods on Types public.
This commit is contained in:
@@ -29,13 +29,8 @@ import java.util.Map;
|
|||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
/**
|
/** Factory methods for types. */
|
||||||
* Static methods for working with types.
|
public final class Types {
|
||||||
*
|
|
||||||
* @author Bob Lee
|
|
||||||
* @author Jesse Wilson
|
|
||||||
*/
|
|
||||||
final class Types {
|
|
||||||
static final Type[] EMPTY_TYPE_ARRAY = new Type[] {};
|
static final Type[] EMPTY_TYPE_ARRAY = new Type[] {};
|
||||||
|
|
||||||
private Types() {
|
private Types() {
|
||||||
@@ -75,7 +70,7 @@ final class Types {
|
|||||||
* Returns a type that is functionally equal but not necessarily equal according to {@link
|
* Returns a type that is functionally equal but not necessarily equal according to {@link
|
||||||
* Object#equals(Object) Object.equals()}.
|
* Object#equals(Object) Object.equals()}.
|
||||||
*/
|
*/
|
||||||
public static Type canonicalize(Type type) {
|
static Type canonicalize(Type type) {
|
||||||
if (type instanceof Class) {
|
if (type instanceof Class) {
|
||||||
Class<?> c = (Class<?>) type;
|
Class<?> c = (Class<?>) type;
|
||||||
return c.isArray() ? new GenericArrayTypeImpl(canonicalize(c.getComponentType())) : c;
|
return c.isArray() ? new GenericArrayTypeImpl(canonicalize(c.getComponentType())) : c;
|
||||||
@@ -98,7 +93,7 @@ final class Types {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Class<?> getRawType(Type type) {
|
static Class<?> getRawType(Type type) {
|
||||||
if (type instanceof Class<?>) {
|
if (type instanceof Class<?>) {
|
||||||
// type is a normal class.
|
// type is a normal class.
|
||||||
return (Class<?>) type;
|
return (Class<?>) type;
|
||||||
@@ -135,7 +130,7 @@ final class Types {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Returns true if {@code a} and {@code b} are equal. */
|
/** Returns true if {@code a} and {@code b} are equal. */
|
||||||
public static boolean equals(Type a, Type b) {
|
static boolean equals(Type a, Type b) {
|
||||||
if (a == b) {
|
if (a == b) {
|
||||||
return true; // Also handles (a == null && b == null).
|
return true; // Also handles (a == null && b == null).
|
||||||
|
|
||||||
@@ -186,7 +181,7 @@ final class Types {
|
|||||||
return o != null ? o.hashCode() : 0;
|
return o != null ? o.hashCode() : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String typeToString(Type type) {
|
static String typeToString(Type type) {
|
||||||
return type instanceof Class ? ((Class<?>) type).getName() : type.toString();
|
return type instanceof Class ? ((Class<?>) type).getName() : type.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -242,7 +237,7 @@ final class Types {
|
|||||||
getGenericSupertype(context, contextRawType, supertype));
|
getGenericSupertype(context, contextRawType, supertype));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Type getGenericSuperclass(Type type) {
|
static Type getGenericSuperclass(Type type) {
|
||||||
Class<?> rawType = Types.getRawType(type);
|
Class<?> rawType = Types.getRawType(type);
|
||||||
return resolve(type, rawType, rawType.getGenericSuperclass());
|
return resolve(type, rawType, rawType.getGenericSuperclass());
|
||||||
}
|
}
|
||||||
@@ -251,7 +246,7 @@ final class Types {
|
|||||||
* Returns the element type of {@code type} if it is an array type, or null if it is not an
|
* Returns the element type of {@code type} if it is an array type, or null if it is not an
|
||||||
* array type.
|
* array type.
|
||||||
*/
|
*/
|
||||||
public static Type arrayComponentType(Type type) {
|
static Type arrayComponentType(Type type) {
|
||||||
if (type instanceof GenericArrayType) {
|
if (type instanceof GenericArrayType) {
|
||||||
return ((GenericArrayType) type).getGenericComponentType();
|
return ((GenericArrayType) type).getGenericComponentType();
|
||||||
} else if (type instanceof Class) {
|
} else if (type instanceof Class) {
|
||||||
@@ -265,7 +260,7 @@ final class Types {
|
|||||||
* Returns the element type of this collection type.
|
* Returns the element type of this collection type.
|
||||||
* @throws IllegalArgumentException if this type is not a collection.
|
* @throws IllegalArgumentException if this type is not a collection.
|
||||||
*/
|
*/
|
||||||
public static Type collectionElementType(Type context, Class<?> contextRawType) {
|
static Type collectionElementType(Type context, Class<?> contextRawType) {
|
||||||
Type collectionType = getSupertype(context, contextRawType, Collection.class);
|
Type collectionType = getSupertype(context, contextRawType, Collection.class);
|
||||||
|
|
||||||
if (collectionType instanceof WildcardType) {
|
if (collectionType instanceof WildcardType) {
|
||||||
@@ -281,7 +276,7 @@ final class Types {
|
|||||||
* Returns a two element array containing this map's key and value types in positions 0 and 1
|
* Returns a two element array containing this map's key and value types in positions 0 and 1
|
||||||
* respectively.
|
* respectively.
|
||||||
*/
|
*/
|
||||||
public static Type[] mapKeyAndValueTypes(Type context, Class<?> contextRawType) {
|
static Type[] mapKeyAndValueTypes(Type context, Class<?> contextRawType) {
|
||||||
// Work around a problem with the declaration of java.util.Properties. That class should extend
|
// Work around a problem with the declaration of java.util.Properties. That class should extend
|
||||||
// Hashtable<String, String>, but it's declared to extend Hashtable<Object, Object>.
|
// Hashtable<String, String>, but it's declared to extend Hashtable<Object, Object>.
|
||||||
if (context == Properties.class) return new Type[] { String.class, String.class };
|
if (context == Properties.class) return new Type[] { String.class, String.class };
|
||||||
@@ -294,7 +289,7 @@ final class Types {
|
|||||||
return new Type[] { Object.class, Object.class };
|
return new Type[] { Object.class, Object.class };
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Type resolve(Type context, Class<?> contextRawType, Type toResolve) {
|
static Type resolve(Type context, Class<?> contextRawType, Type toResolve) {
|
||||||
// This implementation is made a little more complicated in an attempt to avoid object-creation.
|
// This implementation is made a little more complicated in an attempt to avoid object-creation.
|
||||||
while (true) {
|
while (true) {
|
||||||
if (toResolve instanceof TypeVariable) {
|
if (toResolve instanceof TypeVariable) {
|
||||||
|
Reference in New Issue
Block a user