ClassA
package com.huawei.ywj.reflect;
public class ClassA {
private String name = "defaultName";
private int age = 16;
private ClassA() {
}
private ClassA(String name) {
this.name = name;
}
private void setName(String name) {
this.name = name;
}
private String getName() {
return name;
}
@Override
public String toString() {
return "ClassA{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
调用类 ExampleUnitTest
public class ExampleUnitTest {
@Test
public void test() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchFieldException {
// reflectMethod();
// reflectMethod2();
// reflectMethod3();
// reflectField();
reflectConstructor();
}
private void reflectMethod() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
Class<?> clazz = Class.forName("com.huawei.ywj.reflect.ClassA");
Constructor<?> declaredConstructor = clazz.getDeclaredConstructor(String.class);
declaredConstructor.setAccessible(true);
ClassA instance = (ClassA) declaredConstructor.newInstance("xiaoming");
Method method = clazz.getDeclaredMethod("getName");
method.setAccessible(true);
Object result = method.invoke(instance);
System.out.println(result);
}
private void reflectMethod2() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
Class<?> clazz = Class.forName("com.huawei.ywj.reflect.ClassA");
Constructor<?> declaredConstructor = clazz.getDeclaredConstructor();
declaredConstructor.setAccessible(true);
Object instance = declaredConstructor.newInstance();
Method setName = clazz.getDeclaredMethod("setName", String.class);
setName.setAccessible(true);
setName.invoke(instance, "xiaoming");
Method getName = clazz.getDeclaredMethod("getName");
getName.setAccessible(true);
Object result = getName.invoke(instance);
System.out.println(result);
}
private void reflectMethod3() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
Class<ClassA> clazz = ClassA.class;
Constructor declaredConstructor = clazz.getDeclaredConstructor();
declaredConstructor.setAccessible(true);
Object instance = declaredConstructor.newInstance();
Method method = clazz.getDeclaredMethod("getName", null);
method.setAccessible(true);
Object result = method.invoke(instance);
System.out.println(result);
}
private void reflectField() throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class<?> clazz = Class.forName("com.huawei.ywj.reflect.ClassA");
Constructor<?> declaredConstructor = clazz.getDeclaredConstructor(String.class);
declaredConstructor.setAccessible(true);
Object instance = declaredConstructor.newInstance("xiaoming");
Field field = clazz.getDeclaredField("name");
field.setAccessible(true);
field.set(instance, "xiaoming2");
System.out.println(field.get(instance));
}
private void reflectConstructor() throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class<?> clazz = Class.forName("com.huawei.ywj.reflect.ClassA");
Constructor<?> declaredConstructor = clazz.getDeclaredConstructor(String.class);
declaredConstructor.setAccessible(true);
Object instance = declaredConstructor.newInstance("xiaoming");
System.out.println(instance.toString());
}
}