Class DefaultInstances
java.lang.Object
com.comphenix.protocol.reflect.instances.DefaultInstances
- All Implemented Interfaces:
InstanceProvider
Used to construct default instances of any type.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final DefaultInstancesStandard default instance provider. -
Constructor Summary
ConstructorsConstructorDescriptionDefaultInstances(DefaultInstances other) Copy a given instance provider.DefaultInstances(InstanceProvider... instaceProviders) Construct a default instance generator using the given instance providers.DefaultInstances(com.google.common.collect.ImmutableList<InstanceProvider> registered) Construct a default instance generator using the given instance providers. -
Method Summary
Modifier and TypeMethodDescriptionprotected <T> booleancontains(T[] elements, T elementToFind) Create an instance given a type, if possible.protected <T> TcreateInstance(Class<T> type, Constructor<T> constructor, Class<?>[] types, Object[] params) Used by the default instance provider to create a class from a given constructor.static DefaultInstancesfromArray(InstanceProvider... instanceProviders) Construct a default instance generator using the given instance providers.static DefaultInstancesfromCollection(Collection<InstanceProvider> instanceProviders) Construct a default instance generator using the given instance providers.<T> TgetDefault(Class<T> type) Retrieves a default instance or value that is assignable to this type.<T> TgetDefault(Class<T> type, List<InstanceProvider> providers) Retrieves a default instance or value that is assignable to this type.intRetrieve the the maximum height of the hierachy of creates types.<T> Constructor<T> getMinimumConstructor(Class<T> type) Retrieve the constructor with the fewest number of parameters.com.google.common.collect.ImmutableList<InstanceProvider> Retrieves a immutable list of every default object providers that generates instances.booleanhasDefault(Class<?> type) Determines if a given class has a default value.booleanRetrieve whether or not the constructor's parameters must be non-null.voidsetMaximumRecursion(int maximumRecursion) Set the maximum height of the hierachy of creates types.voidsetNonNull(boolean nonNull) Set whether or not the constructor's parameters must be non-null.
-
Field Details
-
DEFAULT
Standard default instance provider.
-
-
Constructor Details
-
DefaultInstances
Construct a default instance generator using the given instance providers.- Parameters:
registered- - list of instance providers.
-
DefaultInstances
Copy a given instance provider.- Parameters:
other- - instance provider to copy.
-
DefaultInstances
Construct a default instance generator using the given instance providers.- Parameters:
instaceProviders- - array of instance providers.
-
-
Method Details
-
fromArray
Construct a default instance generator using the given instance providers.- Parameters:
instanceProviders- - array of instance providers.- Returns:
- An default instance generator.
-
fromCollection
Construct a default instance generator using the given instance providers.- Parameters:
instanceProviders- - collection of instance providers.- Returns:
- An default instance generator.
-
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
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
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
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
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
Description copied from interface:InstanceProviderCreate an instance given a type, if possible.- Specified by:
createin interfaceInstanceProvider- Parameters:
type- - type to create.- Returns:
- The instance, or NULL if the type cannot be created.
-