Package org.apache.flink.util
Class InstantiationUtil
- java.lang.Object
-
- org.apache.flink.util.InstantiationUtil
-
@Internal public final class InstantiationUtil extends Object
Utility class to create instances from class objects and checking failure reasons.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
InstantiationUtil.ClassLoaderObjectInputStream
A custom ObjectInputStream that can load classes using a specific ClassLoader.static class
InstantiationUtil.FailureTolerantObjectInputStream
AnObjectInputStream
that ignores certain serialVersionUID mismatches.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
checkForInstantiation(Class<?> clazz)
Performs a standard check whether the class can be instantiated byClass#newInstance()
.static String
checkForInstantiationError(Class<?> clazz)
static <T extends Serializable>
Tclone(T obj)
Clones the given serializable object using Java serialization.static <T extends Serializable>
Tclone(T obj, ClassLoader classLoader)
Clones the given serializable object using Java serialization, using the given classloader to resolve the cloned classes.static <T extends Serializable>
TcloneUnchecked(T obj)
Unchecked equivalent ofclone(Serializable)
.static <T extends IOReadableWritable>
TcreateCopyWritable(T original)
Clones the given writable using theserialization
.static <T> T
decompressAndDeserializeObject(byte[] bytes, ClassLoader cl)
static <T> T
deserializeFromByteArray(TypeSerializer<T> serializer, byte[] buf)
static <T> T
deserializeFromByteArray(TypeSerializer<T> serializer, T reuse, byte[] buf)
static <T> T
deserializeObject(byte[] bytes, ClassLoader cl)
static <T> T
deserializeObject(InputStream in, ClassLoader cl)
static <T> T
deserializeObject(InputStream in, ClassLoader cl, boolean tolerateKnownVersionMismatch)
static boolean
hasPublicNullaryConstructor(Class<?> clazz)
Checks, whether the given class has a public nullary constructor.static <T> T
instantiate(Class<T> clazz)
Creates a new instance of the given class.static <T> T
instantiate(Class<T> clazz, Class<? super T> castTo)
Creates a new instance of the given class.static <T> T
instantiate(String className, Class<T> targetType, ClassLoader classLoader)
Creates a new instance of the given class name and type using the providedClassLoader
.static boolean
isNonStaticInnerClass(Class<?> clazz)
Checks, whether the class is an inner class that is not statically accessible.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.static boolean
isPublic(Class<?> clazz)
Checks, whether the given class is public.static boolean
isSerializable(Object o)
static <T> T
readObjectFromConfig(Configuration config, String key, ClassLoader cl)
static <T> Class<T>
resolveClassByName(DataInputView in, ClassLoader cl)
Loads a class by name from the given input stream and reflectively instantiates it.static <T> Class<T>
resolveClassByName(DataInputView in, ClassLoader cl, Class<? super T> supertype)
Loads a class by name from the given input stream and reflectively instantiates it.static void
serializeObject(OutputStream out, Object o)
static byte[]
serializeObject(Object o)
static byte[]
serializeObjectAndCompress(Object o)
static <T> byte[]
serializeToByteArray(TypeSerializer<T> serializer, T record)
static void
writeObjectToConfig(Object o, Configuration config, String key)
-
-
-
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 providedClassLoader
.- Type Parameters:
T
- type of the instantiated class- Parameters:
className
- of the class to loadtargetType
- type of the instantiated classclassLoader
- 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 byClass#newInstance()
.- Parameters:
clazz
- The class to check.- Throws:
RuntimeException
- Thrown, if the class cannot be instantiated byClass#newInstance()
.
-
readObjectFromConfig
@Nullable public static <T> T readObjectFromConfig(Configuration config, String key, ClassLoader cl) throws IOException, ClassNotFoundException
- Throws:
IOException
ClassNotFoundException
-
writeObjectToConfig
public static void writeObjectToConfig(Object o, Configuration config, String key) throws IOException
- Throws:
IOException
-
serializeToByteArray
public static <T> byte[] serializeToByteArray(TypeSerializer<T> serializer, T record) throws IOException
- Throws:
IOException
-
deserializeFromByteArray
public static <T> T deserializeFromByteArray(TypeSerializer<T> serializer, byte[] buf) throws IOException
- Throws:
IOException
-
deserializeFromByteArray
public static <T> T deserializeFromByteArray(TypeSerializer<T> serializer, T reuse, byte[] buf) throws IOException
- Throws:
IOException
-
deserializeObject
public static <T> T deserializeObject(byte[] bytes, ClassLoader cl) throws IOException, ClassNotFoundException
- Throws:
IOException
ClassNotFoundException
-
deserializeObject
public static <T> T deserializeObject(InputStream in, ClassLoader cl) throws IOException, ClassNotFoundException
- Throws:
IOException
ClassNotFoundException
-
deserializeObject
public static <T> T deserializeObject(InputStream in, ClassLoader cl, boolean tolerateKnownVersionMismatch) throws IOException, ClassNotFoundException
- Throws:
IOException
ClassNotFoundException
-
decompressAndDeserializeObject
public static <T> T decompressAndDeserializeObject(byte[] bytes, ClassLoader cl) throws IOException, ClassNotFoundException
- Throws:
IOException
ClassNotFoundException
-
serializeObject
public static byte[] serializeObject(Object o) throws IOException
- Throws:
IOException
-
serializeObject
public static void serializeObject(OutputStream out, Object o) throws IOException
- Throws:
IOException
-
serializeObjectAndCompress
public static byte[] serializeObjectAndCompress(Object o) throws IOException
- Throws:
IOException
-
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 cloneclassLoader
- 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 ofclone(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 theserialization
.- 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.
-
-