Class DefaultInstances

java.lang.Object
com.comphenix.protocol.reflect.instances.DefaultInstances
All Implemented Interfaces:
InstanceProvider

public class DefaultInstances extends Object implements InstanceProvider
Used to construct default instances of any type.
  • Field Details

    • DEFAULT

      public static final DefaultInstances DEFAULT
      Standard default instance provider.
  • Constructor Details

    • DefaultInstances

      public DefaultInstances(com.google.common.collect.ImmutableList<InstanceProvider> registered)
      Construct a default instance generator using the given instance providers.
      Parameters:
      registered - - list of instance providers.
    • DefaultInstances

      public DefaultInstances(DefaultInstances other)
      Copy a given instance provider.
      Parameters:
      other - - instance provider to copy.
    • DefaultInstances

      public DefaultInstances(InstanceProvider... instaceProviders)
      Construct a default instance generator using the given instance providers.
      Parameters:
      instaceProviders - - array of instance providers.
  • Method Details

    • fromArray

      public static DefaultInstances fromArray(InstanceProvider... instanceProviders)
      Construct a default instance generator using the given instance providers.
      Parameters:
      instanceProviders - - array of instance providers.
      Returns:
      An default instance generator.
    • fromCollection

      public static DefaultInstances fromCollection(Collection<InstanceProvider> instanceProviders)
      Construct a default instance generator using the given instance providers.
      Parameters:
      instanceProviders - - collection of instance providers.
      Returns:
      An default instance generator.
    • getRegistered

      public com.google.common.collect.ImmutableList<InstanceProvider> getRegistered()
      Retrieves a immutable list of every default object providers that generates instances.
      Returns:
      Table of instance providers.
    • isNonNull

      public boolean isNonNull()
      Retrieve whether or not the constructor's parameters must be non-null.
      Returns:
      TRUE if they must be non-null, FALSE otherwise.
    • setNonNull

      public void setNonNull(boolean nonNull)
      Set whether or not the constructor's parameters must be non-null.
      Parameters:
      nonNull - - TRUE if they must be non-null, FALSE otherwise.
    • getMaximumRecursion

      public int getMaximumRecursion()
      Retrieve the the maximum height of the hierachy of creates types.
      Returns:
      Maximum height.
    • setMaximumRecursion

      public void setMaximumRecursion(int maximumRecursion)
      Set the maximum height of the hierachy of creates types. Used to prevent cycles.
      Parameters:
      maximumRecursion - - maximum recursion height.
    • getDefault

      public <T> T getDefault(Class<T> type)
      Retrieves a default instance or value that is assignable to this type.

      This includes, but isn't limited too:

      • Primitive types. Returns either zero or null.
      • Primitive wrappers.
      • String types. Returns an empty string.
      • Arrays. Returns a zero-length array of the same type.
      • Enums. Returns the first declared element.
      • Collection interfaces, such as List and Set. Returns the most appropriate empty container.
      • Any type with a public constructor that has parameters with defaults.
      Type Parameters:
      T - Type
      Parameters:
      type - - the type to construct a default value.
      Returns:
      A default value/instance, or NULL if not possible.
    • hasDefault

      public boolean hasDefault(Class<?> type)
      Determines if a given class has a default value.
      Parameters:
      type - - the class to check
      Returns:
      true if the class has a default value, false otherwise
    • getMinimumConstructor

      public <T> Constructor<T> getMinimumConstructor(Class<T> type)
      Retrieve the constructor with the fewest number of parameters.
      Type Parameters:
      T - Type
      Parameters:
      type - - type to construct.
      Returns:
      A constructor with the fewest number of parameters, or NULL if the type has no constructors.
    • getDefault

      public <T> T getDefault(Class<T> type, List<InstanceProvider> providers)
      Retrieves a default instance or value that is assignable to this type.

      This includes, but isn't limited too:

      • Primitive types. Returns either zero or null.
      • Primitive wrappers.
      • String types. Returns an empty string.
      • Arrays. Returns a zero-length array of the same type.
      • Enums. Returns the first declared element.
      • Collection interfaces, such as List and Set. Returns the most appropriate empty container.
      • Any type with a public constructor that has parameters with defaults.
      Type Parameters:
      T - Type
      Parameters:
      type - - the type to construct a default value.
      providers - - instance providers used during the construction.
      Returns:
      A default value/instance, or NULL if not possible.
    • createInstance

      protected <T> T createInstance(Class<T> type, Constructor<T> constructor, Class<?>[] types, Object[] params)
      Used by the default instance provider to create a class from a given constructor. The default method uses reflection.
      Type Parameters:
      T - Type
      Parameters:
      type - - the type to create.
      constructor - - the constructor to use.
      types - - type of each parameter in order.
      params - - value of each parameter in order.
      Returns:
      The constructed instance.
    • contains

      protected <T> boolean contains(T[] elements, T elementToFind)
    • create

      public Object create(@Nullable Class<?> type)
      Description copied from interface: InstanceProvider
      Create an instance given a type, if possible.
      Specified by:
      create in interface InstanceProvider
      Parameters:
      type - - type to create.
      Returns:
      The instance, or NULL if the type cannot be created.