Class MethodInfo

java.lang.Object
org.apache.juneau.reflect.ExecutableInfo
org.apache.juneau.reflect.MethodInfo
All Implemented Interfaces:
Comparable<MethodInfo>

public final class MethodInfo extends ExecutableInfo implements Comparable<MethodInfo>
Lightweight utility class for introspecting information about a method.
See Also:
  • Constructor Details

    • MethodInfo

      protected MethodInfo(ClassInfo declaringClass, Method m)
      Constructor.
      Parameters:
      declaringClass - The class that declares this method.
      m - The method being wrapped.
  • Method Details

    • of

      public static MethodInfo of(ClassInfo declaringClass, Method m)
      Convenience method for instantiating a MethodInfo;
      Parameters:
      declaringClass - The class that declares this method.
      m - The method being wrapped.
      Returns:
      A new MethodInfo object, or null if the method was null;
    • of

      public static MethodInfo of(Class<?> declaringClass, Method m)
      Convenience method for instantiating a MethodInfo;
      Parameters:
      declaringClass - The class that declares this method.
      m - The method being wrapped.
      Returns:
      A new MethodInfo object, or null if the method was null;
    • of

      public static MethodInfo of(Method m)
      Convenience method for instantiating a MethodInfo;
      Parameters:
      m - The method being wrapped.
      Returns:
      A new MethodInfo object, or null if the method was null;
    • inner

      public Method inner()
      Returns the wrapped method.
      Returns:
      The wrapped method.
    • canAccept

      public boolean canAccept(Object... args)
      Returns true if this constructor can accept the specified arguments in the specified order.
      Parameters:
      args - The arguments to check.
      Returns:
      true if this constructor can accept the specified arguments in the specified order.
    • canAcceptFuzzy

      public int canAcceptFuzzy(Object... args)
      Returns the number of matching arguments for this method.
      Parameters:
      args - The arguments to check.
      Returns:
      the number of matching arguments for this method.
    • forEachMatching

      Performs an action on all matching declared methods with the same name and arguments on all superclasses and interfaces.

      Methods are accessed from child-to-parent order.

      Parameters:
      filter - A predicate to apply to the entries to determine if action should be performed. Can be null.
      action - An action to perform on the entry.
      Returns:
      This object.
    • forEachMatchingParentFirst

      Performs an action on all matching declared methods with the same name and arguments on all superclasses and interfaces.

      Methods are accessed from parent-to-child order.

      Parameters:
      filter - A predicate to apply to the entries to determine if action should be performed. Can be null.
      action - An action to perform on the entry.
      Returns:
      This object.
    • getAnnotation

      public final <A extends Annotation> A getAnnotation(Class<A> type)
      Finds the annotation of the specified type defined on this method.

      If this is a method and the annotation cannot be found on the immediate method, searches methods with the same signature on the parent classes or interfaces.
      The search is performed in child-to-parent order.

      Type Parameters:
      A - The annotation type to look for.
      Parameters:
      type - The annotation to look for.
      Returns:
      The annotation if found, or null if not.
    • getAnnotation

      public final <A extends Annotation> A getAnnotation(AnnotationProvider annotationProvider, Class<A> type)
      Finds the annotation of the specified type defined on this method.

      Searches all methods with the same signature on the parent classes or interfaces and the return type on the method.

      Type Parameters:
      A - The annotation type to look for.
      Parameters:
      annotationProvider - The annotation provider.
      type - The annotation to look for.
      Returns:
      The first annotation found, or null if it doesn't exist.
    • hasAnnotation

      public final <A extends Annotation> boolean hasAnnotation(Class<A> type)
      Returns true if the specified annotation is present on this method.
      Type Parameters:
      A - The annotation type to look for.
      Parameters:
      type - The annotation to look for.
      Returns:
      true if the specified annotation is present on this method.
    • hasAnnotation

      public final <A extends Annotation> boolean hasAnnotation(AnnotationProvider annotationProvider, Class<A> type)
      Returns true if the specified annotation is present on this method.
      Type Parameters:
      A - The annotation type to look for.
      Parameters:
      annotationProvider - The annotation provider.
      type - The annotation to look for.
      Returns:
      true if the specified annotation is present on this method.
    • hasNoAnnotation

      public final <A extends Annotation> boolean hasNoAnnotation(AnnotationProvider annotationProvider, Class<A> type)
      Returns true if the specified annotation is not present on this method.
      Type Parameters:
      A - The annotation type to look for.
      Parameters:
      annotationProvider - The annotation provider.
      type - The annotation to look for.
      Returns:
      true if the specified annotation is not present on this method.
    • hasNoAnnotation

      public final <A extends Annotation> boolean hasNoAnnotation(Class<A> type)
      Returns true if the specified annotation is not present on this method.
      Type Parameters:
      A - The annotation type to look for.
      Parameters:
      type - The annotation to look for.
      Returns:
      true if the specified annotation is not present on this method.
    • hasAnyAnnotations

      @SafeVarargs public final boolean hasAnyAnnotations(Class<? extends Annotation>... types)
      Returns true if at least one of the specified annotation is present on this method.
      Parameters:
      types - The annotation to look for.
      Returns:
      true if at least one of the specified annotation is present on this method.
    • forEachAnnotation

      public <A extends Annotation> MethodInfo forEachAnnotation(Class<A> type, Predicate<A> filter, Consumer<A> action)
      Performs an action on all matching annotations defined on this method.

      Searches all methods with the same signature on the parent classes or interfaces and the return type on the method.
      Results are parent-to-child ordered.

      Type Parameters:
      A - The annotation type to look for.
      Parameters:
      type - The annotation to look for.
      filter - A predicate to apply to the entries to determine if action should be performed. Can be null.
      action - An action to perform on the entry.
      Returns:
      This object.
    • forEachAnnotation

      public <A extends Annotation> MethodInfo forEachAnnotation(AnnotationProvider annotationProvider, Class<A> type, Predicate<A> filter, Consumer<A> action)
      Performs an action on all matching annotations defined on this method.

      Searches all methods with the same signature on the parent classes or interfaces and the return type on the method.
      Results are parent-to-child ordered.

      Type Parameters:
      A - The annotation type to look for.
      Parameters:
      annotationProvider - The annotation provider.
      type - The annotation type.
      filter - A predicate to apply to the entries to determine if action should be performed. Can be null.
      action - An action to perform on the entry.
      Returns:
      This object.
    • getAnyAnnotation

      @SafeVarargs public final Annotation getAnyAnnotation(Class<? extends Annotation>... types)
      Returns the first annotation in the specified list on this method.
      Parameters:
      types - The annotations to look for.
      Returns:
      The first matching annotation.
    • getAnnotationList

      Constructs an AnnotationList of all annotations found on this method.

      Annotations are appended in the following orders:

      1. On the package of this class.
      2. On interfaces ordered parent-to-child.
      3. On parent classes ordered parent-to-child.
      4. On this class.
      5. On this method and matching methods ordered parent-to-child.
      Returns:
      A new AnnotationList object on every call.
    • getAnnotationList

      Constructs an AnnotationList of all matching annotations found on this method.

      Annotations are appended in the following orders:

      1. On the package of this class.
      2. On interfaces ordered parent-to-child.
      3. On parent classes ordered parent-to-child.
      4. On this class.
      5. On this method and matching methods ordered parent-to-child.
      Parameters:
      filter - A predicate to apply to the entries to determine if value should be added. Can be null.
      Returns:
      A new AnnotationList object on every call.
    • getAnnotationListMethodOnly

      Same as getAnnotationList(Predicate) except only returns annotations defined on methods.
      Parameters:
      filter - A predicate to apply to the entries to determine if value should be added. Can be null.
      Returns:
      A new AnnotationList object on every call.
    • forEachAnnotationInfo

      Perform an action on all matching annotations on this method.
      Parameters:
      filter - A predicate to apply to the entries to determine if action should be performed. Can be null.
      action - An action to perform on the entry.
      Returns:
      This object.
    • getReturnType

      Returns the generic return type of this method as a ClassInfo object.
      Returns:
      The generic return type of this method.
    • hasReturnType

      public boolean hasReturnType(Class<?> c)
      Returns true if this method has this return type.
      Parameters:
      c - The return type to test for.
      Returns:
      true if this method has this return type.
    • hasReturnType

      public boolean hasReturnType(ClassInfo ci)
      Returns true if this method has this return type.
      Parameters:
      ci - The return type to test for.
      Returns:
      true if this method has this return type.
    • hasReturnTypeParent

      public boolean hasReturnTypeParent(Class<?> c)
      Returns true if this method has this parent return type.
      Parameters:
      c - The return type to test for.
      Returns:
      true if this method has this parent return type.
    • hasReturnTypeParent

      public boolean hasReturnTypeParent(ClassInfo ci)
      Returns true if this method has this parent return type.
      Parameters:
      ci - The return type to test for.
      Returns:
      true if this method has this parent return type.
    • matches

      public boolean matches(Predicate<MethodInfo> test)
      Returns true if this object passes the specified predicate test.
      Parameters:
      test - The test to perform.
      Returns:
      true if this object passes the specified predicate test.
    • accept

      Performs an action on this object if the specified predicate test passes.
      Parameters:
      test - A test to apply to determine if action should be executed. Can be null.
      action - An action to perform on this object.
      Returns:
      This object.
    • invoke

      public <T> T invoke(Object obj, Object... args) throws ExecutableException
      Shortcut for calling the invoke method on the underlying method.
      Type Parameters:
      T - The method return type.
      Parameters:
      obj - the object the underlying method is invoked from.
      args - the arguments used for the method call
      Returns:
      The object returned from the method.
      Throws:
      ExecutableException - Exception occurred on invoked constructor/method/field.
    • invokeFuzzy

      public Object invokeFuzzy(Object pojo, Object... args) throws ExecutableException
      Invokes the specified method using fuzzy-arg matching.

      Arguments will be matched to the parameters based on the parameter types.
      Arguments can be in any order.
      Extra arguments will be ignored.
      Missing arguments will be left null.

      Note that this only works for methods that have distinguishable argument types.
      It's not going to work on methods with generic argument types like Object

      Parameters:
      pojo - The POJO the method is being called on.
      Can be null for static methods.
      args - The arguments to pass to the method.
      Returns:
      The results of the method invocation.
      Throws:
      ExecutableException - Exception occurred on invoked constructor/method/field.
    • getSignature

      public String getSignature()
      Returns the signature of this method.

      For no-arg methods, the signature will be a simple string such as "toString". For methods with one or more args, the arguments will be fully-qualified class names (e.g. "append(java.util.StringBuilder,boolean)")

      Returns:
      The methods signature.
    • getPropertyName

      Returns the bean property name if this is a getter or setter.
      Returns:
      The bean property name, or null if this isn't a getter or setter.
    • argsOnlyOfType

      public boolean argsOnlyOfType(Class<?>... args)
      Returns true if the parameters on the method only consist of the types specified in the list.
      Example:

      // Example method: public void foo(String bar, Integer baz); argsOnlyOfType(fooMethod, String.class, Integer.class); // True. argsOnlyOfType(fooMethod, String.class, Integer.class, Map.class); // True. argsOnlyOfType(fooMethod, String.class); // False.

      Parameters:
      args - The valid class types (exact) for the arguments.
      Returns:
      true if the method parameters only consist of the types specified in the list.
    • hasAllArgs

      public boolean hasAllArgs(Class<?>... requiredParams)
      Returns true if this method has at least the specified parameters.

      Method may or may not have additional parameters besides those specified.

      Parameters:
      requiredParams - The parameter types to check for.
      Returns:
      true if this method has at least the specified parameters.
    • hasArg

      public boolean hasArg(Class<?> requiredParam)
      Returns true if this method has the specified parameter.

      Method may or may not have additional parameters besides the one specified.

      Parameters:
      requiredParam - The parameter type to check for.
      Returns:
      true if this method has at least the specified parameter.
    • isBridge

      public boolean isBridge()
      Returns true if this method is a bridge method.
      Returns:
      true if this method is a bridge method.
    • getName

      public String getName()
      Returns the name of this method.
      Returns:
      The name of this method
    • compareTo

      public int compareTo(MethodInfo o)
      Specified by:
      compareTo in interface Comparable<MethodInfo>
    • accessible

      Description copied from class: ExecutableInfo
      Attempts to call x.setAccessible(true) and quietly ignores security exceptions.
      Overrides:
      accessible in class ExecutableInfo
      Returns:
      This object.