public final class MethodUtils extends Object
LookupMode strategies.
It tries to find a constructor of a given datatype, with a given argument datatypelist, where types do not have to match formal types (auto-boxing, supertypes, implemented interfaces and type conversions are allowed as they are included in the lookup cycles). This expanded version tries a simple call first (exact match, which is provided natively by the Java) and when this fails, it generates a list of datatype arrays (signatures) with all possible versions of any type in the original list possible, and combinations thereof.
Observe the following (trivial) example:
interface Foo {
void foo(Double value, Fruit fruit, char c);
}
abstract class A implements Foo {
}
abstract class B extends A {
}
ClassUtils.findCompatibleJavaMethod(B.class, "foo", EnumSet.allOf(LookupMode.class), double.class, Pear.class, String.class)}
In the above example, the method foo will be found by finding all methods named "Foo" on the interfaces implemented by supertype A,
and then foo's method signature will be matched using autoboxing on the double type, a cast to the Fruit supertype for
the Pear type and finally by attempting a common conversion from String to char. This will give you a Java
Method, but you won't be able to invoke it if it was found using a less strict lookup than one with a simple exact match. There are two
ways to do this: use invokeCompatibleMethod(Object, Class, String, Object...) instead or perform the conversion yourself using ValueConversionHelper.convert(Object[], Class[], boolean) prior to invoking the method. ValueConverter.convert(args,
method.getParameterTypes()).
A reverse lookup is also possible: given an ordered list of possible types, is a given Method compatible?
Because this lookup is potentially very expensive, a cache is present to store lookup results.
| Constructor and Description |
|---|
MethodUtils() |
| Modifier and Type | Method and Description |
|---|---|
static <T> Set<InvokableObject<Constructor>> |
findCompatibleConstructor(Class<T> datatype,
Set<LookupMode> lookupMode,
Class<?>... signature)
Tries to find a
Constructor of a given type, with a given typelist, where types do not match due to formal types simple types. |
static @NotNull Set<InvokableObject<Method>> |
findCompatibleMethod(Class<?> datatype,
String methodName,
Set<LookupMode> lookupMode,
Class<?>... signature)
Same as
getConstructor(), except for getting a Method of a classtype, using the name to indicate which method should be
located. |
static Set<InvokableObject<Method>> |
findCompatibleMethod(Class<?> datatype,
String methodName,
Set<LookupMode> lookupMode,
Object... args)
Delegates to
findCompatibleMethod(Class, String, Set, Class[]), with the types of the given arguments extracted using TypeUtils.collectTypes(Object[]). |
static Set<Method> |
findMatchingMethods(Class<?> datatype,
@Nullable Class<?> boundaryMarker,
String methodName,
List<String> paramTypeNames)
Delegates to
findMatchingMethods(Class, Class, String, String...) |
static Set<Method> |
findMatchingMethods(Class<?> datatype,
@Nullable Class<?> boundaryMarker,
String methodName,
String... paramTypeNames) |
static @NotNull Set<InvokableObject<Method>> |
findSimpleCompatibleMethod(Class<?> datatype,
String methodName,
Class<?>... signature)
Delegates to
findCompatibleMethod(Class, String, Set, Class...), using strict lookupmode (no autoboxing, casting etc.) and
optional signature parameters. |
static <T extends Annotation,Target> |
firstParameterArgumentByAnnotation(Method method,
Object[] arguments,
Class<T> annotationClass) |
static <T extends Annotation> |
firstParameterIndexByAnnotation(Method method,
Class<T> annotationClass) |
static @NotNull Method |
getMethod(Class<?> datatype,
String name,
Class<?>... signature)
Searches a specific class object for a
Method using java reflect using a specific signature. |
static <T> T |
invokeCompatibleConstructor(Class<T> datatype,
Object... args)
Locates and invokes a
Constructorusing invokeConstructor(Class, Class[], Object[]) |
static <T> T |
invokeCompatibleMethod(@Nullable Object context,
Class<?> datatype,
String identifier,
Object... args)
Locates a method on an Object using serveral searchmodes for optimization.
|
static <T> T |
invokeConstructor(Class<T> datatype,
Class<?>[] parameterSignature,
Object[] args)
Locates and invokes a
Constructor, using a customized typelist. |
static <T> T |
invokeMethodSimple(Method method,
Object subject,
Object... args)
Delegates to
Method.invoke(Object, Object...) while converting checked exceptions into runtime
exceptions. |
static boolean |
isMethodCompatible(Method method,
Class<?>... signature)
Tests if a list of arguments is compatible with the signature of the given method, allowing for
LookupMode.SIMPLE lookup mode. |
static boolean |
isMethodCompatible(Method method,
Object... signature)
Tests if a list of classes is compatible with the signature of the given method, allowing for
LookupMode.SIMPLE lookup mode. |
static boolean |
isMethodCompatible(Method method,
Set<LookupMode> lookupMode,
Class<?>... signature)
Tests if a list of arguments is compatible with the signature of the given method, allowing for the given lookup modes.
|
static boolean |
isMethodCompatible(Method method,
Set<LookupMode> lookupMode,
Object... signature)
Tests if a list of classes is compatible with the signature of the given method, allowing for the given lookup modes.
|
static boolean |
methodHasCollectionParameter(Method m) |
static Method |
onlyMethod(Set<InvokableObject<Method>> methods) |
static @Nullable LinkedHashMap<MethodParameter,Object> |
zipParametersAndArguments(Method method,
Object... arguments)
Given a method and a list of arguments, return a map of parameters matching their arguments.
|
@Nullable public static <T> T invokeMethodSimple(Method method, Object subject, Object... args)
Method.invoke(Object, Object...) while converting checked exceptions into runtime
exceptions.@Nullable
public static <T> T invokeCompatibleMethod(@Nullable
@Nullable Object context,
Class<?> datatype,
String identifier,
Object... args)
throws NoSuchMethodException,
IllegalArgumentException,
IllegalAccessException,
InvocationTargetException
Method cache is being maintained to quickly
fetch heavily used methods. If not cached before and if a simple search (autoboxing and supertype casts) fails a more complex search is done
where all interfaces are searched for the method as well. If this fails as well, this method will try to autoconvert the types of the arguments
and find a matching signature that way.context - The object to call the method from (can be null).datatype - The class to find the method on.identifier - The name of the method to locate.args - A list of [non-formal] arguments.NoSuchMethodException - Thrown by findCompatibleMethod(Class, String, Set, Class...).IllegalArgumentException - Thrown by Method.invoke(Object, Object...).IllegalAccessException - Thrown by Method.invoke(Object, Object...).InvocationTargetException - Thrown by Method.invoke(Object, Object...).@NotNull public static <T> T invokeCompatibleConstructor(Class<T> datatype, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException
Constructorusing invokeConstructor(Class, Class[], Object[])T - Used to parameterize the returned object so that the caller doesn't need to cast.datatype - The class to find the constructor for.args - A list of [non-formal] arguments.IllegalAccessException - Thrown by invokeConstructor(Class, Class[], Object[]).InvocationTargetException - Thrown by invokeConstructor(Class, Class[], Object[]).InstantiationException - Thrown by invokeConstructor(Class, Class[], Object[]).NoSuchMethodException - Thrown by invokeConstructor(Class, Class[], Object[]).Constructor.newInstance(Object[])@NotNull public static <T> T invokeConstructor(Class<T> datatype, Class<?>[] parameterSignature, Object[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException
Constructor, using a customized typelist. Avoids dynamically trying to find correct parameter type list. Can also
be used to force up/down casting (ie. passing a specific type of List into a generic type)T - Used to parameterize the returned object so that the caller doesn't need to cast.datatype - The class to find the constructor for.parameterSignature - The typelist used to find correct constructor.args - A list of [non-formal] arguments.IllegalAccessException - Thrown by Constructor.newInstance(Object...).InvocationTargetException - Thrown by Constructor.newInstance(Object...).InstantiationException - Thrown by Constructor.newInstance(Object...).NoSuchMethodException - Thrown by findCompatibleConstructor(Class, Set, Class...).Constructor.newInstance(Object[])public static <T> Set<InvokableObject<Constructor>> findCompatibleConstructor(Class<T> datatype, Set<LookupMode> lookupMode, Class<?>... signature) throws NoSuchMethodException
Constructor of a given type, with a given typelist, where types do not match due to formal types simple types.
This expanded version tries a simple call first and when it fails, it generates a list of type arrays with all possible (un)wraps of any type
in the original list possible, and combinations thereof.T - Used to parameterize the returned constructor.datatype - The class to get the constructor from.lookupMode - Flag indicating the search steps that need to be done.signature - The list of types as specified by the user.NoSuchMethodException - Thrown when the Constructor could not be found on the data type, even after performing optional
conversions.@NotNull public static @NotNull Set<InvokableObject<Method>> findSimpleCompatibleMethod(Class<?> datatype, String methodName, Class<?>... signature)
findCompatibleMethod(Class, String, Set, Class...), using strict lookupmode (no autoboxing, casting etc.) and
optional signature parameters.datatype - The class to get the constructor from.methodName - The name of the method to retrieve from the class.signature - The list of types as specified by the user.null in case of a NoSuchMethodException exception.findCompatibleMethod(Class, String, Set, Class...)public static Set<InvokableObject<Method>> findCompatibleMethod(Class<?> datatype, String methodName, Set<LookupMode> lookupMode, Object... args) throws NoSuchMethodException
findCompatibleMethod(Class, String, Set, Class[]), with the types of the given arguments extracted using TypeUtils.collectTypes(Object[]).NoSuchMethodException@NotNull public static @NotNull Set<InvokableObject<Method>> findCompatibleMethod(Class<?> datatype, String methodName, Set<LookupMode> lookupMode, Class<?>... signature) throws NoSuchMethodException
getConstructor(), except for getting a Method of a classtype, using the name to indicate which method should be
located.datatype - The class to get the constructor from.methodName - The name of the method to retrieve from the class.lookupMode - Flag indicating the search steps that need to be done.signature - The list of types as specified by the user.NoSuchMethodException - Thrown when the Method could not be found on the data type, even after performing optional
conversions.@NotNull public static @NotNull Method getMethod(Class<?> datatype, String name, Class<?>... signature) throws NoSuchMethodException
Method using java reflect using a specific signature. This method will first search all
implemented interfaces for the method to avoid visibility problems.Iterator as implemented by the ArrayList. The Iterator is implemented as a
private innerclass and as such not accessible by java reflect (even though the implemented methods are declared public), unlike the
interface's definition.datatype - The class reference to locate the method on.name - The name of the method to find.signature - The signature the method should match.NoSuchMethodException - Thrown when the Method could not be found on the interfaces implemented by the given data type.Class.getMethod(String, Class[])public static boolean isMethodCompatible(Method method, Object... signature)
LookupMode.SIMPLE lookup mode.public static boolean isMethodCompatible(Method method, Set<LookupMode> lookupMode, Object... signature)
public static boolean isMethodCompatible(Method method, Class<?>... signature)
LookupMode.SIMPLE lookup mode.public static boolean isMethodCompatible(Method method, Set<LookupMode> lookupMode, Class<?>... signature)
@Nullable public static @Nullable LinkedHashMap<MethodParameter,Object> zipParametersAndArguments(Method method, Object... arguments)
public static Set<Method> findMatchingMethods(Class<?> datatype, @Nullable @Nullable Class<?> boundaryMarker, String methodName, List<String> paramTypeNames)
findMatchingMethods(Class, Class, String, String...)public static Set<Method> findMatchingMethods(Class<?> datatype, @Nullable @Nullable Class<?> boundaryMarker, String methodName, String... paramTypeNames)
ClassUtils.collectMethods(Class, Class, EnumSet)
and then filters based on the parameter type names.public static boolean methodHasCollectionParameter(Method m)
Iterable.public static Method onlyMethod(Set<InvokableObject<Method>> methods)
@Nullable public static <T extends Annotation,Target> Target firstParameterArgumentByAnnotation(Method method, Object[] arguments, Class<T> annotationClass)
public static <T extends Annotation> int firstParameterIndexByAnnotation(Method method, Class<T> annotationClass)
Copyright © 2011–2019. All rights reserved.