Class ReflectUtil
-
Method Summary
Modifier and TypeMethodDescriptionfindPropertySetters
(Class<?> type, String methodPrefix) Find all setter methods in the given class whose name starts with the given prefix.static Comparator<Class<?>>
Get a comparator that partially orders types where narrower types sort first.static Comparator<Class<?>>
getClassComparator
(boolean incomparableEqual) Get a comparator that calculates which type is narrower, if possible.static <T> T
instantiate
(Class<T> type) Instantiate the given class using its zero-arg constructor.static <T> T
instantiate
(Constructor<T> constructor, Object... params) Instantiate the given class using the given constructor.static Object
Invoke the given method, rethrowing any checked exceptions.static String
propertyNameFromGetterMethod
(Method method) Get the Java bean name corresponding to the given Java bean property getter method.static String
propertyNameFromGetterMethodName
(String methodName) Get the Java bean name corresponding to the given Java bean property getter method name.static void
sortByType
(List<? extends Class<?>> list) Sort the given list so narrower types always appear before wider types.static <T> void
sortByType
(List<T> list, Function<? super T, ? extends Class<?>> typer) Sort the given list so items with narrower types always appear before items with wider types.
-
Method Details
-
propertyNameFromGetterMethod
Get the Java bean name corresponding to the given Java bean property getter method.The method does not need to be public.
Does not support indexed bean properties.
- Parameters:
method
- getter method- Returns:
- corresponding bean property name, or null if
method
is not a valid Java bean getter method - Throws:
IllegalArgumentException
- ifmethod
is null
-
propertyNameFromGetterMethodName
Get the Java bean name corresponding to the given Java bean property getter method name.This method assumes that if the name starts with
"is"
, then the caller has already verified any corresponding method has return type boolean.- Parameters:
methodName
- getter method name- Returns:
- corresponding bean property name, or null if
methodName
is not a value property getter method name - Throws:
IllegalArgumentException
- ifmethodName
is null
-
findPropertySetters
Find all setter methods in the given class whose name starts with the given prefix.A setter method is any public method taking exactly one parameter. Multiple setter methods could have the same name, if they have a different parameter type and/or return type, though method will filter out bridge methods.
- Parameters:
type
- type to introspectmethodPrefix
- setter method name prefix (typically"set"
)- Returns:
- mapping from property name to setter methods, with those having narrower parameter types first
- Throws:
IllegalArgumentException
- if either parameter is null
-
instantiate
Instantiate the given class using its zero-arg constructor.- Type Parameters:
T
- new object type- Parameters:
type
- type to instantiate- Returns:
- new instance
- Throws:
RuntimeException
- if construction failsIllegalArgumentException
- iftype
is null
-
instantiate
Instantiate the given class using the given constructor.- Type Parameters:
T
- new object type- Parameters:
constructor
- constructor to invokeparams
- constructor parameters- Returns:
- method's return value
- Throws:
RuntimeException
- if construction failsIllegalArgumentException
- if either parameter is null
-
invoke
Invoke the given method, rethrowing any checked exceptions.- Parameters:
method
- method to invoketarget
- target object, or null if method is staticparams
- constructor parameters- Returns:
- method's return value
- Throws:
RuntimeException
- if invocation failsIllegalArgumentException
- ifmethod
orparams
is null
-
sortByType
Sort the given list so narrower types always appear before wider types.This is a stable sort. Algorithm is O(n
2 ) however.- Parameters:
list
- list of types
-
sortByType
Sort the given list so items with narrower types always appear before items with wider types.This is a stable sort. Algorithm is O(n
2 ) however.- Parameters:
list
- list of itemstyper
- associates a type with each item
-
getClassComparator
Get a comparator that partially orders types where narrower types sort first.Non-comparable types compare as equal, so they will not reorder under a stable sort.
- Returns:
- stable ordering of types narrowest first
-
getClassComparator
Get a comparator that calculates which type is narrower, if possible.Non-comparable types will compare as equal if
incomparableEqual
is true, or else generate anIllegalArgumentException
.Warning: use this comparator for simple comparisons only; it will not work for sorting because it is not transitive for "equality"; use
sortByType()
instead.- Parameters:
incomparableEqual
- true to return zero for incomparable classes, false to throwIllegalArgumentException
- Returns:
- ordering of types narrowest first
-