Class ExecutableInfo

Direct Known Subclasses:
ConstructorInfo, MethodInfo

public abstract class ExecutableInfo extends AccessibleInfo
Abstract base class containing common functionality for ConstructorInfo and MethodInfo.

This class provides shared functionality for both constructors and methods, which are both types of Executable in Java. It extends AccessibleInfo to provide AccessibleObject functionality for accessing private methods and constructors.

Features:
  • Parameter introspection - access method/constructor parameters
  • Exception handling - get declared exceptions
  • Annotation support - get annotations declared on the executable
  • Name formatting - get short and fully qualified names
  • Parameter matching - match parameters by type (strict and lenient)
  • Accessibility control - make private executables accessible
Use Cases:
  • Working with both methods and constructors in a unified way
  • Finding methods/constructors that match specific parameter types
  • Introspecting parameter and exception information
  • Building frameworks that need to analyze executable signatures
Usage:

// Get ExecutableInfo (could be MethodInfo or ConstructorInfo) MethodInfo mi = ...; ExecutableInfo ei = mi; // MethodInfo extends ExecutableInfo // Get parameters List<ParameterInfo> params = ei.getParameters(); // Get exceptions List<ClassInfo> exceptions = ei.getExceptions(); // Check parameter matching boolean matches = ei.parameterMatches(String.class, Integer.class);

See Also:
  • Field Details

  • Constructor Details

    • ExecutableInfo

      protected ExecutableInfo(ClassInfo declaringClass, Executable inner)
      Constructor.

      Creates a new ExecutableInfo wrapper for the specified executable (method or constructor). This constructor is protected and should not be called directly. Use the constructors of MethodInfo or ConstructorInfo instead.

      Parameters:
      declaringClass - The ClassInfo for the class that declares this method or constructor.
      inner - The constructor or method that this info represents. Must not be null.
  • Method Details

    • accessible

      Attempts to call x.setAccessible(true) and quietly ignores security exceptions.
      Returns:
      This object.
    • canAccept

      public final boolean canAccept(Object... args)
      Returns true if this executable can accept the specified arguments in the specified order.

      This method checks if the provided arguments are compatible with the executable's parameter types in exact order, using Class.isInstance(Object) for type checking.

      Important: For non-static inner class constructors, the first parameter is the implicit outer class instance (e.g., Outer.this). This method checks against the actual parameters including this implicit parameter.

      Examples:

      // Regular method public void foo(String s, Integer i); methodInfo.canAccept("hello", 42); // true // Non-static inner class constructor class Outer { class Inner { Inner(String s) {} } } // Constructor actually has signature: Inner(Outer this$0, String s) Outer outer = new Outer(); constructorInfo.canAccept("hello"); // false - missing outer instance constructorInfo.canAccept(outer, "hello"); // true

      Parameters:
      args - The arguments to check.
      Returns:
      true if this executable can accept the specified arguments in the specified order.
    • getAnnotatedExceptionTypes

      Returns an array of AnnotatedType objects that represent the use of types to specify the declared exceptions.

      The order of the objects corresponds to the order of the exception types in the executable declaration.

      Same as calling Executable.getAnnotatedExceptionTypes().

      Example:

      // Get annotated exception types from method: void myMethod() throws @NotNull IOException MethodInfo mi = ClassInfo.of(MyClass.class).getMethod("myMethod"); AnnotatedType[] exTypes = mi.getAnnotatedExceptionTypes();

      Returns:
      An array of AnnotatedType objects, or an empty array if the executable declares no exceptions.
      See Also:
    • getAnnotatedParameterTypes

      Returns an array of AnnotatedType objects that represent the use of types to specify formal parameter types.

      The order of the objects corresponds to the order of the formal parameter types in the executable declaration.

      Same as calling Executable.getAnnotatedParameterTypes().

      Example:

      // Get annotated parameter types from method: void myMethod(@NotNull String s, @Range(min=0) int i) MethodInfo mi = ClassInfo.of(MyClass.class).getMethod("myMethod", String.class, int.class); AnnotatedType[] paramTypes = mi.getAnnotatedParameterTypes();

      Returns:
      An array of AnnotatedType objects, or an empty array if the executable has no parameters.
      See Also:
    • getAnnotatedReceiverType

      Returns an AnnotatedType object that represents the use of a type to specify the receiver type of the method/constructor.

      Returns null if this executable object represents a top-level type or static member.

      Same as calling Executable.getAnnotatedReceiverType().

      Example:

      // Get annotated receiver type from method: void myMethod(@MyAnnotation MyClass this) MethodInfo mi = ClassInfo.of(MyClass.class).getMethod("myMethod"); AnnotatedType receiverType = mi.getAnnotatedReceiverType();

      Returns:
      An AnnotatedType object representing the receiver type, or null if not applicable.
      See Also:
    • getDeclaredAnnotations

      Returns the declared annotations on this executable.

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

      Returns:
      The declared annotations on this executable as AnnotationInfo objects.
      Repeatable annotations are expanded into individual instances.
    • getDeclaredAnnotations

      public final <A extends Annotation> Stream<AnnotationInfo<A>> getDeclaredAnnotations(Class<A> type)
      Returns the declared annotations of the specified type on this executable.
      Type Parameters:
      A - The annotation type.
      Parameters:
      type - The annotation type.
      Returns:
      A stream of matching annotations.
    • getDeclaringClass

      public final ClassInfo getDeclaringClass()
      Returns metadata about the class that declared this method or constructor.
      Returns:
      Metadata about the class that declared this method or constructor.
    • getExceptionTypes

      public final List<ClassInfo> getExceptionTypes()
      Returns the exception types on this executable.
      Returns:
      The exception types on this executable.
    • getFullName

      public final String getFullName()
      Returns the full name of this executable.
      Examples:
      • "com.foo.MyClass.get(java.util.String)" - Method.
      • "com.foo.MyClass(java.util.String)" - Constructor.
      Returns:
      The underlying executable name.
    • getParameter

      public final ParameterInfo getParameter(int index)
      Returns parameter information at the specified index.
      Parameters:
      index - The parameter index.
      Returns:
      The parameter information, never null.
    • getParameterCount

      public final int getParameterCount()
      Returns the number of parameters in this executable.

      Same as calling Executable.getParameterCount().

      Returns:
      The number of parameters in this executable.
    • getParameters

      public final List<ParameterInfo> getParameters()
      Returns the parameters defined on this executable.

      Same as calling Executable.getParameters() but wraps the results

      Returns:
      An array of parameter information, never null.
    • getParameterTypes

      public final List<ClassInfo> getParameterTypes()
      Returns the parameter types for this executable.

      This is a convenience method that extracts the parameter types from getParameters().

      Example:

      // Get parameter types: void myMethod(String s, int i) MethodInfo mi = ClassInfo.of(MyClass.class).getMethod("myMethod", String.class, int.class); List<ClassInfo> paramTypes = mi.getParameterTypes(); // paramTypes contains ClassInfo for String and int

      Returns:
      A list of parameter types, never null.
    • getShortName

      public final String getShortName()
      Returns the short name of this executable.
      Examples:
      • "MyClass.get(String)" - Method.
      • "MyClass(String)" - Constructor.
      Returns:
      The underlying executable name.
    • getSimpleName

      public final String getSimpleName()
      Returns the simple name of the underlying method.
      Returns:
      The simple name of the underlying method;
    • getTypeParameters

      public final TypeVariable<?>[] getTypeParameters()
      Returns an array of TypeVariable objects that represent the type variables declared by the generic declaration.

      Returns an empty array if the generic declaration declares no type variables.

      Same as calling Executable.getTypeParameters().

      Example:

      // Get type parameters from method: <T extends Number> void myMethod(T value) MethodInfo mi = ClassInfo.of(MyClass.class).getMethod("myMethod", Number.class); TypeVariable<?>[] typeParams = mi.getTypeParameters(); // typeParams[0].getName() returns "T"

      Returns:
      An array of TypeVariable objects, or an empty array if none.
      See Also:
    • hasAnnotation

      public <A extends Annotation> boolean hasAnnotation(Class<A> type)
      Returns true if this executable has the specified annotation.
      Type Parameters:
      A - The annotation type.
      Parameters:
      type - The annotation type.
      Returns:
      true if this executable has the specified annotation.
    • hasAnyName

      public final boolean hasAnyName(Collection<String> names)
      Returns true if this method has a name in the specified set.
      Parameters:
      names - The names to test for.
      Returns:
      true if this method has one of the names.
    • hasAnyName

      public final boolean hasAnyName(String... names)
      Returns true if this method has a name in the specified list.
      Parameters:
      names - The names to test for.
      Returns:
      true if this method has one of the names.
    • hasMatchingParameters

      public final boolean hasMatchingParameters(List<ParameterInfo> params)
      Returns true if this executable has matching parameter types with the provided parameter list.
      Parameters:
      params - The parameters to match against.
      Returns:
      true if this executable has matching parameter types.
    • hasName

      public final boolean hasName(String name)
      Returns true if this method has this name.
      Parameters:
      name - The name to test for.
      Returns:
      true if this method has this name.
    • hasNumParameters

      public final boolean hasNumParameters(int number)
      Returns true if this executable has this number of arguments.

      Same as calling Executable.getParameterCount() and comparing the count.

      Parameters:
      number - The number of expected arguments.
      Returns:
      true if this executable has this number of arguments.
    • hasParameters

      public final boolean hasParameters()
      Returns true if this executable has at least one parameter.

      Same as calling Executable.getParameterCount() and comparing with zero.

      Returns:
      true if this executable has at least one parameter.
    • hasParameterTypeParents

      public final boolean hasParameterTypeParents(Class<?>... args)
      Returns true if this method has the specified argument parent classes.
      Parameters:
      args - The arguments to test for.
      Returns:
      true if this method has this arguments in the exact order.
    • hasParameterTypeParents

      public final boolean hasParameterTypeParents(ClassInfo... args)
      Returns true if this method has the specified argument parent classes.
      Parameters:
      args - The arguments to test for.
      Returns:
      true if this method has this arguments in the exact order.
    • hasParameterTypes

      public final boolean hasParameterTypes(Class<?>... args)
      Returns true if this method has the specified arguments.
      Parameters:
      args - The arguments to test for.
      Returns:
      true if this method has this arguments in the exact order.
    • hasParameterTypes

      public final boolean hasParameterTypes(ClassInfo... args)
      Returns true if this method has the specified arguments.
      Parameters:
      args - The arguments to test for.
      Returns:
      true if this method has this arguments in the exact order.
    • hasParameterTypesLenient

      public final boolean hasParameterTypesLenient(Class<?>... args)
      Returns true if this method has at most only these arguments using lenient matching.

      Lenient matching allows arguments to be matched to parameters based on type compatibility, where arguments can be in any order.

      Parameters:
      args - The arguments to test for.
      Returns:
      true if this method has at most only these arguments in any order.
    • hasParameterTypesLenient

      public final boolean hasParameterTypesLenient(ClassInfo... args)
      Returns true if this method has at most only these arguments using lenient matching.

      Lenient matching allows arguments to be matched to parameters based on type compatibility, where arguments can be in any order.

      Parameters:
      args - The arguments to test for.
      Returns:
      true if this method has at most only these arguments in any order.
    • is

      public boolean is(ElementFlag flag)
      Returns true if all specified flags are applicable to this method.
      Overrides:
      is in class ElementInfo
      Parameters:
      flag - The flag to test for.
      Returns:
      true if all specified flags are applicable to this method.
    • isConstructor

      public final boolean isConstructor()
      Returns true if this executable represents a Constructor.
      Returns:
      true if this executable represents a Constructor and can be cast to ConstructorInfo. false if this executable represents a Method and can be cast to MethodInfo.
    • isDeprecated

      public final boolean isDeprecated()
      Returns true if this method has the @Deprecated annotation on it.
      Returns:
      true if this method has the @Deprecated annotation on it.
    • isNotDeprecated

      public final boolean isNotDeprecated()
      Returns true if this method doesn't have the @Deprecated annotation on it.
      Returns:
      true if this method doesn't have the @Deprecated annotation on it.
    • isSynthetic

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

      Same as calling Executable.isSynthetic().

      Example:

      // Check if method is compiler-generated MethodInfo mi = ClassInfo.of(MyClass.class).getMethod("access$000"); boolean isSynthetic = mi.isSynthetic();

      Returns:
      true if this executable is a synthetic construct.
      See Also:
    • isVarArgs

      public final boolean isVarArgs()
      Returns true if this executable was declared to take a variable number of arguments.

      Same as calling Executable.isVarArgs().

      Example:

      // Check if method accepts varargs MethodInfo mi = ClassInfo.of(MyClass.class).getMethod("myMethod", String[].class); boolean isVarArgs = mi.isVarArgs();

      Returns:
      true if this executable was declared to take a variable number of arguments.
      See Also:
    • isVisible

      public final boolean isVisible(Visibility v)
      Identifies if the specified visibility matches this method.
      Parameters:
      v - The visibility to validate against.
      Returns:
      true if this visibility matches the modifier attribute of this method.
    • parameterMatchesLenientCount

      public final int parameterMatchesLenientCount(Class<?>... argTypes)
      Returns how well this method matches the specified arg types using lenient matching.

      Lenient matching allows arguments to be matched to parameters based on type compatibility, where arguments can be in any order.

      The number returned is the number of method arguments that match the passed in arg types.
      Returns -1 if the method cannot take in one or more of the specified arguments.

      Parameters:
      argTypes - The arg types to check against.
      Returns:
      How many parameters match or -1 if method cannot handle one or more of the arguments.
    • parameterMatchesLenientCount

      public final int parameterMatchesLenientCount(ClassInfo... argTypes)
      Returns how well this method matches the specified arg types using lenient matching.

      Lenient matching allows arguments to be matched to parameters based on type compatibility, where arguments can be in any order.

      The number returned is the number of method arguments that match the passed in arg types.
      Returns -1 if the method cannot take in one or more of the specified arguments.

      Parameters:
      argTypes - The arg types to check against.
      Returns:
      How many parameters match or -1 if method cannot handle one or more of the arguments.
    • parameterMatchesLenientCount

      public final int parameterMatchesLenientCount(Object... argTypes)
      Returns how well this method matches the specified arg types using lenient matching.

      Lenient matching allows arguments to be matched to parameters based on type compatibility, where arguments can be in any order.

      The number returned is the number of method arguments that match the passed in arg types.
      Returns -1 if the method cannot take in one or more of the specified arguments.

      Parameters:
      argTypes - The arg types to check against.
      Returns:
      How many parameters match or -1 if method cannot handle one or more of the arguments.
    • setAccessible

      public final boolean setAccessible()
      Attempts to call x.setAccessible(true) and quietly ignores security exceptions.
      Overrides:
      setAccessible in class AccessibleInfo
      Returns:
      true if call was successful.
    • toGenericString

      public final String toGenericString()
      Returns a string describing this executable, including type parameters.

      The string includes the method/constructor name, parameter types (with generic information), and return type (for methods).

      Same as calling Executable.toGenericString().

      Example:

      // Get generic string for: public <T> List<T> myMethod(T value) throws IOException MethodInfo mi = ClassInfo.of(MyClass.class).getMethod("myMethod", Object.class); String str = mi.toGenericString(); // Returns: "public <T> java.util.List<T> com.example.MyClass.myMethod(T) throws java.io.IOException"

      Returns:
      A string describing this executable.
      See Also:
    • toString

      public String toString()
      Overrides:
      toString in class Object