获取运行时类的结构

通过反射创建对应的运行时类的对象

public void test1() throws IllegalAccessException, InstantiationException {
    Class<Person> clazz = Person.class;
    /*
    newInstance():调用此方法,创建对应的运行时类的对象
    内部调用了运行时类的空参构造器
    要想此方法正常的创建运行时类的对象,要求:
    1.运行时类必须提供空参的构造器
    2.空参的构造器的访问权限得够。通常设置为public

    在javabean中要求提供一个public的空参构造器。原因:
    1.便于反射,创建运行时类的对象
    2.便于子类继承此运行时类时,默认调用super(),保证父类有此构造器
     */
    Person obj = clazz.newInstance();//调用空参构造器
    System.out.println(obj);//Person{name='null', age=0}

}

获取运行时类的方法结构:

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

        }


    }

    /*
    权限修饰符 返回值类型 方法名(参数类型1 形参名1) throws XxxException
     */
    @Test
    public void test2() {

        Class clazz = Person.class;
        Method[] declaredMethod = clazz.getDeclaredMethods();
        for (Method m : declaredMethod
        ) {
            //1.获取方法声明的注解
            Annotation[] annotations = m.getAnnotations();
            for (Annotation a : annotations
            ) {
                System.out.print(a + "\n");
            }
            //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[] parameterTypes = m.getParameterTypes();
            if (!(parameterTypes == null && parameterTypes.length == 0)) {
                for (int i = 0; i < parameterTypes.length; i++) {
                    if (i == parameterTypes.length - 1) {
                        System.out.print(parameterTypes[i] + "args_" + i);
                        break;

                    }
                    System.out.print(parameterTypes[i] + "args_" + i + ",");

                }
            }
            System.out.print(")");
            //6.抛出的异常
            Class[] exceptionTypes = m.getExceptionTypes();
            if (exceptionTypes.length>0) {
                System.out.println("throws" + "\t");
                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();

        }

    }
}

其他结构:

@Test
public void test1(){
    Class clazz= Person.class;
    //获取当前运行时类中声明为public的构造器
    Constructor[] constructors = clazz.getConstructors();
    for (Constructor c: constructors
         ) {
        System.out.println(c);

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

    }

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

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值