Class ReflectionUtils

java.lang.Object
org.apache.juneau.commons.reflect.ReflectionUtils

public class ReflectionUtils extends Object
Utility class providing convenient static methods for creating reflection info objects.

This class provides static factory methods that convert standard Java reflection objects (Class, Method, Field, Constructor) to their corresponding info wrapper objects (ClassInfo, MethodInfo, FieldInfo, ConstructorInfo).

Features:
  • Convenient factory methods - convert reflection objects to info wrappers
  • Null-safe - methods handle null inputs gracefully
  • Unified API - consistent method naming across all reflection types
Use Cases:
  • Converting reflection objects to info wrappers in a consistent way
  • Simplifying code that works with both reflection and info objects
  • Providing a centralized location for reflection-to-info conversions
Usage:

// Convert Class to ClassInfo ClassInfo ci = ReflectionUtils.info(MyClass.class); // Convert Method to MethodInfo Method m = MyClass.class.getMethod("myMethod"); MethodInfo mi = ReflectionUtils.info(m); // Convert Field to FieldInfo Field f = MyClass.class.getField("myField"); FieldInfo fi = ReflectionUtils.info(f); // Convert Constructor to ConstructorInfo Constructor<?> c = MyClass.class.getConstructor(); ConstructorInfo ci2 = ReflectionUtils.info(c);

Null Handling:

All methods in this class handle null inputs gracefully, returning null if the input is null. This makes it safe to use in scenarios where reflection objects may be null.

See Also:
  • Method Details

    • info

      public static final <T> ClassInfoTyped<T> info(Class<T> o)
      Returns the ClassInfoTyped wrapper for the specified class.
      Type Parameters:
      T - The class type.
      Parameters:
      o - The class to wrap. Can be null.
      Returns:
      The ClassInfoTyped wrapper, or null if the input is null.
    • info

      public static final ConstructorInfo info(Constructor<?> o)
      Returns the ConstructorInfo wrapper for the specified constructor.
      Parameters:
      o - The constructor to wrap. Can be null.
      Returns:
      The ConstructorInfo wrapper, or null if the input is null.
    • info

      public static final FieldInfo info(Field o)
      Returns the FieldInfo wrapper for the specified field.
      Parameters:
      o - The field to wrap. Can be null.
      Returns:
      The FieldInfo wrapper, or null if the input is null.
    • info

      public static final MethodInfo info(Method o)
      Returns the MethodInfo wrapper for the specified method.
      Parameters:
      o - The method to wrap. Can be null.
      Returns:
      The MethodInfo wrapper, or null if the input is null.
    • info

      public static final ClassInfo info(Object o)
      Returns the ClassInfo wrapper for the class of the specified object.
      Parameters:
      o - The object whose class to wrap. Can be null.
      Returns:
      The ClassInfo wrapper for the object's class, or null if the input is null.
    • asEnumClass

      public static Class<? extends Enum> asEnumClass(Class<?> clazz)
      Converts a class to an enum class, suppressing rawtypes warnings.

      This utility method wraps Class.asSubclass(Class) for enum classes and suppresses the rawtypes warning that occurs when converting to Class<? extends Enum> for use with methods like EnumSet.allOf(Class).

      Example:

      // Instead of: @SuppressWarnings("rawtypes") Class<? extends Enum> enumClass = clazz.asSubclass(Enum.class); // Use: Class<? extends Enum> enumClass = ReflectionUtils.asEnumClass(clazz);

      Parameters:
      clazz - The class to convert. Must be an enum class. Must not be null.
      Returns:
      The enum class as a raw type suitable for use with EnumSet.allOf(Class).
      Throws:
      ClassCastException - if the class is not an enum class.