public class ReflectionUtils extends Object
ReflectionUtils class provides reflection utilities.| Modifier and Type | Method and Description |
|---|---|
static java.util.stream.Stream<Annotation> |
annotations(Element annotatedElement)
Gets annotations that are present on the element.
|
static java.util.stream.Stream<Annotation> |
annotationsAnnotatedWith(Element annotatedElement,
Class<? extends Annotation> annotationClass)
Gets annotations which are themselves annotated with a given type of
annotation that are present on the element.
|
static <T extends Annotation> |
annotationsByType(Element annotatedElement,
Class<T> annotationClass)
Gets annotations of a specific type that are associated with the
element.
|
static Class<?> |
classFor(DeclaredType type)
Gets a class object corresponding to a given declared type.
|
static Class<?> |
classFor(TypeMirror tm)
Gets a class object corresponding to a given type mirror.
|
static Collection<Class<?>> |
findClasses(String pkg)
Finds all classes defined in a given package using the current classloader.
|
static Collection<Class<?>> |
findClasses(String pkg,
ClassLoader cl)
Finds all classes defined in a given package.
|
static <A extends Annotation> |
findFirstAnnotation(Class<?> clazz,
Class<A> annotationClass)
Finds the first annotation from a given class' and interface's hierarchy of
the specified annotation.
|
static <T> Class<? super T> |
findFirstClassAnnotatedWith(Class<T> clazz,
Class<? extends Annotation> annotationClass)
Finds the first class from a given class' hierarchy that is annotated
with the specified annotation.
|
static <T> Class<? super T> |
findFirstClassNotAnnotatedWith(Class<T> clazz,
Class<? extends Annotation> annotationClass)
Finds the first class from a given class' hierarchy that is not annotated
with the specified annotation.
|
static <T> Class<? super T> |
findLastClassAnnotatedWith(Class<T> clazz,
Class<? extends Annotation> annotationClass)
Finds the last class from a given class' hierarchy that is annotated
with the specified annotation.
|
static Collection<URL> |
findResources(String pkg)
Finds all resources defined in a given package using the current classloader.
|
static Collection<URL> |
findResources(String pkg,
ClassLoader cl)
Finds all resources defined in a given package.
|
static <A extends Annotation> |
getAllAnnotationsForFieldsAnnotatedWith(Class<?> clazz,
Class<A> annotation,
boolean up)
Gets all fields of a given type (up the super class hierarchy) annotated
with the specified annotation.
|
static <T extends Member,A extends Annotation> |
getAllAnnotationsForMembersAnnotatedWith(Class<T> type,
Class<?> clazz,
Class<A> annotation,
boolean up)
Gets all members of a given type (up the super class hierarchy) annotated
with the specified annotation.
|
static <A extends Annotation> |
getAllAnnotationsForMethodsAnnotatedWith(Class<?> clazz,
Class<A> annotation,
boolean up)
Gets all methods of a given type (up the super class hierarchy) annotated
with the specified annotation.
|
static List<Field> |
getAllDeclaredFields(Class<?> clazz,
boolean up)
Gets all declared fields (up the super class hierarchy).
|
static <T extends Member> |
getAllDeclaredMembers(Class<T> type,
Class<?> clazz,
boolean up)
Gets all declared members of a given type (up the super class hierarchy).
|
static List<Method> |
getAllDeclaredMethods(Class<?> clazz,
boolean up)
Gets all declared methods (up the super class hierarchy).
|
static List<Field> |
getAllFieldsAnnotatedWith(Class<?> clazz,
Class<? extends Annotation> annotation,
boolean up)
Gets all fields (up the super class hierarchy) annotated with the specified
annotation.
|
static <T extends Member> |
getAllMembersAnnotatedWith(Class<T> type,
Class<?> clazz,
Class<? extends Annotation> annotation,
boolean up)
Gets all members of a given type (up the super class hierarchy) annotated
with the specified annotation.
|
static List<Method> |
getAllMethodsAnnotatedWith(Class<?> clazz,
Class<? extends Annotation> annotation,
boolean up)
Gets all methods (up the super class hierarchy) annotated with the specified
annotation.
|
static <T extends Annotation> |
getAnnotation(Element annotatedElement,
Class<T> annotationClass)
Gets the specified element's annotation for the specified type if such an
annotation is present, else
null where classes
referenced from the annotation are properly resolved and will not generate
a compile-time mirror exception. |
static Annotation[] |
getAnnotations(Element annotatedElement)
Gets annotations that are present on the element.
|
static Annotation[] |
getAnnotationsAnnotatedWith(Element annotatedElement,
Class<? extends Annotation> annotationClass)
Gets annotations which are themselves annotated with a given type of
annotation that are present on the element.
|
static <K,T extends Annotation> |
getAnnotationsByType(Class<K> keyClass,
Class<T> annotationClass,
AnnotatedElement annotatedElement)
Gets annotations that are associated with the specified element.
|
static <T extends Annotation> |
getAnnotationsByType(Element annotatedElement,
Class<T> annotationClass)
Gets annotations of a specific that are associated with the element.
|
static <T> List<Class<?>> |
getClassHierarchy(Class<?> clazz)
Gets the hierarchy of a given class.
|
static <T extends Member> |
getDeclaredMembers(Class<T> type,
Class<?> clazz)
Gets the declared members of the given type from the specified class.
|
static Class<?> |
getRawClass(Type t)
Gets the raw class from the provided type.
|
static <T> Constructor<T> |
getSerializationConstructorFromAnnotation(Class<T> clazz,
Class<? extends Annotation> annotationClass)
Gets the serialization constructor for the given class based on the given
annotation.
|
static <T> Constructor<T> |
getSerializationConstructorFromBaseClass(Class<T> clazz,
Class<?> bclass)
Gets the serialization constructor for the given class based on the given
base class.
|
static boolean |
isAssignableFrom(Class<?>[] classes,
Class<?> clazz)
Checks if the specified class is assignment compatible (i.e.
|
public static <A extends Annotation> A findFirstAnnotation(Class<?> clazz, Class<A> annotationClass)
A - the type of annotation to search forclazz - the class from which to searchannotationClass - the annotation to search forclazz's hierarchy or
null if none is foundNullPointerException - if clazz or
annotationClass is nullpublic static <T> Class<? super T> findFirstClassAnnotatedWith(Class<T> clazz, Class<? extends Annotation> annotationClass)
T - the type of the class to start searching fromclazz - the class from which to searchannotationClass - the annotation to search forclazz's hierarchy annotated with
the specified annotation or null if none is foundNullPointerException - if clazz or
annotationClass is nullpublic static <T> Class<? super T> findFirstClassNotAnnotatedWith(Class<T> clazz, Class<? extends Annotation> annotationClass)
T - the type of the class to start searching fromclazz - the class from which to searchannotationClass - the annotation to search forclazz's hierarchy not annotated
with the specified annotation or null if none foundNullPointerException - if clazz or
annotationClass is nullpublic static <T> Class<? super T> findLastClassAnnotatedWith(Class<T> clazz, Class<? extends Annotation> annotationClass)
T - the type of the class to start searching fromclazz - the class from which to searchannotationClass - the annotation to search forclazz's hierarchy annotated with
the specified annotation or null if none foundNullPointerException - if clazz or
annotationClass is nullpublic static <T> Constructor<T> getSerializationConstructorFromBaseClass(Class<T> clazz, Class<?> bclass)
Note: The returned constructor results in having a proper object of
the given class instantiated without calling any of the constructors from
its class or super classes. The fields of all classes in the hierarchy will
not be initialized; thus set using default Java initialization (i.e.
null, 0, 0L, false, ...). This is how Java creates new
serialized objects before calling the readObject() method of
each of the subclasses.
T - the class for which to get the serialization constructorclazz - the class for which to get the serialization constructorbclass - the base class to start the initialization from in the
returned serialization constructornull serialization constructor for the given
classNullPointerException - if clazz or bclass
is nullIllegalArgumentException - if bclass is not a base class
for clazzSecurityException - if the request is deniedpublic static <T> Constructor<T> getSerializationConstructorFromAnnotation(Class<T> clazz, Class<? extends Annotation> annotationClass)
Note: The returned constructor results in having a proper object of
the given class instantiated without calling any of the constructors from
its class or super classes. The fields of all classes in the hierarchy will
not be initialized; thus set using default Java initialization (i.e.
null, 0, 0L, false, ...). This is how Java creates new
serialized objects before calling the readObject() method of
each of the subclasses.
T - the class for which to get the serialization constructorclazz - the class for which to get the serialization constructorannotationClass - the annotation to search withnull serialization constructor for the given
classNullPointerException - if clazz or
annotationClass is nullIllegalStateException - if unable to find a default constructor for
the first base class not annotated with annotationClassSecurityException - if the request is deniedpublic static <T extends Member> T[] getDeclaredMembers(Class<T> type, Class<?> clazz)
T - the type of membertype - the type of members to retrieveclazz - the class from which to retrieve the membersnull members of the given type from the
specified classNullPointerException - if type or
clazz is nullIllegalArgumentException - if type is not
Field, Method, or Constructorpublic static <T extends Member> List<T> getAllMembersAnnotatedWith(Class<T> type, Class<?> clazz, Class<? extends Annotation> annotation, boolean up)
T - the type of members to retrieve (either Field,
Method, or Constructor)type - the type of members to retrieveclazz - the class from which to find all annotated membersannotation - the annotation for which to find all membersup - true to look up the class hierarchy;
false to only look at the specified class levelNullPointerException - if type,
clazz or annotation is nullIllegalArgumentException - if type is not
Field, Method, or Constructorpublic static List<Field> getAllFieldsAnnotatedWith(Class<?> clazz, Class<? extends Annotation> annotation, boolean up)
clazz - the class from which to find all annotated fieldsannotation - the annotation for which to find all fieldsup - true to look up the class hierarchy;
false to only look at the specified class levelNullPointerException - if clazz or
annotation is nullpublic static List<Method> getAllMethodsAnnotatedWith(Class<?> clazz, Class<? extends Annotation> annotation, boolean up)
clazz - the class from which to find all annotated methodsannotation - the annotation for which to find all methodsup - true to look up the class hierarchy;
false to only look at the specified class levelNullPointerException - if clazz or
annotation is nullpublic static <T extends Member,A extends Annotation> Map<T,A[]> getAllAnnotationsForMembersAnnotatedWith(Class<T> type, Class<?> clazz, Class<A> annotation, boolean up)
T - the type of members to retrieve (either Field,
Method, or Constructor)A - the type of annotation to search fortype - the type of members to retrieveclazz - the class from which to find all annotated membersannotation - the annotation for which to find all membersup - true to look up the class hierarchy;
false to only look at the specified class levelnull map in the provided order for all annotated
members with their found annotationsNullPointerException - if type,
clazz or annotation is nullIllegalArgumentException - if type is not
Field, Method, or Constructorpublic static <A extends Annotation> Map<Field,A[]> getAllAnnotationsForFieldsAnnotatedWith(Class<?> clazz, Class<A> annotation, boolean up)
A - the type of annotation to search forclazz - the class from which to find all annotated fieldsannotation - the annotation for which to find all fieldsup - true to look up the class hierarchy;
false to only look at the specified class levelnull map in the provided order for all annotated
fields with their found annotationsNullPointerException - if clazz or
annotation is nullpublic static <A extends Annotation> Map<Method,A[]> getAllAnnotationsForMethodsAnnotatedWith(Class<?> clazz, Class<A> annotation, boolean up)
A - the type of annotation to search forclazz - the class from which to find all annotated methodsannotation - the annotation for which to find all methodsup - true to look up the class hierarchy;
false to only look at the specified class levelnull map in the provided order for all annotated
methods with their found annotationsNullPointerException - if clazz or
annotation is nullpublic static <K,T extends Annotation> Map<K,T> getAnnotationsByType(Class<K> keyClass, Class<T> annotationClass, AnnotatedElement annotatedElement)
AnnotatedElement.getAnnotationsByType(Class) is that this method
works on annotation classes that are annotated with the Keyable
annotation in order to determine the key of each annotation. For this to
work, the annotated class must defined a key element as defined in the
Keyable annotation of the same return type as specified in
keyClass. If its argument is a repeatable annotation type
(JLS 9.6), this method, attempts to find one or more annotations of that
type by "looking through" a container annotation. The caller of this method
is free to modify the returned map; it will have no effect on the maps
returned to other callers.
This method will look for both the specified annotation and if the
annotation is annotated with Repeatable, it will also look for this
containing annotation which must have a value()
element with an array type of the specified annotation. The resulting
array will always start with the annotation itself if found followed by
all of those provided by the containing annotation.
K - the type of the keys to findT - the type of the annotation to query for and return if presentkeyClass - the class of the keys to find and returnannotationClass - the type of annotations to retrieveannotatedElement - the element from which to retrieve the annotationsnull ordered map of annotations associated with the
given element properly keyed as defined in each annotation (may be
empty if none found)NullPointerException - if annotatedElement,
annotationClass or keyClass is
nullIllegalArgumentException - if annotationClass is not
annotated with Keyable or if the containing annotation
doesn't define a value() element returning an array
of type annotationClass or if annotationClass
doesn't define an element named as specified in its Keyable
annotation that returns a value of the same class as keyClass
or again if a duplicated keyed annotation is foundpublic static <T extends Member> List<T> getAllDeclaredMembers(Class<T> type, Class<?> clazz, boolean up)
T - the type of members to retrieve (either Field,
Method, or Constructor)type - the type of members to retrieveclazz - the class from which to find all declared membersup - true to look up the class hierarchy;
false to only look at the specified class levelNullPointerException - if type or
clazz is nullIllegalArgumentException - if type is not
Field, Method, or Constructorpublic static List<Field> getAllDeclaredFields(Class<?> clazz, boolean up)
clazz - the class from which to find all declared fieldsup - true to look up the class hierarchy;
false to only look at the specified class levelNullPointerException - if clazz is nullpublic static List<Method> getAllDeclaredMethods(Class<?> clazz, boolean up)
clazz - the class from which to find all declared methodsup - true to look up the class hierarchy;
false to only look at the specified class levelNullPointerException - if clazz is nullpublic static Class<?> getRawClass(Type t)
t - the type for which to get the raw classpublic static <T> List<Class<?>> getClassHierarchy(Class<?> clazz)
T - the type of the class to get the hierarchy forclazz - the class to get the hierarchy forpublic static Collection<Class<?>> findClasses(String pkg)
pkg - the package from which to find all defined classesnull collection of all classes defined in the
given packageNullPointerException - if pkg is nullpublic static Collection<Class<?>> findClasses(String pkg, ClassLoader cl)
pkg - the package from which to find all defined classescl - the classloader to find the classes withnull collection of all classes defined in the
given packageNullPointerException - if pkg or cl is
nullpublic static Collection<URL> findResources(String pkg)
pkg - the package from which to find all defined resourcesnull collection of all resources defined in the
given packageNullPointerException - if pkg is nullpublic static Collection<URL> findResources(String pkg, ClassLoader cl)
pkg - the package from which to find all defined resourcescl - the classloader to find the resources withnull collection of all resources defined in the
given packageNullPointerException - if pkg or cl is
nullpublic static final boolean isAssignableFrom(Class<?>[] classes, Class<?> clazz)
classes - the set of classes to check if clazz is
assignment compatible withclazz - the class to be checkingtrue if clazz is a subclass of at least
one of the specified classespublic static Class<?> classFor(DeclaredType type) throws ClassNotFoundException
type - the declared type for which to get a class objectnull class objectClassNotFoundException - if the class is not foundpublic static Class<?> classFor(TypeMirror tm) throws ClassNotFoundException
tm - the type mirror for which to get a class objectnull class objectClassNotFoundException - if the class is not foundpublic static <T extends Annotation> java.util.stream.Stream<T> annotationsByType(Element annotatedElement, Class<T> annotationClass)
If there are no annotations associated with this element, the return value is an empty stream.
T - the type of the annotation to query for and return if presentannotatedElement - the element to retrieve the annotations fromannotationClass - the type of annotations to retrieveNullPointerException - if the given element or annotation class are
nullpublic static java.util.stream.Stream<Annotation> annotationsAnnotatedWith(Element annotatedElement, Class<? extends Annotation> annotationClass)
If there are no annotations present on the element, the return value is an empty stream.
For example, one could find all annotations annotated with the @Tag annotation, which in the following examples would return @ThisOne and @ThisOneToo annotations but not @NotThisOne:
@Tag
public @interface ThisOne {
String value();
}
@Tag
public @interface ThisOneToo {
String value();
}
public @interface NotThisOne {
String value();
}
annotatedElement - the element to retrieve the annotations fromannotationClass - the type of annotations to retrieve annotations
annotated withNullPointerException - if the given element or annotation class are
nullpublic static java.util.stream.Stream<Annotation> annotations(Element annotatedElement)
If there are no annotations present on the element, the return value is an empty stream.
annotatedElement - the element to retrieve the annotations fromNullPointerException - if the given element is nullpublic static Annotation[] getAnnotationsAnnotatedWith(Element annotatedElement, Class<? extends Annotation> annotationClass)
If there are no annotations present on the element, the return value is an array of length 0.
For example, one could find all annotations annotated with the @Tag annotation, which in the following examples would return @ThisOne and @ThisOneToo annotations but not @NotThisOne:
@Tag
public @interface ThisOne {
String value();
}
@Tag
public @interface ThisOneToo {
String value();
}
public @interface NotThisOne {
String value();
}
annotatedElement - the element to retrieve the annotations fromannotationClass - the type of annotations to retrieve annotations
annotated withNullPointerException - if the given element or annotation class are
nullpublic static Annotation[] getAnnotations(Element annotatedElement)
If there are no annotations present on the element, the return value is an array of length 0.
The caller of this method is free to modify the returned array; it will have no effect on the arrays returned to other callers.
annotatedElement - the element to retrieve the annotations fromNullPointerException - if the given element is nullpublic static <T extends Annotation> T[] getAnnotationsByType(Element annotatedElement, Class<T> annotationClass)
If there are no annotations associated with this element, the return value is an array of length 0.
The difference between this method and
getAnnotation(Element, Class) is that this method
detects if its argument is a repeatable annotation type (JLS 9.6),
and if so, attempts to find one or more annotations of that type by
"looking through" a container annotation.
The caller of this method is free to modify the returned array; it will have no effect on the arrays returned to other callers.
T - the type of the annotation to query for and return if presentannotatedElement - the element to retrieve the annotations fromannotationClass - the type of annotations to retrieveNullPointerException - if the given element or annotation class are
nullpublic static <T extends Annotation> T getAnnotation(Element annotatedElement, Class<T> annotationClass)
null where classes
referenced from the annotation are properly resolved and will not generate
a compile-time mirror exception.
This method is useful when dealing with annotations in a processing environment where Javac does not load classes in the normal manner. In fact it doesn’t at all for classes that are in the source – it’s all contained within a model.
T - the type of the annotation to query for and return if presentannotatedElement - the element to retrieve the annotation fromannotationClass - the type of annotation to retrievenullNullPointerException - if the given element or annotation class are
nullCopyright (C) 2015-2015 The Helenus Driver Project Authors.