DJUTILS - Delft Java Utilities  

Reflection helper classes

The ClassUtil static class can help with a lot of tasks relating to reflection that are not part of the Java reflection package. A number of most used examples are explained below. As resolving constructors and methods is quite expensive, ClassUtil caches all resolved constructors and methods based on their signature.

ClassUtil.getAllFields(class)

The getAllFields(class) method returns a Set of fields of the class, including all fields of the superclasses. The resulting set includes public, protected, package, and private fields. The method uses recursion until it reaches java.lang.Object, which does not have a superclass. Note that private fields of a class can have the same name as private fields of one of their superclasses. Both will be included as theiy are different Field objects.

ClassUtil.getAllMethods(class)

The getAllMethods(class) method returns a Set of methods of the class, including all methods of the superclasses. The resulting set includes public, protected, package, and private methods. The method uses recursion until it reaches java.lang.Object, which does not have a superclass. Note that private methods of a class can have the same name as private methods of one of their superclasses. Both will be included as theiy are different Method objects.

ClassUtil.getAllConstructors(class)

The getAllConstructors(class) method returns a Set of constructors of the class, including all constructors of the superclasses. The resulting set includes public, protected, package, and private constructors. The method uses recursion until it reaches java.lang.Object, which does not have a superclass. Note that you cannot invoke a constructor of a superclass directly, so this method has limited usage, except for understanding the constructor stack of a certain class.

ClassUtil.isVisible(field, callerClass)

Check if the given Field is visible for the caller class. The caller class can be a class in the class hierarchy of the field, or an external class that wants access to the field.

ClassUtil.isVisible(method, callerClass)

Check if the given Method is visible for the caller class. The caller class can be a class in the class hierarchy of the method, or an external class that wants access to the method, e.g., to execute it.

ClassUtil.isVisible(constructor, callerClass)

Check if the given Constructor is visible for the caller class. Before invoking a constructor on a class, it is good to test whether the invoking class has the appropriate access rights to call the constructor.

ClassUtil.resolveField(object, string) or ClassUtil.resolveField(class, string)

Find the given Field within the object or class. Throw a NoSuchFieldException if the field cannot be found.

ClassUtil.resolveMethod(...)

Find the given Method within an object or class. Throw a NoSuchMethodException if the method cannot be found. Methods are searched based on their signature, which is given to resolveMethod as an array of Objects or an array of Classes. Therefore several variants of the resolveMethod method exist:

ClassUtil.resolveConstructor(...)

Find a Constructor that matches the given arguments within a class. Throw a NoSuchMethodException if the constructor cannot be found. Constructors are searched based on their signature, which is given to resolveConstructor as an array of Objects or an array of Classes. Therefore several variants of the resolveConstructor method exist:

Other methods

Several other methods exist, such as isMoreSpecific and matchSignature. See the documentation of the ClassUtil class for these methods.