Package org.djutils.reflection
Class ClassUtil
java.lang.Object
org.djutils.reflection.ClassUtil
public final class ClassUtil extends Object
ClassUtil is a utility class providing assistance for Java Classes.
Copyright (c) 2002-2020 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See for project information https://djutils.org. The DJUTILS project is distributed under a three-clause BSD-style license, which can be found at https://djutils.org/docs/license.html.
- Author:
- Peter Jacobs, Niels Lang, Alexander Verbraeck
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ClassUtil.ClassFileDescriptor
ClassFileDescriptor contains some information about a class file, either stand-alone on the classpath, or within a Jar file.
Copyright (c) 2019-2020 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. -
Method Summary
Modifier and Type Method Description static ClassUtil.ClassFileDescriptor
classFileDescriptor(Class<?> clazz)
Retrieve a file pointer of a class, e.g.static ClassUtil.ClassFileDescriptor
classFileDescriptor(Object object)
Retrieve a file pointer of a class, e.g.static ClassUtil.ClassFileDescriptor
classFileDescriptor(URL clazzUrl)
Retrieve a file pointer of a class, e.g.static Set<Annotation>
getAllAnnotations(Class<?> clazz)
gets all the annotations of a class (public, protected, package, and private).static Set<Annotation>
getAllAnnotations(Class<?> clazz, Set<Annotation> result)
gets all the annotations of a class (public, protected, package, and private) and adds the result to the return value.static <T> Constructor<T>[]
getAllConstructors(Class<T> clazz)
Returns all the constructors of a class.static Set<Field>
getAllFields(Class<?> clazz)
gets all the fields of a class (public, protected, package, and private).static Set<Field>
getAllFields(Class<?> clazz, Set<Field> result)
gets all the fields of a class (public, protected, package, and private) and adds the result to the return value.static List<Method>
getAllMethods(Class<?> clazz)
gets all the methods of a class (public, protected, package, and private).static List<Method>
getAllMethods(Class<?> clazz, String name)
gets all the methods of a class (public, protected, package, and private) with a certain name.static List<Method>
getAllMethods(Class<?> clazz, String name, List<Method> result)
gets all the methods of a class (public, protected, package, and private) with a certain name and adds the result to the return value.static List<Method>
getAllMethods(Class<?> clazz, List<Method> result)
gets all the methods of a class (public, protected, package, and private) and adds the result to the return value.static Class<?>[]
getClass(Object[] array)
Converts an array of objects to their corresponding classes.static boolean
isMoreSpecific(Class<?>[] a, Class<?>[] b)
Determines & returns whether constructor 'a' is more specific than constructor 'b', as defined in the Java Language Specification par 15.12.static boolean
isMoreSpecific(Constructor<?> a, Constructor<?> b)
Determines & returns whether constructor 'a' is more specific than constructor 'b', as defined in the Java Language Specification par 15.12.static boolean
isMoreSpecific(Method a, Method b)
Determines & returns whether constructor 'a' is more specific than constructor 'b', as defined in the Java Language Specification par 15.12.static boolean
isVisible(int modifiers, Class<?> declaringClass, Class<?> caller)
Returns whether a declaringClass is accessible according to the modifiers.static boolean
isVisible(Constructor<?> constructor, Class<?> caller)
Returns whether a constructor is visible for a caller.static boolean
isVisible(Field field, Class<?> caller)
Returns whether a field is visible for a caller.static boolean
isVisible(Method method, Class<?> caller)
Returns whether a method is visible for a caller.static boolean
matchSignature(Constructor<?> constructor, Class<?>[] argTypes)
Filters an array methods for signatures that are compatible with a given signature.static <T> Constructor<T>[]
matchSignature(Constructor<T>[] constructors, Class<?>[] argTypes)
Filters an array methods for signatures that are compatible with a given signature.static boolean
matchSignature(Method method, String name, Class<?>[] argTypes)
Filters an array methods for signatures that are compatible with a given signature.static List<Method>
matchSignature(List<Method> methods, String name, Class<?>[] argTypes)
Filters an array methods for signatures that are compatible with a given signature.static Annotation
resolveAnnotation(Class<?> clazz, Class<? extends Annotation> annotationClass)
resolves the annotation for a class, taking into account inner classes.static <T> Constructor<T>
resolveConstructor(Class<T> clazz, Class<?>[] parameterTypes)
returns the interface method.static <T> Constructor<T>
resolveConstructor(Class<T> clazz, Class<?> callerClass, Class<?>[] parameterTypes)
Returns the constructor of a class with a particular signature if and only if the caller class can invoke that constructor.static <T> Constructor<T>
resolveConstructor(Class<T> clazz, Object[] arguments)
returns the constructor.static Field
resolveField(Class<?> clazz, Class<?> callerClass, String name)
returns the field.static Field
resolveField(Class<?> clazz, String fieldName)
resolves the field for a class, taking into account inner classes.static Field
resolveField(Object object, String fieldName)
resolves the field for a given object instance.static Method
resolveMethod(Class<?> clazz, Class<?> callerClass, String name, Class<?>[] parameterTypes)
returns the interface method.static Method
resolveMethod(Class<?> clazz, String name, Class<?>[] parameterTypes)
returns the interface method.static Method
resolveMethod(Object object, String name, Class<?>[] parameterTypes)
resolves a method the method.static Method
resolveMethod(Object object, String name, Object[] arguments)
returns the method.
-
Method Details
-
getAllConstructors
Returns all the constructors of a class. Public, package, protected and private constructors are returned. This method returns an array of length 0 if the class object represents an interface, a primitive type, an array class, or void. Note that the constructors of the superclass are not returned as these can never be invoked.- Type Parameters:
T
- the class of the constructors- Parameters:
clazz
- Class<?>; the class to resolve the constructors for.- Returns:
- an array with all constructors
-
resolveConstructor
public static <T> Constructor<T> resolveConstructor(Class<T> clazz, Class<?> callerClass, Class<?>[] parameterTypes) throws NoSuchMethodException, IllegalAccessExceptionReturns the constructor of a class with a particular signature if and only if the caller class can invoke that constructor. So a private constructor will not be invoked, unless the caller class is the class that defines the constructor. For a protected class the superclass and classes in the same package can invoke the constructor, but other classes should not be able to invoke the constructor.- Type Parameters:
T
- the class of the constructor- Parameters:
clazz
- Class<?>; the class for which the constructor needs to becallerClass
- Class<?>; the calling class for which the test is carried out whether the constructor is visibleparameterTypes
- Class<?>[]; the parameter types for the constructor's signature- Returns:
- the retrieved constructor
- Throws:
NoSuchMethodException
- if the constructor with the given signature does not existIllegalAccessException
- if the constructor exists but is not callable from the callerClass
-
resolveConstructor
public static <T> Constructor<T> resolveConstructor(Class<T> clazz, Class<?>[] parameterTypes) throws NoSuchMethodExceptionreturns the interface method.- Type Parameters:
T
- the class of the constructor- Parameters:
clazz
- Class<?>; the class to start withparameterTypes
- Class<?>[]; the parameterTypes- Returns:
- Constructor
- Throws:
NoSuchMethodException
- if the method cannot be resolved
-
resolveConstructor
public static <T> Constructor<T> resolveConstructor(Class<T> clazz, Object[] arguments) throws NoSuchMethodExceptionreturns the constructor.- Type Parameters:
T
- the class of the constructor- Parameters:
clazz
- Class<?>; the clazz to start witharguments
- Object[]; the arguments- Returns:
- Constructor
- Throws:
NoSuchMethodException
- on lookup failure
-
matchSignature
Filters an array methods for signatures that are compatible with a given signature.- Parameters:
constructor
- Constructor<?>; which are constructors to be filtered.argTypes
- Class<?>[]; are the constructor's argument types- Returns:
- boolean if methodParameters assignable from argTypes
-
matchSignature
public static <T> Constructor<T>[] matchSignature(Constructor<T>[] constructors, Class<?>[] argTypes)Filters an array methods for signatures that are compatible with a given signature.- Type Parameters:
T
- the class of the constructor- Parameters:
constructors
- Constructor<?>[]; which are constructors to be filtered.argTypes
- Class<?>[]; are the constructor's argument types- Returns:
- Constructor<?>[] An unordered Constructor-array consisting of the elements of 'constructors' that match with the given signature. An array with 0 elements is returned when no matching Method objects are found.
-
getAllFields
gets all the fields of a class (public, protected, package, and private) and adds the result to the return value.- Parameters:
clazz
- Class<?>; the classresult
- Set<Field>; the resulting set- Returns:
- the set of fields including all fields of the field clazz
-
getAllFields
gets all the fields of a class (public, protected, package, and private).- Parameters:
clazz
- Class<?>; the class- Returns:
- all fields of the class
-
resolveField
resolves the field for a class, taking into account inner classes.- Parameters:
clazz
- the class to resolve the field for, including inner classesfieldName
- name of the field- Returns:
- Field the field
- Throws:
NoSuchFieldException
- on no such field
-
resolveField
public static Field resolveField(Class<?> clazz, Class<?> callerClass, String name) throws NoSuchFieldExceptionreturns the field.- Parameters:
clazz
- Class<?>; the class to start withcallerClass
- Class<?>; the calling classname
- String; the fieldName- Returns:
- Constructor
- Throws:
NoSuchFieldException
- if the method cannot be resolved
-
resolveField
resolves the field for a given object instance.- Parameters:
object
- Object; the object to resolve the field forfieldName
- String; name of the field to resolve- Returns:
- the field (if found)
- Throws:
NoSuchFieldException
- if the field cannot be resolved
-
getAllMethods
gets all the methods of a class (public, protected, package, and private) and adds the result to the return value.- Parameters:
clazz
- Class<?>; the classresult
- Set<Field>; the resulting set- Returns:
- the set of methods including all methods of the field clazz
-
getAllMethods
gets all the methods of a class (public, protected, package, and private).- Parameters:
clazz
- Class<?>; the class- Returns:
- all methods of the class
-
getAllMethods
gets all the methods of a class (public, protected, package, and private) with a certain name and adds the result to the return value.- Parameters:
clazz
- Class<?>; the classname
- String; the name of the method to look upresult
- Set<Field>; the resulting set- Returns:
- the set of methods including all methods with the given name of the field clazz
-
getAllMethods
gets all the methods of a class (public, protected, package, and private) with a certain name.- Parameters:
clazz
- Class<?>; the classname
- String; the name of the method to look up- Returns:
- all methods of the class with a certain name
-
resolveMethod
public static Method resolveMethod(Class<?> clazz, Class<?> callerClass, String name, Class<?>[] parameterTypes) throws NoSuchMethodExceptionreturns the interface method.- Parameters:
clazz
- Class<?>; the class to start withcallerClass
- Class<?>; the caller classname
- String; the name of the methodparameterTypes
- Class<?>[]; the parameterTypes- Returns:
- Method
- Throws:
NoSuchMethodException
- on lookup failure
-
resolveMethod
public static Method resolveMethod(Class<?> clazz, String name, Class<?>[] parameterTypes) throws NoSuchMethodExceptionreturns the interface method.- Parameters:
clazz
- Class<?>; the class to start withname
- String; the name of the methodparameterTypes
- Class<?>[]; the parameterTypes- Returns:
- Method
- Throws:
NoSuchMethodException
- on lookup failure
-
resolveMethod
public static Method resolveMethod(Object object, String name, Class<?>[] parameterTypes) throws NoSuchMethodExceptionresolves a method the method.- Parameters:
object
- Object; the object to start withname
- String; the name of the methodparameterTypes
- Class<?>[]; the parameterTypes- Returns:
- Method
- Throws:
NoSuchMethodException
- on lookup failure
-
resolveMethod
public static Method resolveMethod(Object object, String name, Object[] arguments) throws NoSuchMethodExceptionreturns the method.- Parameters:
object
- Object; the object to start withname
- String; the name of the methodarguments
- Object[]; the arguments- Returns:
- Method
- Throws:
NoSuchMethodException
- on lookup failure
-
getAllAnnotations
gets all the annotations of a class (public, protected, package, and private) and adds the result to the return value.- Parameters:
clazz
- Class<?>; the classresult
- Set<Annotation>; the resulting set- Returns:
- the set of annotations including all annotations of the annotation clazz
-
getAllAnnotations
gets all the annotations of a class (public, protected, package, and private).- Parameters:
clazz
- Class<?>; the class- Returns:
- all annotations of the class
-
resolveAnnotation
public static Annotation resolveAnnotation(Class<?> clazz, Class<? extends Annotation> annotationClass) throws NoSuchElementExceptionresolves the annotation for a class, taking into account inner classes.- Parameters:
clazz
- the class to resolve the annotation for, including inner classesannotationClass
- class of the annotation- Returns:
- Annotation the annotation
- Throws:
NoSuchElementException
- on no such annotation
-
isVisible
Returns whether a declaringClass is accessible according to the modifiers.- Parameters:
modifiers
- int; the modifiersdeclaringClass
- Class<?>; the declaringClasscaller
- Class<?>; the caller- Returns:
- boolean isVisible
-
isMoreSpecific
Determines & returns whether constructor 'a' is more specific than constructor 'b', as defined in the Java Language Specification par 15.12.- Parameters:
a
- Class<?>[]; reflects the first constructorb
- Class<?>[]; reflects the second constructor- Returns:
- true if 'a' is more specific than b, false otherwise. 'false' is also returned when constructors are incompatible, e.g. have different names or a different number of parameters.
-
isMoreSpecific
Determines & returns whether constructor 'a' is more specific than constructor 'b', as defined in the Java Language Specification par 15.12.- Parameters:
a
- Constructor<?>; the first constructorb
- Constructor<?>; the second constructor- Returns:
- true if 'a' is more specific than b, false otherwise. 'false' is also returned when constructors are incompatible, e.g. have different names or a different number of parameters.
-
isMoreSpecific
Determines & returns whether constructor 'a' is more specific than constructor 'b', as defined in the Java Language Specification par 15.12.- Parameters:
a
- Method; reflects the first methodb
- Method; reflects the second method- Returns:
- true if 'a' is more specific than b, false otherwise. 'false' is also returned when constructors are incompatible, e.g. have different names or a different number of parameters.
-
isVisible
Returns whether a field is visible for a caller.- Parameters:
field
- Field; The fieldcaller
- Class<?>; The class of the caller for whom invocation visibility is checked.- Returns:
- boolean yes or no
-
isVisible
Returns whether a constructor is visible for a caller.- Parameters:
constructor
- Constructor<?>; The constructorcaller
- Class<?>; The class of the caller for whom invocation visibility is checked.- Returns:
- boolean yes or no
-
isVisible
Returns whether a method is visible for a caller.- Parameters:
method
- Method; The methodcaller
- Class<?>; The class of the caller for whom invocation visibility is checked.- Returns:
- boolean yes or no
-
matchSignature
Filters an array methods for signatures that are compatible with a given signature.- Parameters:
methods
- Method[]; which are methods to be filtered.name
- String; reflects the method's name, part of the signatureargTypes
- Class<?>[]; are the method's argument types- Returns:
- Method[] An unordered Method-array consisting of the elements of 'methods' that match with the given signature. An array with 0 elements is returned when no matching Method objects are found.
-
matchSignature
Filters an array methods for signatures that are compatible with a given signature.- Parameters:
method
- Method; The method to be filtered.name
- String; reflects the method's name, part of the signatureargTypes
- Class<?>[]; are the method's argument types- Returns:
- boolean if methodParameters assignable from argTypes
-
getClass
Converts an array of objects to their corresponding classes. Note that primitive types are always autoboxed to the corresponding object types. So an int in the array will have an Integer.class at the position in the resulting Class array.- Parameters:
array
- Object[]; the array to invoke- Returns:
- Class<?>[] the result;
-
classFileDescriptor
Retrieve a file pointer of a class, e.g. to request the last compilation date.- Parameters:
object
- Object; the object for which the class information should be retrieved- Returns:
- a ClassFileDescriptor with some information of the .class file
-
classFileDescriptor
Retrieve a file pointer of a class, e.g. to request the last compilation date.- Parameters:
clazz
- Class<?>; the class for which a file descriptor should be retrieved- Returns:
- a ClassFileDescriptor with some information of the .class file
-
classFileDescriptor
Retrieve a file pointer of a class, e.g. to request the last compilation date.- Parameters:
clazzUrl
- URL; the URL to a class for which a file descriptor should be retrieved- Returns:
- a ClassFileDescriptor with some information of the .class file
-