java反射获取运行时类的结构

我们可以通过反射,获取对应的运行时类中所有的属性,方法,构造器,父类,接口,父类的泛型,包,注解,异常等
获取属性的方法:

Class cla= Person.class;
// getFields():获取当前与运行时类及其父类中声明为public访问权限的属性
Field[] fields = cla.getFields();
for (Field f:fields){
    System.out.println(f);
}

System.out.println();
// getDeclaredFields():获取当前运行时类中声明的所有属性,不包含父类声明的属性
Field[] declaredFields = cla.getDeclaredFields();
for (Field f:declaredFields){
    System.out.println(f);
}

获取权限修饰符,数据类型,变量的方法:

Class cla=Person.class;
Field[] declaredFields = cla.getDeclaredFields();
for (Field f:declaredFields){
    // 1.权限修饰符
    int modifiers = f.getModifiers();
    System.out.println(Modifier.toString(modifiers));
    // 2.数据类型
    Class type = f.getType();
    System.out.println(type.getName()+"\t");
    // 3.变量名
    String name = f.getName();
    System.out.println(name);

获取运行时类的方法的方式:

Class cla=Person.class;
    // getMethods():获取当前运行时类的方法及其所有父类中声明为public权限的方法
    Method[] methods = cla.getMethods();
    for (Method m:methods){
        System.out.println(m);
    }
    System.out.println();
    // getDeclaredMethods():获取当前运行时类中声明的所有方法,不包含父类中声明的方法
    Method[] declaredMethods = cla.getDeclaredMethods();
    for (Method m:declaredMethods){
        System.out.println(m);
    }
}

获取注解,方法的权限修饰符,返回值类型,方法名(参数类型1 形参名1…)

// 1.获取方法声明的注解
Class cla = Person.class;
Method[] declaredMethods = cla.getDeclaredMethods();
for (Method m : declaredMethods) {
    Annotation[] annotations = m.getAnnotations();
    for (Annotation a : annotations) {
        System.out.println(a);
    }
  // 2.关于权限修饰符
    System.out.print(Modifier.toString(m.getModifiers()) + "\t");
    // 3.返回值类型
    System.out.print(m.getReturnType().getName() + "\t");
    // 4.方法名
    System.out.print(m.getName());
    System.out.print("(");
    // 5.形参列表
    Class[] mExceptionTypes = m.getExceptionTypes();
    if (!(mExceptionTypes == null && mExceptionTypes.length == 0)) {
        for (int i = 0; i < mExceptionTypes.length; i++) {
            if (i == mExceptionTypes.length - 1) {
                System.out.print(mExceptionTypes[i].getName() + "  args_" + i);
                break;
            }
            System.out.print(mExceptionTypes[i].getName() + "  args_" + i + ",");
        }
    }
    System.out.print(")");
    // 6.抛出的异常
    Class[] exceptionTypes = m.getExceptionTypes();
    if (exceptionTypes.length>0){
        System.out.println("throws");
        for (int i = 0; i < exceptionTypes.length; i++) {
            if (i == exceptionTypes.length - 1) {
                System.out.print(exceptionTypes[i].getName());
                break;
            }
            System.out.println(exceptionTypes[i].getName()+",");
        }
    }
    System.out.println();
}

获取构造器

// getConstructors():当前运行时类中声明为public的构造器
Class cla= Person.class;
Constructor[] constructors = cla.getConstructors();
for (Constructor c:constructors){
    System.out.println(c);
}
// getDeclaredConstructors():获取当前运行时类中声明的所有的构造器
Constructor[] declaredConstructors = cla.getDeclaredConstructors();
for (Constructor c:declaredConstructors){
    System.out.println(c);
}

获取运行时的父类,泛型的父类,带泛型的父类的泛型

// 获取运行时类的父类
@Test
public void test1(){
    Class cla=Person.class;
    Class superclass=cla.getSuperclass();
    System.out.println(superclass);
}
// 获取运行时类带泛型的父类
@Test
public void test2(){
    Class cla=Person.class;
    Type genericSuperclass = cla.getGenericSuperclass();
    System.out.println(genericSuperclass);
}

// 获取运行时类带泛型的父类的泛型
@Test
public void test3(){
    Class cla=Person.class;
    Type genericSuperclass = cla.getGenericSuperclass();
    ParameterizedType parameterizedType= (ParameterizedType) genericSuperclass;
    Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
    System.out.println(actualTypeArguments[0].getTypeName());
}

获取运行时类的接口 包 注解

// 获取运行时类的接口
@Test
public void test4(){
    Class cla=Person.class;
    // 获取运行时类的接口
    Class[] interfaces = cla.getInterfaces();
    for (Class c:interfaces){
        System.out.println(c);
    }
    System.out.println();
    // 获取运行时类的父类的接口
    Class[] interfaces1 = cla.getSuperclass().getInterfaces();
    for (Class c:interfaces1){
        System.out.println(c);
    }
}

// 获取运行时类所在的包
@Test
public void test5(){
    Class cla=Person.class;
    Package aPackage = cla.getPackage();
    System.out.println(aPackage);
}

// 获取运行时类所在的注解
@Test
public void test6(){
    Class cla=Person.class;
    Annotation[] annotations = cla.getAnnotations();
    for (Annotation a:annotations){
        System.out.println(a);
    }
}

以上就是在反射中常用到的运行时类的完整结构。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值