CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-keycloak--keycloak-common

Common library and dependencies shared with server and all adapters for the Keycloak identity and access management system

Pending
Overview
Eval results
Files

reflection-utilities.mddocs/

Reflection Utilities

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.

Reflections Utility Class

The Reflections class provides comprehensive utility methods for reflection operations, offering a higher-level API over Java's reflection capabilities.

Type Casting and Validation

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);
}

Field Operations

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);
}

Method Operations

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);
}

Constructor Operations

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);
}

Accessibility Control

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);
}

Annotation Processing

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);
}

Class Loading and Type Utilities

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);
}

Modifier Checking

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);
}

Type Assignability

public class Reflections {
    /**
     * Checks complex type assignability with generics
     */
    public static boolean isAssignableFrom(Class<?> rawType1, Type[] actualTypeArguments1, Class<?> rawType2, Type[] actualTypeArguments2);
}

Constants

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;
}

Types Utility Class

The Types class provides advanced type resolution and generic type manipulation utilities.

Basic Type Operations

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);
}

Generic Type Resolution

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);
}

Collection Type Resolution

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);
}

Interface and Inheritance Analysis

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);
}

Method Compatibility and Implementation

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);
}

Type Support Checking

public class Types {
    /**
     * Checks if type supports object from interface
     */
    public static <T> boolean supports(Class<T> type, Object object, Class<?> fromInterface);
}

Privileged Actions

Security-related classes for setting accessibility with appropriate permissions.

SetAccessiblePrivilegedAction

public class SetAccessiblePrivilegedAction implements PrivilegedAction<Void> {
    public SetAccessiblePrivilegedAction(AccessibleObject accessibleObject);
    public Void run();
}

UnSetAccessiblePrivilegedAction

public class UnSetAccessiblePrivilegedAction implements PrivilegedAction<Void> {
    public UnSetAccessiblePrivilegedAction(AccessibleObject accessibleObject);
    public Void run();
}

Usage Examples

Field Access and Manipulation

// 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);

Method Invocation

// 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"

Generic Type Resolution

// 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);

Annotation Processing

// 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);

Class Loading and Type Checking

// 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);

Constructor Operations

// 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);

Interface Implementation Analysis

// 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
);

Security-Aware Accessibility

// 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));

Advanced Type Utilities

// 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

docs

constants-configuration.md

core-functionality.md

crypto-utilities.md

enums-types.md

index.md

profile-management.md

reflection-utilities.md

utility-functions.md

tile.json