【Java SE】反射


返回自己和超类的 public 字段、方法、构造器
getFields()
getMethods()
getConstructors() 超类没返回

返回自己的所有 字段、方法、构造器
getDeclaredFields()
getDeclaredMethods()
getDeclaredConstructors()

package cn.ycx.common.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * 类
 *
 * @author: ycx
 * @date: 2022-5-21
 * @since 0.0.1
 */
public class YReflect {
    public static void main(String[] args) throws Exception {
        Class c = Class.forName("cn.ycx.common.reflect.Student");
        System.out.println("自己和超类的 字段");
        // 自己和超类的 public 字段
        Field[] fields = c.getFields();
        for (Field item : fields) {
            System.out.println(Modifier.toString(item.getModifiers()) + " " + item.getName());
        }
        System.out.println("自己和超类的 方法");
        // 自己和超类的 public 方法
        Method[] methods = c.getMethods();
        for (Method item : methods) {
            System.out.println(Modifier.toString(item.getModifiers()) + " " + item.getName());
        }
        System.out.println("自己和超类的 构造器");
        // 自己和超类的 Constructor 构造器
        Constructor[] constructors = c.getConstructors();
        for (Constructor item : constructors) {
            System.out.println(Modifier.toString(item.getModifiers()) + " " + item.getName());
        }
        System.out.println("自己的 字段");
        // 自己的 public 字段
        Field[] selfFields = c.getDeclaredFields();
        for (Field item : selfFields) {
            System.out.println(Modifier.toString(item.getModifiers()) + " " + item.getName());
        }
        System.out.println("自己的 方法");
        // 自己的 public 方法
        Method[] selfMethods = c.getDeclaredMethods();
        for (Method item : selfMethods) {
            System.out.println(Modifier.toString(item.getModifiers()) + " " + item.getName());
        }
        System.out.println("自己的 构造器");
        // 自己的 Constructor 构造器
        Constructor[] selfConstructors = c.getDeclaredConstructors();
        for (Constructor item : selfConstructors) {
            System.out.println(Modifier.toString(item.getModifiers()) + " " + item.getName());
        }
    }
}
class Person {
    public Person() {}
    public Person(String p) {}
    private String personPrivateField = "personPrivateField";
    protected String personProtectedField = "personProtectedField";
    public String personPublicField = "personPublicField";
    private String personPrivateMethod() {
        System.out.println("personPrivateMethod");
        return "personPrivateMethod";
    }
    protected String personProtectedMethod() {
        System.out.println("personProtectedMethod");
        return "personProtectedMethod";
    }
    public String personPublicMethod() {
        System.out.println("personPublicMethod");
        return "personPublicMethod";
    }
}
class Student extends Person {
    public Student() {}
    public Student(String s) {super(s);}
    private String studentPrivateField = "studentPrivateField";
    protected String studentProtectedField = "studentProtectedField";
    public String studentPublicField = "studentPublicField";
    private String studentPrivateMethod() {
        System.out.println("studentPrivateMethod");
        return "studentPrivateMethod";
    }
    protected String studentProtectedMethod() {
        System.out.println("studentProtectedMethod");
        return "studentProtectedMethod";
    }
    public String studentPublicMethod() {
        System.out.println("studentPublicMethod");
        return "studentPublicMethod";
    }
}

获取字段值

执行方法

Method studentPrivateMethod = null;
Method studentProtectedMethod = null;
Method studentPublicMethod = null;
Student s = new Student();
Method[] selfMethods = s.getClass().getDeclaredMethods();
for (Method item : selfMethods) {
    if ( "studentPrivateMethod".equals(item.getName())) {
        studentPrivateMethod = item;
    }
    if ("studentProtectedMethod".equals(item.getName())) {
        studentProtectedMethod = item;
    }
    if ("studentPublicMethod".equals(item.getName())) {
        studentPublicMethod = item;
    }
}
studentPrivateMethod.setAccessible(true);// 私有方法设置可访问
studentPrivateMethod.invoke(s,null);
studentProtectedMethod.invoke(s, null);
studentPublicMethod.invoke(s, null);

复制任意数组

public static Object arraycopy(Object src, int length) {
    Class componentType = src.getClass().getComponentType();// 源数组类型
    Object dest = Array.newInstance(componentType, length);
    int minLength = Math.min(Array.getLength(src), length);// 长度最短的
    System.arraycopy(src, 0, dest, 0, minLength);
    return dest;
}
public static void main(String[] args) throws Exception {
    Person[] src = { new Person(), new Person() };
    Person[] dest = (Person[]) arraycopy(src, 10);
    System.out.println(dest);
}