Class TypeReference<T>

java.lang.Object
com.azure.core.util.serializer.TypeReference<T>
Type Parameters:
T - The type being represented.

public abstract class TypeReference<T> extends Object
This class represents a generic Java type, retaining information about generics.

Code sample

 // Construct a TypeReference<T> for a Java generic type.
 // This pattern should only be used for generic types, for classes use the createInstance factory method.
 TypeReference<Map<String, Object>> typeReference = new TypeReference<Map<String, Object>>() { };
 
 // Construct a TypeReference<T> for a Java class.
 // This pattern should only be used for non-generic classes when possible, use the constructor for generic
 // class when possible.
 TypeReference<Integer> typeReference = TypeReference.createInstance(int.class);
 
  • Constructor Details

    • TypeReference

      public TypeReference()
      Constructs a new TypeReference which maintains generic information.
      Throws:
      IllegalArgumentException - If the reference is constructed without type information.
  • Method Details

    • getJavaType

      public Type getJavaType()
      Returns the Type representing T.
      Returns:
      The Type representing T.
    • createInstance

      public static <T> TypeReference<T> createInstance(Class<T> clazz)
      Creates and instance of TypeReference which maintains the generic T of the passed Class.

      This method will cache the instance of TypeReference using the passed Class as the key. This is meant to be used with non-generic types such as primitive object types and POJOs, not Map<String, Object> or List<Integer> parameterized types.

      Type Parameters:
      T - The generic type.
      Parameters:
      clazz - Class that contains generic information used to create the TypeReference.
      Returns:
      Either the cached or new instance of TypeReference.
    • getJavaClass

      public Class<T> getJavaClass()
      Returns the Class representing instance of the TypeReference created.
      Returns:
      The Class representing instance of the TypeReference created using the createInstance(Class), otherwise returns null.