|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.omnaest.utils.reflection.ReflectionUtils
public class ReflectionUtils
Helper for Java Reflection.
| Nested Class Summary | |
|---|---|
static class |
ReflectionUtils.MethodParameterMetaInformation
Represents the meta information about a single Method parameter |
| Constructor Summary | |
|---|---|
ReflectionUtils()
|
|
| Method Summary | ||
|---|---|---|
static
|
annotatedPackageSet(Class<? extends A>... packageAnnotationTypes)
Returns a Set of all Package instances known to the ClassLoader which are annotated with the given
package Annotation type |
|
static
|
annotatedPackageSet(Set<Package> scannedPackageSet,
Class<? extends A>... packageAnnotationTypes)
Returns a Set of all Package instances out of the given Set of Packages which are annotated
with at least one of the given package Annotation types |
|
static
|
annotatedPackageToAnnotationSetMap(Class<? extends A>... packageAnnotationTypes)
Returns a Map of all Packages annotated with at least one of the given package level Annotations
including the Annotation instances related to each Package. |
|
static
|
annotatedPackageToAnnotationSetMap(Set<Package> scannedPackageSet,
Class<? extends A>... packageAnnotationTypes)
Returns a Map of all Packages out of the given Packages Set annotated with at least one of
the given package level Annotations including the Annotation instances related to each Package. |
|
static
|
annotation(Class<?> type,
Class<? extends A> annotationType)
Returns the Annotation instance for the given type and annotation type. |
|
static
|
annotation(Package package_,
Class<? extends A> annotationType)
Returns the Annotation instance for any Annotation declared on a Package with the given type of
Annotation |
|
static
|
annotationIncludingInterfaces(Class<?> type,
Class<? extends A> annotationType)
Returns the Annotation instance for the given type and annotation type. |
|
static List<Annotation> |
annotationList(Class<?> type)
Returns all Class.getAnnotations() as List |
|
static List<Annotation> |
annotationList(Field field)
Returns all AccessibleObject.getAnnotations() as List |
|
static Map<Class<Annotation>,Annotation> |
annotationTypeToAnnotationMap(AnnotatedElement annotatedElement)
Returns all AnnotatedElement.getAnnotations() as Map which has the Annotation type as key and its
related instance as value. |
|
static boolean |
areAssignableFrom(Class<?>[] assignableTypes,
Class<?>[] sourceTypes)
Returns true if all types of the assignable types are Class.isAssignableFrom(Class) to their source type counterpart. |
|
static Set<Class<?>> |
assignableTypeSet(boolean inherited,
boolean onlyReturnInterfaces,
boolean intersection,
Class<?>... types)
Returns as Set of assignable types which are implemented by the given type. |
|
static Set<Class<?>> |
assignableTypeSet(Class<?> type,
boolean inherited)
Returns as Set of assignable types which are implemented by the given type. |
|
static Set<Class<?>> |
assignableTypeSet(Class<?> type,
boolean inherited,
boolean onlyReturnInterfaces)
Returns as Set of assignable types which are implemented by the given type. |
|
static
|
classForName(String className)
Invokes Class.forName(String) for the given type name but does not throw any Exception. |
|
static
|
constructorFor(Class<? extends B> type,
Class<?>... parameterTypes)
Returns the Constructor for the given Class type and parameter types |
|
static
|
constructorFor(Class<? extends B> type,
Object... arguments)
Returns the Constructor for the given Class type and arguments |
|
static
|
declaredAnnotation(Class<?> type,
Class<? extends A> annotationType)
Returns the Annotation if the given Class type declares the given Annotation class |
|
static List<Annotation> |
declaredAnnotationList(Class<?> type)
Returns all Class.getDeclaredAnnotations() as List |
|
static Set<Annotation> |
declaredAnnotationSet(AnnotatedElement annotatedElement)
Returns all AnnotatedElement.getDeclaredAnnotations() as Set |
|
static int |
declaredFieldIndexPosition(Class<?> clazz,
Field field)
Determines the index position of a declared Field within a given Class. |
|
static int |
declaredFieldIndexPosition(Class<?> clazz,
String fieldname)
Determines the index position of a declared Field within a given Class. |
|
static List<Field> |
declaredFieldList(Class<?> type)
Returns the Class.getDeclaredFields() for a given Class. |
|
static int |
declaredMethodIndexPosition(Class<?> clazz,
Method method)
Determines the index position of a declared Method within a given Class. |
|
static List<Method> |
declaredMethodList(Class<?> type)
Returns the Class.getDeclaredMethods() for a given Class. |
|
static List<ReflectionUtils.MethodParameterMetaInformation> |
declaredMethodParameterMetaInformationList(Method method)
Returns an ordered List of ReflectionUtils.MethodParameterMetaInformation instances for each parameter the given
Method has. |
|
static Map<Method,Set<Annotation>> |
declaredMethodToAnnotationSetMap(Class<?> type)
Returns a Map of all declared Methods of the given Class type and a List of declared
Annotations related to the Methods |
|
static Map<Method,Class<?>> |
declaredMethodToReturnTypeMap(Class<?> type)
Returns a Map with the declared Methods of the given type and the return type of the Methods |
|
static List<Field> |
fieldList(Class<?> type)
Returns the Class.getFields() for a given Class. |
|
static boolean |
hasAnnotation(Class<?> type,
Class<? extends Annotation> annotationType)
Returns true if the given Class type declares or inherits the given Annotation class from any supertype, but
not from interfaces |
|
static boolean |
hasAnnotation(Field field,
Class<? extends Annotation> annotationType)
Returns true if the given Field declares the given Annotation class |
|
static boolean |
hasAnnotationIncludingInterfaces(Class<?> type,
Class<? extends Annotation> annotationType)
Returns true if the given Class type declares or inherits the given Annotation class from any supertype, but
not from interfaces |
|
static boolean |
hasAnnotationOnAnyMethod(Class<?> type,
Class<? extends Annotation>... annotationTypes)
Returns true if the given Class type has any annotation of the given types on any of its methods |
|
static boolean |
hasAnnotationOnAnyMethod(Class<?> type,
Class<? extends Annotation> annotationType)
Returns true if the given Class type has any annotation of the given type on any of its methods |
|
static
|
hasConstructorFor(Class<? extends B> type,
Class<?>... parameterTypes)
Returns true if the given Class type has a Constructor for the given parameter types |
|
static
|
hasConstructorFor(Class<? extends B> type,
Object... arguments)
Returns true if the given Class type has a Constructor for the given arguments |
|
static boolean |
hasDeclaredAnnotation(Class<?> type,
Class<? extends Annotation> annotationType)
Returns true if the given Class type declares the given Annotation class |
|
static boolean |
hasDeclaredAnnotation(Method method,
Class<? extends Annotation> annotationType)
Returns true if the given Method declares the given Annotation class |
|
static
|
hasDefaultConstructorFor(Class<? extends B> type)
Returns true if the given Class type has a default Constructor |
|
static Set<Class<?>> |
interfaceSet(Class<?> type,
boolean inherited)
Returns as Set of interfaces which are implemented by the given type. |
|
static
|
invokeMethod(Object object,
String methodName,
Object... arguments)
Invokes the given method name on the given object. |
|
static boolean |
isAssignableFrom(Class<?> targetType,
Class<?> type)
Returns true if the given target Class is Class.isAssignableFrom(Class) for the given Class type. |
|
static List<Method> |
methodList(Class<?> type)
Returns the Class.getMethods() for a given Class. |
|
static List<Method> |
methodListIncludingSuperMethods(Class<?> type)
Similar to methodList(Class) but including the methods of all inherited interfaces and superclasses |
|
static
|
methodToAnnotationMap(Class<?> type,
Class<A> annotationType)
Returns a Map with all Methods of a given Class and the annotation instance for the given method. |
|
static Map<Method,Set<Annotation>> |
methodToAnnotationSetMap(Class<?> type)
Returns a Map of all Methods of the given Class type and a List of declared
Annotations related to the Methods |
|
protected static Map<Method,Set<Annotation>> |
methodToAnnotationSetMap(List<Method> methodList)
Returns a Map of all the given Methods and a List of declared Annotations related to the
Methods |
|
static Map<Method,Class<?>> |
methodToReturnTypeMap(Class<?> type)
Returns a Map with Methods of the given type and the return type of the Methods |
|
protected static Map<Method,Class<?>> |
methodToReturnTypeMap(List<Method> methodList)
Returns a Map with all the given Methods and their return type |
|
static
|
newInstanceByValueOf(Class<? extends B> type,
Object... arguments)
Creates a new instance of a given Class using a possibly present valueOf method which has the same parameter
signature as the provided arguments. |
|
static
|
newInstanceOf(Class<? extends B> type,
Object... arguments)
Creates a new instance of a given Class using a constructor which has the same parameter signature as the provided
arguments. |
|
static int |
numberOfDeclaredFields(Class<?> clazz)
Returns the number of declared Fields of a Class. |
|
static int |
numberOfDeclaredMethods(Class<?> clazz)
Returns the number of declared Methods of a Class. |
|
static
|
resolveConstructorFor(Class<C> type,
Class<?>... parameterTypes)
Resolves a matching constructor for the given type and the given parameter types |
|
static Set<Class<?>> |
supertypeSet(Class<?> type)
Returns a Set of all supertypes the given type extends. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public ReflectionUtils()
| Method Detail |
|---|
public static int declaredMethodIndexPosition(Class<?> clazz,
Method method)
Method within a given Class.
clazz - method -
public static int declaredFieldIndexPosition(Class<?> clazz,
Field field)
Field within a given Class.
clazz - field -
public static int declaredFieldIndexPosition(Class<?> clazz,
String fieldname)
Field within a given Class.
clazz - fieldname -
Field could not be determined at all.public static int numberOfDeclaredFields(Class<?> clazz)
Fields of a Class.
clazz -
Fields or 0 if clazz == nullpublic static int numberOfDeclaredMethods(Class<?> clazz)
Methods of a Class.
clazz -
Methods or 0 if clazz == null
public static <B> boolean hasConstructorFor(Class<? extends B> type,
Object... arguments)
Class type has a Constructor for the given arguments
type - arguments -
public static <B> boolean hasConstructorFor(Class<? extends B> type,
Class<?>... parameterTypes)
Class type has a Constructor for the given parameter types
type - parameterTypes -
public static <B> boolean hasDefaultConstructorFor(Class<? extends B> type)
Class type has a default Constructor
type -
public static <B> Constructor<B> constructorFor(Class<? extends B> type,
Object... arguments)
Constructor for the given Class type and arguments
type - arguments -
public static <B> Constructor<B> constructorFor(Class<? extends B> type,
Class<?>... parameterTypes)
Constructor for the given Class type and parameter types
type - parameterTypes -
public static <C> Constructor<C> resolveConstructorFor(Class<C> type,
Class<?>... parameterTypes)
type - parameterTypes -
public static boolean areAssignableFrom(Class<?>[] assignableTypes,
Class<?>[] sourceTypes)
Class.isAssignableFrom(Class) to their source type counterpart.
Both arrays have to have the same size. Every element of one of the arrays will map to its counterpart with the same index
position.
assignableTypes - sourceTypes -
public static <B> B newInstanceOf(Class<? extends B> type,
Object... arguments)
Class using a constructor which has the same parameter signature as the provided
arguments. Exception instead null is returned.
type - Classarguments - newInstanceByValueOf(Class, Object...)
public static <B> B newInstanceByValueOf(Class<? extends B> type,
Object... arguments)
Class using a possibly present valueOf method which has the same parameter
signature as the provided arguments. Exception instead it will return null, if the invocation fails for any reason.
type - Classarguments - newInstanceOf(java.lang.Class extends B>, java.lang.Object...)public static Map<Method,Class<?>> declaredMethodToReturnTypeMap(Class<?> type)
Map with the declared Methods of the given type and the return type of the Methods
type -
public static Map<Method,Class<?>> methodToReturnTypeMap(Class<?> type)
Map with Methods of the given type and the return type of the Methods
type -
protected static Map<Method,Class<?>> methodToReturnTypeMap(List<Method> methodList)
Map with all the given Methods and their return type
methodList -
public static <A extends Annotation> Map<Method,A> methodToAnnotationMap(Class<?> type,
Class<A> annotationType)
Map with all Methods of a given Class and the annotation instance for the given method.
Methods which will have no matching Annotations will be excluded and will not show as keys.
type - annotationType -
public static List<Annotation> declaredAnnotationList(Class<?> type)
Class.getDeclaredAnnotations() as List
type -
public static List<Annotation> annotationList(Class<?> type)
Class.getAnnotations() as List
type -
public static List<Annotation> annotationList(Field field)
AccessibleObject.getAnnotations() as List
field -
public static <A extends Annotation> A annotationIncludingInterfaces(Class<?> type,
Class<? extends A> annotationType)
Annotation instance for the given type and annotation type. If no matching annotation type is found null
is returned. The matching includes any super interfaces as well.
type - annotationType -
hasAnnotationIncludingInterfaces(Class, Class),
annotation(Class, Class),
declaredAnnotation(Class, Class)
public static boolean hasAnnotation(Class<?> type,
Class<? extends Annotation> annotationType)
Class type declares or inherits the given Annotation class from any supertype, but
not from interfaces
type - annotationType -
hasDeclaredAnnotation(Class, Class),
hasAnnotationIncludingInterfaces(Class, Class),
annotation(Class, Class)
public static <A extends Annotation> A annotation(Package package_,
Class<? extends A> annotationType)
Annotation instance for any Annotation declared on a Package with the given type of
Annotation
package_ - annotationType -
public static <A extends Annotation> A annotation(Class<?> type,
Class<? extends A> annotationType)
Annotation instance for the given type and annotation type. If no matching annotation type is found null
is returned.
type - annotationType -
hasAnnotation(Class, Class),
annotationIncludingInterfaces(Class, Class),
declaredAnnotation(Class, Class)
public static boolean hasAnnotation(Field field,
Class<? extends Annotation> annotationType)
Field declares the given Annotation class
field - annotationType -
public static boolean hasAnnotationIncludingInterfaces(Class<?> type,
Class<? extends Annotation> annotationType)
Class type declares or inherits the given Annotation class from any supertype, but
not from interfaces
type - annotationType -
annotationIncludingInterfaces(Class, Class)
public static boolean hasDeclaredAnnotation(Class<?> type,
Class<? extends Annotation> annotationType)
Class type declares the given Annotation class
type - annotationType -
public static <A extends Annotation> A declaredAnnotation(Class<?> type,
Class<? extends A> annotationType)
Annotation if the given Class type declares the given Annotation class
type - annotationType -
annotation(Class, Class),
annotationIncludingInterfaces(Class, Class)
public static boolean hasDeclaredAnnotation(Method method,
Class<? extends Annotation> annotationType)
Method declares the given Annotation class
method - annotationType -
public static boolean hasAnnotationOnAnyMethod(Class<?> type,
Class<? extends Annotation> annotationType)
Class type has any annotation of the given type on any of its methods
type - annotationType -
public static boolean hasAnnotationOnAnyMethod(Class<?> type,
Class<? extends Annotation>... annotationTypes)
Class type has any annotation of the given types on any of its methods
type - annotationTypes -
public static Set<Annotation> declaredAnnotationSet(AnnotatedElement annotatedElement)
AnnotatedElement.getDeclaredAnnotations() as Set
annotatedElement - Field
public static Map<Class<Annotation>,Annotation> annotationTypeToAnnotationMap(AnnotatedElement annotatedElement)
AnnotatedElement.getAnnotations() as Map which has the Annotation type as key and its
related instance as value.
annotatedElement - Field
Map instance, even if the annotatedElement is nullpublic static Map<Method,Set<Annotation>> methodToAnnotationSetMap(Class<?> type)
Map of all Methods of the given Class type and a List of declared
Annotations related to the Methods
type -
declaredMethodList(Class),
declaredAnnotationSet(AnnotatedElement)public static Map<Method,Set<Annotation>> declaredMethodToAnnotationSetMap(Class<?> type)
Map of all declared Methods of the given Class type and a List of declared
Annotations related to the Methods
type -
declaredMethodList(Class),
declaredAnnotationSet(AnnotatedElement)protected static Map<Method,Set<Annotation>> methodToAnnotationSetMap(List<Method> methodList)
Map of all the given Methods and a List of declared Annotations related to the
Methods
methodList -
declaredMethodList(Class),
#declaredAnnotationSet(Method)public static List<ReflectionUtils.MethodParameterMetaInformation> declaredMethodParameterMetaInformationList(Method method)
List of ReflectionUtils.MethodParameterMetaInformation instances for each parameter the given
Method has.
method -
public static List<Method> declaredMethodList(Class<?> type)
Class.getDeclaredMethods() for a given Class. Returns always a List instance.
type -
public static List<Field> declaredFieldList(Class<?> type)
Class.getDeclaredFields() for a given Class. Returns always a List instance.
type -
public static List<Field> fieldList(Class<?> type)
Class.getFields() for a given Class. Returns always a List instance.
type -
public static List<Method> methodList(Class<?> type)
Class.getMethods() for a given Class. Returns always a List instance.
type -
public static List<Method> methodListIncludingSuperMethods(Class<?> type)
methodList(Class) but including the methods of all inherited interfaces and superclasses
type -
public static Set<Class<?>> interfaceSet(Class<?> type,
boolean inherited)
Set of interfaces which are implemented by the given type. This includes inherited interfaces if the
respective parameter is set to true. If the given type is an interface it is included in the result.
type -
public static Set<Class<?>> assignableTypeSet(Class<?> type,
boolean inherited)
Set of assignable types which are implemented by the given type. This includes inherited types if the
respective parameter is set to true. The given type itself is included in the returned Set.
type - inherited -
public static Set<Class<?>> assignableTypeSet(Class<?> type,
boolean inherited,
boolean onlyReturnInterfaces)
Set of assignable types which are implemented by the given type. This includes inherited types if the
respective parameter is set to true. The given type is returned within the result Set if it is compliant to the
onlyReturnInterfaces flag.
type - inherited - onlyReturnInterfaces -
public static Set<Class<?>> supertypeSet(Class<?> type)
Set of all supertypes the given type extends. The given type is not within the result Set.
type -
public static Set<Class<?>> assignableTypeSet(boolean inherited,
boolean onlyReturnInterfaces,
boolean intersection,
Class<?>... types)
Set of assignable types which are implemented by the given type. This includes inherited types if the
respective parameter is set to true. The given types are included within the result Set if they are compliant to the
onlyReturnInterfaces flag.
inherited - intersection - : false -> types are merged, true -> intersection of type sets for each typeonlyReturnInterfaces - types -
public static boolean isAssignableFrom(Class<?> targetType,
Class<?> type)
Class is Class.isAssignableFrom(Class) for the given Class type.
targetType - type -
public static <A extends Annotation> Set<Package> annotatedPackageSet(Class<? extends A>... packageAnnotationTypes)
Set of all Package instances known to the ClassLoader which are annotated with the given
package Annotation type
packageAnnotationTypes -
annotatedPackageToAnnotationSetMap(Class...)
public static <A extends Annotation> Set<Package> annotatedPackageSet(Set<Package> scannedPackageSet,
Class<? extends A>... packageAnnotationTypes)
Set of all Package instances out of the given Set of Packages which are annotated
with at least one of the given package Annotation types
scannedPackageSet - packageAnnotationTypes -
annotatedPackageToAnnotationSetMap(Class...)public static <A extends Annotation> Map<Package,Set<A>> annotatedPackageToAnnotationSetMap(Class<? extends A>... packageAnnotationTypes)
Map of all Packages annotated with at least one of the given package level Annotations
including the Annotation instances related to each Package. Annotation is specified an empty Map is returned.
packageAnnotationTypes -
annotatedPackageSet(Class...)
public static <A extends Annotation> Map<Package,Set<A>> annotatedPackageToAnnotationSetMap(Set<Package> scannedPackageSet,
Class<? extends A>... packageAnnotationTypes)
Map of all Packages out of the given Packages Set annotated with at least one of
the given package level Annotations including the Annotation instances related to each Package. Annotation or Package is specified an empty Map is returned.
scannedPackageSet - packageAnnotationTypes -
annotatedPackageSet(Class...)public static <T> Class<T> classForName(String className)
Class.forName(String) for the given type name but does not throw any Exception. Instead it returns
null, if the resolvation failed.
className -
public static <E> E invokeMethod(Object object,
String methodName,
Object... arguments)
Exception.
object - methodName - arguments -
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||