Package org.eclipse.epsilon.eol.util
Class ReflectionUtil
- java.lang.Object
-
- org.eclipse.epsilon.eol.util.ReflectionUtil
-
public class ReflectionUtil extends java.lang.Object
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.Class<?>[]
discoverPublicClasses(java.lang.Class<?> clazz)
static java.lang.Object
executeMethod(java.lang.Object obj, java.lang.reflect.Method method, ModuleElement ast, java.lang.Object... parameters)
static java.lang.Object
executeMethod(java.lang.Object obj, java.lang.String methodName, java.lang.Object... parameters)
static java.lang.reflect.Method
findApplicableMethodOrThrow(java.lang.Object obj, java.lang.String methodName, java.util.function.Predicate<java.lang.reflect.Method> criteria, java.util.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 java.util.List<java.lang.reflect.Field>
getAllInheritedInstanceFields(java.lang.Class<?> klazz)
static java.lang.reflect.Field
getField(java.lang.Class<?> clazz, java.lang.String fieldName)
Gets a field of a class using reflection by introspecting the class and its supertype(s)static java.lang.Object
getFieldValue(java.lang.Object object, java.lang.String fieldName)
Returns the value of a field of an objectstatic java.lang.reflect.Method
getLegalMethod(java.lang.Object obj, java.lang.reflect.Method method)
This tries to find a method such that invoking via reflection won't be illegal in Java 9+static java.lang.reflect.Method
getMethodFor(java.lang.Object obj, java.lang.String methodName, java.lang.Object[] parameters, boolean includeInheritedMethods, boolean allowContravariantConversionForParameters)
protected static java.lang.String
getMethodName(java.lang.reflect.Method method)
static java.util.Set<java.lang.String>
getMethodNames(java.lang.Object obj, boolean includeInheritedMethods)
static java.lang.reflect.Method[]
getMethodsFromPublicClassesForName(java.lang.Object obj, java.lang.String methodName)
static boolean
hasMethods(java.lang.Object obj, java.lang.String methodName)
static boolean
isInstance(java.lang.Class<?> clazz, java.lang.Object instance)
Checks if the instance is an instance of clazz Necessary because in Java, int.class != Integer.class etcstatic java.lang.String
methodToString(java.lang.reflect.Method method)
Returns a string representation of the method
-
-
-
Method Detail
-
hasMethods
public static boolean hasMethods(java.lang.Object obj, java.lang.String methodName)
-
getMethodNames
public static java.util.Set<java.lang.String> getMethodNames(java.lang.Object obj, boolean includeInheritedMethods)
-
getMethodName
protected static java.lang.String getMethodName(java.lang.reflect.Method method)
-
findApplicableMethodOrThrow
public static java.lang.reflect.Method findApplicableMethodOrThrow(java.lang.Object obj, java.lang.String methodName, java.util.function.Predicate<java.lang.reflect.Method> criteria, java.util.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 java.lang.Class<?>[] discoverPublicClasses(java.lang.Class<?> clazz)
- Parameters:
clazz
-- Returns:
- Since:
- 1.6
-
getMethodsFromPublicClassesForName
public static java.lang.reflect.Method[] getMethodsFromPublicClassesForName(java.lang.Object obj, java.lang.String methodName)
- Parameters:
obj
-methodName
-- Returns:
- Since:
- 1.6
-
getMethodFor
public static java.lang.reflect.Method getMethodFor(java.lang.Object obj, java.lang.String methodName, java.lang.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 java.lang.Object executeMethod(java.lang.Object obj, java.lang.String methodName, java.lang.Object... parameters) throws java.lang.Throwable
- Throws:
java.lang.Throwable
-
executeMethod
public static java.lang.Object executeMethod(java.lang.Object obj, java.lang.reflect.Method method, ModuleElement ast, java.lang.Object... parameters) throws EolRuntimeException
- Throws:
EolRuntimeException
-
getLegalMethod
public static java.lang.reflect.Method getLegalMethod(java.lang.Object obj, java.lang.reflect.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 java.lang.String methodToString(java.lang.reflect.Method method)
Returns a string representation of the method- Parameters:
method
-- Returns:
-
getFieldValue
public static java.lang.Object getFieldValue(java.lang.Object object, java.lang.String fieldName)
Returns the value of a field of an object- Parameters:
object
-fieldName
-- Returns:
-
getField
public static java.lang.reflect.Field getField(java.lang.Class<?> clazz, java.lang.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(java.lang.Class<?> clazz, java.lang.Object instance)
Checks if the instance is an instance of clazz Necessary because in Java, int.class != Integer.class etc- Parameters:
clazz
-instance
-- Returns:
-
getAllInheritedInstanceFields
public static java.util.List<java.lang.reflect.Field> getAllInheritedInstanceFields(java.lang.Class<?> klazz)
-
-