使用这个工具类,可以完成父类,基类,方法,字段,无论什么权限都可以调用.
package com.reflect;
/**
* 基类
* @author jianghui
*/
public class GrandParent {
public String publicField = "1";
String defaultField = "2";
protected String protectedField = "3";
private String privateField = "4";
public void publicMethod() {
System.out.println("publicMethod...");
}
void defaultMethod() {
System.out.println("defaultMethod...");
}
protected void protectedMethod() {
System.out.println("protectedMethod...");
}
private void privateMethod() {
System.out.println("privateMethod...");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
package com.reflect;
/**
* 父类
* @author jianghui
*/
public class Parent extends GrandParent {
}
package com.reflect;
/**
* 子类
* @author jianghui
*/
public class Son extends Parent {
}
package com.reflect;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* 方法类
*
* @author jianghui
*/
public class ReflectionUtils {
/**
* 循环向上转型, 获取对象的 DeclaredMethod
*
* @param object
* : 子类对象
* @param methodName
* : 父类中的方法名
* @param parameterTypes
* : 父类中的方法参数类型
* @return 父类中的方法对象
*/
public static Method getDeclaredMethod(Object object, String methodName, Class<?>... parameterTypes) {
Method method = null;
for (Class<?> clazz = object.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) {
try {
method = clazz.getDeclaredMethod(methodName, parameterTypes);
return method;
} catch (Exception e) {
}
}
return null;
}
/**
* 直接调用对象方法, 而忽略修饰符(private, protected, default)
*
* @param object
* : 子类对象
* @param methodName
* : 父类中的方法名
* @param parameterTypes
* : 父类中的方法参数类型
* @param parameters
* : 父类中的方法参数
* @return 父类中方法的执行结果
*/
public static Object invokeMethod(Object object, String methodName, Class<?>[] parameterTypes,
Object[] parameters) {
Method method = getDeclaredMethod(object, methodName, parameterTypes);
method.setAccessible(true);
try {
if (null != method) {
return method.invoke(object, parameters);
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
/**
* 循环向上转型, 获取对象的 DeclaredField
*
* @param object
* : 子类对象
* @param fieldName
* : 父类中的属性名
* @return 父类中的属性对象
*/
public static Field getDeclaredField(Object object, String fieldName) {
Field field = null;
Class<?> clazz = object.getClass();
for (; clazz != Object.class; clazz = clazz.getSuperclass()) {
try {
field = clazz.getDeclaredField(fieldName);
return field;
} catch (Exception e) {
}
}
return null;
}
/**
* 直接设置对象属性值, 忽略 private/protected 修饰符, 也不经过 setter
*
* @param object
* : 子类对象
* @param fieldName
* : 父类中的属性名
* @param value
* : 将要设置的值
*/
public static void setFieldValue(Object object, String fieldName, Object value) {
Field field = getDeclaredField(object, fieldName);
field.setAccessible(true);
try {
field.set(object, value);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
/**
* 直接读取对象的属性值, 忽略 private/protected 修饰符, 也不经过 getter
*
* @param object
* : 子类对象
* @param fieldName
* : 父类中的属性名
* @return : 父类中的属性值
*/
public static Object getFieldValue(Object object, String fieldName) {
Field field = getDeclaredField(object, fieldName);
field.setAccessible(true);
try {
return field.get(object);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
package com.reflect;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.junit.Test;
/**
* 测试类
* @author jianghui
*/
public class ReflectionUtilsTest {
/**
* 测试获取父类的各个方法对象
*/
@Test
public void testGetDeclaredMethod() {
Object obj = new Son() ;
Method publicMethod = ReflectionUtils.getDeclaredMethod(obj, "publicMethod") ;
System.out.println(publicMethod.getName());
Method defaultMethod = ReflectionUtils.getDeclaredMethod(obj, "defaultMethod") ;
System.out.println(defaultMethod.getName());
Method protectedMethod = ReflectionUtils.getDeclaredMethod(obj, "protectedMethod") ;
System.out.println(protectedMethod.getName());
Method privateMethod = ReflectionUtils.getDeclaredMethod(obj, "privateMethod") ;
System.out.println(privateMethod.getName());
}
/**
* 测试调用父类的方法
* @throws Exception
*/
@Test
public void testInvokeMethod() throws Exception {
Object obj = new Son() ;
ReflectionUtils.invokeMethod(obj, "publicMethod", null , null) ;
ReflectionUtils.invokeMethod(obj, "defaultMethod", null , null) ;
ReflectionUtils.invokeMethod(obj, "protectedMethod", null , null) ;
ReflectionUtils.invokeMethod(obj, "privateMethod", null , null) ;
}
/**
* 测试获取父类的各个属性名
*/
@Test
public void testGetDeclaredField() {
Object obj = new Son() ;
Field publicField = ReflectionUtils.getDeclaredField(obj, "publicField") ;
System.out.println(publicField.getName());
Field defaultField = ReflectionUtils.getDeclaredField(obj, "defaultField") ;
System.out.println(defaultField.getName());
Field protectedField = ReflectionUtils.getDeclaredField(obj, "protectedField") ;
System.out.println(protectedField.getName());
Field privateField = ReflectionUtils.getDeclaredField(obj, "privateField") ;
System.out.println(privateField.getName());
}
@Test
public void testSetFieldValue() {
Object obj = new Son() ;
System.out.println("原来的各个属性的值: ");
System.out.println("publicField = " + ReflectionUtils.getFieldValue(obj, "publicField"));
System.out.println("defaultField = " + ReflectionUtils.getFieldValue(obj, "defaultField"));
System.out.println("protectedField = " + ReflectionUtils.getFieldValue(obj, "protectedField"));
System.out.println("privateField = " + ReflectionUtils.getFieldValue(obj, "privateField"));
ReflectionUtils.setFieldValue(obj, "publicField", "a") ;
ReflectionUtils.setFieldValue(obj, "defaultField", "b") ;
ReflectionUtils.setFieldValue(obj, "protectedField", "c") ;
ReflectionUtils.setFieldValue(obj, "privateField", "d") ;
System.out.println("***********************************************************");
System.out.println("将属性值改变后的各个属性值: ");
System.out.println("publicField = " + ReflectionUtils.getFieldValue(obj, "publicField"));
System.out.println("defaultField = " + ReflectionUtils.getFieldValue(obj, "defaultField"));
System.out.println("protectedField = " + ReflectionUtils.getFieldValue(obj, "protectedField"));
System.out.println("privateField = " + ReflectionUtils.getFieldValue(obj, "privateField"));
}
/**
* 获取字段的值
*/
@Test
public void testGetFieldValue() {
Object obj = new Son() ;
System.out.println("publicField = " + ReflectionUtils.getFieldValue(obj, "publicField"));
System.out.println("defaultField = " + ReflectionUtils.getFieldValue(obj, "defaultField"));
System.out.println("protectedField = " + ReflectionUtils.getFieldValue(obj, "protectedField"));
System.out.println("privateField = " + ReflectionUtils.getFieldValue(obj, "privateField"));
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129