Class MethodInfo

java.lang.Object
org.jboss.jandex.MethodInfo
All Implemented Interfaces:
AnnotationTarget, Declaration, Descriptor, GenericSignature

public final class MethodInfo extends Object implements Declaration, Descriptor, GenericSignature
Represents a Java method, constructor, or static initializer.

Jandex makes reasonable attempts to not include implicitly declared (aka mandated) and synthetic parameters in parameters(), parameterTypes(), parameterName(int), parameterType(int) and parametersCount(). However, descriptorParameterTypes() and descriptorParametersCount() may be used to obtain information about all parameters, including mandated and synthetic.

As an exception to the rule above, implicitly declared parameters are always included in case of methods that don't have an explicit parameter list. These are:

  • the implicitly declared valueOf() method in enums;
  • the compact constructor in records.

Thread-Safety

This class is immutable and can be shared between threads without safe publication.
  • Field Details

    • EMPTY_PARAMETER_NAMES

      static final String[] EMPTY_PARAMETER_NAMES
    • methodInternal

      private MethodInternal methodInternal
    • clazz

      private ClassInfo clazz
  • Constructor Details

    • MethodInfo

      MethodInfo()
    • MethodInfo

      MethodInfo(ClassInfo clazz, MethodInternal methodInternal)
    • MethodInfo

      MethodInfo(ClassInfo clazz, byte[] name, byte[][] parameterNames, Type[] parameterTypes, Type returnType, short flags)
    • MethodInfo

      MethodInfo(ClassInfo clazz, byte[] name, byte[][] parameterNames, Type[] parameterTypes, Type returnType, short flags, Type[] typeParameters, Type[] exceptions)
  • Method Details

    • create

      public static MethodInfo create(ClassInfo clazz, String name, Type[] parameterTypes, Type returnType, short flags)
      Construct a new mock Method instance.
      Parameters:
      clazz - the class declaring the field
      name - the name of the field
      parameterTypes - a read only array containing the types of each parameter in parameter order
      returnType - the return value type
      flags - the method attributes
      Returns:
      a mock method
    • create

      public static MethodInfo create(ClassInfo clazz, String name, Type[] parameterTypes, Type returnType, short flags, TypeVariable[] typeParameters, Type[] exceptions)
      Construct a new mock Method instance.
      Parameters:
      clazz - the class declaring the field
      name - the name of the field
      parameterTypes - a read only array containing the types of each parameter in parameter order
      returnType - the return value type
      flags - the method attributes
      typeParameters - the generic type parameters for this method
      exceptions - the exceptions declared as thrown by this method
      Returns:
      a mock method
      Since:
      2.1
    • create

      public static MethodInfo create(ClassInfo clazz, String name, String[] parameterNames, Type[] parameterTypes, Type returnType, short flags, TypeVariable[] typeParameters, Type[] exceptions)
      Construct a new mock Method instance.
      Parameters:
      clazz - the class declaring the field
      name - the name of the field
      parameterNames - the names of the method parameter
      parameterTypes - a read only array containing the types of each parameter in parameter order
      returnType - the return value type
      flags - the method attributes
      typeParameters - the generic type parameters for this method
      exceptions - the exceptions declared as thrown by this method
      Returns:
      a mock method
      Since:
      2.2
    • name

      public final String name()
      Returns the name of this method
      Returns:
      the name of the method
    • parameterName

      public final String parameterName(int i)
      Returns the name of the given parameter.
      Parameters:
      i - the parameter index, zero-based
      Returns:
      the name of the given parameter, or null if not known
    • parameterType

      public final Type parameterType(int i)
      Returns the type of the given parameter.
      Parameters:
      i - the parameter index, zero-based
      Returns:
      the type of the given parameter
    • kind

      public final AnnotationTarget.Kind kind()
      Description copied from interface: AnnotationTarget
      Returns the kind of object this target represents.
      Specified by:
      kind in interface AnnotationTarget
      Returns:
      the target kind.
    • declaringClass

      public final ClassInfo declaringClass()
      Returns the class that declared this method
      Returns:
      the declaring class
    • args

      @Deprecated public final Type[] args()
      Deprecated.
      Returns an array of parameter types in declaration order. Jandex makes reasonable attempts to not include implicitly declared (aka mandated) and synthetic parameters.

      This method performs a defensive array copy per call, and should be avoided. Instead, the parameterTypes() method should be used.

      Returns:
      an array copy contain parameter types
    • copyParameters

      final Type[] copyParameters()
    • parametersCount

      public final int parametersCount()
      Returns the number of parameters this method declares. Jandex makes reasonable attempts to not count implicitly declared (aka mandated) and synthetic parameters.
      Returns:
      the number of parameters this method declares
    • parameterTypes

      public final List<Type> parameterTypes()
      Returns a list of types of parameters declared on this method, in declaration order. Jandex makes reasonable attempts to not include implicitly declared (aka mandated) and synthetic parameters. Positions of types in this list may be used to retrieve a name using parameterName(int) or look for annotations.
      Returns:
      immutable list of parameter types of this method, never null
    • parameters

      public final List<MethodParameterInfo> parameters()
      Returns a list of parameters declared on this method, in declaration order. Jandex makes reasonable attempts to not include implicitly declared (aka mandated) and synthetic parameters.
      Returns:
      immutable list of parameter types of this method, never null
    • descriptorParametersCount

      public final int descriptorParametersCount()
      Returns the number of all parameters present on this method, based on the method descriptor. This always includes implicitly declared (aka mandated) and synthetic parameters.
      Returns:
      the number of all parameters present on this method
    • descriptorParameterTypes

      public final List<Type> descriptorParameterTypes()
      Returns a list of types of all parameters present on this method, based on the method descriptor. This always includes implicitly declared (aka mandated) and synthetic parameters. These types are never annotated and their position in the list cannot be used to retrieve a name using parameterName(int) or look for annotations.
    • returnType

      public final Type returnType()
      Returns this method's return parameter type. If this method has a void return, a special void type is returned. This method will never return null.
      Returns:
      the type of this method's return value
    • receiverType

      public final Type receiverType()
      Returns the receiver type of this method (a declaration of the "this" reference), if specified. This is used to convey annotations on the "this" instance.
      Returns:
      the receiver type of this method
    • exceptions

      public final List<Type> exceptions()
      Returns the list of throwable classes declared to be thrown by this method. This method may return an empty list, but never null.
      Returns:
      immutable list of throwable classes thrown by this method
    • copyExceptions

      final Type[] copyExceptions()
    • typeParameters

      public final List<TypeVariable> typeParameters()
      Returns the generic type parameters defined by this method. This list will contain resolved type variables which may reference other type parameters, including those declared by the enclosing class of this method.
      Returns:
      immutable list of generic type parameters for this method, or an empty list if none
    • hasAnnotation

      public final boolean hasAnnotation(DotName name)
      Returns whether an annotation instance with given name is declared on this method, its parameters or any type within its signature.
      Specified by:
      hasAnnotation in interface AnnotationTarget
      Parameters:
      name - name of the annotation type to look for, must not be null
      Returns:
      true if the annotation is present, false otherwise
      See Also:
    • annotation

      public final AnnotationInstance annotation(DotName name)
      Returns the annotation instance with given name declared on this method, any of its parameters or any type within its signature. The target() method of the returned annotation instance may be used to determine the exact location of the annotation instance.

      The following is a non-exhaustive list of examples of annotations returned by this method:

       @MyMethodAnnotation
       public void foo() {...}
      
       public void foo(@MyParamAnnotation int param) {...}
      
       public void foo(List<@MyTypeAnnotation String> list) {...}
      
       public <@MyTypeAnnotation T> void foo(T t) {...}
       

      In case an annotation with given name occurs more than once, the result of this method is not deterministic. For such situations, annotations(DotName) is preferable.

      Specified by:
      annotation in interface AnnotationTarget
      Parameters:
      name - name of the annotation type to look for, must not be null
      Returns:
      the annotation instance, or null if not found
      Since:
      3.0
      See Also:
    • annotations

      public final List<AnnotationInstance> annotations(DotName name)
      Returns the annotation instances with given name declared on this method, any of its parameters or any type within its signature. The target() method of the returned annotation instances may be used to determine the exact location of the respective annotation instance.

      The following is a non-exhaustive list of examples of annotations returned by this method:

       @MyMethodAnnotation
       public void foo() {...}
      
       public void foo(@MyParamAnnotation int param) {...}
      
       public void foo(List<@MyTypeAnnotation String> list) {...}
      
       public <@MyTypeAnnotation T> void foo(T t) {...}
       
      Specified by:
      annotations in interface AnnotationTarget
      Parameters:
      name - name of the annotation type, must not be null
      Returns:
      immutable list of annotation instances, never null
      See Also:
    • annotationsWithRepeatable

      public final List<AnnotationInstance> annotationsWithRepeatable(DotName name, IndexView index)
      Returns the annotation instances with given name declared on this method, any of its parameters or any type within its signature. The target() method of the returned annotation instances may be used to determine the exact location of the respective annotation instance.

      If the specified annotation is repeatable, the result also contains all values from the container annotation instance. In this case, the AnnotationInstance.target() returns the target of the container annotation instance.

      Specified by:
      annotationsWithRepeatable in interface AnnotationTarget
      Parameters:
      name - name of the annotation type, must not be null
      index - index used to obtain the annotation type, must not be null
      Returns:
      immutable list of annotation instances, never null
      Throws:
      IllegalArgumentException - if the index is null, if the index does not contain the annotation type or if name does not identify an annotation type
      See Also:
    • annotations

      public final List<AnnotationInstance> annotations()
      Returns the annotation instances declared on this method, any of its parameters or any type within its signature. The target() method of the returned annotation instances may be used to determine the exact location of the respective annotation instance.

      The following is a non-exhaustive list of examples of annotations returned by this method:

       @MyMethodAnnotation
       public void foo() {...}
      
       public void foo(@MyParamAnnotation int param) {...}
      
       public void foo(List<@MyTypeAnnotation String> list) {...}
      
       public <@AnotherTypeAnnotation T> void foo(T t) {...}
       
      Specified by:
      annotations in interface AnnotationTarget
      Returns:
      immutable list of annotation instances, never null
    • hasDeclaredAnnotation

      public boolean hasDeclaredAnnotation(DotName name)
      Returns whether an annotation instance with given name is declared on this method.

      Unlike hasAnnotation(DotName), this method ignores annotations declared on the method parameters and types within the method signature.

      Specified by:
      hasDeclaredAnnotation in interface AnnotationTarget
      Parameters:
      name - name of the annotation type to look for, must not be null
      Returns:
      true if the annotation is present, false otherwise
      Since:
      3.0
      See Also:
    • declaredAnnotation

      public AnnotationInstance declaredAnnotation(DotName name)
      Returns the annotation instance with given name declared on this method.

      Unlike annotation(DotName), this method doesn't return annotations declared on the method parameters and types within the method signature.

      Specified by:
      declaredAnnotation in interface AnnotationTarget
      Parameters:
      name - name of the annotation type to look for, must not be null
      Returns:
      the annotation instance, or null if not found
      Since:
      3.0
      See Also:
    • declaredAnnotationsWithRepeatable

      public List<AnnotationInstance> declaredAnnotationsWithRepeatable(DotName name, IndexView index)
      Returns the annotation instances with given name declared on this method.

      If the specified annotation is repeatable, the result also contains all values from the container annotation instance. In this case, the AnnotationInstance.target() returns the target of the container annotation instance.

      Unlike annotationsWithRepeatable(DotName, IndexView), this method doesn't return annotations declared on the method parameters and types within the method signature.

      Specified by:
      declaredAnnotationsWithRepeatable in interface AnnotationTarget
      Parameters:
      name - name of the annotation type, must not be null
      index - index used to obtain the annotation type, must not be null
      Returns:
      immutable list of annotation instances, never null
      Throws:
      IllegalArgumentException - if the index is null, if the index does not contain the annotation type or if name does not identify an annotation type
      Since:
      3.0
      See Also:
    • declaredAnnotations

      public List<AnnotationInstance> declaredAnnotations()
      Returns the annotation instances declared on this method.

      Unlike annotations(), this method doesn't return annotations declared on the method parameters and types within the method signature.

      Specified by:
      declaredAnnotations in interface AnnotationTarget
      Returns:
      immutable list of annotation instances, never null
      Since:
      3.0
      See Also:
    • defaultValue

      public AnnotationValue defaultValue()
      Returns the default annotation value if this method represents an annotation member with a default value. Otherwise null is returned
      Returns:
      default annotation value if available, otherwise null
      Since:
      2.1
    • flags

      public final short flags()
      Returns the access fields of this method. Modifier can be used on this value.
      Returns:
      the access flags of this method
    • isSynthetic

      public boolean isSynthetic()
      Returns:
      true if this method is a synthetic method
    • isBridge

      public boolean isBridge()
      Returns:
      true if this method is a bridge method as defined by the JLS, false otherwise
    • isConstructor

      public boolean isConstructor()
      Returns:
      true if this method is a constructor
    • isStaticInitializer

      public boolean isStaticInitializer()
      Returns:
      true if this method is a static initializer
    • isDefault

      public boolean isDefault()
      A default method is a public non-abstract non-static method declared in an interface.
      Returns:
      true if this method is a default interface method, false otherwise
    • requiresGenericSignature

      public boolean requiresGenericSignature()
      Returns whether this method must have a generic signature. That is, whether the Java compiler when compiling this method had to emit the Signature bytecode attribute.
      Specified by:
      requiresGenericSignature in interface GenericSignature
      Returns:
      whether this method must have a generic signature
    • genericSignature

      public String genericSignature(Function<String,Type> typeVariableSubstitution)
      Returns a generic signature of this method, possibly without any generic-related information. That is, produces a correct generic signature even if this method is not generic and does not use any type variables.

      Signatures of type variables are substituted for signatures of types provided by the substitution function typeVariableSubstitution. If the substitution function returns null for some type variable identifier, no substitution happens and the type variable signature is used unmodified.

      Note that the return value does not come directly from bytecode. Jandex does not store the signature strings. Instead, the return value is reconstructed from the Jandex object model.

      Specified by:
      genericSignature in interface GenericSignature
      Parameters:
      typeVariableSubstitution - a substitution function from type variable identifiers to types
      Returns:
      a generic signature of this method with type variables substituted, never null
    • descriptor

      public String descriptor(Function<String,Type> typeVariableSubstitution)
      Returns a bytecode descriptor of this method.

      Descriptors of type variables are substituted for descriptors of types provided by the substitution function typeVariableSubstitution. If the substitution function returns null for some type variable identifier, or if it returns the type variable itself, no substitution happens and the type variable descriptor is used unmodified.

      Note that the return value does not come directly from bytecode. Jandex does not store the descriptor strings. Instead, the return value is reconstructed from the Jandex object model.

      Specified by:
      descriptor in interface Descriptor
      Parameters:
      typeVariableSubstitution - a substitution function from type variable identifiers to types
      Returns:
      the bytecode descriptor of this method
    • toString

      public String toString()
      Returns a string representation describing this method. It is similar although not necessarily identical to a Java source code declaration of this method.
      Overrides:
      toString in class Object
      Returns:
      a string representation of this method
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • asClass

      public final ClassInfo asClass()
      Description copied from interface: AnnotationTarget
      Casts and returns this target as a ClassInfo if it is of kind CLASS
      Specified by:
      asClass in interface AnnotationTarget
      Returns:
      this instance cast to a class
    • asField

      public final FieldInfo asField()
      Description copied from interface: AnnotationTarget
      Casts and returns this target as a FieldInfo if it is of kind FIELD
      Specified by:
      asField in interface AnnotationTarget
      Returns:
      this instance cast to a field
    • asMethod

      public final MethodInfo asMethod()
      Description copied from interface: AnnotationTarget
      Casts and returns this target as a MethodInfo if it is of kind METHOD
      Specified by:
      asMethod in interface AnnotationTarget
      Returns:
      this instance cast to a method
    • asMethodParameter

      public final MethodParameterInfo asMethodParameter()
      Description copied from interface: AnnotationTarget
      Casts and returns this target as a MethodParameterInfo if it is of kind METHOD_PARAMETER
      Specified by:
      asMethodParameter in interface AnnotationTarget
      Returns:
      this instance cast to a method parameter
    • asType

      public final TypeTarget asType()
      Description copied from interface: AnnotationTarget
      Casts and returns this target as a TypeTarget if it is of kind TYPE
      Specified by:
      asType in interface AnnotationTarget
      Returns:
      this instance cast to a type target
    • asRecordComponent

      public final RecordComponentInfo asRecordComponent()
      Description copied from interface: AnnotationTarget
      Casts and returns this target as a RecordComponentInfo if it is of kind RECORD_COMPONENT
      Specified by:
      asRecordComponent in interface AnnotationTarget
      Returns:
      this instance cast to a record component
    • methodInternal

      final MethodInternal methodInternal()
    • setMethodInternal

      final void setMethodInternal(MethodInternal methodInternal)
    • setClassInfo

      final void setClassInfo(ClassInfo clazz)
    • typeParameterArray

      final Type[] typeParameterArray()
    • setTypeParameters

      void setTypeParameters(Type[] typeParameters)
    • setParameters

      void setParameters(Type[] parameters)
    • setReturnType

      void setReturnType(Type returnType)
    • setExceptions

      void setExceptions(Type[] exceptions)
    • setReceiverType

      void setReceiverType(Type receiverType)
    • setAnnotations

      void setAnnotations(List<AnnotationInstance> annotations)
    • setDefaultValue

      void setDefaultValue(AnnotationValue defaultValue)