Class ReflectionUtils
java.lang.Object
io.fluxzero.common.reflection.ReflectionUtils
Utility class for high-performance reflection-based operations across the Fluxzero Runtime.
This class is intended for internal use only. It provides a wide range of methods for accessing and manipulating object members via reflection, including:
- Resolving annotated fields and methods (with support for meta-annotations)
- Accessing nested property paths via getters and setters (e.g.,
"user/address/street") - Efficient member access using
DefaultMemberInvoker, which leveragesLambdaMetafactoryfor near-native invocation performance - Generic type resolution, including collection element types and supertype inference
- Annotation hierarchy traversal and support for Kotlin nullability reflection (if available)
- Utilities for determining override relationships, class hierarchies, and bean property metadata
- Robust handling of
Classloading and safe access to field/method/property values
Performance Considerations
This class uses extensive caching and memoization to avoid redundant reflective operations at runtime. All public methods are safe to use in high-performance contexts such as annotation scanning, handler dispatching, and JSON schema validation.Modular Use
If Fluxzero adopts JPMS (Project Jigsaw) in the future, this utility would likely be placed in an internal module and not exported to consumers, to preserve encapsulation and forward compatibility.- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classLazily computed reflection metadata for a single Java type. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Class<?> static <T> TasInstance(Object classOrInstance) static Class<?> Converts a primitive type to its wrapper class and leaves non-primitive types unchanged.static booleanclassExists(String className) static Class<?> classForName(String type) static Class<?> classForName(String type, Class<?> defaultClass) static <T> TconvertAnnotation(Annotation annotation, Class<? extends T> returnType) static <V> VcopyFields(V source, V target) static booleandeclaresField(Class<?> target, String fieldName) determineCommonAncestors(Collection<?> elements) static <T extends AccessibleObject>
TensureAccessible(T member) getAllInterfaces(Class<?> type) Returns all interfaces implemented by the supplied type, including interfaces inherited through superclasses and parent interfaces.getAllMethods(Class<?> type) getAnnotatedFields(Class<?> target, Class<? extends Annotation> annotation) getAnnotatedFields(Object target, Class<? extends Annotation> annotation) getAnnotatedMethods(Class<?> target, Class<? extends Annotation> annotation) getAnnotatedMethods(Object target, Class<? extends Annotation> annotation) static List<? extends AccessibleObject> getAnnotatedProperties(Class<?> target, Class<? extends Annotation> annotation) static Optional<? extends AccessibleObject> getAnnotatedProperty(Class<?> target, Class<? extends Annotation> annotation) static Optional<? extends AccessibleObject> getAnnotatedProperty(Object target, Class<? extends Annotation> annotation) static Optional<MemberInvoker> getAnnotatedPropertyInvoker(Class<?> target, Class<? extends Annotation> annotation) getAnnotatedPropertyName(Object target, Class<? extends Annotation> annotation) getAnnotatedPropertyValue(Object target, Class<? extends Annotation> annotation) static Collection<Object> getAnnotatedPropertyValues(Object target, Class<? extends Annotation> annotation) static <A extends Annotation>
Optional<A> getAnnotation(AnnotatedElement m, Class<A> a) static <T> Optional<T> getAnnotationAs(Annotation annotation, Class<? extends Annotation> targetAnnotation, Class<? extends T> returnType) static <T> Optional<T> getAnnotationAs(Class<?> target, Class<? extends Annotation> annotationType, Class<T> returnType) static <T> Optional<T> getAnnotationAs(AnnotatedElement member, Class<? extends Annotation> annotationType, Class<? extends T> returnType) static <T> Optional<T> getAnnotationAttribute(Annotation annotation, String name, Class<T> expectedType) Returns a single annotation attribute value when it exists and matches the requested type.getAnnotationAttributes(Annotation annotation) Returns all no-argument attributes declared by an annotation instance.static List<Annotation> getAnnotations(AnnotatedElement element) Returns the direct annotations present on an annotated element.static Class<?> getCollectionElementType(AccessibleObject fieldOrMethod) getCollectionElementType(Type parameterizedType) static Optional<Constructor<?>> getDefaultConstructor(Class<?> type) Returns the default constructor declared by the supplied type, if present.static Class<?> getEnclosingClass(AccessibleObject fieldOrMethod) static <A extends Annotation>
Optional<A> getFieldAnnotation(Field f, Class<? extends Annotation> a) static <T> Optional<T> getFieldValue(String fieldName, Object target) static <T extends Type>
TgetFirstTypeArgument(Type genericType) static TypegetGenericPropertyType(AccessibleObject fieldOrMethod) static TypegetGenericType(Class<?> candidate, Class<?> wantedClass) static <A extends Annotation>
Optional<A> getMemberAnnotation(Class<?> type, String memberName, Class<? extends Annotation> a) static <A extends Annotation>
Optional<A> getMethodAnnotation(Executable m, Class<? extends Annotation> a) static <A extends Annotation>
List<A> getMethodAnnotations(Executable m, Class<? extends Annotation> a) getMethodOverrideHierarchy(Method method) static StringgetName(AccessibleObject fieldOrMethod) static <A extends Annotation>
Optional<A> getPackageAnnotation(Package p, Class<A> annotationType) static Collection<? extends Annotation> static Collection<? extends Annotation> getPackageAnnotations(Package p, boolean recursive) static intgetParameterIndex(Parameter parameter) getParameterOverrideHierarchy(Parameter parameter) static <T> Optional<T> getPropertyAnnotation(String propertyPath, Object target) static StringgetPropertyName(AccessibleObject property) getPropertyPathMetadata(Class<?> target, String propertyPath) static Class<?> getPropertyType(AccessibleObject fieldOrMethod) static StringgetSimpleName(Class<?> c) static Stringstatic StringgetSimpleName(String fullyQualifiedName) static <A extends Annotation>
AgetTypeAnnotation(Class<?> type, Class<? extends Annotation> annotationType) static Collection<? extends Annotation> getTypeAnnotations(Class<?> type) static <T extends Type>
TgetTypeArgument(Type genericType, int index) getTypeArguments(Type genericType) static ReflectionUtils.TypeMetadatagetTypeMetadata(@NonNull Class<?> type) static ObjectgetValue(AccessibleObject fieldOrMethod, Object target) static ObjectgetValue(AccessibleObject fieldOrMethod, Object target, boolean forceAccess) static booleanhas(Class<? extends Annotation> annotationClass, Method method) static booleanhas(Class<? extends Annotation> annotationClass, Parameter parameter) static booleanhasNonDefaultAnnotationAttribute(Annotation annotation, String name) Returns whether an annotation attribute has a value that differs from its declared default.static booleanhasProperty(String propertyPath, Object target) static booleanhasReturnType(Executable executable) static Class<?> static booleanisAnnotationPresent(Class<?> type, Class<? extends Annotation> annotationType) static booleanisAnnotationPresent(Parameter parameter, Class<? extends Annotation> annotationType) static booleanstatic booleanstatic booleanisLeafValue(Object value) Returns whether the given value should be treated as a terminal scalar during reflective traversal.static booleanisMethodAnnotationPresent(Executable method, Class<? extends Annotation> annotation) static booleanisNullable(Parameter parameter) static booleanisOrHas(Annotation annotation, Class<? extends Annotation> annotationType) static booleanisOrHas(Class<?> type, Class<? extends Annotation> annotationType) static booleanisStatic(Executable method) static Class<?> static <T> Optional<T> readProperty(String propertyPath, Object target) static voidstatic voidstatic Class<?> Converts a primitive wrapper type to its primitive class and leaves other types unchanged.static voidwriteProperty(String propertyPath, Object target, Object value)
-
Constructor Details
-
ReflectionUtils
public ReflectionUtils()
-
-
Method Details
-
getMethodOverrideHierarchy
-
getParameterOverrideHierarchy
-
isKotlinReflectionSupported
public static boolean isKotlinReflectionSupported() -
ifClass
-
isClass
-
asClass
-
getTypeMetadata
-
getAllMethods
-
getDefaultConstructor
Returns the default constructor declared by the supplied type, if present. -
getAllInterfaces
-
box
-
unbox
-
rawClass
-
getMethod
-
getAnnotatedProperties
public static List<? extends AccessibleObject> getAnnotatedProperties(Class<?> target, Class<? extends Annotation> annotation) -
getAnnotatedProperty
public static Optional<? extends AccessibleObject> getAnnotatedProperty(Object target, Class<? extends Annotation> annotation) -
getAnnotatedProperty
public static Optional<? extends AccessibleObject> getAnnotatedProperty(Class<?> target, Class<? extends Annotation> annotation) -
getAnnotatedPropertyInvoker
public static Optional<MemberInvoker> getAnnotatedPropertyInvoker(Class<?> target, Class<? extends Annotation> annotation) -
getPropertyPathMetadata
public static ReflectionUtils.TypeMetadata.PropertyPathMetadata getPropertyPathMetadata(Class<?> target, String propertyPath) -
getAnnotatedPropertyValue
public static Optional<Object> getAnnotatedPropertyValue(Object target, Class<? extends Annotation> annotation) -
getAnnotatedPropertyValues
public static Collection<Object> getAnnotatedPropertyValues(Object target, Class<? extends Annotation> annotation) -
getAnnotatedPropertyName
public static Optional<String> getAnnotatedPropertyName(Object target, Class<? extends Annotation> annotation) -
getPropertyName
-
getAnnotatedMethods
public static List<Method> getAnnotatedMethods(Class<?> target, Class<? extends Annotation> annotation) -
getAnnotatedMethods
public static List<Method> getAnnotatedMethods(Object target, Class<? extends Annotation> annotation) -
isMethodAnnotationPresent
public static boolean isMethodAnnotationPresent(Executable method, Class<? extends Annotation> annotation) -
getAnnotatedFields
public static List<Field> getAnnotatedFields(Class<?> target, Class<? extends Annotation> annotation) -
getAnnotatedFields
-
isAnnotationPresent
public static boolean isAnnotationPresent(Class<?> type, Class<? extends Annotation> annotationType) -
getTypeAnnotation
public static <A extends Annotation> A getTypeAnnotation(Class<?> type, Class<? extends Annotation> annotationType) -
getTypeAnnotations
-
getAnnotations
Returns the direct annotations present on an annotated element.For type, field, method, constructor, and parameter elements, the result is cached in the declaring
ReflectionUtils.TypeMetadata. UsegetTypeAnnotations(Class)when inherited Fluxzero type annotation semantics are needed, andgetPackageAnnotations(Package)for recursive package annotation lookup.- Parameters:
element- the annotated element to inspect- Returns:
- annotations present on the element, or an empty list when
elementisnull
-
getPackageAnnotation
public static <A extends Annotation> Optional<A> getPackageAnnotation(Package p, Class<A> annotationType) -
getPackageAnnotations
-
getPackageAnnotations
-
readProperty
-
getTypeArguments
-
getFirstTypeArgument
-
getTypeArgument
-
getPropertyAnnotation
-
hasProperty
-
hasReturnType
-
getFieldValue
-
getValue
-
getValue
-
getName
-
getEnclosingClass
-
getPropertyType
-
getGenericPropertyType
-
writeProperty
-
isOrHas
-
isOrHas
-
getField
-
getCallerClass
-
isNullable
-
asInstance
-
getParameterIndex
-
determineCommonAncestors
-
getPackageAndParentPackages
-
getMemberAnnotation
public static <A extends Annotation> Optional<A> getMemberAnnotation(Class<?> type, String memberName, Class<? extends Annotation> a) -
isStatic
-
isLeafValue
Returns whether the given value should be treated as a terminal scalar during reflective traversal.Leaf values are not recursively inspected for nested annotated properties. This is used by infrastructure such as search indexing and data protection to decide whether a value should be processed as a whole or traversed further.
A value is considered a leaf when it is:
null- one of the built-in scalar types known to Fluxzero, such as strings, numbers, booleans, temporal values, URIs, locales, and similar primitives/value objects
- an enum
- an instance of a type implementing
Leaf
- Parameters:
value- the value to inspect- Returns:
trueif the value should be treated as a leaf,falseotherwise
-
isAnnotationPresent
public static boolean isAnnotationPresent(Parameter parameter, Class<? extends Annotation> annotationType) -
getGenericType
-
getCollectionElementType
-
getCollectionElementType
-
declaresField
-
setField
-
setField
-
ensureAccessible
-
getAnnotationAttributes
Returns all no-argument attributes declared by an annotation instance.- Parameters:
annotation- the annotation to inspect- Returns:
- annotation attribute values keyed by attribute name
-
getAnnotationAttribute
public static <T> Optional<T> getAnnotationAttribute(Annotation annotation, String name, Class<T> expectedType) Returns a single annotation attribute value when it exists and matches the requested type.- Type Parameters:
T- attribute value type- Parameters:
annotation- the annotation to inspectname- the attribute nameexpectedType- the expected attribute value type- Returns:
- the attribute value, or empty if the attribute is absent or has another type
-
hasNonDefaultAnnotationAttribute
Returns whether an annotation attribute has a value that differs from its declared default.- Parameters:
annotation- the annotation to inspectname- the attribute name- Returns:
trueif the attribute has a default and the actual value differs from it
-
getAnnotation
-
getAnnotationAs
public static <T> Optional<T> getAnnotationAs(Class<?> target, Class<? extends Annotation> annotationType, Class<T> returnType) -
getAnnotationAs
public static <T> Optional<T> getAnnotationAs(AnnotatedElement member, Class<? extends Annotation> annotationType, Class<? extends T> returnType) -
getAnnotationAs
public static <T> Optional<T> getAnnotationAs(Annotation annotation, Class<? extends Annotation> targetAnnotation, Class<? extends T> returnType) -
convertAnnotation
-
has
-
has
-
getFieldAnnotation
public static <A extends Annotation> Optional<A> getFieldAnnotation(Field f, Class<? extends Annotation> a) -
getMethodAnnotation
public static <A extends Annotation> Optional<A> getMethodAnnotation(Executable m, Class<? extends Annotation> a) -
getMethodAnnotations
public static <A extends Annotation> List<A> getMethodAnnotations(Executable m, Class<? extends Annotation> a) -
copyFields
public static <V> V copyFields(V source, V target) -
classForName
-
classForName
-
classExists
-
getSimpleName
-
getSimpleName
-
getSimpleName
-