反射

package reflenction;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.util.Arrays;

public class Test1 {

    public static void main(String[] args) {
        Class cls = ReflenctionExample.class;
        // 返回类全名
        System.out.println(cls.getName());
        // 返回类名
        System.out.println(cls.getSimpleName());
        // 获取类的修饰符,返回int
        System.out.println(cls.getModifiers());
        System.out.println(Modifier.isPublic(cls.getModifiers()));

        // 获取包信息
        Package pa = cls.getPackage();
        System.out.println(pa.getName());

        // 返回父类
        Class superclass = cls.getSuperclass();
        System.out.println(superclass.getSimpleName());

        // 返回实现的接口,只会返回当前类的接口,而不会返回父类实现的接口
        Class[] interfaces = cls.getInterfaces();
        System.out.println(interfaces[0].getName());

        // 返回构造方法,不会返回默认的构造方法,即此类没有显示的定义构造方法将返回为空
        Constructor[] constructors = cls.getConstructors();
        System.out.println("构造方法名:" + constructors[0].getName());
        try {
            Constructor constructor = cls.getConstructor(new Class[]{int.class});
            // 利用Constructor对象实例化一个类
            ReflenctionExample example = (ReflenctionExample)constructor.newInstance(1);
            System.out.println("example.field3:" + example.field3);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {

        }


        // 返回所有公共方法
        Method[] methods = cls.getMethods();
        System.out.print("公共方法名:");
        for (Method m : methods) {
            System.out.print(m.getName() + ",");
        }
        System.out.println();
        // 返回所有所有方法
        Method[] method2 = cls.getDeclaredMethods();
        System.out.printf("所有方法名:");
        for (Method m : method2) {
            System.out.print(m.getName() + ",");
        }
        System.out.println();

        // 返回所有公共属性
        Field[] fields = cls.getFields();
        System.out.print("公共属性名:");
        for (Field field : fields) {
            System.out.print(field.getName() + ",");
        }
        System.out.println();


        // 返回所有属性
        Field[] fields2 = cls.getDeclaredFields();
        System.out.print("所有属性名:");
        for (Field field : fields2) {
            System.out.print(field.getName() + ",");
        }
        System.out.println();

        // 返回所有注解
        Annotation[] annotations = cls.getAnnotations();
        System.out.print("所有注解名:");
        for (Annotation annotation : annotations) {
            System.out.println(annotation.annotationType().getName() + ",");
        }
        System.out.println();



    }
}

@RunWith(value = BlockJUnit4ClassRunner.class)
class ReflenctionExample implements InterfaceExample {
    private int field1;
    protected int field2;
    public int field3;

    public ReflenctionExample(int flag) {
        this.field3 = flag;
    }

    public void test1() {
    }

    protected void test2() {
    }

    private void test3() {
    }

}

interface InterfaceExample {

}

 

posted @ 2018-04-03 11:46  幺刀  阅读(149)  评论(0编辑  收藏  举报