public class ReflectTools extends Object implements Serializable
For internal use only. May be renamed or removed in a future release.
Constructor and Description |
---|
ReflectTools() |
Modifier and Type | Method and Description |
---|---|
static void |
checkClassAccessibility(Class<?> clazz)
Makes a check whether the
clazz is externally accessible for
instantiation (e.g. |
static Class<?> |
convertPrimitiveType(Class<?> type)
Converts the given primitive type to its boxed version.
|
static <T> T |
createInstance(Class<T> cls)
Creates an instance of the given class with a no-arg constructor.
|
static Type |
createParameterizedType(Class<?> rawType,
Type subType)
Creates a parameterized type, e.g.
|
static <T> T |
createProxyInstance(Class<T> proxyClass,
Class<?> originalClass)
Creates an instance of the given
proxyClass with no-arg
constructor. |
static Optional<ClassLoader> |
findClosestCommonClassLoaderAncestor(ClassLoader classLoaderA,
ClassLoader classLoaderB)
Finds the common ancestor of the two
ClassLoaders . |
static Class<?> |
findCommonBaseType(Class<?> a,
Class<?> b)
Finds the most specific class that both provided classes extend from.
|
static Method |
findMethod(Class<?> cls,
String methodName,
Class<?>... parameterTypes)
Locates the method in the given class.
|
static List<Integer> |
getConstantIntValues(Class<?> clazz)
Collect all the integer values for public static final constants found
for the given class.
|
static Method |
getFunctionalMethod(Class<?> functionalClass)
Get the functional interface method name defined for given interface
class.
|
static Class<?> |
getGenericInterfaceType(Class<?> clazz,
Class<?> interfaceType)
Finds the Class type for the generic interface class extended by given
class if exists.
|
static List<Class<?>> |
getGenericInterfaceTypes(Class<?> clazz,
Class<?> interfaceType)
Finds the Class type for all parameters defined by the generic interface
class extended by given class if exists.
|
static Optional<Method> |
getGetter(Class<? extends Object> beanClass,
String propertyName)
Finds a getter for a property in a bean type.
|
static Stream<Method> |
getGetterMethods(Class<?> type)
Return all the getter methods from the given type.
|
static Object |
getJavaFieldValue(Object object,
Field field)
Returns the value of the java field.
|
static Object |
getJavaFieldValue(Object object,
Field field,
Class<?> propertyType)
Returns the value of the java field that is assignable to the property
type.
|
static Serializable |
getPrimitiveDefaultValue(Class<?> primitiveType)
Gets default value for given
primitiveType . |
static String |
getPropertyName(Method method)
Parses the property name from the given getter or setter method.
|
static Type |
getPropertyType(Method method)
Returns property type from the given getter or setter method.
|
static Stream<Method> |
getSetterMethods(Class<?> type)
Return all the setter methods from the given type.
|
static boolean |
isCheckedException(Class<?> exceptionClass)
Checks if the given exception class represents a checked exception.
|
static boolean |
isGetter(Method method)
Checks whether the given method is a valid getter according to JavaBeans
Specification.
|
static boolean |
isGetterName(String methodName,
boolean isBoolean)
Checks whether the given method name is a valid getter name according to
the JavaBeans Specification.
|
static boolean |
isNotObjectMethod(Method method)
Returns whether the given method is NOT declared in
Object
. |
static boolean |
isSetter(Method method)
Checks whether the given method is a valid setter according to the
JavaBeans Specification.
|
static boolean |
isSetterName(String methodName)
Checks whether the given method name is a valid setter name according to
the JavaBeans Specification.
|
static void |
setJavaFieldValue(Object object,
Field field,
Object value)
Sets the value of a java field.
|
public static Method findMethod(Class<?> cls, String methodName, Class<?>... parameterTypes) throws ExceptionInInitializerError
cls
- Class that contains the methodmethodName
- The name of the methodparameterTypes
- The parameter types for the method.ExceptionInInitializerError
- Wraps any exception in an ExceptionInInitializerError
so this method can be called from a static initializer.public static Object getJavaFieldValue(Object object, Field field) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
Uses getter if present, otherwise tries to access even private fields directly.
object
- The object containing the fieldfield
- The field we want to get the value forInvocationTargetException
- If the value could not be retrievedIllegalAccessException
- If the value could not be retrievedIllegalArgumentException
- If the value could not be retrievedpublic static Object getJavaFieldValue(Object object, Field field, Class<?> propertyType) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
Uses getter if a getter for the correct return type is present, otherwise tries to access even private fields directly. If the java field is not assignable to the property type throws an IllegalArgumentException.
object
- The object containing the fieldfield
- The field we want to get the value forpropertyType
- The type the field must be assignable toInvocationTargetException
- If the value could not be retrievedIllegalAccessException
- If the value could not be retrievedIllegalArgumentException
- If the value could not be retrievedpublic static void setJavaFieldValue(Object object, Field field, Object value) throws IllegalArgumentException
object
- The object containing the fieldfield
- The field we want to set the value forvalue
- The value to setIllegalArgumentException
- If the value could not be assigned to the fieldpublic static Class<?> convertPrimitiveType(Class<?> type)
type
- the primitive type to convertpublic static Serializable getPrimitiveDefaultValue(Class<?> primitiveType)
primitiveType
.primitiveType
- the primitive typepublic static boolean isSetter(Method method)
method
- the method to checktrue
if the method is a setter, false
if notpublic static boolean isSetterName(String methodName)
methodName
- the method name to checktrue
if the method name is a setter name,
false
if notpublic static boolean isGetterName(String methodName, boolean isBoolean)
methodName
- the method name to checkisBoolean
- whether the method is getter for boolean typetrue
if the method name is a getter name,
false
if notpublic static boolean isGetter(Method method)
method
- the method to checktrue
if the method is a getter, false
if notpublic static Stream<Method> getGetterMethods(Class<?> type)
Any getter methods from Object
are excluded.
type
- the type to get getters frompublic static Stream<Method> getSetterMethods(Class<?> type)
type
- the type to get setters frompublic static boolean isNotObjectMethod(Method method)
Object
.method
- the method to checktrue
if method is NOT declared in Object,
false
if it ispublic static String getPropertyName(Method method)
If the given method does not have a valid setter or getter name, this method may produce unexpected results.
method
- the method to parseisSetter(Method)
,
isGetter(Method)
public static Type getPropertyType(Method method)
method
- the method to inspectisSetter(Method)
,
isGetter(Method)
public static <T> T createInstance(Class<T> cls)
Catches all exceptions which might occur and wraps them in a
IllegalArgumentException
with a descriptive error message hinting
of what might be wrong with the class that could not be instantiated.
T
- the instance typecls
- the class to instantiatepublic static <T> T createProxyInstance(Class<T> proxyClass, Class<?> originalClass)
proxyClass
with no-arg
constructor.
Catches all exceptions which might occur and wraps them in a
IllegalArgumentException
with a descriptive error message hinting
of what might be wrong with the class that could not be instantiated.
Descriptive message is derived based on the information about the
originalClass
.
T
- type of a proxy classproxyClass
- the proxy class to instantiateoriginalClass
- the class that is used to determine exception description, if
creation failsIllegalArgumentException
- if class instance creation failspublic static void checkClassAccessibility(Class<?> clazz)
clazz
is externally accessible for
instantiation (e.g. it's not inner class (nested and not static) and is
not a local class).clazz
- type to checkpublic static Type createParameterizedType(Class<?> rawType, Type subType)
List<Bean>
.rawType
- the raw type, e.g. List
subType
- the sub type, e.g. Bean
public static Class<?> getGenericInterfaceType(Class<?> clazz, Class<?> interfaceType)
clazz
- class that should extend interfaceinterfaceType
- class type of interface to get generic fornull
public static List<Class<?>> getGenericInterfaceTypes(Class<?> clazz, Class<?> interfaceType)
clazz
- class that should extend interfaceinterfaceType
- class type of interface to get generic forpublic static Optional<Method> getGetter(Class<? extends Object> beanClass, String propertyName)
beanClass
- the bean type, not null
propertyName
- the property name, not null
public static boolean isCheckedException(Class<?> exceptionClass)
exceptionClass
- the class to checktrue
if the class represents a checked exception,
false otherwisepublic static Method getFunctionalMethod(Class<?> functionalClass)
functionalClass
- interface class to get the functional method forpublic static List<Integer> getConstantIntValues(Class<?> clazz)
clazz
- class to collect constants frompublic static Class<?> findCommonBaseType(Class<?> a, Class<?> b)
a
- one class to get the base type for, not null
b
- another class to get the base type for, not null
null
IllegalArgumentException
- if a or b are interfaces or primitive typespublic static Optional<ClassLoader> findClosestCommonClassLoaderAncestor(ClassLoader classLoaderA, ClassLoader classLoaderB)
ClassLoaders
. The class
loaders themselves are acceptable ancestors; If they are equal, classLoaderA
is returned. An empty optional is returned if the two class
loaders aren't equal, no shared ancestor is found, or the implementation
of the class loader treats bootstrap class loader as null
when it
is the parent of a class loaders (see ClassLoader.getParent()
.classLoaderA
- class loader AclassLoaderB
- class loader Bnull
(as per
ClassLoader.getParent()
).Copyright © 2025. All rights reserved.