public final class ClassUtil extends Object
Copyright (c) 2002-2019 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.
Modifier and Type | Class and 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-2019 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. |
Modifier and Type | Method and Description |
---|---|
static void |
changeAnnotationValue(Annotation annotation,
String key,
Object newValue)
Change the value of a property of an annotation through reflection.
|
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 Method[] |
getAllMethods(Class<?> clazz,
String name,
Method[] result)
gets all the methods of a class and adds the result to result.
|
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 ???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 ???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 ???15.12.
|
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(int modifiers,
Class<?> declaringClass,
Class<?> caller)
Returns whether a declaringClass is accessible according to the modifiers.
|
static boolean |
isVisible(Method method,
Class<?> caller)
Returns whether a method is visible for a caller.
|
static <T> Constructor<T>[] |
matchSignature(Constructor<T>[] constructors,
Class<?>[] argTypes)
Filters an array methods for signatures that are compatible with a given signature.
|
static <T> boolean |
matchSignature(Constructor<T> constructor,
Class<?>[] argTypes)
Filters an array methods for signatures that are compatible with a given signature.
|
static Method[] |
matchSignature(Method[] methods,
String name,
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 Annotation |
resolveAnnotation(Class<?> clazz,
Class<? extends Annotation> annotationClass)
resolves the annotation for a class, taking into account inner classes.
|
static Annotation |
resolveAnnotation(Object object,
String annotationName)
resolves the annotation for a given object instance.
|
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.
|
public static <T> Constructor<T>[] getAllConstructors(Class<T> clazz)
clazz
- Class<?>; the class to resolve the constructors for.public static <T> Constructor<T> resolveConstructor(Class<T> clazz, Class<?> callerClass, Class<?>[] parameterTypes) throws NoSuchMethodException, IllegalAccessException
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 signatureNoSuchMethodException
- if the constructor with the given signature does not existIllegalAccessException
- if the constructor exists but is not callable from the callerClasspublic static <T> Constructor<T> resolveConstructor(Class<T> clazz, Class<?>[] parameterTypes) throws NoSuchMethodException
clazz
- Class<?>; the class to start withparameterTypes
- Class<?>[]; the parameterTypesNoSuchMethodException
- if the method cannot be resolvedpublic static <T> Constructor<T> resolveConstructor(Class<T> clazz, Object[] arguments) throws NoSuchMethodException
clazz
- Class<?>; the clazz to start witharguments
- Object[]; the argumentsNoSuchMethodException
- on lookup failurepublic static <T> boolean matchSignature(Constructor<T> constructor, Class<?>[] argTypes)
constructor
- Constructor<?>; which are constructors to be filtered.argTypes
- Class<?>[]; are the constructor's argument typespublic static <T> Constructor<T>[] matchSignature(Constructor<T>[] constructors, Class<?>[] argTypes)
constructors
- Constructor<?>[]; which are constructors to be filtered.argTypes
- Class<?>[]; are the constructor's argument typespublic static Set<Field> getAllFields(Class<?> clazz, Set<Field> result)
clazz
- Class<?>; the classresult
- Set<Field>; the resulting setpublic static Set<Field> getAllFields(Class<?> clazz)
clazz
- Class<?>; the classpublic static Field resolveField(Class<?> clazz, String fieldName) throws NoSuchFieldException
clazz
- the class to resolve the field for, including inner classesfieldName
- name of the fieldNoSuchFieldException
- on no such fieldpublic static Field resolveField(Class<?> clazz, Class<?> callerClass, String name) throws NoSuchFieldException
clazz
- Class<?>; the class to start withcallerClass
- Class<?>; the calling classname
- String; the fieldNameNoSuchFieldException
- if the method cannot be resolvedpublic static Field resolveField(Object object, String fieldName) throws NoSuchFieldException
object
- Object; the object to resolve the field forfieldName
- String; name of the field to resolveNoSuchFieldException
- if the field cannot be resolvedpublic static Method[] getAllMethods(Class<?> clazz, String name, Method[] result)
clazz
- Class<?>; the classname
- String; the name of the methodresult
- Method[]; the resulting setpublic static Method resolveMethod(Class<?> clazz, Class<?> callerClass, String name, Class<?>[] parameterTypes) throws NoSuchMethodException
clazz
- Class<?>; the class to start withcallerClass
- Class<?>; the caller classname
- String; the name of the methodparameterTypes
- Class<?>[]; the parameterTypesNoSuchMethodException
- on lookup failurepublic static Method resolveMethod(Class<?> clazz, String name, Class<?>[] parameterTypes) throws NoSuchMethodException
clazz
- Class<?>; the class to start withname
- String; the name of the methodparameterTypes
- Class<?>[]; the parameterTypesNoSuchMethodException
- on lookup failurepublic static Method resolveMethod(Object object, String name, Class<?>[] parameterTypes) throws NoSuchMethodException
object
- Object; the object to start withname
- String; the name of the methodparameterTypes
- Class<?>[]; the parameterTypesNoSuchMethodException
- on lookup failurepublic static Method resolveMethod(Object object, String name, Object[] arguments) throws NoSuchMethodException
object
- Object; the object to start withname
- String; the name of the methodarguments
- Object[]; the argumentsNoSuchMethodException
- on lookup failurepublic static Set<Annotation> getAllAnnotations(Class<?> clazz, Set<Annotation> result)
clazz
- Class<?>; the classresult
- Set<Annotation>; the resulting setpublic static Set<Annotation> getAllAnnotations(Class<?> clazz)
clazz
- Class<?>; the classpublic static Annotation resolveAnnotation(Class<?> clazz, Class<? extends Annotation> annotationClass) throws NoSuchElementException
clazz
- the class to resolve the annotation for, including inner classesannotationClass
- class of the annotationNoSuchElementException
- on no such annotationpublic static Annotation resolveAnnotation(Object object, String annotationName) throws NoSuchElementException
object
- Object; the object to resolve the annotation forannotationName
- String; name of the annotation to resolveNoSuchElementException
- if the annotation cannot be resolvedpublic static boolean isVisible(int modifiers, Class<?> declaringClass, Class<?> caller)
modifiers
- int; the modifiersdeclaringClass
- Class<?>; the declaringClasscaller
- Class<?>; the callerpublic static boolean isMoreSpecific(Class<?>[] a, Class<?>[] b)
a
- Class<?>[]; reflects the first constructorb
- Class<?>[]; reflects the second constructorpublic static boolean isMoreSpecific(Constructor<?> a, Constructor<?> b)
a
- Constructor<?>; reflects the first constructorb
- Constructor<?>; reflects the second constructorpublic static boolean isMoreSpecific(Method a, Method b)
a
- Method; reflects the first methodb
- Method; reflects the second methodpublic static boolean isVisible(Field field, Class<?> caller)
field
- Field; The fieldcaller
- Class<?>; The class of the caller for whom invocation visibility is checked.public static boolean isVisible(Constructor<?> constructor, Class<?> caller)
constructor
- Constructor<?>; The constructorcaller
- Class<?>; The class of the caller for whom invocation visibility is checked.public static boolean isVisible(Method method, Class<?> caller)
method
- Method; The methodcaller
- Class<?>; The class of the caller for whom invocation visibility is checked.public static Method[] matchSignature(Method[] methods, String name, Class<?>[] argTypes)
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 typespublic static boolean matchSignature(Method method, String name, Class<?>[] argTypes)
method
- Method; The method to be filtered.name
- String; reflects the method's name, part of the signatureargTypes
- Class<?>[]; are the method's argument typespublic static Class<?>[] getClass(Object[] array)
array
- Object[]; the array to invokepublic static void changeAnnotationValue(Annotation annotation, String key, Object newValue)
annotation
- the annotation to changekey
- the field to look for in the annotationnewValue
- the value to set the annotation field toIllegalStateException
- when the annotation has no member values or access to the member values is deniedIllegalArgumentException
- when the value that is changed is of a different type than the type of the newValuepublic static ClassUtil.ClassFileDescriptor classFileDescriptor(Object object)
object
- Object; the object for which the class information should be retrievedpublic static ClassUtil.ClassFileDescriptor classFileDescriptor(Class<?> clazz)
clazz
- Class<?>; the class for which a file descriptor should be retrievedpublic static ClassUtil.ClassFileDescriptor classFileDescriptor(URL clazzUrl)
clazzUrl
- URL; the URL to a class for which a file descriptor should be retrievedCopyright © 2018–2019 Delft University of Technology. All rights reserved.