Class ReflectionUtil

java.lang.Object
in.co.akshitbansal.springwebquery.util.ReflectionUtil

public class ReflectionUtil extends Object
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 Details

    • ReflectionUtil

      public ReflectionUtil()
  • Method Details

    • resolveField

      public static Field resolveField(Class<?> type, String name)
      Resolves a Field for 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 getDeclaredField while 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 List
       

      This method performs structural type resolution only. It does not access or inspect runtime values.

      Parameters:
      type - the root class from which resolution starts
      name - a dot-separated field path (e.g. "a.b.c")
      Returns:
      the Field corresponding to the last segment in the path
      Throws:
      RuntimeException - if any segment of the path cannot be resolved
      UnsupportedOperationException - if an intermediate collection type does not expose resolvable generic information
    • resolveFieldPath

      public static List<Field> resolveFieldPath(Class<?> type, String name)
      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 starts
      name - dot-separated field path
      Returns:
      ordered list of fields from first segment to last segment
      Throws:
      RuntimeException - if any segment cannot be resolved
      UnsupportedOperationException - if intermediate collection generics cannot be resolved