Top Description Inners Fields Constructors Methods
com.google.gson.internal

public final Class GsonTypes

extends Object
Class Inheritance
Static Imports
java.util.Objects.requireNonNull

Static methods for working with types.
Authors
Bob Lee, Jesse Wilson

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class
private static class
GsonTypes.WildcardTypeImpl

The WildcardType interface supports multiple upper bounds and multiple lower bounds.

Field Summary

Modifier and TypeField and Description
pack-priv static final Type[]

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static GenericArrayType

Returns:

a generic array type.
arrayOf
(Type componentType)

Returns an array type whose elements are all instances of componentType.

public static Type
canonicalize(Type type)

Returns a type that is functionally equal but not necessarily equal according to Object.equals().

pack-priv static void
private static Class<?>
declaringClassOf(TypeVariable<?> typeVariable)

Returns the declaring class of typeVariable, or null if it was not declared by a class.

private static boolean
public static boolean
equals(Type a, Type b)

Returns true if a and b are equal.

public static Type
getArrayComponentType(Type array)

Returns the component type of this array type.

public static Type
getCollectionElementType(Type context, Class<?> contextRawType)

Returns the element type of this collection type.

private static Type
getGenericSupertype(Type context, Class<?> rawType, Class<?> supertype)

Returns the generic supertype for supertype.

public static Type[]
getMapKeyAndValueTypes(Type context, Class<?> contextRawType)

Returns a two element array containing this map's key and value types in positions 0 and 1 respectively.

public static Class<?>
private static Type
getSupertype(Type context, Class<?> contextRawType, Class<?>
a superclass of, or interface implemented by, this.
supertype
)

Returns the generic form of supertype.

private static int
indexOf(Object[] array, Object toFind)

public static ParameterizedType

Returns:

a parameterized type.
newParameterizedTypeWithOwner
(Type ownerType, Class<?> rawType, Type... typeArguments)

Returns a new parameterized type, applying typeArguments to rawType and enclosed by ownerType.

public static boolean
requiresOwnerType(Type rawType)

Whether an owner type must be specified when constructing a ParameterizedType for rawType.

public static Type
resolve(Type context, Class<?> contextRawType, Type toResolve)

private static Type
resolve(Type context, Class<?> contextRawType, Type toResolve, Map<TypeVariable<?>, Type> visitedTypeVariables)

private static Type
resolveTypeVariable(Type context, Class<?> contextRawType, TypeVariable<?> unknown)

public static WildcardType
subtypeOf(Type bound)

Returns a type that represents an unknown type that extends bound.

public static WildcardType
supertypeOf(Type bound)

Returns a type that represents an unknown supertype of bound.

public static String
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

EMPTY_TYPE_ARRAYback to summary
pack-priv static final Type[] EMPTY_TYPE_ARRAY

Constructor Detail

GsonTypesback to summary
private GsonTypes()

Method Detail

arrayOfback to summary
public static GenericArrayType arrayOf(Type componentType)

Returns an array type whose elements are all instances of componentType.

Returns:GenericArrayType

a generic array type.

canonicalizeback to summary
public static Type canonicalize(Type type)

Returns a type that is functionally equal but not necessarily equal according to Object.equals().

checkNotPrimitiveback to summary
pack-priv static void checkNotPrimitive(Type type)
declaringClassOfback to summary
private static Class<?> declaringClassOf(TypeVariable<?> typeVariable)

Returns the declaring class of typeVariable, or null if it was not declared by a class.

equalback to summary
private static boolean equal(Object a, Object b)
equalsback to summary
public static boolean equals(Type a, Type b)

Returns true if a and b are equal.

getArrayComponentTypeback to summary
public static Type getArrayComponentType(Type array)

Returns the component type of this array type.

Exceptions
ClassCastException:
if this type is not an array.
getCollectionElementTypeback to summary
public static Type getCollectionElementType(Type context, Class<?> contextRawType)

Returns the element type of this collection type.

Exceptions
IllegalArgumentException:
if this type is not a collection.
getGenericSupertypeback to summary
private static Type getGenericSupertype(Type context, Class<?> rawType, Class<?> supertype)

Returns the generic supertype for supertype. For example, given a class IntegerSet, the result for when supertype is Set.class is Set<Integer> and the result when the supertype is Collection.class is Collection<Integer>.

getMapKeyAndValueTypesback to summary
public static Type[] getMapKeyAndValueTypes(Type context, Class<?> contextRawType)

Returns a two element array containing this map's key and value types in positions 0 and 1 respectively.

getRawTypeback to summary
public static Class<?> getRawType(Type type)
getSupertypeback to summary
private static Type getSupertype(Type context, Class<?> contextRawType, Class<?> supertype)

Returns the generic form of supertype. For example, if this is ArrayList<String>, this returns Iterable<String> given the input Iterable.class.

Parameters
supertype:Class<?>

a superclass of, or interface implemented by, this.

indexOfback to summary
private static int indexOf(Object[] array, Object toFind)
newParameterizedTypeWithOwnerback to summary
public static ParameterizedType newParameterizedTypeWithOwner(Type ownerType, Class<?> rawType, Type... typeArguments)

Returns a new parameterized type, applying typeArguments to rawType and enclosed by ownerType.

Returns:ParameterizedType

a parameterized type.

requiresOwnerTypeback to summary
public static boolean requiresOwnerType(Type rawType)

Whether an owner type must be specified when constructing a ParameterizedType for rawType.

Note that this method might not require an owner type for all cases where Java reflection would create parameterized types with owner type.

resolveback to summary
public static Type resolve(Type context, Class<?> contextRawType, Type toResolve)
resolveback to summary
private static Type resolve(Type context, Class<?> contextRawType, Type toResolve, Map<TypeVariable<?>, Type> visitedTypeVariables)
resolveTypeVariableback to summary
private static Type resolveTypeVariable(Type context, Class<?> contextRawType, TypeVariable<?> unknown)
subtypeOfback to summary
public static WildcardType subtypeOf(Type bound)

Returns a type that represents an unknown type that extends bound. For example, if bound is CharSequence.class, this returns ? extends CharSequence. If bound is Object.class, this returns ?, which is shorthand for ? extends Object.

supertypeOfback to summary
public static WildcardType supertypeOf(Type bound)

Returns a type that represents an unknown supertype of bound. For example, if bound is String.class, this returns ? super String.

typeToStringback to summary
public static String typeToString(Type type)
com.google.gson.internal back to summary

private final Class GsonTypes.GenericArrayTypeImpl

extends Object
implements GenericArrayType
Class Inheritance
All Implemented Interfaces
java.lang.reflect.GenericArrayType, java.lang.reflect.Type

Field Summary

Modifier and TypeField and Description
private final Type

Constructor Summary

AccessConstructor and Description
pack-priv
GenericArrayTypeImpl(Type componentType)

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(Object o)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public Type
getGenericComponentType()

Implements java.lang.reflect.GenericArrayType.getGenericComponentType.

Returns a Type object representing the component type of this array.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

componentTypeback to summary
private final Type componentType

Constructor Detail

GenericArrayTypeImplback to summary
pack-priv GenericArrayTypeImpl(Type componentType)

Method Detail

equalsback to summary
public boolean equals(Object o)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Annotations
@Override
getGenericComponentTypeback to summary
public Type getGenericComponentType()

Implements java.lang.reflect.GenericArrayType.getGenericComponentType.

Doc from java.lang.reflect.GenericArrayType.getGenericComponentType.

Returns a Type object representing the component type of this array. This method creates the component type of the array. See the declaration of ParameterizedType for the semantics of the creation process for parameterized types and see TypeVariable for the creation process for type variables.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Annotations
@Override
com.google.gson.internal back to summary

private final Class GsonTypes.ParameterizedTypeImpl

extends Object
implements ParameterizedType
Class Inheritance
All Implemented Interfaces
java.lang.reflect.ParameterizedType, java.lang.reflect.Type

Field Summary

Modifier and TypeField and Description
private final Type
private final Type
private final Type[]

Constructor Summary

AccessConstructor and Description
pack-priv
ParameterizedTypeImpl(Type ownerType, Class<?> rawType, Type... typeArguments)

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(Object other)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public Type[]
getActualTypeArguments()

Implements java.lang.reflect.ParameterizedType.getActualTypeArguments.

Returns an array of Type objects representing the actual type arguments to this type.

public Type
getOwnerType()

Implements java.lang.reflect.ParameterizedType.getOwnerType.

Returns a Type object representing the type that this type is a member of.

public Type
getRawType()

Implements java.lang.reflect.ParameterizedType.getRawType.

Returns the Type object representing the class or interface that declared this type.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

private static int
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

ownerTypeback to summary
private final Type ownerType
rawTypeback to summary
private final Type rawType
typeArgumentsback to summary
private final Type[] typeArguments

Constructor Detail

ParameterizedTypeImplback to summary
pack-priv ParameterizedTypeImpl(Type ownerType, Class<?> rawType, Type... typeArguments)

Method Detail

equalsback to summary
public boolean equals(Object other)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Annotations
@Override
getActualTypeArgumentsback to summary
public Type[] getActualTypeArguments()

Implements java.lang.reflect.ParameterizedType.getActualTypeArguments.

Doc from java.lang.reflect.ParameterizedType.getActualTypeArguments.

Returns an array of Type objects representing the actual type arguments to this type.

Note that in some cases, the returned array be empty. This can occur if this type represents a non-parameterized type nested within a parameterized type.

Annotations
@Override
getOwnerTypeback to summary
public Type getOwnerType()

Implements java.lang.reflect.ParameterizedType.getOwnerType.

Doc from java.lang.reflect.ParameterizedType.getOwnerType.

Returns a Type object representing the type that this type is a member of. For example, if this type is O<T>.I<S>, return a representation of O<T>.

If this type is a top-level type, null is returned.

Annotations
@Override
getRawTypeback to summary
public Type getRawType()

Implements java.lang.reflect.ParameterizedType.getRawType.

Doc from java.lang.reflect.ParameterizedType.getRawType.

Returns the Type object representing the class or interface that declared this type.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Annotations
@Override
hashCodeOrZeroback to summary
private static int hashCodeOrZero(Object o)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Annotations
@Override
com.google.gson.internal back to summary

private final Class GsonTypes.WildcardTypeImpl

extends Object
implements WildcardType
Class Inheritance
All Implemented Interfaces
java.lang.reflect.WildcardType, java.lang.reflect.Type

The WildcardType interface supports multiple upper bounds and multiple lower bounds. We only support what the target Java version supports - at most one bound, see also https://bugs.openjdk.java.net/browse/JDK-8250660. If a lower bound is set, the upper bound must be Object.class.

Field Summary

Modifier and TypeField and Description
private final Type
private final Type

Constructor Summary

AccessConstructor and Description
pack-priv
WildcardTypeImpl(Type[] upperBounds, Type[] lowerBounds)

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(Object other)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public Type[]
getLowerBounds()

Implements java.lang.reflect.WildcardType.getLowerBounds.

Returns an array of Type objects representing the lower bound(s) of this type variable.

public Type[]
getUpperBounds()

Implements java.lang.reflect.WildcardType.getUpperBounds.

Returns an array of Type objects representing the upper bound(s) of this type variable.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

lowerBoundback to summary
private final Type lowerBound
upperBoundback to summary
private final Type upperBound

Constructor Detail

WildcardTypeImplback to summary
pack-priv WildcardTypeImpl(Type[] upperBounds, Type[] lowerBounds)

Method Detail

equalsback to summary
public boolean equals(Object other)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Annotations
@Override
getLowerBoundsback to summary
public Type[] getLowerBounds()

Implements java.lang.reflect.WildcardType.getLowerBounds.

Doc from java.lang.reflect.WildcardType.getLowerBounds.

Returns an array of Type objects representing the lower bound(s) of this type variable. If no lower bound is explicitly declared, the lower bound is the type of null. In this case, a zero length array is returned.

For each lower bound B :

  • if B is a parameterized type or a type variable, it is created, (see ParameterizedType for the details of the creation process for parameterized types).
  • Otherwise, B is resolved.
Annotations
@Override
getUpperBoundsback to summary
public Type[] getUpperBounds()

Implements java.lang.reflect.WildcardType.getUpperBounds.

Doc from java.lang.reflect.WildcardType.getUpperBounds.

Returns an array of Type objects representing the upper bound(s) of this type variable. If no upper bound is explicitly declared, the upper bound is Object.

For each upper bound B :

  • if B is a parameterized type or a type variable, it is created, (see ParameterizedType for the details of the creation process for parameterized types).
  • Otherwise, B is resolved.
Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Annotations
@Override