反射使用(二)

反射使用(二)

import org.junit.Test;
import java.lang.annotation.Annotation;
import java.lang.reflect.*;

/**
 * @author 小邱
 * @version 0.0.1
 * @description ReflectionTest1
 * @since 2021/9/15 12:03
 */
public class ReflectionTest1 {
    //Field
    @Test
    public void test1() {
        Class clazz = Person.class;

        //获取属性结构
        //获取当前运行类以及父类中所有public属性
        Field[] fields1 = clazz.getFields();
        //获取当前运行类中所有修饰的属性(不包含父类)
        Field[] fields2 = clazz.getDeclaredFields();

        for (Field f : fields2) {
            //获取权限修饰符
            int modifiers = f.getModifiers();//返回修饰符权限对应的数值
            System.out.print(Modifier.toString(modifiers) + "\t");//还原修饰符

            //获取数据类型
            Class type = f.getType();
            System.out.print(type.getName() + "\t");

            //获取变量名
            String name = f.getName();
            System.out.println(name);

        }
    }

    //method
    @Test
    public void test2() {
        Class clazz = Person.class;

        //获取方法
        //获取当前运行类以及父类中所有public方法
        Method[] methods1 = clazz.getMethods();
        //获取当前运行类中所有修饰的方法(不包含父类)
        Method[] methods2 = clazz.getDeclaredMethods();

        for (Method method : methods2) {
            //获取注解
            Annotation[] annotations = method.getAnnotations();
            for (Annotation a : annotations) {
                System.out.println(a);//@MyAnnotation(value=method)
            }
            //获取权限修饰符
            int modifiers = method.getModifiers();
            System.out.print(Modifier.toString(modifiers) + "\t");
            //获取返回值类型
            Class type = method.getReturnType();
            System.out.print(type.getName() + "\t");
            //获取方法名
            String name = method.getName();
            System.out.print(name + "(");
            //获取形参列表
            Class[] types = method.getParameterTypes();
            if (types != null || types.length != 0) {
                for (int i = 0; i < types.length; i++) {
                    if (i == types.length - 1) {
                        System.out.print(types[i].getName() + "\t" + "args_" + i);
                        break;
                    }
                    System.out.print(types[i].getName() + "\t" + "args_" + i + "," + "\t");
                }
            }
            System.out.print(")" + "\t");
            //获取异常
            Class[] exceptionTypes = method.getExceptionTypes();
            if (exceptionTypes.length > 0) {
                System.out.print("throws" + "\t");
                for (int i = 0; i < exceptionTypes.length; i++) {
                    if (i == exceptionTypes.length - 1) {
                        System.out.print(exceptionTypes[i].getName());
                        break;
                    }
                    System.out.print(exceptionTypes[i].getName() + ',');
                }
            }

            System.out.println();

        }
    }

    //constructor
    @Test
    public void test3(){
        Class clazz = Person.class;
        //获取当前运行类中的public构造器
        Constructor[] constructor = clazz.getConstructors();
        //获取当前运行类中的所有构造器
        Constructor[] constructors = clazz.getDeclaredConstructors();


    }

    //运行时的父类及父类泛型
    @Test
    public void test4(){
        Class clazz = Person.class;

        //获取父类
        Class superclass = clazz.getSuperclass();
        System.out.println(superclass);//class Creature

        //获取带泛型的父类
        Type genericSuperclass = clazz.getGenericSuperclass();
        System.out.println(genericSuperclass);//Creature<java.lang.String>
        //获取泛型类型
        ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
        Type[] types = parameterizedType.getActualTypeArguments();
        System.out.println(types[0].getTypeName());//java.lang.String
        System.out.println(((Class)types[0]).getName());//java.lang.String


    }

    //运行时类的接口、包、注解
    @Test
    public void test5(){
        Class clazz = Person.class;
        //获取运行时类实现的接口
        Class[] interfaces1 = clazz.getInterfaces();
        for (Class i : interfaces1) {
            System.out.println(i);
        }
        //获取运行时的父类实现的接口
        Class[] interfaces2 = clazz.getSuperclass().getInterfaces();
        for (Class i : interfaces2) {
            System.out.println(i);
        }
        //获取运行时类所在的包
        Package aPackage = clazz.getPackage();
        System.out.println(aPackage);

        获取运行时类声明的注解
        Annotation[] annotations = clazz.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);//@MyAnnotation(value=class)
        }

    }

    //调用运行时类指定的结构
    @Test
    public void test6() throws Exception {
        Class clazz = Person.class;

        //创建运行时类对象
        Object o = clazz.newInstance();

        //获取指定属性
        //getField获取为public的(不推荐)
        Field id = clazz.getField("id");
        //getDeclaredField获取指定变量属性
        Field name = clazz.getDeclaredField("name");
        //修改成可访问的
        name.setAccessible(true);

        //设置当前属性值
        id.set(o,1001);
        name.set(o,"tom");

        //获取当前属性值
        System.out.println(id.get(o));//1001
        System.out.println(name.get(o));//tom

        //获取指定的方法
        //getDeclaredMethod("方法名",形参.class)
        Method show = clazz.getDeclaredMethod("show", String.class);
        //修改成可访问的
        show.setAccessible(true);
        //invoke(调用对象,实参)返回值为调用方法的返回值
        Object china = show.invoke(o, "China");//我的国籍是China
        System.out.println(china);//China

        //调用静态方法
        Method showDesc = clazz.getDeclaredMethod("showDesc");
        showDesc.setAccessible(true);
        //静态方法可以不需要传对象
        Object invoke = showDesc.invoke(null);//我是静态方法
        //没有返回值即为null
        System.out.println(invoke);//null

        //调用指定的构造器
        //getDeclaredConstructor(形参.class)
        Constructor name1 = clazz.getDeclaredConstructor(String.class);
        //修改成可访问的
        name1.setAccessible(true);
        //创建对象
        Object tom = name1.newInstance("tom");
        System.out.println(tom);//Person{name='tom', age=0, id=0}


    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值