Class ReflectionUtil
java.lang.Object
in.co.akshitbansal.springwebquery.util.ReflectionUtil
Utility class for performing reflection-based operations on entity classes.
This class provides methods for resolving fields from dot-separated paths, handling inheritance hierarchies, and unwrapping container types (arrays and collections).
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic FieldresolveField(Class<?> type, String name) Resolves aFieldfor the given dot-separated field path, starting from the supplied root type and traversing the type hierarchy and container types as needed.resolveFieldPath(Class<?> type, String name) Resolves all fields in a dot-separated path, preserving traversal order.
-
Constructor Details
-
ReflectionUtil
public ReflectionUtil()
-
-
Method Details
-
resolveField
Resolves aFieldfor the given dot-separated field path, starting from the supplied root type and traversing the type hierarchy and container types as needed.The resolution rules are:
- Each path segment is resolved using
getDeclaredFieldwhile walking up the superclass hierarchy. - If a resolved field is an array, traversal continues with its component type.
- If a resolved field is a
Collection, traversal continues with the collection's generic element type. - Only the last field in the path is returned.
Examples:
resolveField(User.class, "name.first"); resolveField(Order.class, "items.product.id"); resolveField(Foo.class, "values"); // List<List<String>> resolves to ListThis method performs structural type resolution only. It does not access or inspect runtime values.
- Parameters:
type- the root class from which resolution startsname- a dot-separated field path (e.g."a.b.c")- Returns:
- the
Fieldcorresponding to the last segment in the path - Throws:
RuntimeException- if any segment of the path cannot be resolvedUnsupportedOperationException- if an intermediate collection type does not expose resolvable generic information
- Each path segment is resolved using
-
resolveFieldPath
Resolves all fields in a dot-separated path, preserving traversal order.Unlike
resolveField(Class, String), which returns only the terminal field, this method returns the complete sequence of resolved fields. It applies the same hierarchy lookup and container unwrapping rules at each step.- Parameters:
type- root class from which traversal startsname- dot-separated field path- Returns:
- ordered list of fields from first segment to last segment
- Throws:
RuntimeException- if any segment cannot be resolvedUnsupportedOperationException- if intermediate collection generics cannot be resolved
-