Class MethodInfo

  • All Implemented Interfaces:
    AnnotationTarget

    public final class MethodInfo
    extends java.lang.Object
    implements AnnotationTarget
    Represents a Java method, constructor, or static initializer.

    Thread-Safety

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

      • EMPTY_PARAMETER_NAMES

        static final java.lang.String[] EMPTY_PARAMETER_NAMES
    • Constructor Detail

      • MethodInfo

        MethodInfo()
      • MethodInfo

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

        MethodInfo​(ClassInfo clazz,
                   byte[] name,
                   byte[][] parameterNames,
                   Type[] parameters,
                   Type returnType,
                   short flags,
                   Type[] typeParameters,
                   Type[] exceptions)
    • Method Detail

      • create

        public static MethodInfo create​(ClassInfo clazz,
                                        java.lang.String name,
                                        Type[] args,
                                        Type returnType,
                                        short flags)
        Construct a new mock Method instance.
        Parameters:
        clazz - the class declaring the field
        name - the name of the field
        args - 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,
                                        java.lang.String name,
                                        Type[] args,
                                        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
        args - 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,
                                        java.lang.String name,
                                        java.lang.String[] parameterNames,
                                        Type[] args,
                                        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
        args - 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 java.lang.String name()
        Returns the name of this method
        Returns:
        the name of the method
      • parameterName

        public final java.lang.String parameterName​(int i)
        Returns the name of the given parameter.
        Parameters:
        i - the parameter index
        Returns:
        the name of the given parameter, or null.
      • 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 containing parameter types in parameter order. This method performs a defensive array copy per call, and should be avoided. Instead the parameters() method should be used.
        Returns:
        an array copy contain parameter types
      • copyParameters

        final Type[] copyParameters()
      • parameters

        public final java.util.List<Type> parameters()
        Returns a list containing the types of all parameters declared on this method, in parameter order. This method may return an empty list, but never null.
        Returns:
        all parameter types on this method
      • 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 java.util.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:
        the list of throwable classes thrown by this method
      • copyExceptions

        final Type[] copyExceptions()
      • typeParameters

        public final java.util.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:
        the list of generic type parameters for this method, or an empty list if none
      • annotations

        public final java.util.List<AnnotationInstance> annotations()
        Returns the annotation instances declared on this method. This includes annotations which are defined against method parameters, as well as type annotations declared on any usage within the method signature. The target() 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> list) {...}
        
             public <@AnotherTypeAnnotation T> void foo(T t) {...}
         
        Returns:
        the annotation instances declared on this method or its parameters, or an empty list if none
      • annotation

        public final AnnotationInstance annotation​(DotName name)
        Retrieves an annotation instance declared on this method, it parameters, or any type within the signature of the method, by the name of the annotation. If an annotation by that name is not present, null will be returned.

        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> list) {...}
        
             public <@AnotherTypeAnnotation T> void foo(T t) {...}
         
        Parameters:
        name - the name of the annotation to locate within the method
        Returns:
        the annotation if found, otherwise, null
      • annotationsWithRepeatable

        public final java.util.List<AnnotationInstance> annotationsWithRepeatable​(DotName name,
                                                                                  IndexView index)
        Retrieves annotations declared on this method, by the name of the annotation. This includes annotations which are defined against method parameters, as well as type annotations declared on any usage within the method signature. The target() of the returned annotation instances may be used to determine the exact location of the respective annotation instance. If the specified annotation is repeatable (JLS 9.6), the result also contains all values from the container annotation instances. In this case, the AnnotationInstance.target() returns the target of the container annotation instance.
        Parameters:
        name - the name of the annotation
        index - the index used to obtain the annotation class
        Returns:
        the annotation instances declared on this method or its parameters, or an empty list if none
        Throws:
        java.lang.IllegalArgumentException - If the index does not contain the annotation definition or if it does not represent an annotation type
      • annotations

        public final java.util.List<AnnotationInstance> annotations​(DotName name)
        Retrieves annotations declared on this method, by the name of the annotation. This includes annotations which are defined against method parameters, as well as type annotations declared on any usage within the method signature. The target() of the returned annotation instances may be used to determine the exact location of the respective annotation instance.
        Parameters:
        name -
        Returns:
        the annotation instances declared on this method or its parameters, or an empty list if none
      • hasAnnotation

        public final boolean hasAnnotation​(DotName name)
        Returns whether or not the annotation instance with the given name occurs on this method, its parameters or its signature
        Parameters:
        name - the name of the annotation to look for
        Returns:
        true if the annotation is present, false otherwise
        See Also:
        annotations(), annotation(DotName)
      • 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 final boolean isSynthetic()
        Returns:
        true if this method is a synthetic method
      • toString

        public java.lang.String toString()
        Returns a string representation describing this field. It is similar although not necessarily equivalent to a Java source code expression representing this field.
        Overrides:
        toString in class java.lang.Object
        Returns:
        a string representation for this field
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.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
      • 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
      • 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)