Class ExecutableInfo
- Direct Known Subclasses:
ConstructorInfo,MethodInfo
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:
See Also:
MethodInfo- Method introspectionConstructorInfo- Constructor introspectionParameterInfo- Parameter introspection- Reflection Package
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedExecutableInfo(ClassInfo declaringClass, Executable inner) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionAttempts to callx.setAccessible(and quietly ignores security exceptions.true )final booleanReturnstrue if this executable can accept the specified arguments in the specified order.final AnnotatedType[]Returns an array ofAnnotatedTypeobjects that represent the use of types to specify the declared exceptions.final AnnotatedType[]Returns an array ofAnnotatedTypeobjects that represent the use of types to specify formal parameter types.final AnnotatedTypeReturns anAnnotatedTypeobject that represents the use of a type to specify the receiver type of the method/constructor.final List<AnnotationInfo<Annotation>>Returns the declared annotations on this executable.final <A extends Annotation>
Stream<AnnotationInfo<A>>getDeclaredAnnotations(Class<A> type) Returns the declared annotations of the specified type on this executable.final ClassInfoReturns metadata about the class that declared this method or constructor.Returns the exception types on this executable.final StringReturns the full name of this executable.final ParameterInfogetParameter(int index) Returns parameter information at the specified index.final intReturns the number of parameters in this executable.final List<ParameterInfo>Returns the parameters defined on this executable.Returns the parameter types for this executable.final StringReturns the short name of this executable.final StringReturns the simple name of the underlying method.final TypeVariable<?>[]Returns an array ofTypeVariableobjects that represent the type variables declared by the generic declaration.<A extends Annotation>
booleanhasAnnotation(Class<A> type) Returnstrue if this executable has the specified annotation.final booleanhasAnyName(String... names) Returnstrue if this method has a name in the specified list.final booleanhasAnyName(Collection<String> names) Returnstrue if this method has a name in the specified set.final booleanhasMatchingParameters(List<ParameterInfo> params) Returnstrue if this executable has matching parameter types with the provided parameter list.final booleanReturnstrue if this method has this name.final booleanhasNumParameters(int number) Returnstrue if this executable has this number of arguments.final booleanReturnstrue if this executable has at least one parameter.final booleanhasParameterTypeParents(Class<?>... args) Returnstrue if this method has the specified argument parent classes.final booleanhasParameterTypeParents(ClassInfo... args) Returnstrue if this method has the specified argument parent classes.final booleanhasParameterTypes(Class<?>... args) Returnstrue if this method has the specified arguments.final booleanhasParameterTypes(ClassInfo... args) Returnstrue if this method has the specified arguments.final booleanhasParameterTypesLenient(Class<?>... args) Returnstrue if this method has at most only these arguments using lenient matching.final booleanhasParameterTypesLenient(ClassInfo... args) Returnstrue if this method has at most only these arguments using lenient matching.booleanis(ElementFlag flag) Returnstrue if all specified flags are applicable to this method.final booleanReturnstrue if this executable represents aConstructor.final booleanReturnstrue if this method has the@Deprecatedannotation on it.final booleanReturnstrue if this method doesn't have the@Deprecatedannotation on it.final booleanReturnstrue if this executable is a synthetic construct as defined by the Java Language Specification.final booleanReturnstrue if this executable was declared to take a variable number of arguments.final booleanIdentifies if the specified visibility matches this method.final intparameterMatchesLenientCount(Class<?>... argTypes) Returns how well this method matches the specified arg types using lenient matching.final intparameterMatchesLenientCount(Object... argTypes) Returns how well this method matches the specified arg types using lenient matching.final intparameterMatchesLenientCount(ClassInfo... argTypes) Returns how well this method matches the specified arg types using lenient matching.final booleanAttempts to callx.setAccessible(and quietly ignores security exceptions.true )final StringReturns a string describing this executable, including type parameters.toString()Methods inherited from class org.apache.juneau.commons.reflect.AccessibleInfo
isAccessibleMethods inherited from class org.apache.juneau.commons.reflect.ElementInfo
ai, getModifiers, isAbstract, isAll, isAny, isFinal, isInterface, isNative, isNotAbstract, isNotFinal, isNotInterface, isNotNative, isNotPrivate, isNotProtected, isNotPublic, isNotStatic, isNotSynchronized, isNotTransient, isNotVolatile, isPrivate, isProtected, isPublic, isStatic, isSynchronized, isTransient, isVolatile
-
Field Details
-
declaringClass
-
-
Constructor Details
-
ExecutableInfo
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
MethodInfoorConstructorInfoinstead.- Parameters:
declaringClass- The ClassInfo for the class that declares this method or constructor.inner- The constructor or method that this info represents. Must not benull .
-
-
Method Details
-
accessible
Attempts to callx.setAccessible(and quietly ignores security exceptions.true )- Returns:
- This object.
-
canAccept
Returnstrue 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(Strings , Integeri );methodInfo .canAccept("hello" , 42);// true // Non-static inner class constructor class Outer {class Inner { Inner(Strings ) {} } }// Constructor actually has signature: Inner(Outer this$0, String s) Outerouter =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 ofAnnotatedTypeobjects 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 MethodInfomi = ClassInfo.of (MyClass.class ).getMethod("myMethod" ); AnnotatedType[]exTypes =mi .getAnnotatedExceptionTypes();- Returns:
- An array of
AnnotatedTypeobjects, or an empty array if the executable declares no exceptions. - See Also:
-
getAnnotatedParameterTypes
Returns an array ofAnnotatedTypeobjects 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) MethodInfomi = ClassInfo.of (MyClass.class ).getMethod("myMethod" , String.class ,int .class ); AnnotatedType[]paramTypes =mi .getAnnotatedParameterTypes();- Returns:
- An array of
AnnotatedTypeobjects, or an empty array if the executable has no parameters. - See Also:
-
getAnnotatedReceiverType
Returns anAnnotatedTypeobject 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) MethodInfomi = ClassInfo.of (MyClass.class ).getMethod("myMethod" ); AnnotatedTypereceiverType =mi .getAnnotatedReceiverType();- Returns:
- An
AnnotatedTypeobject representing the receiver type, ornull 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@Beanannotations, this method returns each@Beanannotation separately, rather than the container annotation.- Returns:
- The declared annotations on this executable as
AnnotationInfoobjects.
Repeatable annotations are expanded into individual instances.
-
getDeclaredAnnotations
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
Returns metadata about the class that declared this method or constructor.- Returns:
- Metadata about the class that declared this method or constructor.
-
getExceptionTypes
Returns the exception types on this executable.- Returns:
- The exception types on this executable.
-
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
Returns parameter information at the specified index.- Parameters:
index- The parameter index.- Returns:
- The parameter information, never
null .
-
getParameterCount
Returns the number of parameters in this executable.Same as calling
Executable.getParameterCount().- Returns:
- The number of parameters in this executable.
-
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
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) MethodInfomi = 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
Returns the short name of this executable.Examples:
"MyClass.get(String)" - Method."MyClass(String)" - Constructor.
- Returns:
- The underlying executable name.
-
getSimpleName
Returns the simple name of the underlying method.- Returns:
- The simple name of the underlying method;
-
getTypeParameters
Returns an array ofTypeVariableobjects 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) MethodInfomi = ClassInfo.of (MyClass.class ).getMethod("myMethod" , Number.class ); TypeVariable<?>[]typeParams =mi .getTypeParameters();// typeParams[0].getName() returns "T" - Returns:
- An array of
TypeVariableobjects, or an empty array if none. - See Also:
-
hasAnnotation
Returnstrue 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
Returnstrue 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
Returnstrue 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
Returnstrue 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
Returnstrue if this method has this name.- Parameters:
name- The name to test for.- Returns:
true if this method has this name.
-
hasNumParameters
Returnstrue 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
Returnstrue 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
Returnstrue 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
Returnstrue 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
Returnstrue 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
Returnstrue 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
Returnstrue 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
Returnstrue 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
Returnstrue if all specified flags are applicable to this method.- Overrides:
isin classElementInfo- Parameters:
flag- The flag to test for.- Returns:
true if all specified flags are applicable to this method.
-
isConstructor
Returnstrue if this executable represents aConstructor.- Returns:
true if this executable represents aConstructorand can be cast toConstructorInfo.false if this executable represents aMethodand can be cast toMethodInfo.
-
isDeprecated
Returnstrue if this method has the@Deprecatedannotation on it.- Returns:
true if this method has the@Deprecatedannotation on it.
-
isNotDeprecated
Returnstrue if this method doesn't have the@Deprecatedannotation on it.- Returns:
true if this method doesn't have the@Deprecatedannotation on it.
-
isSynthetic
Returnstrue 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 MethodInfomi = ClassInfo.of (MyClass.class ).getMethod("access$000" );boolean isSynthetic =mi .isSynthetic();- Returns:
true if this executable is a synthetic construct.- See Also:
-
isVarArgs
Returnstrue if this executable was declared to take a variable number of arguments.Same as calling
Executable.isVarArgs().Example:
// Check if method accepts varargs MethodInfomi = 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
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
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
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
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
Attempts to callx.setAccessible(and quietly ignores security exceptions.true )- Overrides:
setAccessiblein classAccessibleInfo- Returns:
true if call was successful.
-
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 MethodInfomi = ClassInfo.of (MyClass.class ).getMethod("myMethod" , Object.class ); Stringstr =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
-