Package net.automatalib.common.util
Class ReflectUtil
- java.lang.Object
-
- net.automatalib.common.util.ReflectUtil
-
public final class ReflectUtil extends Object
Utility methods for using Java reflection.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> @Nullable Constructor<T>
findConstructor(Class<T> clazz, Class<?>... params)
Tries to find a constructor that is able to accept parameters of the given types.static @Nullable Method
findMatchingMethod(Class<?> clazz, String name, @Nullable Object... args)
static @Nullable Method
findMethod(Class<?> clazz, String name, Class<?>... params)
Tries to find a method of the given name that is able to accept parameters of the given types.static @Nullable Method
findMethodRT(Class<?> clazz, String name, @Nullable Class<?> returnType, Class<?>... params)
-
-
-
Method Detail
-
findConstructor
public static <T> @Nullable Constructor<T> findConstructor(Class<T> clazz, Class<?>... params)
Tries to find a constructor that is able to accept parameters of the given types. First tries to find the constructor matching the exact parameter types. If such a constructor does not exist, tries to find any (the first match of arbitrary order) constructor that is able to accept the parameter types by means of auto-boxing, i.e. aConstructor(int)
would be returned for the parameters classInteger.class
.Returns
null
if no such constructor could be found.- Type Parameters:
T
- the class type- Parameters:
clazz
- the class which should be scanned for constructorsparams
- the types of the constructor arguments- Returns:
- A constructor that is able of accepting parameters of the specified types,
null
if such a constructor could not be found.
-
findMethod
public static @Nullable Method findMethod(Class<?> clazz, String name, Class<?>... params)
Tries to find a method of the given name that is able to accept parameters of the given types. First tries to find the method matching the exact parameter types. If such a method does not exist, tries to find any (the first match of arbitrary order) method that is able to accept the parameter types by means of auto-boxing, i.e. aMethod(int)
would be returned for the parameters classInteger.class
.Returns
null
if no such method could be found.- Parameters:
clazz
- the class which should be scanned for methodsname
- the name of the methodparams
- the types of the method arguments- Returns:
- A method that is able to accept parameters of the specified types,
null
if such a method could not be found.
-
findMatchingMethod
public static @Nullable Method findMatchingMethod(Class<?> clazz, String name, @Nullable Object... args)
SeefindMethod(Class, String, Class...)
. This variation does not require the types of input parameters, but can handle the actual objects, which should be passed to the method.- Parameters:
clazz
- the class which should be scanned for methodsname
- the name of the methodargs
- the objects that should be passed to the method, may containnull
s- Returns:
- A method that is able to accept of the specified objects,
null
if such a method could not be found.
-
findMethodRT
public static @Nullable Method findMethodRT(Class<?> clazz, String name, @Nullable Class<?> returnType, Class<?>... params)
SeefindMethod(Class, String, Class...)
. This variation allows to additionally narrow the method by specifying its return type.- Parameters:
clazz
- the class which should be scanned for methodsname
- the name of the methodreturnType
- the type of the returned object, ifnull
, the return type will be ignoredparams
- the types of the method arguments- Returns:
- A method that is able to accept of the specified objects,
null
if such a method could not be found.
-
-