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对象:
也类似