java反射机制学习 -1-反射机制的定义和基本操作

定义:
指在JAVA程序运行状态中
给定一个类(class)对象,通过反射获得这个类对象的所有成员结构;
给定的一个具体的对象,能够动态地调用它的方法及对任意属性值进行获取和赋值;
这种动态获取类的内容、创建对象、以及动态调用对象的方法及操作属性的机制.
优势:
增加代码的灵活性,避免将固有的逻辑程序写死在代码里;
代码简介,可读性强,可提高代码的复用率;
缺点:
在直接调用量大的情景下反射性能下降;
存在一些内部暴露和安全隐患;
反射的一些基本操作:
import lombok.Data;

/**
 * Created by 于占峰 on 2020/3/24/024.
 * 反射机制
 * 指在JAVA程序运行状态中
 * 给定一个类(class)对象,通过反射获得这个类对象的所有成员结构;
 * 给定的一个具体的对象,能够动态地调用它的方法及对任意属性值进行获取和赋值;
 * 这种动态获取类的内容、创建对象、以及动态调用对象的方法及操作属性的机制.
 */
@Data
public class DemoBean {
    public String name;
    public Integer age;
    private String sex;

    public DemoBean() {
    }
    private DemoBean(String name) {
    }

    public DemoBean(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
    private DemoBean(Integer age) {
        this.age = age;
    }

    public void demoMethod() {
        System.out.println("类的方法");
    }

    public static void main(String[] args) {
        try {
            //获得类的实例的四种方法
            //1.调用运行时类本身的.class属性
            Class clazz = DemoBean.class;
            DemoBean demoBean = (DemoBean) clazz.newInstance();
            System.out.println(demoBean);
            //2.通过类名获得实例
            Class clazz1 = Class.forName("com.yzf.demo.demo.ReflectionMechanism.DemoBean");
            DemoBean demoBean1 = (DemoBean) clazz1.newInstance();
            System.out.println(demoBean1);
            //3.通过运行时类的对象获取
            DemoBean demoBean2 = new DemoBean();
            Class clazz2 = demoBean2.getClass();
            DemoBean demoBean3 = (DemoBean) clazz2.newInstance();
            System.out.println(demoBean3);
            //4.通过类加载器
            ClassLoader clssLoader = clazz.getClassLoader();
            Class clazz3 = clssLoader.loadClass("com.yzf.demo.demo.ReflectionMechanism.DemoBean");
            DemoBean demoBean4 = (DemoBean) clazz3.newInstance();
            System.out.println(demoBean4);
            //一些基本操作方法
            System.out.println(clazz.getModifiers());//获得类的修饰符
            System.out.println(clazz.getSuperclass());//获得父类
            System.out.println(clazz.getClassLoader());//获得类的加载器
            System.out.println(clazz.getPackage());//获得类的包名
            System.out.println(clazz.getName());//获得全路径类名
            System.out.println(clazz.getSimpleName());//获得类的简单名称
            System.out.println(clazz.getDeclaredField("name"));//获得类的指定字段
            System.out.println(clazz.getDeclaredFields());//获得类的所有字段列表
            System.out.println(clazz.getMethod("demoMethod"));//根据方法名获得类的方法
            System.out.println(clazz.getMethods());//获得类的方法列表
            System.out.println(clazz.getInterfaces());//获得类实现的接口列表
            System.out.println(clazz.getDeclaredConstructor());//获得类的构造器
            System.out.println(clazz.getAnnotation(Data.class));//获得类的注解
            System.out.println(clazz.getAnnotations());//获得类的注解列表
            System.out.println(clazz.getAnnotatedSuperclass());//返回父类的注解的AnnotatedType
            System.out.println(clazz.getAnnotatedInterfaces());//返回注释的AnnotatedType
            //返回表示某些接口的 Type,这些接口由此对象所表示的类或接口直接实现
            System.out.println(clazz.getGenericInterfaces());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

jdk8 官方API文档:https://docs.oracle.com/javase/8/docs/api/index.html

获得类的修饰符
getModifiers() 类型为int
public class GetModifiers {
 /**
    class.getModifiers() 返回类的修饰符 int类型值
    public static final int    ABSTRACT    1024
    public static final int    FINAL   16
    public static final int    INTERFACE   512
    public static final int    NATIVE  256
    public static final int    PRIVATE 2
    public static final int    PROTECTED   4
    public static final int    PUBLIC  1
    public static final int    STATIC  8
    public static final int    STRICT  2048
    public static final int    SYNCHRONIZED    32
    public static final int    TRANSIENT   128
    public static final int    VOLATILE    64
  **/
 public static void main(String[] args) {
     Class clazz = DemoBean.class;
    int mdifiers = clazz.getModifiers();
     System.out.println(mdifiers);
 }
}

1
获得包名
getPackage() 返回值为Package类
public class GetPackge {
    public static void main(String[] args) {
        Class clazz = DemoBean.class;
        Package pck = clazz.getPackage();
        System.out.println(pck);
    }
}

package com.yzf.demo.demo.ReflectionMechanism
获得类的加载器
getClassLoader() 返回值为 ClassLoader
public class GetClassLoader {
    public static void main(String[] args) {
        Class clazz = DemoBean.class;
        ClassLoader classLoader = clazz.getClassLoader();
        System.out.println(classLoader);
    }
}

sun.misc.Launcher$AppClassLoader@18b4aac2
获得类的全路径类名
getName() 返回值为String
public class getName {
    public static void main(String[] args) {
        Class clazz = DemoBean.class;
        String name = clazz.getName();
        System.out.println(name);
    }
}

com.yzf.demo.demo.ReflectionMechanism.DemoBean
获得类的简单名称
getSimpleName() 返回值为String
public class getSimpleName {
    public static void main(String[] args) {
        Class clazz = DemoBean.class;
        String name = clazz.getSimpleName();
        System.out.println(name);
    }
}

DemoBean
获得类的字段
GetDeclaredField() GetDeclaredFields() 返回值为 Field[] Field
public class GetDeclaredFields {
    public static void main(String[] args) throws NoSuchFieldException {
        Class clazz = DemoBean.class;
        Field[] fields = clazz.getDeclaredFields();
        Field field = clazz.getDeclaredField("name");
        System.out.println(fields);
        System.out.println(field);
    }
}

[Ljava.lang.reflect.Field;@49476842
public java.lang.String com.yzf.demo.demo.ReflectionMechanism.DemoBean.name
获得类的方法
getMethod() getMethods() 返回值为 Method Method[]
public class GetMethod {
    public static void main(String[] args) throws NoSuchMethodException {
        Class clazz = DemoBean.class;
        Method method = clazz.getMethod("demoMethod");
        Method[] methods = clazz.getMethods();
        System.out.println(method);
        System.out.println(methods);
    }
}

public void com.yzf.demo.demo.ReflectionMechanism.DemoBean.demoMethod()
[Ljava.lang.reflect.Method;@78308db
获得类实现的接口
getInterfaces() 返回值为 Class[]
public class GetInterfaces {
    public static void main(String[] args) throws NoSuchMethodException {
        Class clazz = DemoBean.class;
        Class[] interfaces = clazz.getInterfaces();
        System.out.println(interfaces);
    }
}

[Ljava.lang.Class;@7440e464
获得类的构造器
getDeclaredConstructor() 返回指定参数类型的所有构造器,包括 public的和非pulic的
getConstructor() 只返回指定参数类型 访问权限是public的构造函数
public class GetConstructorAndGetDeclaredConstructor {
    public static void main(String[] args) throws NoSuchMethodException {
        Class clazz = DemoBean.class;
        Constructor declaredConstructor = clazz.getDeclaredConstructor();
        Constructor declaredConstructor2 = clazz.getDeclaredConstructor(String.class,Integer.class);
        Constructor declaredConstructor3 = clazz.getDeclaredConstructor(String.class);
        Constructor constructor = clazz.getConstructor();
        Constructor constructor2 = clazz.getConstructor(String.class,Integer.class);
        //Constructor constructor3 = clazz.getConstructor(Integer.class); //私有的构造函数 会报错
        System.out.println(declaredConstructor);
        System.out.println(declaredConstructor2);
        System.out.println(declaredConstructor3);
        System.out.println(constructor);
        System.out.println(constructor2);
    }
}

public com.yzf.demo.demo.ReflectionMechanism.DemoBean()
public com.yzf.demo.demo.ReflectionMechanism.DemoBean(java.lang.String,java.lang.Integer)
private com.yzf.demo.demo.ReflectionMechanism.DemoBean(java.lang.String)
public com.yzf.demo.demo.ReflectionMechanism.DemoBean()
public com.yzf.demo.demo.ReflectionMechanism.DemoBean(java.lang.String,java.lang.Integer)
获得类的注解
getAnnotation() 返回指定类型的注解 getAnnotations()返回类的注解列表
public class GetAnnotation {
    public static void main(String[] args) {
        Class clazz = DemoBean.class;
        Annotation annotation = clazz.getAnnotation(Data.class);
        Annotation[] annotations = clazz.getAnnotations();
        System.out.println(annotation);
        System.out.println(annotations);
    }
}

null
[Ljava.lang.annotation.Annotation;@2c7b84de