package J20250806.reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @author WanJl
 * @version 1.0
 * @title Demo08_MethodTest
 * @description
 * @create 2025/8/6
 */
public class Demo08_MethodTest {
    public static void main(String[] args) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class<Person> personClass = Person.class;
        //通过Class对象获取Person的构造方法对象
        Constructor<Person> constructor = personClass.getDeclaredConstructor();
        constructor.setAccessible(true);
        //通过构造方法对象创建Person对象
        Person person = constructor.newInstance();
        Method showName = personClass.getDeclaredMethod("showName", String.class);
        //取消方法的权限检查
        showName.setAccessible(true);
        Object o = showName.invoke(person, "张三");
        System.out.println(o);
        //获取方法的返回值类型  getReturnType()
        Class<?> returnType = showName.getReturnType();
        System.out.println("showName方法的返回值类型是："+returnType);
        //获取方法的参数类型列表 getParameterTypes()
        Class<?>[] parameterTypes = showName.getParameterTypes();
        System.out.println(Arrays.toString(parameterTypes));
        //获取这个成员方法的修饰符    getModifiers()
        int modifiers = showName.getModifiers();
        System.out.println(modifiers);  // 2 表示私有化的 private
    }
}
