org.omnaest.utils.reflection
Class ReflectionUtils

java.lang.Object
  extended by org.omnaest.utils.reflection.ReflectionUtils

public class ReflectionUtils
extends Object

Helper for Java Reflection.

Author:
Omnaest

Nested Class Summary
static class ReflectionUtils.MethodParameterMetaInformation
          Represents the meta information about a single Method parameter
 
Constructor Summary
ReflectionUtils()
           
 
Method Summary
static
<A extends Annotation>
Set<Package>
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
<A extends Annotation>
Set<Package>
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
<A extends Annotation>
Map<Package,Set<A>>
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
<A extends Annotation>
Map<Package,Set<A>>
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
<A extends Annotation>
A
annotation(Class<?> type, Class<? extends A> annotationType)
          Returns the Annotation instance for the given type and annotation type.
static
<A extends Annotation>
A
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
<A extends Annotation>
A
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
<T> Class<T>
classForName(String className)
          Invokes Class.forName(String) for the given type name but does not throw any Exception.
static
<B> Constructor<B>
constructorFor(Class<? extends B> type, Class<?>... parameterTypes)
          Returns the Constructor for the given Class type and parameter types
static
<B> Constructor<B>
constructorFor(Class<? extends B> type, Object... arguments)
          Returns the Constructor for the given Class type and arguments
static
<A extends Annotation>
A
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
<B> boolean
hasConstructorFor(Class<? extends B> type, Class<?>... parameterTypes)
          Returns true if the given Class type has a Constructor for the given parameter types
static
<B> boolean
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
<B> boolean
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
<E> E
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
<A extends Annotation>
Map<Method,A>
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
<B> B
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
<B> B
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
<C> Constructor<C>
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

ReflectionUtils

public ReflectionUtils()
Method Detail

declaredMethodIndexPosition

public static int declaredMethodIndexPosition(Class<?> clazz,
                                              Method method)
Determines the index position of a declared Method within a given Class.

Parameters:
clazz -
method -
Returns:

declaredFieldIndexPosition

public static int declaredFieldIndexPosition(Class<?> clazz,
                                             Field field)
Determines the index position of a declared Field within a given Class.

Parameters:
clazz -
field -
Returns:

declaredFieldIndexPosition

public static int declaredFieldIndexPosition(Class<?> clazz,
                                             String fieldname)
Determines the index position of a declared Field within a given Class.

Parameters:
clazz -
fieldname -
Returns:
-1 if the Field could not be determined at all.

numberOfDeclaredFields

public static int numberOfDeclaredFields(Class<?> clazz)
Returns the number of declared Fields of a Class.

Parameters:
clazz -
Returns:
number of declared Fields or 0 if clazz == null

numberOfDeclaredMethods

public static int numberOfDeclaredMethods(Class<?> clazz)
Returns the number of declared Methods of a Class.

Parameters:
clazz -
Returns:
number of declared Methods or 0 if clazz == null

hasConstructorFor

public static <B> boolean hasConstructorFor(Class<? extends B> type,
                                            Object... arguments)
Returns true if the given Class type has a Constructor for the given arguments

Parameters:
type -
arguments -
Returns:

hasConstructorFor

public static <B> boolean hasConstructorFor(Class<? extends B> type,
                                            Class<?>... parameterTypes)
Returns true if the given Class type has a Constructor for the given parameter types

Parameters:
type -
parameterTypes -
Returns:

hasDefaultConstructorFor

public static <B> boolean hasDefaultConstructorFor(Class<? extends B> type)
Returns true if the given Class type has a default Constructor

Parameters:
type -
Returns:

constructorFor

public static <B> Constructor<B> constructorFor(Class<? extends B> type,
                                                Object... arguments)
Returns the Constructor for the given Class type and arguments

Parameters:
type -
arguments -
Returns:

constructorFor

public static <B> Constructor<B> constructorFor(Class<? extends B> type,
                                                Class<?>... parameterTypes)
Returns the Constructor for the given Class type and parameter types

Parameters:
type -
parameterTypes -
Returns:

resolveConstructorFor

public static <C> Constructor<C> resolveConstructorFor(Class<C> type,
                                                       Class<?>... parameterTypes)
Resolves a matching constructor for the given type and the given parameter types

Parameters:
type -
parameterTypes -
Returns:

areAssignableFrom

public 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. 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.

Parameters:
assignableTypes -
sourceTypes -
Returns:

newInstanceOf

public static <B> B 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.

Primitive arguments will be matched to their java wrapper types.

This method never throws an Exception instead null is returned.

Parameters:
type - Class
arguments -
See Also:
newInstanceByValueOf(Class, Object...)

newInstanceByValueOf

public static <B> B 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.

Any primitive arguments will match their java wrapper type signature as well.

This method does not throw any Exception instead it will return null, if the invocation fails for any reason.

Parameters:
type - Class
arguments -
See Also:
newInstanceOf(java.lang.Class, java.lang.Object...)

declaredMethodToReturnTypeMap

public 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

Parameters:
type -
Returns:

methodToReturnTypeMap

public static Map<Method,Class<?>> methodToReturnTypeMap(Class<?> type)
Returns a Map with Methods of the given type and the return type of the Methods

Parameters:
type -
Returns:

methodToReturnTypeMap

protected static Map<Method,Class<?>> methodToReturnTypeMap(List<Method> methodList)
Returns a Map with all the given Methods and their return type

Parameters:
methodList -
Returns:

methodToAnnotationMap

public static <A extends Annotation> Map<Method,A> methodToAnnotationMap(Class<?> type,
                                                                         Class<A> annotationType)
Returns a 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.

Parameters:
type -
annotationType -
Returns:

declaredAnnotationList

public static List<Annotation> declaredAnnotationList(Class<?> type)
Returns all Class.getDeclaredAnnotations() as List

Parameters:
type -
Returns:

annotationList

public static List<Annotation> annotationList(Class<?> type)
Returns all Class.getAnnotations() as List

Parameters:
type -
Returns:

annotationList

public static List<Annotation> annotationList(Field field)
Returns all AccessibleObject.getAnnotations() as List

Parameters:
field -
Returns:

annotationIncludingInterfaces

public static <A extends Annotation> A annotationIncludingInterfaces(Class<?> type,
                                                                     Class<? extends A> annotationType)
Returns the 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.

Parameters:
type -
annotationType -
Returns:
See Also:
hasAnnotationIncludingInterfaces(Class, Class), annotation(Class, Class), declaredAnnotation(Class, Class)

hasAnnotation

public 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

Parameters:
type -
annotationType -
Returns:
See Also:
hasDeclaredAnnotation(Class, Class), hasAnnotationIncludingInterfaces(Class, Class), annotation(Class, Class)

annotation

public static <A extends Annotation> A annotation(Package package_,
                                                  Class<? extends A> annotationType)
Returns the Annotation instance for any Annotation declared on a Package with the given type of Annotation

Parameters:
package_ -
annotationType -
Returns:

annotation

public static <A extends Annotation> A annotation(Class<?> type,
                                                  Class<? extends A> annotationType)
Returns the Annotation instance for the given type and annotation type. If no matching annotation type is found null is returned.

Parameters:
type -
annotationType -
Returns:
See Also:
hasAnnotation(Class, Class), annotationIncludingInterfaces(Class, Class), declaredAnnotation(Class, Class)

hasAnnotation

public static boolean hasAnnotation(Field field,
                                    Class<? extends Annotation> annotationType)
Returns true if the given Field declares the given Annotation class

Parameters:
field -
annotationType -
Returns:

hasAnnotationIncludingInterfaces

public 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

Parameters:
type -
annotationType -
Returns:
See Also:
annotationIncludingInterfaces(Class, Class)

hasDeclaredAnnotation

public static boolean hasDeclaredAnnotation(Class<?> type,
                                            Class<? extends Annotation> annotationType)
Returns true if the given Class type declares the given Annotation class

Parameters:
type -
annotationType -
Returns:

declaredAnnotation

public static <A extends Annotation> A declaredAnnotation(Class<?> type,
                                                          Class<? extends A> annotationType)
Returns the Annotation if the given Class type declares the given Annotation class

Parameters:
type -
annotationType -
Returns:
See Also:
annotation(Class, Class), annotationIncludingInterfaces(Class, Class)

hasDeclaredAnnotation

public static boolean hasDeclaredAnnotation(Method method,
                                            Class<? extends Annotation> annotationType)
Returns true if the given Method declares the given Annotation class

Parameters:
method -
annotationType -
Returns:

hasAnnotationOnAnyMethod

public 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

Parameters:
type -
annotationType -
Returns:

hasAnnotationOnAnyMethod

public 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

Parameters:
type -
annotationTypes -
Returns:

declaredAnnotationSet

public static Set<Annotation> declaredAnnotationSet(AnnotatedElement annotatedElement)
Returns all AnnotatedElement.getDeclaredAnnotations() as Set

Parameters:
annotatedElement - Field
Returns:

annotationTypeToAnnotationMap

public 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.

Parameters:
annotatedElement - Field
Returns:
always a Map instance, even if the annotatedElement is null

methodToAnnotationSetMap

public 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

Parameters:
type -
Returns:
See Also:
declaredMethodList(Class), declaredAnnotationSet(AnnotatedElement)

declaredMethodToAnnotationSetMap

public 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

Parameters:
type -
Returns:
See Also:
declaredMethodList(Class), declaredAnnotationSet(AnnotatedElement)

methodToAnnotationSetMap

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

Parameters:
methodList -
Returns:
See Also:
declaredMethodList(Class), #declaredAnnotationSet(Method)

declaredMethodParameterMetaInformationList

public static List<ReflectionUtils.MethodParameterMetaInformation> declaredMethodParameterMetaInformationList(Method method)
Returns an ordered List of ReflectionUtils.MethodParameterMetaInformation instances for each parameter the given Method has.

Parameters:
method -
Returns:

declaredMethodList

public static List<Method> declaredMethodList(Class<?> type)
Returns the Class.getDeclaredMethods() for a given Class. Returns always a List instance.

Parameters:
type -
Returns:

declaredFieldList

public static List<Field> declaredFieldList(Class<?> type)
Returns the Class.getDeclaredFields() for a given Class. Returns always a List instance.

Parameters:
type -
Returns:

fieldList

public static List<Field> fieldList(Class<?> type)
Returns the Class.getFields() for a given Class. Returns always a List instance.

Parameters:
type -
Returns:

methodList

public static List<Method> methodList(Class<?> type)
Returns the Class.getMethods() for a given Class. Returns always a List instance.

Parameters:
type -
Returns:

interfaceSet

public static Set<Class<?>> interfaceSet(Class<?> type,
                                         boolean inherited)
Returns as 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.

Parameters:
type -
Returns:

assignableTypeSet

public static Set<Class<?>> assignableTypeSet(Class<?> type,
                                              boolean inherited)
Returns as 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.

Parameters:
type -
inherited -
Returns:

assignableTypeSet

public static Set<Class<?>> assignableTypeSet(Class<?> type,
                                              boolean inherited,
                                              boolean onlyReturnInterfaces)
Returns as 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.

Parameters:
type -
inherited -
onlyReturnInterfaces -
Returns:

supertypeSet

public static Set<Class<?>> supertypeSet(Class<?> type)
Returns a Set of all supertypes the given type extends. The given type is not within the result Set.

Parameters:
type -
Returns:

assignableTypeSet

public 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. 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.

Parameters:
inherited -
intersection - : false -> types are merged, true -> intersection of type sets for each type
onlyReturnInterfaces -
types -
Returns:

isAssignableFrom

public static boolean isAssignableFrom(Class<?> targetType,
                                       Class<?> type)
Returns true if the given target Class is Class.isAssignableFrom(Class) for the given Class type.

Parameters:
targetType -
type -
Returns:

annotatedPackageSet

public static <A extends Annotation> Set<Package> 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

Parameters:
packageAnnotationTypes -
Returns:
See Also:
annotatedPackageToAnnotationSetMap(Class...)

annotatedPackageSet

public static <A extends Annotation> Set<Package> 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

Parameters:
scannedPackageSet -
packageAnnotationTypes -
Returns:
See Also:
annotatedPackageToAnnotationSetMap(Class...)

annotatedPackageToAnnotationSetMap

public static <A extends Annotation> Map<Package,Set<A>> 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.

If no Annotation is specified an empty Map is returned.

Parameters:
packageAnnotationTypes -
Returns:
See Also:
annotatedPackageSet(Class...)

annotatedPackageToAnnotationSetMap

public static <A extends Annotation> Map<Package,Set<A>> 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.

If no Annotation or Package is specified an empty Map is returned.

Parameters:
scannedPackageSet -
packageAnnotationTypes -
Returns:
See Also:
annotatedPackageSet(Class...)

classForName

public static <T> Class<T> classForName(String className)
Invokes Class.forName(String) for the given type name but does not throw any Exception. Instead it returns null, if the resolvation failed.

Parameters:
className -
Returns:

invokeMethod

public static <E> E invokeMethod(Object object,
                                 String methodName,
                                 Object... arguments)
Invokes the given method name on the given object. This does not throw any Exception.

Parameters:
object -
methodName -
arguments -
Returns:


Copyright © 2013. All Rights Reserved.