Java源码解析(4) —— Class(3)

时间:2022-06-14 22:32:44
//返回类中所有内部类,这里的类包括数组、接口等
@CallerSensitive
public Class<?>[] getDeclaredClasses() throws SecurityException {
checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), false);//检查访问权限,下同
return getDeclaredClasses0();
}
/**
* 1.getDeclaredClasses():返回类中定义的公共、私有、保护的、默认的(即所有访
* 问属性限制的)内部类,包括接口,不包括匿名内部类,以及继承的内部类,如果没有或此
* Class对象表示的是基本类型、数组类或 Void,则返回长度为零的数组
**/

——————————————————————————————————————————————————————————————————
//返回类中成员字段
@CallerSensitive
public Field[] getDeclaredFields() throws SecurityException {
checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
return copyFields(privateGetDeclaredFields(false));
}
/**
* 2.getDeclaredFields():返回类中所有字段,不管是私有的还是公有、默认、保护
* 的,但不包括继承的,若此Class对象表示的是基本类型、数组类或 Void,则返回长度为
* 零的数组
*/

——————————————————————————————————————————————————————————————————
//返回类中所有成员方法
@CallerSensitive
public Method[] getDeclaredMethods() throws SecurityException {
checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
return copyMethods(privateGetDeclaredMethods(false));
}
/**
* 3.getDeclaredMethods():同上,但返回的是Method数组,即获取类中所有的方法
*/

——————————————————————————————————————————————————————————————————
//返回类中所有构造器
@CallerSensitive
public Constructor<?>[] getDeclaredConstructors() throws SecurityException {
checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
return copyConstructors(privateGetDeclaredConstructors(false));
}
/**
* 4.getDeclaredMethods():同上,但返回的是Constructor数组,即获取类中所有的
* 构造器
*/

——————————————————————————————————————————————————————————————————
//返回对应的字段Field对象
@CallerSensitive
public Field getDeclaredField(String name)
throws NoSuchFieldException, SecurityException {
checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
Field field = searchFields(privateGetDeclaredFields(false), name);
if (field == null) {
throw new NoSuchFieldException(name);
}
return field;
}
/**
* 5.getDeclaredField():对给出的name获取对应的类中的字段(Field对象)
* 若不存在,则抛出NoSuchFieldException异常
*/

——————————————————————————————————————————————————————————————————
//返回对应的Method对象,name是方法名称,parameterTypes是对应形参
@CallerSensitive
public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
throws NoSuchMethodException, SecurityException {
checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
Method method = searchMethods(privateGetDeclaredMethods(false), name, parameterTypes);
if (method == null) {
throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
}
return method;
}
/**
* 6.getDeclaredMethod():对给出的name获取对应的类中的方法(Method对象)
* 若不存在,则抛出NoSuchMethodException异常
*/

——————————————————————————————————————————————————————————————————
//类似上面的getDeclaredMethod
@CallerSensitive
public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
throws NoSuchMethodException, SecurityException {
checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
return getConstructor0(parameterTypes, Member.DECLARED);
}
//获取参数中指定的资源,以字节流返回
public InputStream getResourceAsStream(String name) {
name = resolveName(name);
ClassLoader cl = getClassLoader0();
if (cl==null) {
return ClassLoader.getSystemResourceAsStream(name);
}
return cl.getResourceAsStream(name);
}
//获取参数中指定的资源,以URL对象返回
public java.net.URL getResource(String name) {
name = resolveName(name);
ClassLoader cl = getClassLoader0();
if (cl==null) {
return ClassLoader.getSystemResource(name);
}
return cl.getResource(name);
}
//关于getResource() 可以参考
//http://www.cnblogs.com/yejg1212/p/3270152.html
private static java.security.ProtectionDomain allPermDomain;
//Java的保护域,以下关于Java安全管理机制及策略相关知识,我自己也没看233
public java.security.ProtectionDomain getProtectionDomain() {
}
private native java.security.ProtectionDomain getProtectionDomain0();
native void setProtectionDomain0(java.security.ProtectionDomain pd);
static native Class getPrimitiveClass(String name);
private static class SecurityManagerHelper {}
private static volatile SecurityManagerHelper smHelper;
private static boolean isCheckMemberAccessOverridden(SecurityManager sm) {}
private void checkMemberAccess(int which, Class<?> caller, boolean checkProxyInterfaces) {}
private void checkPackageAccess(final ClassLoader ccl, boolean checkProxyInterfaces) {
private String resolveName(String name) {}
private static class Atomic {//原子操作支持
private static boolean useCaches = true;
static class ReflectionData<T> {//反射获得信息类集合
volatile Field[] declaredFields;
volatile Field[] publicFields;
volatile Method[] declaredMethods;
volatile Method[] publicMethods;
volatile Constructor<T>[] declaredConstructors;
volatile Constructor<T>[] publicConstructors;
// Intermediate results for getFields and getMethods
volatile Field[] declaredPublicFields;
volatile Method[] declaredPublicMethods;
// Value of classRedefinedCount when we created this ReflectionData instance
final int redefinedCount;

ReflectionData(int redefinedCount) {
this.redefinedCount = redefinedCount;
}
}

  关于checkMemberAccess(),默认是要做访问权限检查的,所谓的访问权限,即public/protected/默认/private修饰权限,如果反射想要访问没权限访问的字段(属性)、方法等,可以进行设置从而获得访问权限,比如字段:Field field = fields.get(0);field.setAccessible(true);才能访问private等修饰的字段值。当然,这其实是一种对访问权限的破坏,其操作可能带来不安全,所以操作需谨慎。