Class Reflections
- java.lang.Object
- 
- org.keycloak.common.util.reflections.Reflections
 
- 
 public class Reflections extends Object Utility class for working with JDK Reflection and also CDI's {link Annotated} metadata.
- 
- 
Field SummaryFields Modifier and Type Field Description static Annotation[]EMPTY_ANNOTATION_ARRAYAn empty array of typeAnnotation, useful converting lists to arrays.static Class<?>[]EMPTY_CLASSESstatic Object[]EMPTY_OBJECT_ARRAYAn empty array of typeObject, useful for converting lists to arrays.static Type[]EMPTY_TYPES
 - 
Method SummaryAll Methods Static Methods Concrete Methods Modifier and Type Method Description static Map<Class<?>,Type>buildTypeMap(Set<Type> types)static <T> Tcast(Object obj)Perform a runtime cast.static <T> Class<T>classForName(String name, ClassLoader... loaders)Loads and initializes a class for the given name.static booleancontainsWildcards(Type[] types)static Constructor<?>findDeclaredConstructor(Class<?> clazz, Class<?>... args)Search the class hierarchy for a constructor with the given arguments.static FieldfindDeclaredField(Class<?> clazz, String name)Search the class hierarchy for a field with the given name.static MethodfindDeclaredMethod(Class<?> clazz, String name, Class<?>... args)Search the class hierarchy for a method with the given name and arguments.static Set<Constructor<?>>getAllDeclaredConstructors(Class<?> clazz)Get all the declared constructors on the class hierarchy.static Set<Field>getAllDeclaredFields(Class<?> clazz)Get all the declared fields on the class hierarchy.static Set<Method>getAllDeclaredMethods(Class<?> clazz)Get all the declared methods on the class hierarchy.static Set<Annotation>getAnnotationsWithMetaAnnotation(Set<Annotation> annotations, Class<? extends Annotation> metaAnnotationType)Search for annotations with the specified meta annotation typestatic ObjectgetFieldValue(Field field, Object instance)static <T> TgetFieldValue(Field field, Object instance, Class<T> expectedType)Get the value of the field, on the specified instance, casting the value of the field to the expected type.static Class<?>getMemberType(Member member)Get the type of the memberstatic intgetNesting(Class<?> clazz)static ObjectgetNonPrivateFinalMethodOrType(Class<?> type)static StringgetPropertyName(Method method)Gets the property name from a getter method.static <T> Class<T>getRawType(Type type)Extract the raw type, given a type.static <T> TinvokeMethod(boolean setAccessible, Method method, Class<T> expectedReturnType, Object instance, Object... args)Invoke the method on the instance, with any arguments specified, casting the result of invoking the method to the expected return type.static ObjectinvokeMethod(boolean setAccessible, Method method, Object instance, Object... args)Invoke the specified method on the provided instance, passing any additional arguments included in this method as arguments to the specified method.static <T> TinvokeMethod(Method method, Class<T> expectedReturnType, Object instance, Object... args)Invoke the specified method on the provided instance, passing any additional arguments included in this method as arguments to the specified method.static ObjectinvokeMethod(Method method, Object instance, Object... args)Invoke the specified method on the provided instance, passing any additional arguments included in this method as arguments to the specified method.static booleanisAbstract(Method method)Checks if a method is abstractstatic booleanisArrayType(Class<?> rawType)Checks if raw type is array typestatic booleanisAssignableFrom(Class<?> rawType1, Type[] actualTypeArguments1, Class<?> rawType2, Type[] actualTypeArguments2)Check the assignability of one type to another, taking into account the actual type arguementsstatic booleanisAssignableFrom(Class<?> rawType1, Type[] actualTypeArguments1, Type type2)static booleanisAssignableFrom(Type[] types1, Type type2)static booleanisAssignableFrom(Type[] actualTypeArguments1, Type[] actualTypeArguments2)static booleanisAssignableFrom(Type type1, Type type2)static booleanisAssignableFrom(Type type1, Type[] types2)static booleanisAssignableFrom(Type type1, Set<? extends Type> types2)static booleanisAssignableFrom(Set<Type> types1, Type type2)Check the assignability of a set of flattened types.static booleanisAssignableFrom(Set<Type> types1, Set<Type> types2)Check the assiginability of a set of flattened types.static booleanisCacheable(Annotation[] annotations)static booleanisCacheable(Set<Annotation> annotations)static booleanisFinal(Class<?> clazz)Checks if class is finalstatic booleanisFinal(Member member)Checks if member is finalstatic booleanisPackagePrivate(int mod)static booleanisParamerterizedTypeWithWildcard(Class<?> type)static booleanisParameterizedType(Class<?> type)Checks if type is parameterized typestatic booleanisPrimitive(Type type)static booleanisPrivate(Member member)Checks if member is privatestatic booleanisSerializable(Class<?> clazz)Check if a class is serializable.static booleanisStatic(Class<?> type)Checks if type is staticstatic booleanisStatic(Member member)Checks if member is staticstatic booleanisTransient(Member member)static booleanisTypeBounded(Type type, Type[] lowerBounds, Type[] upperBounds)static booleanisTypeOrAnyMethodFinal(Class<?> type)Checks if type or member is finalstatic booleanmatches(Class<?> rawType1, Type[] actualTypeArguments1, Class<?> rawType2, Type[] actualTypeArguments2)static booleanmatches(Class<?> rawType1, Type[] actualTypeArguments1, Type type2)static booleanmatches(Type type1, Type type2)static booleanmatches(Type type1, Set<? extends Type> types2)static booleanmatches(Set<Type> types1, Set<Type> types2)Check whether whether any of the types1 matches a type in types2static booleanmethodExists(Class<?> clazz, String name)Determine if a method exists in a specified class hierarchystatic <T> TnewInstance(Class<?> type, String fullQualifiedName)Creates a new instance of a class given itsfullQualifiedName.static <T> TnewInstance(Class<T> fromClass)Creates a new instance of a class.static Class<?>resolveListType(Field field, Object instance)static <A extends AccessibleObject>
 AsetAccessible(A member)Set the accessibility flag on theAccessibleObjectas described inAccessibleObject.setAccessible(boolean)within the context of a {link PrivilegedAction}.static <A extends AccessibleObject>
 AunsetAccessible(A member)Set the accessibility flag on theAccessibleObjectto false as described inAccessibleObject.setAccessible(boolean)within the context of a {link PrivilegedAction}.
 
- 
- 
- 
Field Detail- 
EMPTY_ANNOTATION_ARRAYpublic static final Annotation[] EMPTY_ANNOTATION_ARRAY An empty array of typeAnnotation, useful converting lists to arrays.
 - 
EMPTY_OBJECT_ARRAYpublic static final Object[] EMPTY_OBJECT_ARRAY An empty array of typeObject, useful for converting lists to arrays.
 - 
EMPTY_TYPESpublic static final Type[] EMPTY_TYPES 
 - 
EMPTY_CLASSESpublic static final Class<?>[] EMPTY_CLASSES 
 
- 
 - 
Method Detail- 
castpublic static <T> T cast(Object obj) Perform a runtime cast. Similar to Class.cast(Object), but useful when you do not have aClassobject for type you wish to cast to.Class.cast(Object)should be used if possible- Type Parameters:
- T- the type to cast to
- Parameters:
- obj- the object to perform the cast on
- Returns:
- the casted object
- Throws:
- ClassCastException- if the type T is not a subtype of the object
- See Also:
- Class.cast(Object)
 
 - 
getAllDeclaredFieldspublic static Set<Field> getAllDeclaredFields(Class<?> clazz) Get all the declared fields on the class hierarchy. This will return overridden fields.- Parameters:
- clazz- The class to search
- Returns:
- the set of all declared fields or an empty set if there are none
 
 - 
findDeclaredFieldpublic static Field findDeclaredField(Class<?> clazz, String name) Search the class hierarchy for a field with the given name. Will return the nearest match, starting with the class specified and searching up the hierarchy.- Parameters:
- clazz- The class to search
- name- The name of the field to search for
- Returns:
- The field found, or null if no field is found
 
 - 
getAnnotationsWithMetaAnnotationpublic static Set<Annotation> getAnnotationsWithMetaAnnotation(Set<Annotation> annotations, Class<? extends Annotation> metaAnnotationType) Search for annotations with the specified meta annotation type- Parameters:
- annotations- The annotation set to search
- metaAnnotationType- The type of the meta annotation to search for
- Returns:
- The set of annotations with the specified meta annotation, or an empty set if none are found
 
 - 
methodExistspublic static boolean methodExists(Class<?> clazz, String name) Determine if a method exists in a specified class hierarchy- Parameters:
- clazz- The class to search
- name- The name of the method
- Returns:
- true if a method is found, otherwise false
 
 - 
getAllDeclaredMethodspublic static Set<Method> getAllDeclaredMethods(Class<?> clazz) Get all the declared methods on the class hierarchy. This will return overridden methods.- Parameters:
- clazz- The class to search
- Returns:
- the set of all declared methods or an empty set if there are none
 
 - 
findDeclaredMethodpublic static Method findDeclaredMethod(Class<?> clazz, String name, Class<?>... args) Search the class hierarchy for a method with the given name and arguments. Will return the nearest match, starting with the class specified and searching up the hierarchy.- Parameters:
- clazz- The class to search
- name- The name of the method to search for
- args- The arguments of the method to search for
- Returns:
- The method found, or null if no method is found
 
 - 
findDeclaredConstructorpublic static Constructor<?> findDeclaredConstructor(Class<?> clazz, Class<?>... args) Search the class hierarchy for a constructor with the given arguments. Will return the nearest match, starting with the class specified and searching up the hierarchy.- Parameters:
- clazz- The class to search
- args- The arguments of the constructor to search for
- Returns:
- The constructor found, or null if no constructor is found
 
 - 
getAllDeclaredConstructorspublic static Set<Constructor<?>> getAllDeclaredConstructors(Class<?> clazz) Get all the declared constructors on the class hierarchy. This will return overridden constructors.- Parameters:
- clazz- The class to search
- Returns:
- the set of all declared constructors or an empty set if there are none
 
 - 
getMemberTypepublic static Class<?> getMemberType(Member member) Get the type of the member- Parameters:
- member- The member
- Returns:
- The type of the member
- Throws:
- UnsupportedOperationException- if the member is not a field, method, or constructor
 
 - 
classForNamepublic static <T> Class<T> classForName(String name, ClassLoader... loaders) throws ClassNotFoundException Loads and initializes a class for the given name. If the Thread Context Class Loader is available, it will be used, otherwise the classloader used to load Reflectionswill be usedIt is also possible to specify additional classloaders to attempt to load the class with. If the first attempt fails, then these additional loaders are tried in order. - Parameters:
- name- the name of the class to load
- loaders- additional classloaders to use to attempt to load the class
- Returns:
- the class object
- Throws:
- ClassNotFoundException- if the class cannot be found
 
 - 
invokeMethodpublic static Object invokeMethod(Method method, Object instance, Object... args) Invoke the specified method on the provided instance, passing any additional arguments included in this method as arguments to the specified method. This method provides the same functionality and throws the same exceptions as invokeMethod(boolean, Method, Class, Object, Object...), with the expected return type set toObjectand no change to the method's accessibility.
 - 
invokeMethodpublic static Object invokeMethod(boolean setAccessible, Method method, Object instance, Object... args) Invoke the specified method on the provided instance, passing any additional arguments included in this method as arguments to the specified method. This method attempts to set the accessible flag of the method in a {link PrivilegedAction} before invoking the method if the first argument is true. This method provides the same functionality and throws the same exceptions as invokeMethod(boolean, Method, Class, Object, Object...), with the expected return type set toObject.
 - 
invokeMethodpublic static <T> T invokeMethod(Method method, Class<T> expectedReturnType, Object instance, Object... args) Invoke the specified method on the provided instance, passing any additional arguments included in this method as arguments to the specified method. This method provides the same functionality and throws the same exceptions as invokeMethod(boolean, Method, Class, Object, Object...), with the expected return type set toObjectand honoring the accessibility of the method.
 - 
invokeMethodpublic static <T> T invokeMethod(boolean setAccessible, Method method, Class<T> expectedReturnType, Object instance, Object... args)Invoke the method on the instance, with any arguments specified, casting the result of invoking the method to the expected return type. This method wraps Method.invoke(Object, Object...), converting the checked exceptions thatMethod.invoke(Object, Object...)specifies to runtime exceptions.If instructed, this method attempts to set the accessible flag of the method in a {link PrivilegedAction} before invoking the method. - Parameters:
- setAccessible- flag indicating whether method should first be set as accessible
- method- the method to invoke
- instance- the instance to invoke the method
- args- the arguments to the method
- Returns:
- the result of invoking the method, or null if the method's return type is void
- Throws:
- RuntimeException- if this- Methodobject enforces Java language access control and the underlying method is inaccessible or if the underlying method throws an exception or if the initialization provoked by this method fails.
- IllegalArgumentException- if the method is an instance method and the specified- instanceargument is not an instance of the class or interface declaring the underlying method (or of a subclass or implementor thereof); if the number of actual and formal parameters differ; if an unwrapping conversion for primitive arguments fails; or if, after possible unwrapping, a parameter value cannot be converted to the corresponding formal parameter type by a method invocation conversion.
- NullPointerException- if the specified- instanceis null and the method is an instance method.
- ClassCastException- if the result of invoking the method cannot be cast to the expectedReturnType
- ExceptionInInitializerError- if the initialization provoked by this method fails.
- See Also:
- Method.invoke(Object, Object...)
 
 - 
setAccessiblepublic static <A extends AccessibleObject> A setAccessible(A member) Set the accessibility flag on theAccessibleObjectas described inAccessibleObject.setAccessible(boolean)within the context of a {link PrivilegedAction}.- Type Parameters:
- A- member the accessible object type
- Parameters:
- member- the accessible object
- Returns:
- the accessible object after the accessible flag has been altered
 
 - 
unsetAccessiblepublic static <A extends AccessibleObject> A unsetAccessible(A member) Set the accessibility flag on theAccessibleObjectto false as described inAccessibleObject.setAccessible(boolean)within the context of a {link PrivilegedAction}.- Type Parameters:
- A- member the accessible object type
- Parameters:
- member- the accessible object
- Returns:
- the accessible object after the accessible flag has been altered
 
 - 
getFieldValuepublic static <T> T getFieldValue(Field field, Object instance, Class<T> expectedType) Get the value of the field, on the specified instance, casting the value of the field to the expected type. This method wraps Field.get(Object), converting the checked exceptions thatField.get(Object)specifies to runtime exceptions.- Type Parameters:
- T- the type of the field's value
- Parameters:
- field- the field to operate on
- instance- the instance from which to retrieve the value
- expectedType- the expected type of the field's value
- Returns:
- the value of the field
- Throws:
- RuntimeException- if the underlying field is inaccessible.
- IllegalArgumentException- if the specified- instanceis not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof).
- NullPointerException- if the specified- instanceis null and the field is an instance field.
- ExceptionInInitializerError- if the initialization provoked by this method fails.
 
 - 
getRawTypepublic static <T> Class<T> getRawType(Type type) Extract the raw type, given a type.- Type Parameters:
- T- the type
- Parameters:
- type- the type to extract the raw type from
- Returns:
- the raw type, or null if the raw type cannot be determined.
 
 - 
isSerializablepublic static boolean isSerializable(Class<?> clazz) Check if a class is serializable.- Parameters:
- clazz- The class to check
- Returns:
- true if the class implements serializable or is a primitive
 
 - 
isCacheablepublic static boolean isCacheable(Set<Annotation> annotations) 
 - 
isCacheablepublic static boolean isCacheable(Annotation[] annotations) 
 - 
getPropertyNamepublic static String getPropertyName(Method method) Gets the property name from a getter method. We extend JavaBean conventions, allowing the getter method to have parameters- Parameters:
- method- The getter method
- Returns:
- The name of the property. Returns null if method wasn't JavaBean getter-styled
 
 - 
isFinalpublic static boolean isFinal(Class<?> clazz) Checks if class is final- Parameters:
- clazz- The class to check
- Returns:
- True if final, false otherwise
 
 - 
getNestingpublic static int getNesting(Class<?> clazz) 
 - 
isFinalpublic static boolean isFinal(Member member) Checks if member is final- Parameters:
- member- The member to check
- Returns:
- True if final, false otherwise
 
 - 
isPrivatepublic static boolean isPrivate(Member member) Checks if member is private- Parameters:
- member- The member to check
- Returns:
- True if final, false otherwise
 
 - 
isTypeOrAnyMethodFinalpublic static boolean isTypeOrAnyMethodFinal(Class<?> type) Checks if type or member is final- Parameters:
- type- Type or member
- Returns:
- True if final, false otherwise
 
 - 
isPackagePrivatepublic static boolean isPackagePrivate(int mod) 
 - 
isStaticpublic static boolean isStatic(Class<?> type) Checks if type is static- Parameters:
- type- Type to check
- Returns:
- True if static, false otherwise
 
 - 
isStaticpublic static boolean isStatic(Member member) Checks if member is static- Parameters:
- member- Member to check
- Returns:
- True if static, false otherwise
 
 - 
isTransientpublic static boolean isTransient(Member member) 
 - 
isAbstractpublic static boolean isAbstract(Method method) Checks if a method is abstract- Parameters:
- method-
- Returns:
 
 - 
isArrayTypepublic static boolean isArrayType(Class<?> rawType) Checks if raw type is array type- Parameters:
- rawType- The raw type to check
- Returns:
- True if array, false otherwise
 
 - 
isParameterizedTypepublic static boolean isParameterizedType(Class<?> type) Checks if type is parameterized type- Parameters:
- type- The type to check
- Returns:
- True if parameterized, false otherwise
 
 - 
isParamerterizedTypeWithWildcardpublic static boolean isParamerterizedTypeWithWildcard(Class<?> type) 
 - 
containsWildcardspublic static boolean containsWildcards(Type[] types) 
 - 
isAssignableFrompublic static boolean isAssignableFrom(Class<?> rawType1, Type[] actualTypeArguments1, Class<?> rawType2, Type[] actualTypeArguments2) Check the assignability of one type to another, taking into account the actual type arguements- Parameters:
- rawType1- the raw type of the class to check
- actualTypeArguments1- the actual type arguements to check, or an empty array if not a parameterized type
- rawType2- the raw type of the class to check
- actualTypeArguments2- the actual type arguements to check, or an empty array if not a parameterized type
- Returns:
 
 - 
matchespublic static boolean matches(Class<?> rawType1, Type[] actualTypeArguments1, Class<?> rawType2, Type[] actualTypeArguments2) 
 - 
isAssignableFrompublic static boolean isAssignableFrom(Type[] actualTypeArguments1, Type[] actualTypeArguments2) 
 - 
isTypeBoundedpublic static boolean isTypeBounded(Type type, Type[] lowerBounds, Type[] upperBounds) 
 - 
isAssignableFrompublic static boolean isAssignableFrom(Class<?> rawType1, Type[] actualTypeArguments1, Type type2) 
 - 
isAssignableFrompublic static boolean isAssignableFrom(Set<Type> types1, Set<Type> types2) Check the assiginability of a set of flattened types. This algorithm will check whether any of the types1 matches a type in types2- Parameters:
- types1-
- types2-
- Returns:
 
 - 
matchespublic static boolean matches(Set<Type> types1, Set<Type> types2) Check whether whether any of the types1 matches a type in types2- Parameters:
- types1-
- types2-
- Returns:
 
 - 
isAssignableFrompublic static boolean isAssignableFrom(Set<Type> types1, Type type2) Check the assignability of a set of flattened types. This algorithm will check whether any of the types1 matches a type in types2- Parameters:
- types1-
- type2-
- Returns:
 
 - 
isPrimitivepublic static boolean isPrimitive(Type type) 
 - 
newInstancepublic static <T> T newInstance(Class<T> fromClass) throws ClassNotFoundException, IllegalAccessException, InstantiationException Creates a new instance of a class. This method will use the same class loader of the given class to create the new instance. - Parameters:
- fromClass- The class from where the instance should be created.
- Returns:
- A newly allocated instance of the class.
- Throws:
- ClassNotFoundException
- IllegalAccessException
- InstantiationException
 
 - 
newInstancepublic static <T> T newInstance(Class<?> type, String fullQualifiedName) throws ClassNotFoundException, IllegalAccessException, InstantiationException Creates a new instance of a class given its fullQualifiedName.This method will use the same class loader of typeto create the new instance.- Parameters:
- type- The class that will be used to get the class loader from.
- fullQualifiedName- The full qualified name of the class from which the instance will be created.
- Returns:
- A newly allocated instance of the class.
- Throws:
- ClassNotFoundException
- IllegalAccessException
- InstantiationException
 
 - 
resolveListTypepublic static Class<?> resolveListType(Field field, Object instance) throws IllegalAccessException Resolves the type of items for a Fielddeclared as aList.This method will first try to check the parametrized type of the field type. If none is defined, it will try to infer the type of items by looking at the value of the field for the given instance.Make sure the field is accessible before invoking this method. - Parameters:
- field- the field declared as- List
- instance- the instance that should be used to obtain infer the type in case no parametrized type is found in the field.
- Returns:
- if the field is not a List, it returns null. Otherwise the type of items of the list. If the type for items can not be inferred, theObjecttype is returned.
- Throws:
- IllegalAccessException- in case it fails to obtain the value of the field from the- instance
 
 
- 
 
-