Package net.obvj.confectory.util
Enum ObjectFactory
- java.lang.Object
-
- java.lang.Enum<ObjectFactory>
-
- net.obvj.confectory.util.ObjectFactory
-
- All Implemented Interfaces:
Serializable,Comparable<ObjectFactory>
public enum ObjectFactory extends Enum<ObjectFactory>
A factory that encapsulates the logic to reflectively produce new objects depending on the desired strategy.- Since:
- 2.5.0
- Author:
- oswaldo.bapvic.jr (Oswaldo Junior)
-
-
Enum Constant Summary
Enum Constants Enum Constant Description CONSTRUCTOR_BASEDConstructor-based object factory.FASTA factory that builds objects by allocating an instance directly on the heap, without any constructor being called.OBJENESISAlternative object factory that uses a variety of approaches to attempt to instantiate the object, depending on the type of object, JVM version, JVM vendor and Security Manager present.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract <T> TnewObject(Class<T> type)Creates a new instance of the specified class.static ObjectFactoryvalueOf(String name)Returns the enum constant of this type with the specified name.static ObjectFactory[]values()Returns an array containing the constants of this enum type, in the order they are declared.
-
-
-
Enum Constant Detail
-
CONSTRUCTOR_BASED
public static final ObjectFactory CONSTRUCTOR_BASED
Constructor-based object factory.It's safer but it requires the existence of a default (public and no-arguments) constructor in the class to allow the instantiation.
-
FAST
public static final ObjectFactory FAST
A factory that builds objects by allocating an instance directly on the heap, without any constructor being called.Final fields are assigned with default values as specified by JavaTM Tutorials > Language Basics > Variables > Primitive Data Types, i.e., zero for numeric types,
falseforboolean, andnullfor String (or any object type).Note: This is the default strategy since 2.5.0.
-
OBJENESIS
public static final ObjectFactory OBJENESIS
Alternative object factory that uses a variety of approaches to attempt to instantiate the object, depending on the type of object, JVM version, JVM vendor and Security Manager present.IMPORTANT: This strategy requires the optional dependency
org.objenesis:objenesisin the class path.
-
-
Method Detail
-
values
public static ObjectFactory[] values()
Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:for (ObjectFactory c : ObjectFactory.values()) System.out.println(c);
- Returns:
- an array containing the constants of this enum type, in the order they are declared
-
valueOf
public static ObjectFactory valueOf(String name)
Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)- Parameters:
name- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException- if this enum type has no constant with the specified nameNullPointerException- if the argument is null
-
newObject
public abstract <T> T newObject(Class<T> type) throws ReflectiveOperationException
Creates a new instance of the specified class.- Type Parameters:
T- the target type- Parameters:
type- the target type- Returns:
- a new instance of the specified class
- Throws:
ReflectiveOperationException- in case of failure during the instantiation
-
-