Package org.eclipse.epsilon.eol.util
Class ReflectionUtil
- java.lang.Object
-
- org.eclipse.epsilon.eol.util.ReflectionUtil
-
public class ReflectionUtil extends Object
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Class<?>[]
discoverPublicClasses(Class<?> clazz)
static Object
executeMethod(Object obj, Method method, ModuleElement ast, Object... parameters)
static Object
executeMethod(Object obj, String methodName, Object... parameters)
static Method
findApplicableMethodOrThrow(Object obj, String methodName, Predicate<Method> criteria, Collection<?> parameters, ModuleElement ast, PrettyPrinterManager ppm)
Searches for a method matching the name and criteria for the given object, including all super methods and super-interfaces recursively.static List<Field>
getAllInheritedInstanceFields(Class<?> klazz)
static Field
getField(Class<?> clazz, String fieldName)
Gets a field of a class using reflection by introspecting the class and its supertype(s)static Object
getFieldValue(Object object, String fieldName)
Returns the value of a field of an objectstatic Method
getLegalMethod(Object obj, Method method)
This tries to find a method such that invoking via reflection won't be illegal in Java 9+static Method
getMethodFor(Object obj, String methodName, Object[] parameters, boolean includeInheritedMethods, boolean allowContravariantConversionForParameters)
protected static String
getMethodName(Method method)
static Set<String>
getMethodNames(Object obj, boolean includeInheritedMethods)
static Method[]
getMethodsFromPublicClassesForName(Object obj, String methodName)
static boolean
hasMethods(Object obj, String methodName)
static boolean
isInstance(Class<?> clazz, Object instance)
Checks if the instance is an instance of clazz Necessary because in Java, int.class != Integer.class etcstatic String
methodToString(Method method)
Returns a string representation of the method
-
-
-
Method Detail
-
getMethodNames
public static Set<String> getMethodNames(Object obj, boolean includeInheritedMethods)
-
findApplicableMethodOrThrow
public static Method findApplicableMethodOrThrow(Object obj, String methodName, Predicate<Method> criteria, Collection<?> parameters, ModuleElement ast, PrettyPrinterManager ppm) throws EolIllegalOperationException, EolIllegalOperationParametersException
Searches for a method matching the name and criteria for the given object, including all super methods and super-interfaces recursively.- Parameters:
obj
- The target object to look for methods on.methodName
- The name of the method to find.criteria
- Function which limits the search scope of methods.- Returns:
- A method (chosen non-deterministically) which matches the criteria.
- Throws:
EolIllegalOperationException
- If no method matching the criteria can be found.EolIllegalOperationParametersException
- If the method parameters are invalid.- Since:
- 1.6
-
discoverPublicClasses
public static Class<?>[] discoverPublicClasses(Class<?> clazz)
- Parameters:
clazz
-- Returns:
- Since:
- 1.6
-
getMethodsFromPublicClassesForName
public static Method[] getMethodsFromPublicClassesForName(Object obj, String methodName)
- Parameters:
obj
-methodName
-- Returns:
- Since:
- 1.6
-
getMethodFor
public static Method getMethodFor(Object obj, String methodName, Object[] parameters, boolean includeInheritedMethods, boolean allowContravariantConversionForParameters)
- Parameters:
allowContravariantConversionForParameters
- when false, parameters will have exactly the same class as the arguments to the returned method when true, parameters may have a type that is more specific than the arguments to the returned method
-
executeMethod
public static Object executeMethod(Object obj, String methodName, Object... parameters) throws Throwable
- Throws:
Throwable
-
executeMethod
public static Object executeMethod(Object obj, Method method, ModuleElement ast, Object... parameters) throws EolRuntimeException
- Throws:
EolRuntimeException
-
getLegalMethod
public static Method getLegalMethod(Object obj, Method method)
This tries to find a method such that invoking via reflection won't be illegal in Java 9+- Parameters:
obj
-method
-- Returns:
- Since:
- 1.6
-
methodToString
public static String methodToString(Method method)
Returns a string representation of the method- Parameters:
method
-- Returns:
-
getFieldValue
public static Object getFieldValue(Object object, String fieldName)
Returns the value of a field of an object- Parameters:
object
-fieldName
-- Returns:
-
getField
public static Field getField(Class<?> clazz, String fieldName)
Gets a field of a class using reflection by introspecting the class and its supertype(s)- Parameters:
clazz
-fieldName
-- Returns:
-
isInstance
public static boolean isInstance(Class<?> clazz, Object instance)
Checks if the instance is an instance of clazz Necessary because in Java, int.class != Integer.class etc- Parameters:
clazz
-instance
-- Returns:
-
-