Class ReflectionArguments

java.lang.Object
io.micronaut.reflection.ReflectionArguments

public final class ReflectionArguments extends Object
Builds Arguments from the types read reflectively from parameters, fields, return types and type hierarchies, in the shape the annotation processors give the arguments they generate: the raw type, the type arguments recursively, the type variables as GenericPlaceholders, and the annotations of the element and of every level of its type as metadata built by ReflectionAnnotations.

The reverse direction, toType(Argument), renders an argument as a Type for the APIs defined on java.lang.reflect, either keeping an unresolved type variable or rendering it as the type it is bounded by.

Since:
5.2.0
Author:
Denis Stepanov
  • Method Summary

    Modifier and Type
    Method
    Description
    static Argument<?>[]
    argumentsOf(Executable executable)
    Converts the parameters of a method or constructor to Arguments.
    static Argument<?>[]
    argumentsOf(Executable executable, Class<?> context)
    Converts the parameters of a method or constructor to Arguments as the type reading them sees them.
    static Argument<?>
    of(@Nullable String name, AnnotatedType annotatedType)
    Converts an annotated type to a named Argument.
    static Argument<?>
    of(@Nullable String name, Type type)
    Converts a type to a named Argument.
    static Argument<?>
    of(AnnotatedType annotatedType)
    Converts an annotated type to an Argument: the raw type, the type arguments recursively, and the type-use annotations of each level.
    static Argument<?>
    of(Field field)
    Converts a field to an Argument named after it, whose metadata holds the annotations of the field and the type-use annotations of its type.
    static Argument<?>
    of(Field field, Class<?> context)
    Converts a field to an Argument as the type reading it sees it: a variable the declaring type leaves open and the reading type gives a value to - T of a class Base<T> read through a class Impl extends Base<Book> - is resolved to that value rather than left a GenericPlaceholder, which is what the processors generate for the reading type.
    static Argument<?>
    of(Parameter parameter)
    Converts a parameter to an Argument named after it, whose metadata holds the annotations of the parameter and the type-use annotations of its type.
    static Argument<?>
    of(Parameter parameter, Class<?> context)
    Converts a parameter to an Argument as the type reading it sees it.
    static Argument<?>
    of(Type type)
    Converts a type to an Argument.
    static <T> @Nullable Argument<T>
    resolveGenericToArgument(Class<?> type, Class<T> superType)
    Resolves the type arguments a type gives to one of its super types, as an argument of the super type: for a class A extends AbstractList<@Nullable String>, the argument of Collection is Collection<@Nullable String>.
    static Argument<?>
    returnOf(Method method)
    Converts the return type of a method to an Argument whose metadata holds the annotations of the method and the type-use annotations of its return type, as of(Parameter) builds the argument of a parameter from the parameter and its type: a generated executable method of a bean definition reports the return value with the annotations of the method on it, and a constraint declared on a method constrains the value it returns.
    static Argument<?>
    returnOf(Method method, Class<?> context)
    Converts the return type of a method to an Argument as the type invoking it sees it, with the annotations of the method and the type-use annotations of its return type.
    static Type
    toType(Argument<?> argument)
    Renders an argument as a Type: the raw class when it has no type argument, a ParameterizedType otherwise, and a TypeVariable bounded by either when the argument is a GenericPlaceholder.
    static Type
    toType(Argument<?> argument, boolean typeVariables)
    Renders an argument as a Type, choosing what an unresolved type variable becomes.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • of

      public static Argument<?> of(AnnotatedType annotatedType)
      Converts an annotated type to an Argument: the raw type, the type arguments recursively, and the type-use annotations of each level.
      Parameters:
      annotatedType - The annotated type
      Returns:
      The argument
    • of

      public static Argument<?> of(@Nullable String name, AnnotatedType annotatedType)
      Converts an annotated type to a named Argument.
      Parameters:
      name - The name of the argument, can be null
      annotatedType - The annotated type
      Returns:
      The argument
    • of

      public static Argument<?> of(Type type)
      Converts a type to an Argument. Unlike Argument.of(Type), a type variable is converted to a GenericPlaceholder of its first bound, a wildcard to its upper bound and a generic array to the array of its raw component.
      Parameters:
      type - The type
      Returns:
      The argument
    • of

      public static Argument<?> of(@Nullable String name, Type type)
      Converts a type to a named Argument.
      Parameters:
      name - The name of the argument, can be null
      type - The type
      Returns:
      The argument
      See Also:
    • of

      public static Argument<?> of(Parameter parameter)
      Converts a parameter to an Argument named after it, whose metadata holds the annotations of the parameter and the type-use annotations of its type.
      Parameters:
      parameter - The parameter
      Returns:
      The argument
    • of

      public static Argument<?> of(Field field)
      Converts a field to an Argument named after it, whose metadata holds the annotations of the field and the type-use annotations of its type.
      Parameters:
      field - The field
      Returns:
      The argument
    • argumentsOf

      public static Argument<?>[] argumentsOf(Executable executable)
      Converts the parameters of a method or constructor to Arguments.
      Parameters:
      executable - The method or constructor
      Returns:
      The arguments, in the order of the parameters
    • returnOf

      public static Argument<?> returnOf(Method method)
      Converts the return type of a method to an Argument whose metadata holds the annotations of the method and the type-use annotations of its return type, as of(Parameter) builds the argument of a parameter from the parameter and its type: a generated executable method of a bean definition reports the return value with the annotations of the method on it, and a constraint declared on a method constrains the value it returns. An annotation written before the return type whose target includes TYPE_USE is on both the method and the type (JLS 9.7.4) and is read once.
      Parameters:
      method - The method
      Returns:
      The argument
    • of

      public static Argument<?> of(Field field, Class<?> context)
      Converts a field to an Argument as the type reading it sees it: a variable the declaring type leaves open and the reading type gives a value to - T of a class Base<T> read through a class Impl extends Base<Book> - is resolved to that value rather than left a GenericPlaceholder, which is what the processors generate for the reading type.
      Parameters:
      field - The field
      context - The type the field is read through, an implementation of its declaring type
      Returns:
      The argument
    • of

      public static Argument<?> of(Parameter parameter, Class<?> context)
      Converts a parameter to an Argument as the type reading it sees it.
      Parameters:
      parameter - The parameter
      context - The type the parameter is read through, an implementation of the type declaring the method or constructor
      Returns:
      The argument
      See Also:
    • argumentsOf

      public static Argument<?>[] argumentsOf(Executable executable, Class<?> context)
      Converts the parameters of a method or constructor to Arguments as the type reading them sees them.
      Parameters:
      executable - The method or constructor
      context - The type the executable is read through, an implementation of its declaring type
      Returns:
      The arguments, in the order of the parameters
      See Also:
    • returnOf

      public static Argument<?> returnOf(Method method, Class<?> context)
      Converts the return type of a method to an Argument as the type invoking it sees it, with the annotations of the method and the type-use annotations of its return type.
      Parameters:
      method - The method
      context - The type the method is invoked on, an implementation of its declaring type
      Returns:
      The argument
      See Also:
    • resolveGenericToArgument

      public static <T> @Nullable Argument<T> resolveGenericToArgument(Class<?> type, Class<T> superType)
      Resolves the type arguments a type gives to one of its super types, as an argument of the super type: for a class A extends AbstractList<@Nullable String>, the argument of Collection is Collection<@Nullable String>. The type-use annotations of every extends and implements clause of the hierarchy are carried.
      Type Parameters:
      T - The super type
      Parameters:
      type - The type, an implementation of the super type
      superType - The super type, a class or an interface
      Returns:
      The argument of the super type, or null when the type does not extend or implement it
    • toType

      public static Type toType(Argument<?> argument)
      Renders an argument as a Type: the raw class when it has no type argument, a ParameterizedType otherwise, and a TypeVariable bounded by either when the argument is a GenericPlaceholder.
      Parameters:
      argument - The argument
      Returns:
      The type
      See Also:
    • toType

      public static Type toType(Argument<?> argument, boolean typeVariables)
      Renders an argument as a Type, choosing what an unresolved type variable becomes.

      An API that describes a declaration - the type of an injection point, of an observed event - wants the variable itself, so that it can tell List<T> from List<String>; an API that compares types by assignability wants the type the variable is bounded by, which is what the erasure of the declaration yields. The generated metadata carries a GenericPlaceholder for both cases, so the caller says which of the two it means.

      Parameters:
      argument - The argument
      typeVariables - Whether a GenericPlaceholder is rendered as a TypeVariable rather than as the type it is bounded by
      Returns:
      The type