diff options
author | Keuin <[email protected]> | 2020-04-23 00:16:13 +0800 |
---|---|---|
committer | keuin <[email protected]> | 2020-04-23 00:20:29 +0800 |
commit | 28ebd3ae91dc6087a9f9acd93f80dd92be2d64aa (patch) | |
tree | 9a051d4784cd41953a5c6aa02e55005e14d907bf /src/main/java/com/keuin/kbackupfabric/util/ReflectionUtils.java | |
parent | d00b439204b7ddfe381f1ff7c0dc1f02e16da8de (diff) |
Finish backup.
TODO:
- restore
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/util/ReflectionUtils.java')
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/util/ReflectionUtils.java | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/util/ReflectionUtils.java b/src/main/java/com/keuin/kbackupfabric/util/ReflectionUtils.java new file mode 100644 index 0000000..f7bc351 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/ReflectionUtils.java @@ -0,0 +1,59 @@ +package com.keuin.kbackupfabric.util; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/** + * @Author 落叶飞翔的蜗牛 + * @Date 2018/3/10 + * @Description + */ +public class ReflectionUtils { + + /** + * 获取私有成员变量的值 + * + * @param instance + * @param filedName + * @return + */ + public static Object getPrivateField(Object instance, String filedName) throws NoSuchFieldException, IllegalAccessException { + Field field = instance.getClass().getDeclaredField(filedName); + field.setAccessible(true); + return field.get(instance); + } + + /** + * 设置私有成员的值 + * + * @param instance + * @param fieldName + * @param value + * @throws NoSuchFieldException + * @throws IllegalAccessException + */ + public static void setPrivateField(Object instance, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException { + Field field = instance.getClass().getDeclaredField(fieldName); + field.setAccessible(true); + field.set(instance, value); + } + + /** + * 访问私有方法 + * + * @param instance + * @param methodName + * @param classes + * @param objects + * @return + * @throws NoSuchMethodException + * @throws InvocationTargetException + * @throws IllegalAccessException + */ + public static Object invokePrivateMethod(Object instance, String methodName, Class[] classes, String objects) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + Method method = instance.getClass().getDeclaredMethod(methodName, classes); + method.setAccessible(true); + return method.invoke(instance, objects); + } +}
\ No newline at end of file |