public final class ClassUtils extends Object
Classes, such as acquiring them from the
application ClassLoaders and instantiating Objects from them.
Class original found in Apache Shiro but modified for enhanced functionality.
| Modifier and Type | Method and Description |
|---|---|
static Class |
forName(String fqcn)
Attempts to load the specified class name from the current thread's
context class loader, then the
current ClassLoader (ClassUtils.class.getClassLoader()), then the system/application
ClassLoader (ClassLoader.getSystemClassLoader(), in that order. |
static InputStream |
getResourceAsStream(String name)
Returns the specified resource by checking the current thread's
context class loader, then the
current ClassLoader (ClassUtils.class.getClassLoader()), then the system/application
ClassLoader (ClassLoader.getSystemClassLoader(), in that order, using
getResourceAsStream(name). |
static boolean |
isAvailable(String fullyQualifiedClassName)
Checks if the fullyQualifiedClassName is available on any classloader.
|
static <T> T |
newInstance(Class clazz)
Creates an instance of the class using the no-arg constructor.
|
static <T> T |
newInstance(Class clazz,
Object... args)
Creates an instance of the class with a constructor matching the arguments.
|
static <T> T |
newInstance(String fqcn)
Creates an instance of the class where it is searched by the algorithm described at the
forName(String) method using the no-arg constructor. |
static <T> T |
newInstance(String fqcn,
Object... args)
Creates an instance of the class where it is searched by the algorithm described at the
forName(String) method with a constructor matching the arguments. |
public static InputStream getResourceAsStream(String name)
context class loader, then the
current ClassLoader (ClassUtils.class.getClassLoader()), then the system/application
ClassLoader (ClassLoader.getSystemClassLoader(), in that order, using
getResourceAsStream(name).name - the name of the resource to acquire from the classloader(s).null if the resource cannot be found from any
of the three mentioned ClassLoaders.public static Class forName(String fqcn)
context class loader, then the
current ClassLoader (ClassUtils.class.getClassLoader()), then the system/application
ClassLoader (ClassLoader.getSystemClassLoader(), in that order. If any of them cannot locate
the specified class, an UnknownClassException is thrown (our RuntimeException equivalent of
the JRE's ClassNotFoundException.fqcn - the fully qualified class name to loadUnknownClassException - if the class cannot be found.public static boolean isAvailable(String fullyQualifiedClassName)
fullyQualifiedClassName - The FQCN to checkpublic static <T> T newInstance(String fqcn)
forName(String) method using the no-arg constructor.T - Convenient generic so cast is not needed.fqcn - The FQCN to instantiate.UnknownClassException - if the class cannot be found.InstantiationException - if the class instantiation went wrong.public static <T> T newInstance(String fqcn, Object... args)
forName(String) method with a constructor matching the arguments.T - Convenient generic so cast is not needed.fqcn - The FQCN to instantiate.args - The arguments for the constructorUnknownClassException - if the class cannot be found.InstantiationException - if the class instantiation went wrong.public static <T> T newInstance(Class clazz)
T - Convenient generic so cast is not needed.clazz - Class which needs to instantiated.InstantiationException - if the class instantiation went wrong.public static <T> T newInstance(Class clazz, Object... args)
T - Convenient generic so cast is not needed.clazz - Class which needs to instantiated.args - The arguments for the constructorInstantiationException - if the class instantiation went wrong.Copyright © 2014–2018. All rights reserved.