|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.pfsw.reflect.ReflectUtil
public class ReflectUtil
The sole instance (Singleton) of this class can be accessed by method current().
It provides convenience methods on top of the normal standard Java reflection API. However, it allows access to fields, methods and constructors regardless of their visibility (i.e. private, protected, default, public). Of course ignoring the visibility is only possible in environments that have not Java 2 Security turned on. With a security manager present such access to normally invisible members will cause an exception.
Field Summary | |
---|---|
static java.lang.Class[] |
EMPTY_CLASS_ARRAY
A reusable empty array of type Class[] |
static java.lang.reflect.Constructor[] |
EMPTY_CONSTRUCTOR_ARRAY
A reusable empty array of type Constructor[] |
static java.lang.reflect.Field[] |
EMPTY_FIELD_ARRAY
A reusable empty array of type Field[] |
static java.lang.reflect.Method[] |
EMPTY_METHOD_ARRAY
A reusable empty array of type Method[] |
Constructor Summary | |
---|---|
protected |
ReflectUtil()
Initialize the new instance with default values. |
|
ReflectUtil(java.lang.ClassLoader classLoader)
Initialize the new instance with a different class loader. |
Method Summary | ||
---|---|---|
protected void |
addFieldsToList(java.util.List fieldList,
java.lang.reflect.Field[] fields)
|
|
protected void |
addInheritedFields(java.util.List fields,
java.lang.Class aClass)
|
|
protected void |
addInheritedMethods(java.util.List methods,
java.lang.Class aClass)
|
|
protected void |
addMethodsToList(java.util.List methodList,
java.lang.reflect.Method[] methods)
|
|
boolean |
classExists(java.lang.String className)
Returns true, if the class with the given name can be found in the classpath. |
|
protected void |
collectInterfaces(java.util.Set result,
java.lang.Class aClass)
|
|
protected boolean |
compatibleTypes(java.lang.Class[] paramTypes,
java.lang.Class[] signatureTypes)
Returns true if the types of the first array are assignable to the types of the second array. |
|
boolean |
contains(java.lang.Class[] classes,
java.lang.Class aClass)
Returns true if the given class is found in the provided class array. |
|
java.lang.Object |
createInstanceOf(java.lang.String className,
java.lang.Object caller,
java.lang.Object... params)
Tries to create an instance of the class with the given name. |
|
|
createInstanceOfType(java.lang.Class<T> type,
java.lang.String className,
java.lang.Object caller,
java.lang.Object... params)
Tries to create an instance of the class with the given name. |
|
static ReflectUtil |
current()
Returns an instance this class supports. |
|
java.lang.Class |
findClass(java.lang.String className)
Returns the class object corresponding to the given class name or null, if the class can't be found. |
|
java.lang.Class[] |
findClasses(java.lang.String... classNames)
Returns the classes corresponding to the given (full qualified) class names. |
|
|
findConstructor(java.lang.Class<T> aClass,
java.lang.Class[] paramTypes)
Returns the constructor of the given class for the specified parameter types of null if no such constructor can be found. |
|
java.lang.reflect.Field |
findField(java.lang.Class aClass,
java.lang.String name)
Returns the field with the specified name in the given class. |
|
java.lang.reflect.Field |
findField(java.lang.Class aClass,
java.lang.String name,
int modifiers)
Returns the field with the specified name in the given class and all the specified modifiers set. |
|
java.lang.reflect.Method |
findMethod(java.lang.Class aClass,
java.lang.String methodName,
java.lang.Class... paramTypes)
Returns the method with the specified name if it exists in the given class or any of its superclasses regardless of the method's visibility. |
|
java.lang.reflect.Method |
findMethod(java.lang.Class aClass,
java.lang.String methodName,
java.lang.Class[] paramTypes,
int modifiers)
Returns the method with the specified name if it exists in the given class. |
|
java.util.List<java.lang.Class> |
getAllTypesOf(java.lang.Object object)
Returns all types of the given object or an empty collection if the object is null. |
|
java.lang.String |
getAnnotationValueFrom(java.lang.Class aClass,
java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
Tries to find the annotation of the specified type and return its "value" as String. |
|
java.util.List |
getConstructorsOf(java.lang.Class aClass)
Returns a list of all constructors the given class contains. |
|
java.util.List |
getConstructorsOf(java.lang.Object object)
Returns a list of all constructors the class of the given object contains. |
|
java.lang.reflect.Field |
getField(java.lang.Object object,
java.lang.String name)
Returns the field with the specified name in the given object. |
|
java.util.List |
getFieldsOf(java.lang.Class aClass)
Returns a list of all fields the given class contains. |
|
java.util.List |
getFieldsOf(java.lang.Object obj)
Returns a list of all fields the given objects contains. |
|
java.lang.Class[] |
getInterfacesOf(java.lang.Class aClass)
Returns all interfaces the given class implements. |
|
java.lang.Class[] |
getInterfacesOf(java.lang.Object object)
Returns all interfaces the given object's class implements. |
|
protected java.lang.ClassLoader |
getLoader()
Returns the externally assigned class loader or if not present the class load of this class. |
|
java.lang.reflect.Method |
getMethod(java.lang.Object object,
java.lang.String methodName,
java.lang.Class... paramTypes)
Returns the method with the specified name if it exists in the class of the given object or any of its superclasses regardless the method's visibility. |
|
java.util.List |
getMethodsOf(java.lang.Class aClass)
Returns a list of all methods the given class contains. |
|
java.util.List |
getMethodsOf(java.lang.Object obj)
Returns a list of all methods the given objects contains. |
|
java.lang.Class |
getTypeOf(java.lang.Object object)
Returns the type of the given object. |
|
java.lang.Class[] |
getTypesFromParameters(java.lang.Object... params)
Returns an array of the types (classes) corresponding to the parameter objects given to this methods. |
|
java.lang.Object |
getValueOf(java.lang.Object obj,
java.lang.String name)
Returns the current value of the field with the specified name in the given object. |
|
java.lang.String |
getVisibility(int modifiers)
Returns the visibility defined by the given modifiers as string. |
|
boolean |
hasPublicMethod(java.lang.Class aClass,
java.lang.String methodName,
java.lang.Class... paramTypes)
Returns true if a public method with the specified name exists in the given class or any of its superclasses. |
|
boolean |
hasPublicMethod(java.lang.Object obj,
java.lang.String methodName,
java.lang.Class... paramTypes)
Returns true if a public method with the specified name exists in the class of the given object or any of its superclasses. |
|
boolean |
implementsInterface(java.lang.Class aClass,
java.lang.Class anInterface)
Returns true if the given class implements the specified interfaceType. |
|
boolean |
implementsInterface(java.lang.Object object,
java.lang.Class anInterface)
Returns true if the class of the given object implements the specified interfaceType. |
|
int |
indexOf(java.lang.Class[] classes,
java.lang.Class aClass)
Returns the index of the given class in the provided class array or -1 if the class is not in the array. |
|
boolean |
isDefaultVisibility(int modifiers)
Returns true, if the visibility defined by the given modifiers is the default (package) visibility. |
|
protected boolean |
isNullOrEmpty(java.util.Collection collection)
|
|
protected boolean |
isNullOrEmpty(java.lang.Object[] objects)
|
|
boolean |
isPackageVisible(java.lang.reflect.Field field)
Returns true if the given field is not null and is package visible. |
|
boolean |
isPackageVisible(java.lang.reflect.Method method)
Returns true if the given method is not null and is package visible. |
|
|
newInstance(java.lang.Class<T> aClass)
If the given class has a constructor without parameters it will be used to create a new instance. |
|
|
newInstance(java.lang.Class<T> aClass,
java.lang.Object param)
If the given class has a constructor with one parameter type matching the given parameter it will be used to create a new instance. |
|
|
newInstance(java.lang.Class<T> aClass,
java.lang.Object[] params)
If the given class has a constructor with types corresponding to the given parameters it will be used to create a new instance. |
|
|
newInstance(java.lang.Class<T> aClass,
java.lang.Object param1,
java.lang.Object param2)
If the given class has a constructor with two parameter type matching the given parameters it will be used to create a new instance. |
|
java.lang.Object |
newInstance(java.lang.String className)
Tries to find the class with the given name and to create an instance of it. |
|
java.lang.Object |
newInstance(java.lang.String className,
java.lang.Object param)
Tries to find the class with the given name and to create an instance of it. |
|
java.lang.Object |
newInstance(java.lang.String className,
java.lang.Object[] params)
Tries to find the class with the given name and to create an instance of it. |
|
java.lang.Object |
newInstance(java.lang.String className,
java.lang.Object param1,
java.lang.Object param2)
Tries to find the class with the given name and to create an instance of it. |
|
|
newInstanceOf(java.lang.Class<T> aClass,
java.lang.Object... params)
If the given class has a constructor with types corresponding to the given parameters it will be used to create a new instance. |
|
void |
setValueOf(java.lang.Object obj,
java.lang.String name,
boolean value)
Sets the value of the field with the specified name to the given value. |
|
void |
setValueOf(java.lang.Object obj,
java.lang.String name,
byte value)
Sets the value of the field with the specified name to the given value. |
|
void |
setValueOf(java.lang.Object obj,
java.lang.String name,
char value)
Sets the value of the field with the specified name to the given value. |
|
void |
setValueOf(java.lang.Object obj,
java.lang.String name,
double value)
Sets the value of the field with the specified name to the given value. |
|
void |
setValueOf(java.lang.Object obj,
java.lang.String name,
float value)
Sets the value of the field with the specified name to the given value. |
|
void |
setValueOf(java.lang.Object obj,
java.lang.String name,
int value)
Sets the value of the field with the specified name to the given value. |
|
void |
setValueOf(java.lang.Object obj,
java.lang.String name,
long value)
Sets the value of the field with the specified name to the given value. |
|
void |
setValueOf(java.lang.Object obj,
java.lang.String name,
java.lang.Object value)
Sets the value of the field with the specified name to the given value. |
|
protected void |
setValueOf(java.lang.Object obj,
java.lang.String name,
java.lang.Object value,
boolean isPrimitive)
|
|
void |
setValueOf(java.lang.Object obj,
java.lang.String name,
short value)
Sets the value of the field with the specified name to the given value. |
|
|
toArray(java.util.Collection coll,
java.lang.String methodName,
java.lang.Class<T> elementType)
Returns an array containing all objects that are returned by the specified method name executed against each element in the given collection. |
|
java.lang.String[] |
toStringArray(java.util.Collection coll,
java.lang.String methodName)
Returns a string array containing all objects that are returned by the specified method name executed against each element in the given collection. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final java.lang.Class[] EMPTY_CLASS_ARRAY
public static final java.lang.reflect.Field[] EMPTY_FIELD_ARRAY
public static final java.lang.reflect.Method[] EMPTY_METHOD_ARRAY
public static final java.lang.reflect.Constructor[] EMPTY_CONSTRUCTOR_ARRAY
Constructor Detail |
---|
protected ReflectUtil()
public ReflectUtil(java.lang.ClassLoader classLoader)
classLoader
- The class loader to be used to load classes by name.Method Detail |
---|
public static ReflectUtil current()
public boolean classExists(java.lang.String className)
public java.lang.Class[] findClasses(java.lang.String... classNames) throws java.lang.ClassNotFoundException
classNames
- The full qualified names of the classes to look for.
java.lang.ClassNotFoundException
- If any of the classes cannot be found.public java.lang.Class findClass(java.lang.String className)
className
- The full qualified name of the classpublic java.lang.Object createInstanceOf(java.lang.String className, java.lang.Object caller, java.lang.Object... params)
className
- The name of the class to instantiate.caller
- The object which class will be used to search for the className.params
- 0-n parameters that define the constructor to be used.
ReflectionException
- A runtime exception that wraps the original exception.public <T> T createInstanceOfType(java.lang.Class<T> type, java.lang.String className, java.lang.Object caller, java.lang.Object... params)
type
- The expected type of the instance (could be an interface or also java.lang.Object)className
- The name of the class to instantiate.caller
- The object which class will be used to search for the className.params
- 0-n parameters that define the constructor to be used.
ReflectionException
- A runtime exception that wraps the original exception.public java.lang.Class[] getInterfacesOf(java.lang.Object object)
public java.lang.Class[] getInterfacesOf(java.lang.Class aClass)
public java.lang.reflect.Method findMethod(java.lang.Class aClass, java.lang.String methodName, java.lang.Class[] paramTypes, int modifiers)
aClass
- The class in which to search the methodmethodName
- The name of the searched methodparamTypes
- The types of the method's parameters (null and Class[0] ar the same)modifiers
- The modifiers that must be set at the method too look for
getMethod(Object, String, Class[])
,
findMethod(Class, String, Class[])
public java.lang.reflect.Method findMethod(java.lang.Class aClass, java.lang.String methodName, java.lang.Class... paramTypes)
aClass
- The class in which to search the methodmethodName
- The name of the searched methodparamTypes
- The types of the method's parameters (null and Class[0] ar the same)
getMethod(Object, String, Class[])
public java.lang.reflect.Method getMethod(java.lang.Object object, java.lang.String methodName, java.lang.Class... paramTypes)
object
- The object in which the method should be foundmethodName
- The name of the searched methodparamTypes
- The types of the method's parameters (null and Class[0] are the same)
findMethod(Class, String, Class[])
public java.util.List getMethodsOf(java.lang.Object obj)
obj
- The object of which to get the methods
public java.util.List getMethodsOf(java.lang.Class aClass)
aClass
- The class of which to get the methods
public java.util.List getConstructorsOf(java.lang.Object object)
object
- The object of which to get the constructors
public java.util.List getConstructorsOf(java.lang.Class aClass)
aClass
- The class of which to get the constructors
public java.util.List getFieldsOf(java.lang.Object obj)
obj
- The object to get the fields from
public java.util.List getFieldsOf(java.lang.Class aClass)
aClass
- The class to get the fields from
public java.lang.reflect.Field findField(java.lang.Class aClass, java.lang.String name, int modifiers)
aClass
- The class that might contain the fieldname
- The name of the field to look formodifiers
- The modifiers the field must have set
java.lang.IllegalArgumentException
- If aClass or name is nullpublic java.lang.reflect.Field findField(java.lang.Class aClass, java.lang.String name)
aClass
- The class that might contain the fieldname
- The name of the field to look for
java.lang.IllegalArgumentException
- If aClass or name is nullpublic java.lang.reflect.Field getField(java.lang.Object object, java.lang.String name)
If the given object is an instance of Class, the field will be looked-up in the type represented by the class and not in the class object itself.
object
- The object that (perhaps) contains the field. This can also be an instance of Class.name
- The name of the field to look for
java.lang.IllegalArgumentException
- If object or name is nullpublic java.lang.Object getValueOf(java.lang.Object obj, java.lang.String name) throws java.lang.NoSuchFieldException
obj
- The object that contains the fieldname
- The name of the field to look for
java.lang.NoSuchFieldException
- If the field is unknown in the given object
java.lang.IllegalArgumentException
- If obj or name is nullpublic void setValueOf(java.lang.Object obj, java.lang.String name, java.lang.Object value) throws java.lang.NoSuchFieldException
obj
- The object that contains the fieldname
- The name of the field to setvalue
- The value to assign to the field
java.lang.NoSuchFieldException
- If the field is unknown in the given object
java.lang.IllegalArgumentException
- If obj or name is nullpublic void setValueOf(java.lang.Object obj, java.lang.String name, char value) throws java.lang.NoSuchFieldException
obj
- The object that contains the fieldname
- The name of the field to setvalue
- The value to assign to the field
java.lang.NoSuchFieldException
- If the field is unknown in the given object
java.lang.IllegalArgumentException
- If obj or name is nullpublic void setValueOf(java.lang.Object obj, java.lang.String name, int value) throws java.lang.NoSuchFieldException
obj
- The object that contains the fieldname
- The name of the field to setvalue
- The value to assign to the field
java.lang.NoSuchFieldException
- If the field is unknown in the given object
java.lang.IllegalArgumentException
- If obj or name is nullpublic void setValueOf(java.lang.Object obj, java.lang.String name, byte value) throws java.lang.NoSuchFieldException
obj
- The object that contains the fieldname
- The name of the field to setvalue
- The value to assign to the field
java.lang.NoSuchFieldException
- If the field is unknown in the given object
java.lang.IllegalArgumentException
- If obj or name is nullpublic void setValueOf(java.lang.Object obj, java.lang.String name, boolean value) throws java.lang.NoSuchFieldException
obj
- The object that contains the fieldname
- The name of the field to setvalue
- The value to assign to the field
java.lang.NoSuchFieldException
- If the field is unknown in the given object
java.lang.IllegalArgumentException
- If obj or name is nullpublic void setValueOf(java.lang.Object obj, java.lang.String name, long value) throws java.lang.NoSuchFieldException
obj
- The object that contains the fieldname
- The name of the field to setvalue
- The value to assign to the field
java.lang.NoSuchFieldException
- If the field is unknown in the given object
java.lang.IllegalArgumentException
- If obj or name is nullpublic void setValueOf(java.lang.Object obj, java.lang.String name, short value) throws java.lang.NoSuchFieldException
obj
- The object that contains the fieldname
- The name of the field to setvalue
- The value to assign to the field
java.lang.NoSuchFieldException
- If the field is unknown in the given object
java.lang.IllegalArgumentException
- If obj or name is nullpublic void setValueOf(java.lang.Object obj, java.lang.String name, double value) throws java.lang.NoSuchFieldException
obj
- The object that contains the fieldname
- The name of the field to setvalue
- The value to assign to the field
java.lang.NoSuchFieldException
- If the field is unknown in the given object
java.lang.IllegalArgumentException
- If obj or name is nullpublic void setValueOf(java.lang.Object obj, java.lang.String name, float value) throws java.lang.NoSuchFieldException
obj
- The object that contains the fieldname
- The name of the field to setvalue
- The value to assign to the field
java.lang.NoSuchFieldException
- If the field is unknown in the given object
java.lang.IllegalArgumentException
- If obj or name is nullpublic boolean hasPublicMethod(java.lang.Class aClass, java.lang.String methodName, java.lang.Class... paramTypes)
aClass
- The class in which to look for the methodmethodName
- The name of the method to look forparamTypes
- The types of the method's parameters (null and Class[0] ar the same)
public boolean hasPublicMethod(java.lang.Object obj, java.lang.String methodName, java.lang.Class... paramTypes)
obj
- The object in which the method should be foundmethodName
- The name of the method to look forparamTypes
- The types of the method's parameters (null and Class[0] ar the same)
public boolean isPackageVisible(java.lang.reflect.Field field)
public boolean isPackageVisible(java.lang.reflect.Method method)
public boolean isDefaultVisibility(int modifiers)
public java.lang.String getVisibility(int modifiers)
public <T> T newInstance(java.lang.Class<T> aClass)
aClass
- The class of which a new instance must be created (must not be null)
ReflectionException
- A runtime exception that wraps the original exception.public <T> T newInstance(java.lang.Class<T> aClass, java.lang.Object param)
aClass
- The class of which a new instance must be created (must not be null)param
- The initialization parameter for the constructor (must not be null)
ReflectionException
- A runtime exception that wraps the original exception.public <T> T newInstance(java.lang.Class<T> aClass, java.lang.Object param1, java.lang.Object param2)
aClass
- The class of which a new instance must be created (must not be null)param1
- The first initialization parameter for the constructor (must not be null)param2
- The second initialization parameter for the constructor (must not be null)
ReflectionException
- A runtime exception that wraps the original exception.public <T> T newInstance(java.lang.Class<T> aClass, java.lang.Object[] params)
newInstanceOf(Class, Object...)
.
It is just kept for compatibility.
aClass
- The class of which a new instance must be created (must not be null)params
- The initialization parameters for the constructor (may be null)
ReflectionException
- A runtime exception that wraps the original exception.public <T> T newInstanceOf(java.lang.Class<T> aClass, java.lang.Object... params)
newInstance(Class, Object[])
.
It is just uses the varargs declaration that is available since Java 1.5.
aClass
- The class of which a new instance must be created (must not be null)params
- The initialization parameters for the constructor (may be null)
ReflectionException
- A runtime exception that wraps the original exception.public java.lang.Object newInstance(java.lang.String className)
className
- The name of the class of which a new instance must be created (must not be null)
ReflectionException
- A runtime exception that wraps the original exception.public java.lang.Object newInstance(java.lang.String className, java.lang.Object param)
className
- The name of the class of which a new instance must be created (must not be null)param
- The initialization parameter for the constructor (must not be null)
ReflectionException
- A runtime exception that wraps the original exception.public java.lang.Object newInstance(java.lang.String className, java.lang.Object param1, java.lang.Object param2)
className
- The name of the class of which a new instance must be created (must not be null)param1
- The first initialization parameter for the constructor (must not be null)param2
- The second initialization parameter for the constructor (must not be null)
ReflectionException
- A runtime exception that wraps the original exception.public java.lang.Object newInstance(java.lang.String className, java.lang.Object[] params)
className
- The name of the class of which a new instance must be created (must not be null)params
- The initialization parameters for the constructor (may be null)
ReflectionException
- A runtime exception that wraps the original exception.public <T> java.lang.reflect.Constructor<T> findConstructor(java.lang.Class<T> aClass, java.lang.Class[] paramTypes)
newInstance(Class)
public java.lang.Class[] getTypesFromParameters(java.lang.Object... params)
params
- The parameters to derive the types from (may be null)
public java.lang.Class getTypeOf(java.lang.Object object)
object
- The object of which to determine the typepublic java.util.List<java.lang.Class> getAllTypesOf(java.lang.Object object)
object
- The object to derive all types ofpublic boolean contains(java.lang.Class[] classes, java.lang.Class aClass)
public int indexOf(java.lang.Class[] classes, java.lang.Class aClass)
public boolean implementsInterface(java.lang.Object object, java.lang.Class anInterface)
public boolean implementsInterface(java.lang.Class aClass, java.lang.Class anInterface)
public <T> T[] toArray(java.util.Collection coll, java.lang.String methodName, java.lang.Class<T> elementType)
coll
- The collection with elements of which the objects have to be extractedmethodName
- The name of the method to be executed on the collections's elements (must not be null)
This method must have no argument and must return an object of the specified elementType.elementType
- The type of the elements returned by the method and of the elements in the return array
public java.lang.String[] toStringArray(java.util.Collection coll, java.lang.String methodName)
coll
- The collection with elements of which the strings have to be extractedmethodName
- The name of the method to be executed on the collections's elements (must not be null).
This method must have no argument and must return a string.
public java.lang.String getAnnotationValueFrom(java.lang.Class aClass, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
aClass
- The potentionally annotated classannotationType
- The annotation to look for
protected void addMethodsToList(java.util.List methodList, java.lang.reflect.Method[] methods)
protected void addInheritedMethods(java.util.List methods, java.lang.Class aClass) throws java.lang.SecurityException
java.lang.SecurityException
protected void addFieldsToList(java.util.List fieldList, java.lang.reflect.Field[] fields)
protected void addInheritedFields(java.util.List fields, java.lang.Class aClass) throws java.lang.SecurityException
java.lang.SecurityException
protected void setValueOf(java.lang.Object obj, java.lang.String name, java.lang.Object value, boolean isPrimitive) throws java.lang.NoSuchFieldException
java.lang.NoSuchFieldException
protected boolean compatibleTypes(java.lang.Class[] paramTypes, java.lang.Class[] signatureTypes)
protected void collectInterfaces(java.util.Set result, java.lang.Class aClass)
protected boolean isNullOrEmpty(java.lang.Object[] objects)
protected boolean isNullOrEmpty(java.util.Collection collection)
protected java.lang.ClassLoader getLoader()
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |