Java反射(二)

四.获取类结构

public class relect1 {

    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
        Class c = Class.forName("com.se.reflection.Stu");

        // 获取类名
        System.out.println(c.getName());
        System.out.println(c.getSimpleName());

        // 获取类属性(public)
        for (Field field : c.getFields()) {
            System.out.println(field);
        }
        // 获取类全部属性
        for (Field declaredField : c.getDeclaredFields()) {
            System.out.println(declaredField);
        }
        // 获取指定类属性
//        System.out.println(c.getField("age"));
        System.out.println(c.getDeclaredField("age"));

        // 获取类方法(public)
        for (Method method : c.getMethods()) {
            System.out.println(method);
        }
        for (Method declaredMethod : c.getDeclaredMethods()) {
            System.out.println(declaredMethod);
        }

        System.out.println("------------------------------");

        // 获取类构造器
        for (Constructor constructor : c.getConstructors()) {
            System.out.println(constructor);
        }
        for (Constructor declaredConstructor : c.getDeclaredConstructors()) {
            System.out.println(declaredConstructor);
        }
    }
}

 五.创建对象执行方法

创建类的对象:

  • 通过Class类的getDeclaredConstructor()获取指定参数类型的构造器(无参的为getDeclaredConstructor().newInstance())
  • 向构造器中传递参数
  • 通过Constructor实例化对象
public class reflect2 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class c = Class.forName("com.se.reflection.Stu");

        // 构造对象(调用无参构造器)
        Stu stu = (Stu) c.getDeclaredConstructor().newInstance();
        System.out.println(stu);

        // 使用构造器创建对象
        Constructor declaredConstructor = c.getDeclaredConstructor(String.class, int.class, int.class);
        Stu stu2 = (Stu) declaredConstructor.newInstance("jack",20,001);
        System.out.println(stu2);
    }
}

 调用方法:

  • 通过Class类的getMethod()获得一个Method对象
  • 使用invoke()调用,如果原方法为私有的。使用前需先调用方法的setAccessiable(true)
public class reflect2 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class c = Class.forName("com.se.reflection.Stu");

        // 通过反射操作属性
        Stu stu3 = (Stu) c.getDeclaredConstructor().newInstance();
        stu3.setAge(19);
        System.out.println(stu3.getAge());

        // 通过反射调用普通方法
        Stu stu4 = (Stu) c.getDeclaredConstructor().newInstance();
        // 获取一个方法
        Method setId = stu4.getClass().getDeclaredMethod("setId", int.class);
        setId.invoke(stu4,007);
        System.out.println(stu4.getId());
    }
}

 六.获取注解

public class reflect3 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        Class c = Stu.class;

        // 通过反射获取注解
        for (Annotation annotation : c.getAnnotations()) {
            System.out.println(annotation);
        }

        // 通过反射获取注解的value
        TableStu tableStu = (TableStu) c.getAnnotation(TableStu.class);
        System.out.println(tableStu.value());

        // 获取指定注解
        Field age = c.getDeclaredField("age");
        Fields annotation = age.getAnnotation(Fields.class);
        System.out.println(annotation.colName());
        System.out.println(annotation.type());
        System.out.println(annotation.length());
    }
}
点击查看代码
@TableStu("db_stu")
class Stu{

    @Fields(colName = "db_name",type = "varchar",length = 6)
    private String name;
    @Fields(colName = "db_age",type = "int",length = 2)
    private int age;
    @Fields(colName = "db_id",type = "int",length = 6)
    private int id;

    public Stu() {
    }

    public Stu(String name, int age, int id) {
        this.name = name;
        this.age = age;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Stu{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", id=" + id +
                '}';
    }
}

@Target(value = ElementType.TYPE)
@Retention(value = RetentionPolicy.RUNTIME)
@interface TableStu{
    String value();
}

@Target(value = ElementType.FIELD)
@Retention(value = RetentionPolicy.RUNTIME)
@interface Fields{
    String colName();
    String type();
    int length();
}

 

posted @ 2021-10-05 23:37  酥炸小黄瓜  阅读(44)  评论(0)    收藏  举报