public class ClassUtil
extends java.lang.Object
Class
objects.Constructor and Description |
---|
ClassUtil() |
Modifier and Type | Method and Description |
---|---|
static <C> java.lang.Class<? extends C> |
checkAndLoadClass(java.lang.ClassLoader loader,
java.lang.String name,
boolean publicNoArgConstructor,
java.lang.Class<C> iface,
java.lang.Class<?>... interfaces)
Check if a specified class exists and, optionally, if it has public no-argument
constructor and implements a set of specific interfaces or superclasses.
|
static void |
checkClass(java.lang.Class<?> clazz,
boolean publicNoArgConstructor,
java.lang.Class<?>... interfaces)
Check if a specified class has public no-argument
constructor and implements a set of specific interfaces or superclasses.
|
static <I> java.lang.reflect.Constructor<I> |
findConstructor(java.lang.Class<I> clazz,
java.lang.Object[] parameters)
Find a constructor for the specified class which takes parameters of
the specified types taking superclasses and interfaces into
account.
|
static java.lang.reflect.Method |
findMethod(java.lang.Class<?> clazz,
java.lang.String name,
java.lang.Class<?>... arguments)
Find a method in a
clazz with the given name
that takes the given list of arguments . |
static java.util.Set<java.lang.Class<?>> |
getInterfaces(java.lang.Class<?> clazz)
Get all interfaces a class implements, including those implemented
by superclasses.
|
static boolean |
isAssignable(java.lang.Class<?>[] classes,
java.lang.Object[] values)
Check if one array of class objects is assignment compatible with values in
another array.
|
static <I> I |
newInstance(java.lang.Class<I> clazz)
Utility method for creating a new instance from a class with a default
no-argument constructor.
|
public static <I> I newInstance(java.lang.Class<I> clazz)
public static <I> java.lang.reflect.Constructor<I> findConstructor(java.lang.Class<I> clazz, java.lang.Object[] parameters)
clazz
- The class to find a constructor forparameters
- The parameters that are going to be sent to the
constructor's Constructor.newInstance(Object[])
methodpublic static boolean isAssignable(java.lang.Class<?>[] classes, java.lang.Object[] values)
Class.isAssignableFrom(Class)
method returns
true for each element in the classes
array with respect to the class
of the corresponding value in the values
array.
Null elements in the values
array are ignored.classes
- The class objects representing the parameters we want to assign values tovalues
- The parameter values to assignpublic static java.util.Set<java.lang.Class<?>> getInterfaces(java.lang.Class<?> clazz)
clazz
- The Class
to checkSet
containing the Class
objects
for the interfacespublic static <C> java.lang.Class<? extends C> checkAndLoadClass(java.lang.ClassLoader loader, java.lang.String name, boolean publicNoArgConstructor, java.lang.Class<C> iface, java.lang.Class<?>... interfaces) throws java.lang.ClassNotFoundException, java.lang.NoSuchMethodException, java.lang.ClassCastException
loader
- The classloader used to load the class, or null
to use the default classloadername
- The fully qualified name of class to checkpublicNoArgConstructor
- TRUE to check if a public no-argument constructor existsinterfaces
- An array of class objects representing interfaces or superclasses
that the class to check must implement or extendClass
object for the named classjava.lang.ClassNotFoundException
- If a class with the specified name isn't foundjava.lang.NoSuchMethodException
- If a public no-argument constructor isn't foundjava.lang.ClassCastException
- If the class doesn't implement or extend all of
the interfaces/superclassespublic static void checkClass(java.lang.Class<?> clazz, boolean publicNoArgConstructor, java.lang.Class<?>... interfaces) throws java.lang.NoSuchMethodException, java.lang.ClassCastException
clazz
- The clazz to checkpublicNoArgConstructor
- TRUE to check if a public no-argument constructor existsinterfaces
- An array of class objects representing interfaces or superclasses
that the class to check must implement or extendjava.lang.NoSuchMethodException
- If a public no-argument constructor isn't foundjava.lang.ClassCastException
- If the class doesn't implement or extend all of
the interfaces/superclassespublic static java.lang.reflect.Method findMethod(java.lang.Class<?> clazz, java.lang.String name, java.lang.Class<?>... arguments)
clazz
with the given name
that takes the given list of arguments
. This method is
just calling Class.getMethod(String, Class...)
but returns
null instead of throwing an exception in case the method is not found.clazz
- The class to look for the method inname
- The name of the methodarguments
- The class types of the arguments