public class ObjectFactoryUtil extends Object
ObjectFactory
.
Devs should add the following import:
import static co.codewizards.cloudstore.core.objectfactory.ObjectFactoryUtil.*;They can then invoke one of the
createObject(...)
methods simply without any prefix.
In order to register a sub-class as replacement for a certain base-class, implementors have to provide a
ClassExtension
and register it using the ServiceLoader
-mechanism.
Important: If a certain base-class should be replaceable using this mechanism, all occurrences
of new MyBaseClass(...)
in the entire code-base must be replaced by createObject(MyBaseClass.class, ...)
.
Important 2: It is urgently recommended not to use this approach, whenever it is possible to use a better solution,
preferably a well-defined service (=> ServiceLoader
). There are situations, though, e.g. data-model-classes
(a.k.a. entities), where services are not possible and ObjectFactoryUtil
+ ClassExtension
is the perfect solution.
Modifier | Constructor and Description |
---|---|
protected |
ObjectFactoryUtil() |
Modifier and Type | Method and Description |
---|---|
static <T> T |
createObject(Class<T> clazz)
Create an instance of the given class or a sub-class registered as replacement.
|
static <T> T |
createObject(Class<T> clazz,
Class<?>[] parameterTypes,
Object... parameters)
Create an instance of the given class or a sub-class registered as replacement.
|
static <T> T |
createObject(Class<T> clazz,
Object... parameters)
Create an instance of the given class or a sub-class registered as replacement.
|
static <T> Class<? extends T> |
getExtendingClass(Class<T> clazz) |
protected ObjectFactoryUtil()
public static <T> T createObject(Class<T> clazz)
If at least one ClassExtension
is registered for the given class, the ClassExtension
with the highest priority
is chosen and its
extendingClass
instantiated instead of the base-class passed as clazz
.
This method always invokes the default constructor (without any parameters).
clazz
- the base-class to be instantiated. Must not be null
.ClassExtension
.
Never null
.createObject(Class, Class[], Object...)
public static <T> T createObject(Class<T> clazz, Object... parameters)
If at least one ClassExtension
is registered for the given class, the ClassExtension
with the highest priority
is chosen and its
extendingClass
instantiated instead of the base-class passed as clazz
.
This method tries to automatically find the best constructor matching the given parameters.
If multiple constructors might match and they are not semantically the same (only doing some implicit conversions),
you should instead use createObject(Class, Class[], Object...)
and pass the specific parameter types.
clazz
- the base-class to be instantiated. Must not be null
.parameters
- the parameters to be passed to the constructor.ClassExtension
.
Never null
.createObject(Class, Class[], Object...)
public static <T> T createObject(Class<T> clazz, Class<?>[] parameterTypes, Object... parameters)
If at least one ClassExtension
is registered for the given class, the ClassExtension
with the highest priority
is chosen and its
extendingClass
instantiated instead of the base-class passed as clazz
.
This method invokes the constructor matching exactly the given parameter-types. If parameterTypes
is
null
, a matching constructor is searched automatically based on the concrete parameters
.
clazz
- the base-class to be instantiated. Must not be null
.parameterTypes
- either null
or the exact argument-types of the constructor to be invoked.parameters
- the parameters to be passed to the constructor.ClassExtension
.
Never null
.public static <T> Class<? extends T> getExtendingClass(Class<T> clazz)
Copyright © 2013–2019. All rights reserved.