Enum 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 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, false for boolean, and null for 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:objenesis in 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 name
        NullPointerException - 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