Interface ReflectedObject

All Superinterfaces:
AnnotatedElement

@Experimental public interface ReflectedObject extends AnnotatedElement
Represents a Class, Constructor, Field or Method. The main purpose of this class is to create an abstraction layer for common methods such as the annotation methods (AnnotatedElement), getModifiers(), accessFlags(), name() and getDeclaringClass() as well as a common class representing such types and grouping them together instead of a vague Object. A proper instance can be created using the of() method.

The Object.toString() (with a prefix), Object.equals(Object) and Object.hashCode() are delegated to the underlying unreflect() object.

Since:
13.0.0
  • Method Details

    • of

      @NotNull @Contract(value="_ -> new", pure=true) static @NotNull ReflectedObject of(@NotNull @NotNull Class<?> clazz)
    • of

      @NotNull @Contract(value="_ -> new", pure=true) static @NotNull ReflectedObject of(@NotNull @NotNull Constructor<?> constructor)
    • of

      @NotNull @Contract(value="_ -> new", pure=true) static @NotNull ReflectedObject of(@NotNull @NotNull Method method)
    • of

      @NotNull @Contract(value="_ -> new", pure=true) static @NotNull ReflectedObject of(@NotNull @NotNull Field field)
    • unreflect

      @NotNull @Contract(pure=true) @NotNull Object unreflect()
      Gets the underlying Class, Constructor, Field or Method object.
    • type

      @NotNull @Contract(pure=true) @NotNull ReflectedObject.Type type()
    • name

      @NotNull @Contract(pure=true) @NotNull String name()
      Gets the underlying JVM object which this class is a wrapper for. Mostly useful as a friendly and compact alternative to Object.toString().
    • getDeclaringClass

      @Nullable @Contract(pure=true) @Nullable Class<?> getDeclaringClass()
      The class that defines this reflected object. This is obvious for Constructor, Field and Method objects, but for Class.getDeclaringClass() it'd be enclosing class or null if none.

      The subtilty with Class.getDeclaringClass() is that anonymous inner classes are not counted as member of a class, whereas named inner classes are. Therefore, this method returns null for an anonymous class. The alternative method Class.getEnclosingClass() works for both anonymous and named classes

    • getModifiers

      @Contract(pure=true) @MagicConstant(flagsFromClass=java.lang.reflect.Modifier.class) int getModifiers()
      Returns the Java language modifiers for this class or interface, encoded in an integer. The modifiers consist of the Java Virtual Machine's constants for public, protected, private, final, static, abstract and interface; they should be decoded using the methods of class Modifier.

      If the underlying class is an array class:

      • its public, private and protected modifiers are the same as those of its component type
      • its abstract and final modifiers are always true
      • its interface modifier is always false, even when the component type is an interface
      If this Class object represents a primitive type or void, its public, abstract, and final modifiers are always true. For Class objects representing void, primitive types, and arrays, the values of other modifiers are false other than as specified above.

      The modifier encodings are defined in section (JVM section 4.1) of The Java Virtual Machine Specification.

      Returns:
      the int representing the modifiers for this class
      Since:
      Java 1.1 JLS 8.1.1 Class Modifiers JLS 9.1.1. Interface Modifiers JVM 4.1 The ClassFile Structure
      See Also:
    • accessFlags

      @NotNull @Contract(value="-> new", pure=true) default @NotNull @Unmodifiable Set<XAccessFlag> accessFlags()
      Returns:
      An unmodifiable set of the access flags for this class, possibly empty

      If the underlying class is an array class:

      • its PUBLIC, PRIVATE and PROTECTED access flags are the same as those of its component type
      • its ABSTRACT and FINAL flags are present
      • its INTERFACE flag is absent, even when the component type is an interface
      If this Class object represents a primitive type or void, the flags are PUBLIC, ABSTRACT, and FINAL. For Class objects representing void, primitive types, and arrays, access flags are absent other than as specified above.
      Since:
      Java 20
      See Also: