Package org.qiunet.utils.reflect
Class ReflectUtil
java.lang.Object
org.qiunet.utils.reflect.ReflectUtil
反射工具类
- Author:
- qiunet 2020-09-27 11:00
-
Field Summary
FieldsModifier and TypeFieldDescriptionPre-built MethodFilter that matches all non-bridge non-synthetic methods which are not declared onjava.lang.Object. -
Method Summary
Modifier and TypeMethodDescriptionstatic voiddoWithFields(Class<?> clazz, Consumer<Field> consumer) Invoke the given callback on all fields in the target class, going up the class hierarchy to get all declared fields.static voidInvoke the given callback on all fields in the target class, going up the class hierarchy to get all declared fields.static voiddoWithMethods(Class<?> clazz, Consumer<Method> mc) Perform the given callback operation on all matching methods of the given class and superclasses.static voidPerform the given callback operation on all matching methods of the given class and superclasses (or given interface and super-interfaces).static Fieldstatic FieldfindFieldList(Class<?> clazz, Predicate<Field> filter) static Class<?>findGenericParameterizedType(Class<?> oriClazz, BiPredicate<Class<?>, Class<?>> filter) 找出Class 上对应的泛型参数类型static Class<?>findGenericParameterizedType(Class<?> oriClazz, Predicate<Class<?>> filter) 找出Class 上对应的泛型参数类型static Method[]getAllDeclaredMethods(Class<?> leafClass) Get all declared methods on the leaf class and all superclasses.static Field[]getDeclaredFields(Class<?> clazz) This variant retrievesClass.getDeclaredFields()from a local cache in order to avoid the JVM's SecurityManager check and defensive array copying.static Method[]getDeclaredMethods(Class<?> clazz) Variant ofClass.getDeclaredMethods()that uses a local cache in order to avoid the JVM's SecurityManager check and new Method instances.static ObjectGet the field represented by the suppliedfield objecton the specifiedtarget object.static ObjectGet the field represented by the suppliedfield objecton the specifiedtarget object.static Class<?>得到list field class的泛型static <T> Constructor<T>getMatchConstructor(Class<T> clazz, Object... params) 获得匹配的构造static voidmakeAccessible(Field field) Make the given field accessible, explicitly setting it accessible if necessary.static voidmakeAccessible(Method method) Make the given method accessible, explicitly setting it accessible if necessary.static voidmodifyAnnotationValue(Annotation annotation, String key, Object value) 修改注解的值static <T> TnewInstance(Class<T> clazz, Object... params) 生成新的对象static <T> TnewInstance(Constructor<T> constructor, Object... params) 生成新的对象.static void给字段设置值static voidSet the field represented by the suppliedfield objecton the specifiedtarget objectto the specifiedvalue.
-
Field Details
-
USER_DECLARED_METHODS
Pre-built MethodFilter that matches all non-bridge non-synthetic methods which are not declared onjava.lang.Object.- Since:
- 3.0.5
-
-
Method Details
-
findFieldList
Attempt to find afieldon the suppliedClasswith the suppliedname. Searches all superclasses up toObject.- Parameters:
clazz- the class to introspect- Returns:
- the corresponding Field object, or
nullif not found
-
findField
Attempt to find afieldon the suppliedClasswith the suppliedname. Searches all superclasses up toObject.- Parameters:
clazz- the class to introspectname- the name of the field- Returns:
- the corresponding Field object, or
nullif not found
-
findField
Attempt to find afieldon the suppliedClasswith the suppliednameand/ortype. Searches all superclasses up toObject.- Parameters:
clazz- the class to introspectname- the name of the field (may benullif type is specified)type- the type of the field (may benullif name is specified)- Returns:
- the corresponding Field object, or
nullif not found
-
getField
Get the field represented by the suppliedfield objecton the specifiedtarget object. In accordance withField.get(Object)semantics, the returned value is automatically wrapped if the underlying field has a primitive type.Thrown exceptions are handled via a call to
- Parameters:
t- the target object from which to get the fieldname- the field to get- Returns:
- the field's current value
-
makeAccessible
Make the given method accessible, explicitly setting it accessible if necessary. ThesetAccessible(true)method is only called when actually necessary, to avoid unnecessary conflicts with a JVM SecurityManager (if active).- Parameters:
method- the method to make accessible- See Also:
-
makeAccessible
Make the given field accessible, explicitly setting it accessible if necessary. ThesetAccessible(true)method is only called when actually necessary, to avoid unnecessary conflicts with a JVM SecurityManager (if active).- Parameters:
field- the field to make accessible- See Also:
-
setField
Set the field represented by the suppliedfield objecton the specifiedtarget objectto the specifiedvalue. In accordance withField.set(Object, Object)semantics, the new value is automatically unwrapped if the underlying field has a primitive type.Thrown exceptions are handled via a call to
- Parameters:
declaringObj- the target object on which to set the fieldname- the field to setvalue- the value to set; may benull
-
setField
给字段设置值- Parameters:
declaringObj- the target object on which to set the fieldfield- the field to setvalue- the value to set; may benull
-
getField
Get the field represented by the suppliedfield objecton the specifiedtarget object. In accordance withField.get(Object)Thrown exceptions are handled via a call to
- Parameters:
field- the field to getobj- the field declare object instance. if field is static. it is null.
-
newInstance
生成新的对象- Type Parameters:
T-- Parameters:
clazz-params-- Returns:
-
getMatchConstructor
获得匹配的构造- Type Parameters:
T-- Parameters:
clazz-params-- Returns:
-
newInstance
生成新的对象.- Type Parameters:
T-- Parameters:
constructor-params-- Returns:
-
doWithFields
Invoke the given callback on all fields in the target class, going up the class hierarchy to get all declared fields.- Parameters:
clazz- the target class to analyzeconsumer- the callback to invoke for each field- Throws:
IllegalStateException- if introspection fails
-
doWithFields
public static void doWithFields(Class<?> clazz, Consumer<Field> consumer, Predicate<Field> predicate) Invoke the given callback on all fields in the target class, going up the class hierarchy to get all declared fields.- Parameters:
clazz- the target class to analyzeconsumer- the callback to invoke for each fieldpredicate- the filter that determines the fields to apply the callback to- Throws:
IllegalStateException- if introspection fails
-
getDeclaredFields
This variant retrievesClass.getDeclaredFields()from a local cache in order to avoid the JVM's SecurityManager check and defensive array copying.- Parameters:
clazz- the class to introspect- Returns:
- the cached array of fields
- Throws:
IllegalStateException- if introspection fails- See Also:
-
doWithMethods
Perform the given callback operation on all matching methods of the given class and superclasses.The same named method occurring on subclass and superclass will appear twice, unless excluded by a
Predicate.- Parameters:
clazz- the class to introspectmc- the callback to invoke for each method- Throws:
IllegalStateException- if introspection fails- See Also:
-
doWithMethods
Perform the given callback operation on all matching methods of the given class and superclasses (or given interface and super-interfaces).The same named method occurring on subclass and superclass will appear twice, unless excluded by the specified
Predicate.- Parameters:
clazz- the class to introspectmc- the callback to invoke for each methodmf- the filter that determines the methods to apply the callback to- Throws:
IllegalStateException- if introspection fails
-
getAllDeclaredMethods
Get all declared methods on the leaf class and all superclasses. Leaf class methods are included first.- Parameters:
leafClass- the class to introspect- Throws:
IllegalStateException- if introspection fails
-
getDeclaredMethods
Variant ofClass.getDeclaredMethods()that uses a local cache in order to avoid the JVM's SecurityManager check and new Method instances. In addition, it also includes Java 8 default methods from locally implemented interfaces, since those are effectively to be treated just like declared methods.- Parameters:
clazz- the class to introspect- Returns:
- the cached array of methods
- Throws:
IllegalStateException- if introspection fails- Since:
- 5.2
- See Also:
-
findGenericParameterizedType
找出Class 上对应的泛型参数类型- Parameters:
oriClazz- 原始classfilter- 类型过滤 仅给出泛型class- Returns:
-
findGenericParameterizedType
public static Class<?> findGenericParameterizedType(Class<?> oriClazz, BiPredicate<Class<?>, Class<?>> filter) 找出Class 上对应的泛型参数类型- Parameters:
oriClazz- 原始classfilter- 类型过滤 给出所在class 以及 泛型class- Returns:
-
getListGenericParameterizedType
得到list field class的泛型- Returns:
-
modifyAnnotationValue
修改注解的值- Parameters:
annotation- 注解对象key- 注解字段名.value- 设置的值
-