Class InstancioOfClassApiImpl<T>

java.lang.Object
org.instancio.internal.InstancioApiImpl<T>
org.instancio.internal.InstancioOfClassApiImpl<T>
All Implemented Interfaces:
InstancioApi<T>, InstancioOfClassApi<T>
Direct Known Subclasses:
InstancioOfCollectionApiImpl, InstancioOfMapApiImpl

public class InstancioOfClassApiImpl<T> extends InstancioApiImpl<T> implements InstancioOfClassApi<T>
  • Constructor Details

    • InstancioOfClassApiImpl

      public InstancioOfClassApiImpl(Class<T> klass)
  • Method Details

    • withTypeParameters

      public InstancioApi<T> withTypeParameters(Class<?>... type)
      Description copied from interface: InstancioOfClassApi
      Method for supplying type parameters for generic classes.

      Example:

      
         List<Address> addresses = Instancio.of(List.class)
                 .withTypeParameters(Address.class)
                 .create();
       

      This method can be used when the type parameters need to be supplied dynamically at runtime, however it also has a couple of limitations:

      • its usage produces an unchecked assignment warning
      • it cannot be used for nested generics, such as Map<String, List<Integer>>

      The recommended approach for instantiating generic classes is using TypeTokens:

      
         List<Address> addresses = Instancio.create(new TypeToken<List<Address>>() {});
       
      Specified by:
      withTypeParameters in interface InstancioOfClassApi<T>
      Parameters:
      type - one or more type arguments
      Returns:
      API builder reference
      See Also: