Common library and dependencies shared with server and all adapters for the Keycloak identity and access management system
—
This document covers the advanced reflection capabilities in the org.keycloak.common.util.reflections package that provide comprehensive utilities for type resolution, method invocation, field access, and annotation handling.
The Reflections class provides comprehensive utility methods for reflection operations, offering a higher-level API over Java's reflection capabilities.
public class Reflections {
/**
* Runtime cast utility with improved type safety
*/
public static <T> T cast(Object obj);
/**
* Checks if a class is serializable
*/
public static boolean isSerializable(Class<?> clazz);
/**
* Extracts raw type from parameterized types
*/
public static <T> Class<T> getRawType(Type type);
/**
* Checks if type is array type
*/
public static boolean isArrayType(Class<?> rawType);
/**
* Checks if type is parameterized type
*/
public static boolean isParameterizedType(Class<?> type);
}public class Reflections {
/**
* Gets all declared fields in class hierarchy
*/
public static Set<Field> getAllDeclaredFields(Class<?> clazz);
/**
* Finds field by name in class hierarchy
*/
public static Field findDeclaredField(Class<?> clazz, String name);
/**
* Gets field value from instance
*/
public static Object getFieldValue(Field field, Object instance);
/**
* Gets typed field value from instance
*/
public static <T> T getFieldValue(Field field, Object instance, Class<T> expectedType);
/**
* Resolves list element type from field
*/
public static Class<?> resolveListType(Field field, Object instance);
}public class Reflections {
/**
* Checks if method exists in class
*/
public static boolean methodExists(Class<?> clazz, String name);
/**
* Gets all declared methods in class hierarchy
*/
public static Set<Method> getAllDeclaredMethods(Class<?> clazz);
/**
* Finds method by name and parameter types
*/
public static Method findDeclaredMethod(Class<?> clazz, String name, Class<?>... args);
/**
* Invokes method on instance
*/
public static Object invokeMethod(Method method, Object instance, Object... args);
/**
* Invokes method with type checking and accessibility control
*/
public static <T> T invokeMethod(boolean setAccessible, Method method, Class<T> expectedReturnType, Object instance, Object... args);
/**
* Gets JavaBean property name from getter method
*/
public static String getPropertyName(Method method);
}public class Reflections {
/**
* Finds constructor by parameter types
*/
public static Constructor<?> findDeclaredConstructor(Class<?> clazz, Class<?>... args);
/**
* Gets all declared constructors
*/
public static Set<Constructor<?>> getAllDeclaredConstructors(Class<?> clazz);
/**
* Creates new instance (deprecated, use Constructor.newInstance())
*/
@Deprecated
public static <T> T newInstance(Class<T> fromClass);
}public class Reflections {
/**
* Sets accessible flag on member
*/
public static <A extends AccessibleObject> A setAccessible(A member);
/**
* Unsets accessible flag on member
*/
public static <A extends AccessibleObject> A unsetAccessible(A member);
}public class Reflections {
/**
* Gets annotations with specified meta-annotation
*/
public static Set<Annotation> getAnnotationsWithMetaAnnotation(Set<Annotation> annotations, Class<? extends Annotation> metaAnnotationType);
/**
* Checks if annotations are cacheable
*/
public static boolean isCacheable(Set<Annotation> annotations);
}public class Reflections {
/**
* Loads class by name with multiple class loaders
*/
public static <T> Class<T> classForName(String name, ClassLoader... loaders);
/**
* Gets member type (field type, method return type, etc.)
*/
public static Class<?> getMemberType(Member member);
/**
* Builds type map from set of types
*/
public static Map<Class<?>, Type> buildTypeMap(Set<Type> types);
}public class Reflections {
/**
* Checks if class is final
*/
public static boolean isFinal(Class<?> clazz);
/**
* Checks if member is final
*/
public static boolean isFinal(Member member);
/**
* Checks if member is private
*/
public static boolean isPrivate(Member member);
/**
* Checks if type is static
*/
public static boolean isStatic(Class<?> type);
/**
* Checks if member is static
*/
public static boolean isStatic(Member member);
/**
* Checks if member is transient
*/
public static boolean isTransient(Member member);
/**
* Checks if method is abstract
*/
public static boolean isAbstract(Method method);
}public class Reflections {
/**
* Checks complex type assignability with generics
*/
public static boolean isAssignableFrom(Class<?> rawType1, Type[] actualTypeArguments1, Class<?> rawType2, Type[] actualTypeArguments2);
}public class Reflections {
public static final Annotation[] EMPTY_ANNOTATION_ARRAY;
public static final Object[] EMPTY_OBJECT_ARRAY;
public static final Type[] EMPTY_TYPES;
public static final Class<?>[] EMPTY_CLASSES;
}The Types class provides advanced type resolution and generic type manipulation utilities.
public class Types {
/**
* Gets boxed type for primitives
*/
public static Type boxedType(Type type);
/**
* Gets boxed class for primitive types
*/
public static Class<?> boxedClass(Class<?> type);
/**
* Gets raw type from generic type
*/
public static Class<?> getRawType(Type type);
/**
* Gets raw type without throwing exceptions
*/
public static Class<?> getRawTypeNoException(Type type);
}public class Types {
/**
* Gets first type argument from generic type
*/
public static Class<?> getTypeArgument(Type genericType);
/**
* Gets type argument at specific index from parameterized type
*/
public static Class getArgumentType(ParameterizedType pType, int index);
/**
* Resolves type variables in context of root class
*/
public static Type resolveTypeVariables(Class<?> root, Type type);
/**
* Resolves single type variable in context of root class
*/
public static Type resolveTypeVariable(Class<?> root, TypeVariable<?> typeVariable);
}public class Types {
/**
* Gets collection element type
*/
public static Class getCollectionBaseType(Class type, Type genericType);
/**
* Gets map key type
*/
public static Class getMapKeyType(Type genericType);
/**
* Gets map value type
*/
public static Class getMapValueType(Type genericType);
}public class Types {
/**
* Checks if class is assignable from parameterized type
*/
public static boolean isA(Class clazz, ParameterizedType pType);
/**
* Gets template parameter of interface
*/
public static Class getTemplateParameterOfInterface(Class base, Class desiredInterface);
/**
* Gets actual type arguments of interface
*/
public static Type[] getActualTypeArgumentsOfAnInterface(Class<?> classToSearch, Class<?> interfaceToFind);
/**
* Finds parameterized types in class hierarchy
*/
public static Type[] findParameterizedTypes(Class<?> root, Class<?> searchedFor);
}public class Types {
/**
* Checks if two methods are compatible
*/
public static boolean isCompatible(Method method, Method intfMethod);
/**
* Gets implementing method for interface method
*/
public static Method getImplementingMethod(Class clazz, Method intfMethod);
}public class Types {
/**
* Checks if type supports object from interface
*/
public static <T> boolean supports(Class<T> type, Object object, Class<?> fromInterface);
}Security-related classes for setting accessibility with appropriate permissions.
public class SetAccessiblePrivilegedAction implements PrivilegedAction<Void> {
public SetAccessiblePrivilegedAction(AccessibleObject accessibleObject);
public Void run();
}public class UnSetAccessiblePrivilegedAction implements PrivilegedAction<Void> {
public UnSetAccessiblePrivilegedAction(AccessibleObject accessibleObject);
public Void run();
}// Get all fields in class hierarchy
Class<?> clazz = MyClass.class;
Set<Field> allFields = Reflections.getAllDeclaredFields(clazz);
// Find specific field
Field nameField = Reflections.findDeclaredField(clazz, "name");
// Access field value safely
Object instance = new MyClass();
Reflections.setAccessible(nameField);
String name = Reflections.getFieldValue(nameField, instance, String.class);
// Resolve generic field types
Field listField = Reflections.findDeclaredField(clazz, "items");
Class<?> elementType = Reflections.resolveListType(listField, instance);// Find and invoke methods
Class<?> clazz = MyService.class;
Method method = Reflections.findDeclaredMethod(clazz, "processData", String.class, int.class);
Object service = new MyService();
String result = Reflections.invokeMethod(true, method, String.class, service, "data", 42);
// Check method existence
boolean hasMethod = Reflections.methodExists(clazz, "validate");
// Get property name from getter
Method getter = Reflections.findDeclaredMethod(clazz, "getName");
String propertyName = Reflections.getPropertyName(getter); // "name"// Resolve collection element types
Field listField = MyClass.class.getDeclaredField("stringList");
Type fieldType = listField.getGenericType();
Class<?> elementType = Types.getCollectionBaseType(List.class, fieldType);
// Resolve map types
Field mapField = MyClass.class.getDeclaredField("stringMap");
Type mapType = mapField.getGenericType();
Class<?> keyType = Types.getMapKeyType(mapType);
Class<?> valueType = Types.getMapValueType(mapType);
// Resolve type variables
class Container<T> {
List<T> items;
}
Type resolvedType = Types.resolveTypeVariables(Container.class, itemsFieldType);// Get annotations with meta-annotations
Set<Annotation> annotations = new HashSet<>();
// ... populate annotations
Class<? extends Annotation> metaType = MyMetaAnnotation.class;
Set<Annotation> filtered = Reflections.getAnnotationsWithMetaAnnotation(annotations, metaType);
// Check if annotations are cacheable
boolean cacheable = Reflections.isCacheable(annotations);// Load class with fallback loaders
ClassLoader[] loaders = {
Thread.currentThread().getContextClassLoader(),
MyClass.class.getClassLoader(),
ClassLoader.getSystemClassLoader()
};
Class<?> loadedClass = Reflections.classForName("com.example.MyClass", loaders);
// Type checking
Class<?> rawType = Types.getRawType(someGenericType);
boolean isArray = Reflections.isArrayType(String[].class);
boolean isParameterized = Reflections.isParameterizedType(List.class);// Find and use constructors
Constructor<?> constructor = Reflections.findDeclaredConstructor(MyClass.class, String.class, int.class);
Reflections.setAccessible(constructor);
Object instance = constructor.newInstance("test", 42);
// Get all constructors
Set<Constructor<?>> constructors = Reflections.getAllDeclaredConstructors(MyClass.class);// Check method compatibility
Method interfaceMethod = MyInterface.class.getDeclaredMethod("process", String.class);
Method implementationMethod = MyImplementation.class.getDeclaredMethod("process", String.class);
boolean compatible = Types.isCompatible(implementationMethod, interfaceMethod);
// Find implementing method
Method impl = Types.getImplementingMethod(MyImplementation.class, interfaceMethod);
// Get interface type parameters
Type[] typeArgs = Types.getActualTypeArgumentsOfAnInterface(
MyGenericImplementation.class,
MyGenericInterface.class
);// Use privileged actions in security-managed environments
AccessibleObject member = somePrivateField;
// Set accessible with proper permissions
AccessController.doPrivileged(new SetAccessiblePrivilegedAction(member));
// Use the member
// ...
// Unset accessible when done
AccessController.doPrivileged(new UnSetAccessiblePrivilegedAction(member));// Type boxing and conversion
Type primitiveType = int.class;
Type boxedType = Types.boxedType(primitiveType); // Integer.class
Class<?> boxedClass = Types.boxedClass(int.class); // Integer.class
// Complex assignability checking
boolean assignable = Reflections.isAssignableFrom(
List.class, new Type[]{String.class},
ArrayList.class, new Type[]{String.class}
);
// Build type maps for analysis
Set<Type> types = Set.of(String.class, Integer.class, List.class);
Map<Class<?>, Type> typeMap = Reflections.buildTypeMap(types);This reflection utilities package provides powerful capabilities for runtime type analysis, dynamic method invocation, and complex generic type resolution, essential for building flexible and dynamic Java applications.
Install with Tessl CLI
npx tessl i tessl/maven-org-keycloak--keycloak-common