Class ParameterInfo

java.lang.Object
org.apache.juneau.commons.reflect.ElementInfo
org.apache.juneau.commons.reflect.ParameterInfo
All Implemented Interfaces:
Annotatable

public class ParameterInfo extends ElementInfo implements Annotatable
Lightweight utility class for introspecting information about a method or constructor parameter.

This class provides a convenient wrapper around Parameter that extends the standard Java reflection API with additional functionality for parameter introspection, annotation handling, and name resolution. It supports resolving parameter names from bytecode (when compiled with -parameters) or from @Name annotations.

Features:
  • Parameter introspection - access parameter metadata, type, annotations
  • Name resolution - resolve parameter names from bytecode or annotations
  • Qualifier support - extract qualifier names from annotations
  • Hierarchy traversal - find matching parameters in parent methods/constructors
  • Annotation support - get annotations declared on the parameter
Use Cases:
  • Introspecting parameter metadata for code generation or analysis
  • Resolving parameter names for dependency injection frameworks
  • Finding annotations on parameters
  • Working with parameter types and qualifiers
  • Building frameworks that need to analyze method/constructor signatures
Usage:

// Get ParameterInfo from a method MethodInfo mi = ...; ParameterInfo param = mi.getParameters().get(0); // Get parameter type ClassInfo type = param.getParameterType(); // Get resolved name (from bytecode or @Name annotation) String name = param.getResolvedName(); // Get annotations List<AnnotationInfo<MyAnnotation>> annotations = param.getAnnotations(MyAnnotation.class).toList();

Parameter Name Resolution:

Parameter names are resolved in the following order:

  1. @Name annotation value (if present)
  2. Bytecode parameter names (if compiled with -parameters flag)
  3. arg0, arg1, etc. (fallback if names unavailable)
See Also:
  • Constructor Details

    • ParameterInfo

      protected ParameterInfo(ExecutableInfo executable, Parameter inner, int index, ClassInfo type)
      Constructor.

      Creates a new ParameterInfo wrapper for the specified parameter. This constructor is protected and should not be called directly. ParameterInfo instances are typically obtained from ExecutableInfo.getParameters() or of(Parameter).

      Parameters:
      executable - The ExecutableInfo (MethodInfo or ConstructorInfo) that contains this parameter.
      inner - The parameter being wrapped.
      index - The zero-based index of this parameter in the method/constructor signature.
      type - The ClassInfo representing the parameter type.
  • Method Details

    • of

      public static ParameterInfo of(Parameter inner)
      Creates a ParameterInfo wrapper for the specified parameter.

      This convenience method automatically determines the declaring executable from the parameter and finds the matching ParameterInfo.

      Example:

      Parameter p = ...; ParameterInfo pi = ParameterInfo.of(p);

      Parameters:
      inner - The parameter being wrapped. Must not be null.
      Returns:
      A ParameterInfo object wrapping the parameter.
      Throws:
      IllegalArgumentException - If the parameter is null or cannot be found in its declaring executable.
    • canAccept

      public boolean canAccept(Object value)
      Returns true if this parameter can accept the specified value.
      Parameters:
      value - The value to check.
      Returns:
      true if this parameter can accept the specified value.
    • getAnnotatableType

      Description copied from interface: Annotatable
      Returns the type of this annotatable object.
      Specified by:
      getAnnotatableType in interface Annotatable
      Returns:
      The type of annotatable object this represents.
    • getAnnotatedType

      Returns an AnnotatedType object that represents the use of a type to specify the type of this parameter.

      Same as calling Parameter.getAnnotatedType().

      Example:

      // Get annotated type: void method(@NotNull String value) ParameterInfo pi = ...; AnnotatedType aType = pi.getAnnotatedType(); // Check for @NotNull on the type

      Returns:
      An AnnotatedType object representing the type of this parameter.
      See Also:
    • getAnnotations

      Returns all annotations declared on this parameter.

      Returns annotations directly declared on this parameter, wrapped as AnnotationInfo objects.

      Note on Repeatable Annotations: Repeatable annotations (those marked with @Repeatable) are automatically expanded into their individual annotation instances. For example, if a parameter has multiple @Bean annotations, this method returns each @Bean annotation separately, rather than the container annotation.

      Returns:
      An unmodifiable list of annotations on this parameter, never null.
      Repeatable annotations are expanded into individual instances.
    • getAnnotations

      public <A extends Annotation> Stream<AnnotationInfo<A>> getAnnotations(Class<A> type)
      Returns a stream of annotation infos of the specified type declared on this parameter.
      Type Parameters:
      A - The annotation type.
      Parameters:
      type - The annotation type.
      Returns:
      A stream of annotation infos, never null.
    • getConstructor

      Returns the constructor that this parameter belongs to.
      Returns:
      The constructor that this parameter belongs to, or null if it belongs to a method.
    • getDeclaringExecutable

      Returns the ExecutableInfo which declares this parameter.

      Same as calling Parameter.getDeclaringExecutable() but returns ExecutableInfo instead.

      Example:

      // Get the method or constructor that declares this parameter ParameterInfo pi = ...; ExecutableInfo executable = pi.getDeclaringExecutable(); if (executable.isConstructor()) { ConstructorInfo ci = (ConstructorInfo)executable; }

      Returns:
      The ExecutableInfo declaring this parameter.
      See Also:
    • getIndex

      public int getIndex()
      Returns the index position of this parameter.
      Returns:
      The index position of this parameter.
    • getLabel

      public String getLabel()
      Description copied from interface: Annotatable
      Returns a human-readable label for this annotatable element.

      The label format depends on the type of annotatable:

      • CLASS_TYPE - Simple class name (e.g., "MyClass")
      • METHOD_TYPE - Class and method with parameter types (e.g., "MyClass.myMethod(String,int)")
      • FIELD_TYPE - Class and field name (e.g., "MyClass.myField")
      • CONSTRUCTOR_TYPE - Class and constructor with parameter types (e.g., "MyClass.MyClass(String)")
      • PARAMETER_TYPE - Class, method/constructor, and parameter index (e.g., "MyClass.myMethod[0]")
      • PACKAGE_TYPE - Package name (e.g., "com.example.package")
      Specified by:
      getLabel in interface Annotatable
      Returns:
      The human-readable label for this annotatable element.
    • getMatchingParameters

      Returns this parameter and all matching parameters in parent classes.

      For constructors, searches parent class constructors for parameters with matching name and type, regardless of parameter count or position. This allows finding annotated parameters that may be inherited by child classes even when constructor signatures differ.

      For methods, searches matching methods (same signature) in parent classes/interfaces for parameters at the same index with matching name and type.

      Examples:

      // Constructor with different parameter counts: class A { A(String foo, int bar) {} } class B extends A { B(String foo) {} } // For B's foo parameter, returns: [B.foo, A.foo] ParameterInfo pi = ...; List<ParameterInfo> matching = pi.getMatchingParameters();

      Returns:
      A list of matching parameters including this one, in child-to-parent order.
    • getMethod

      Returns the method that this parameter belongs to.
      Returns:
      The method that this parameter belongs to, or null if it belongs to a constructor.
    • getModifiers

      public int getModifiers()
      Returns the Java language modifiers for the parameter represented by this object, as an integer.

      The Modifier class should be used to decode the modifiers.

      Same as calling Parameter.getModifiers().

      Example:

      // Check if parameter is final ParameterInfo pi = ...; int modifiers = pi.getModifiers(); boolean isFinal = Modifier.isFinal(modifiers);

      Overrides:
      getModifiers in class ElementInfo
      Returns:
      The Java language modifiers for this parameter.
      See Also:
    • getName

      public String getName()
      Returns the name of the parameter.

      Searches for the name in the following order:

      1. @Name annotation value (takes precedence over bytecode parameter names)
      2. Bytecode parameter name (if compiled with -parameters flag)
      3. Matching parameters in parent classes/interfaces (for methods)
      4. Synthetic name like "arg0", "arg1", etc. (fallback)

      This method works with any annotation named "Name" (from any package) that has a String value() method.

      Returns:
      The name of the parameter, never null.
      See Also:
    • getParameterizedType

      Returns a Type object that identifies the parameterized type for this parameter.

      Same as calling Parameter.getParameterizedType().

      Example:

      // Get generic type information for parameter: List<String> values ParameterInfo pi = ...; Type type = pi.getParameterizedType(); if (type instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType)type; // pType.getActualTypeArguments()[0] is String.class }

      Returns:
      A Type object identifying the parameterized type.
      See Also:
    • getParameterType

      Returns the class type of this parameter.
      Returns:
      The class type of this parameter.
    • getResolvedName

      Finds the name of this parameter for bean property mapping.

      Searches for the parameter name in the following order:

      1. @Name annotation value
      2. Bytecode parameter name (if available and not disabled via system property)
      3. Matching parameters in parent classes/interfaces

      This method is used for mapping constructor parameters to bean properties.

      Note: This is different from getResolvedQualifier() which looks for @Named annotations for bean injection purposes.

      Returns:
      The parameter name if found, or null if not available.
      See Also:
    • getResolvedQualifier

      Finds the bean injection qualifier for this parameter.

      Searches for the @Named annotation value to determine which named bean should be injected.

      This method is used by the BeanStore for bean injection.

      Note: This is different from getResolvedName() which looks for @Name annotations for bean property mapping.

      Returns:
      The bean qualifier name if @Named annotation is found, or null if not annotated.
      See Also:
    • hasName

      public boolean hasName()
      Returns true if the parameter has a name.

      This returns true if the parameter has an annotation with the simple name "Name", or if the parameter's name is present in the class file.

      Returns:
      true if the parameter has a name.
    • inner

      public Parameter inner()
      Returns the wrapped Parameter object.
      Returns:
      The wrapped Parameter object.
    • equals

      public boolean equals(Object obj)
      Compares this ParameterInfo with the specified object for equality.

      Two ParameterInfo objects are considered equal if they wrap the same underlying Parameter object. This delegates to the underlying Parameter.equals(Object) method.

      This method makes ParameterInfo suitable for use as keys in hash-based collections such as HashMap and HashSet.

      Overrides:
      equals in class Object
      Parameters:
      obj - The object to compare with.
      Returns:
      true if the objects are equal, false otherwise.
    • hashCode

      public int hashCode()
      Returns a hash code value for this ParameterInfo.

      This delegates to the underlying Parameter.hashCode() method.

      This method makes ParameterInfo suitable for use as keys in hash-based collections such as HashMap and HashSet.

      Overrides:
      hashCode in class Object
      Returns:
      A hash code value for this ParameterInfo.
    • is

      public boolean is(ElementFlag flag)
      Description copied from class: ElementInfo
      Returns true if the specified flag is applicable to this element.

      Subclasses should override this method and call super.is(flag) to handle common modifier flags, then handle their own specific flags.

      Overrides:
      is in class ElementInfo
      Parameters:
      flag - The flag to test for.
      Returns:
      true if the specified flag is applicable to this element.
    • isImplicit

      public boolean isImplicit()
      Returns true if this parameter is implicitly declared in source code.

      Returns true if this parameter is neither explicitly nor implicitly declared in source code.

      Same as calling Parameter.isImplicit().

      Example:

      // Filter out implicit parameters ParameterInfo pi = ...; if (! pi.isImplicit()) { // Process explicit parameter }

      Returns:
      true if this parameter is implicitly declared.
      See Also:
    • isNamePresent

      public boolean isNamePresent()
      Returns true if the parameter has a name according to the .class file.

      Same as calling Parameter.isNamePresent().

      Note: This method is different from hasName() which also checks for the presence of a @Name annotation. This method only checks if the name is present in the bytecode.

      Example:

      // Check if parameter name is in bytecode ParameterInfo pi = ...; if (pi.isNamePresent()) { String name = pi.getName(); }

      Returns:
      true if the parameter has a name in the bytecode.
      See Also:
    • isSynthetic

      public boolean isSynthetic()
      Returns true if this parameter is a synthetic construct as defined by the Java Language Specification.

      Same as calling Parameter.isSynthetic().

      Example:

      // Filter out compiler-generated parameters ParameterInfo pi = ...; if (! pi.isSynthetic()) { // Process real parameter }

      Returns:
      true if this parameter is a synthetic construct.
      See Also:
    • isType

      public boolean isType(Class<?> c)
      Returns true if the parameter type is an exact match for the specified class.
      Parameters:
      c - The type to check.
      Returns:
      true if the parameter type is an exact match for the specified class.
    • isVarArgs

      public boolean isVarArgs()
      Returns true if this parameter represents a variable argument list.

      Same as calling Parameter.isVarArgs().

      Only returns true for the last parameter of a variable arity method.

      Example:

      // Check if this is a varargs parameter ParameterInfo pi = ...; if (pi.isVarArgs()) { // Handle variable arguments }

      Returns:
      true if this parameter represents a variable argument list.
      See Also:
    • toString

      public String toString()
      Overrides:
      toString in class Object