Class InstantiationUtil


  • @Internal
    public final class InstantiationUtil
    extends Object
    Utility class to create instances from class objects and checking failure reasons.
    • Method Detail

      • instantiate

        public static <T> T instantiate​(String className,
                                        Class<T> targetType,
                                        ClassLoader classLoader)
                                 throws FlinkException
        Creates a new instance of the given class name and type using the provided ClassLoader.
        Type Parameters:
        T - type of the instantiated class
        Parameters:
        className - of the class to load
        targetType - type of the instantiated class
        classLoader - to use for loading the class
        Returns:
        Instance of the given class name
        Throws:
        FlinkException - if the class could not be found
      • instantiate

        public static <T> T instantiate​(Class<T> clazz,
                                        Class<? super T> castTo)
        Creates a new instance of the given class.
        Type Parameters:
        T - The generic type of the class.
        Parameters:
        clazz - The class to instantiate.
        castTo - Optional parameter, specifying the class that the given class must be a subclass off. This argument is added to prevent class cast exceptions occurring later.
        Returns:
        An instance of the given class.
        Throws:
        RuntimeException - Thrown, if the class could not be instantiated. The exception contains a detailed message about the reason why the instantiation failed.
      • instantiate

        public static <T> T instantiate​(Class<T> clazz)
        Creates a new instance of the given class.
        Type Parameters:
        T - The generic type of the class.
        Parameters:
        clazz - The class to instantiate.
        Returns:
        An instance of the given class.
        Throws:
        RuntimeException - Thrown, if the class could not be instantiated. The exception contains a detailed message about the reason why the instantiation failed.
      • hasPublicNullaryConstructor

        public static boolean hasPublicNullaryConstructor​(Class<?> clazz)
        Checks, whether the given class has a public nullary constructor.
        Parameters:
        clazz - The class to check.
        Returns:
        True, if the class has a public nullary constructor, false if not.
      • isPublic

        public static boolean isPublic​(Class<?> clazz)
        Checks, whether the given class is public.
        Parameters:
        clazz - The class to check.
        Returns:
        True, if the class is public, false if not.
      • isProperClass

        public static boolean isProperClass​(Class<?> clazz)
        Checks, whether the class is a proper class, i.e. not abstract or an interface, and not a primitive type.
        Parameters:
        clazz - The class to check.
        Returns:
        True, if the class is a proper class, false otherwise.
      • isNonStaticInnerClass

        public static boolean isNonStaticInnerClass​(Class<?> clazz)
        Checks, whether the class is an inner class that is not statically accessible. That is especially true for anonymous inner classes.
        Parameters:
        clazz - The class to check.
        Returns:
        True, if the class is a non-statically accessible inner class.
      • checkForInstantiation

        public static void checkForInstantiation​(Class<?> clazz)
        Performs a standard check whether the class can be instantiated by Class#newInstance().
        Parameters:
        clazz - The class to check.
        Throws:
        RuntimeException - Thrown, if the class cannot be instantiated by Class#newInstance().
      • checkForInstantiationError

        public static String checkForInstantiationError​(Class<?> clazz)
      • isSerializable

        public static boolean isSerializable​(Object o)
      • clone

        public static <T extends Serializable> T clone​(T obj)
                                                throws IOException,
                                                       ClassNotFoundException
        Clones the given serializable object using Java serialization.
        Type Parameters:
        T - Type of the object to clone
        Parameters:
        obj - Object to clone
        Returns:
        The cloned object
        Throws:
        IOException - Thrown if the serialization or deserialization process fails.
        ClassNotFoundException - Thrown if any of the classes referenced by the object cannot be resolved during deserialization.
      • clone

        public static <T extends Serializable> T clone​(T obj,
                                                       ClassLoader classLoader)
                                                throws IOException,
                                                       ClassNotFoundException
        Clones the given serializable object using Java serialization, using the given classloader to resolve the cloned classes.
        Type Parameters:
        T - Type of the object to clone
        Parameters:
        obj - Object to clone
        classLoader - The classloader to resolve the classes during deserialization.
        Returns:
        Cloned object
        Throws:
        IOException - Thrown if the serialization or deserialization process fails.
        ClassNotFoundException - Thrown if any of the classes referenced by the object cannot be resolved during deserialization.
      • cloneUnchecked

        public static <T extends Serializable> T cloneUnchecked​(T obj)
        Unchecked equivalent of clone(Serializable).
        Type Parameters:
        T - Type of the object to clone
        Parameters:
        obj - Object to clone
        Returns:
        The cloned object
      • createCopyWritable

        public static <T extends IOReadableWritable> T createCopyWritable​(T original)
                                                                   throws IOException
        Clones the given writable using the serialization.
        Type Parameters:
        T - Type of the object to clone
        Parameters:
        original - Object to clone
        Returns:
        Cloned object
        Throws:
        IOException - Thrown is the serialization fails.
      • resolveClassByName

        public static <T> Class<T> resolveClassByName​(DataInputView in,
                                                      ClassLoader cl)
                                               throws IOException
        Loads a class by name from the given input stream and reflectively instantiates it.

        This method will use DataInput.readUTF() to read the class name, and then attempt to load the class from the given ClassLoader.

        Parameters:
        in - The stream to read the class name from.
        cl - The class loader to resolve the class.
        Throws:
        IOException - Thrown, if the class name could not be read, the class could not be found.
      • resolveClassByName

        public static <T> Class<T> resolveClassByName​(DataInputView in,
                                                      ClassLoader cl,
                                                      Class<? super T> supertype)
                                               throws IOException
        Loads a class by name from the given input stream and reflectively instantiates it.

        This method will use DataInput.readUTF() to read the class name, and then attempt to load the class from the given ClassLoader.

        The resolved class is checked to be equal to or a subtype of the given supertype class.

        Parameters:
        in - The stream to read the class name from.
        cl - The class loader to resolve the class.
        supertype - A class that the resolved class must extend.
        Throws:
        IOException - Thrown, if the class name could not be read, the class could not be found, or the class is not a subtype of the given supertype class.