Package org.web3j.abi

Class TypeReference<T extends Type>

java.lang.Object
org.web3j.abi.TypeReference<T>
All Implemented Interfaces:
Comparable<TypeReference<T>>
Direct Known Subclasses:
TypeReference.StaticArrayTypeReference

public abstract class TypeReference<T extends Type> extends Object implements Comparable<TypeReference<T>>
Type wrapper to get around limitations of Java's type erasure. This is so that we can pass around Typed Array types.

See this blog post for further details.

It may make sense to switch to using Java's reflection Type to avoid working around this fundamental generics limitation.

  • Field Details

  • Constructor Details

    • TypeReference

      protected TypeReference()
    • TypeReference

      protected TypeReference(boolean indexed, List<TypeReference<?>> innerTypesIn)
    • TypeReference

      protected TypeReference(boolean indexed)
  • Method Details

    • getSubTypeReference

      public TypeReference getSubTypeReference()
      getSubTypeReference() is used by instantiateType to see what TypeReference is wrapped by this one. eg calling getSubTypeReference() on a TypeReference to DynamicArray[StaticArray3[Uint256]] would return a TypeReference to StaticArray3[Uint256]
      Returns:
      the type wrapped by this Array TypeReference, or null if not Array
    • getInnerTypes

      public List<TypeReference<?>> getInnerTypes()
    • compareTo

      public int compareTo(TypeReference<T> o)
      Specified by:
      compareTo in interface Comparable<T extends Type>
    • getType

      public Type getType()
    • isIndexed

      public boolean isIndexed()
    • getClassType

      public Class<T> getClassType() throws ClassNotFoundException
      Workaround to ensure type does not come back as T due to erasure, this enables you to create a TypeReference via Class<T>.
      Returns:
      the parameterized Class type if applicable, otherwise a regular class
      Throws:
      ClassNotFoundException - if the class type cannot be determined
    • create

      public static <T extends Type> TypeReference<T> create(Class<T> cls)
    • create

      public static <T extends Type> TypeReference<T> create(Class<T> cls, boolean indexed)
    • getAtomicTypeClass

      protected static Class<? extends Type> getAtomicTypeClass(String solidityType, boolean primitives) throws ClassNotFoundException
      This is a helper method that only works for atomic types (uint, bytes, etc). Array types must be wrapped by a ParameterizedType.
      Parameters:
      solidityType - the solidity as a string eg Address Int
      primitives - is it a primitive type
      Returns:
      returns
      Throws:
      ClassNotFoundException - when the class cannot be found.
    • makeTypeReference

      public static TypeReference makeTypeReference(String solidityType) throws ClassNotFoundException
      Throws:
      ClassNotFoundException
    • makeTypeReference

      public static TypeReference makeTypeReference(String solidityType, boolean indexed, boolean primitives) throws ClassNotFoundException
      Throws:
      ClassNotFoundException