Top Description Fields Constructors Methods
org.easymock.internal

public final Class ReflectionUtils

extends Object
Class Inheritance
Imports
java.lang.reflect.Constructor, .Method, .Modifier, java.util.Arrays, .HashMap, .Map, .Set, java.util.function.Predicate, java.util.stream.Collectors

Helper class for reflection.
Author
Henri Tremblay

Field Summary

Modifier and TypeField and Description
public static final Predicate<Method>
public static final Method
public static final Method
public static final Method
public static final Method
private static final Map<Class<?>, Class<?>>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static Method
findDefaultMethod(Class<?> searchedClass, String name, Class<?>[] paramTypes)

public static Method

Returns:

the Method object, or null if none found
findMethod
(Class<?>
the class to introspect
clazz
,
String
the name of the method
name
,
Predicate<Method>
tells what methods to ignore in the research
filter
)

Attempt to find a Method on the supplied class with the supplied name and no parameters.

public static Method

Returns:

the Method object, or null if none found
findMethod
(Class<?>
the class to introspect
clazz
,
String
the name of the method
name
,
Predicate<Method>
tells what methods to ignore in the research
filter
,
Class<?>...
the parameter types of the method (may be null to indicate any signature)
paramTypes
)

Attempt to find a Method on the supplied class with the supplied name and parameter types.

public static <
type of the class searched
T
>
Constructor<T>

Returns:

a constructor with the arguments in objs
getConstructor
(Class<T>
class on which we are searching the constructor
clazz
,
Object...
list of arguments of the constructor
objs
)

Returns a constructor that contains objs as arguments.

public static Method

Returns:

the method searched
getDeclaredMethod
(Class<?>
class on which the getDeclaredMethod is called
clazz
,
String
method name
name
,
Class<?>...
method parameters
paramTypes
)

Basically calls getDeclaredMethod on a Class but wraps the NoSuchMethodException into a Runtime.

public static Set<Method>

Returns:

all default methods
getDefaultMethods
(Class<?>
class for which we want the default method
clazz
)

Return all default methods of an interface and it's inherited interface.

public static boolean

Returns:

true if the class was found
isClassAvailable
(String
full class name
className
)

Tells if a class is available in the classpath

private static boolean
isMatchingConstructor(Constructor<?> classConstructor, Object... objs)

Returns true if objects in objs are eligible to be passed to classConstructor.

private static Class<?>

Returns:

the boxed equivalent
wrapPrimitive
(Class<?>
the primitive type
parameterType
)

Given a primitive type, returns its boxed equivalent.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

NOT_PRIVATEback to summary
public static final Predicate<Method> NOT_PRIVATE
OBJECT_EQUALSback to summary
public static final Method OBJECT_EQUALS
OBJECT_FINALIZEback to summary
public static final Method OBJECT_FINALIZE
OBJECT_HASHCODEback to summary
public static final Method OBJECT_HASHCODE
OBJECT_TOSTRINGback to summary
public static final Method OBJECT_TOSTRING
primitiveToWrapperTypeback to summary
private static final Map<Class<?>, Class<?>> primitiveToWrapperType

Constructor Detail

ReflectionUtilsback to summary
private ReflectionUtils()

Method Detail

findDefaultMethodback to summary
private static Method findDefaultMethod(Class<?> searchedClass, String name, Class<?>[] paramTypes)
findMethodback to summary
public static Method findMethod(Class<?> clazz, String name, Predicate<Method> filter)

Attempt to find a Method on the supplied class with the supplied name and no parameters. Searches all superclasses up to Object. The filter is used to ignore some kind of methods the caller doesn't want to see returned. In this case they are totally ignored and can't clash with a non-ignored one to cause ambiguity.

Returns null if no Method can be found.

Parameters
clazz:Class<?>

the class to introspect

name:String

the name of the method

filter:Predicate<Method>

tells what methods to ignore in the research

Returns:Method

the Method object, or null if none found

findMethodback to summary
public static Method findMethod(Class<?> clazz, String name, Predicate<Method> filter, Class<?>... paramTypes)

Attempt to find a Method on the supplied class with the supplied name and parameter types. Searches all superclasses up to Object. The filter is used to ignore some kind of methods the caller doesn't want to see returned. In this case they are totally ignored and can't clash with a non-ignored one to cause ambiguity.

Returns null if no Method can be found.

Parameters
clazz:Class<?>

the class to introspect

name:String

the name of the method

filter:Predicate<Method>

tells what methods to ignore in the research

paramTypes:Class<?>[]

the parameter types of the method (may be null to indicate any signature)

Returns:Method

the Method object, or null if none found

getConstructorback to summary
public static <T> Constructor<T> getConstructor(Class<T> clazz, Object... objs) throws NoSuchMethodException

Returns a constructor that contains objs as arguments.

We could not do something like clazz.getConstructor(objs.class()) because that would require casting all the passed arguments to the exact parameter types of the desired constructor.

Parameters
<T>
type of the class searched
clazz:Class<T>

class on which we are searching the constructor

objs:Object[]

list of arguments of the constructor

Returns:Constructor<T>

a constructor with the arguments in objs

Annotations
@SuppressWarnings:unchecked
Exceptions
NoSuchMethodException:
when the constructor with args does not exist or is ambiguous
getDeclaredMethodback to summary
public static Method getDeclaredMethod(Class<?> clazz, String name, Class<?>... paramTypes)

Basically calls getDeclaredMethod on a Class but wraps the NoSuchMethodException into a Runtime.

Parameters
clazz:Class<?>

class on which the getDeclaredMethod is called

name:String

method name

paramTypes:Class<?>[]

method parameters

Returns:Method

the method searched

getDefaultMethodsback to summary
public static Set<Method> getDefaultMethods(Class<?> clazz)

Return all default methods of an interface and it's inherited interface.

Parameters
clazz:Class<?>

class for which we want the default method

Returns:Set<Method>

all default methods

isClassAvailableback to summary
public static boolean isClassAvailable(String className)

Tells if a class is available in the classpath

Parameters
className:String

full class name

Returns:boolean

true if the class was found

isMatchingConstructorback to summary
private static boolean isMatchingConstructor(Constructor<?> classConstructor, Object... objs)

Returns true if objects in objs are eligible to be passed to classConstructor.

wrapPrimitiveback to summary
private static Class<?> wrapPrimitive(Class<?> parameterType)

Given a primitive type, returns its boxed equivalent. For instance, given int, returns Integer.

Parameters
parameterType:Class<?>

the primitive type

Returns:Class<?>

the boxed equivalent