java反射总结

Class类中Medthod、Filed、Constructor的方法都有Declare,这跟不加declare有什么区别,我们看一下源码定义:

以获取Constructor对象的方法为例看一下:

@CallerSensitive
public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
    throws NoSuchMethodException, SecurityException {
    checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
    return getConstructor0(parameterTypes, Member.DECLARED);
}
@CallerSensitive
public Constructor<T> getConstructor(Class<?>... parameterTypes)
    throws NoSuchMethodException, SecurityException {
    checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
    return getConstructor0(parameterTypes, Member.PUBLIC);
}

自行对比这两个方法,返回值处传的参数不同,Member.DECLARED和Member.PUBLIC正好与方法名对应,继续看一下这个常量的定义:

/**
 * Identifies the set of all public members of a class or interface,
 * including inherited members.
 */
public static final int PUBLIC = 0;

/**
 * Identifies the set of declared members of a class or interface.
 * Inherited members are not included.
 */
public static final int DECLARED = 1;

可以看到,PUBLIC是获取所有public修饰的类后者接口的构造方法。包含继承的类。DECLARED标识public修饰的类后者接口的构造方法,但不包含继承的成员。

由此我们可以得出以getDeclared获取所有类各个部分的组件对象,包含继承的类。

我们可以基本推测出class类中所有的方法了,如下:

Method对象:

Method getMethod(String name, Class<?> … parameterType)

Method[] getMethods()

Method getDeclaredMethod(String name, Class<?> … parameterType)

Method[] getDeclaredMethods()

Constructor对象:

Constructor getConstructor(Class<?>… parameterTypes)

Constructor<?>[] getConstructors()

Constructor getDeclaredConstructor(Class<?>… parameterTypes)

Constructor<?>[] getDeclaredConstructors()

Field对象:

Field getField(String name)

Field[] getFields()

Field getDeclaredField(String name)

Field[] getDeclaredFields()

Annotation对象:

也类似

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值