Class类的实例表示正在运行的Java应用程序中的类和接口。每个数组属于被映射为Class对象的一个类,所有具有相同元素类型和纬数的数组共享该Class对象。Class对象是在加载类时由虚拟机通过类加载器defineClass方法自动构造的。所有定位维护的类和接口,都是Class的实例。Object类方法唯一引入返回的类型。基本数据类型和void关键字都是Class类的实例。Final修饰,是不变类,不能被继承。实现了反射包java.lang.reflect中的三接口:AnnotatedElement、GenericDeclaration、Type。Type是java语言中所有类型的公共高级接口。包括原始类型、参数化类型、数组类型、类型变量、基本类型(1.5版本才有的);1.8版本对此接口增加一个默认方法。
package java.lang.reflect;
public interface Type {
/**
* 返回类型的字符串描述
*/
default String getTypeName() {
return toString();
}
}
GenericDeclaration是声明类型变量的所有实例的公共接口。返回一个声明顺序的类型变量的数组,如果未声明类型变量,返回数组长度是0。
/**
* 返回Class类的一个对象,就是接口或者类。相当于查找加载类
*/
@CallerSensitive
public static Class<?> forName(String className)throws ClassNotFoundException {
Class<?> caller = Reflection.getCallerClass();
return forName0(className, true, ClassLoader.getClassLoader(caller), caller);
}
/**
* 返回一个指定加载器的Class类的一个对象实例(类和接口)
* 是否必须初始化类
*/
@CallerSensitive
public static Class<?> forName(String name, boolean initialize,ClassLoaderloader)
throws ClassNotFoundException {
Class<?> caller = null;
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
caller = Reflection.getCallerClass();
if (sun.misc.VM.isSystemDomainLoader(loader)) {
ClassLoader ccl = ClassLoader.getClassLoader(caller);
if (!sun.misc.VM.isSystemDomainLoader(ccl)) {
sm.checkPermission(
SecurityConstants.GET_CLASSLOADER_PERMISSION);
}
}
}
return forName0(name, initialize, loader, caller);
}
private static native Class<?> forName0(String name, boolean initialize,
ClassLoader loader,
Class<?> caller)
throws ClassNotFoundException;
@CallerSensitive
public T newInstance() throws InstantiationException, IllegalAccessException {
if (System.getSecurityManager() != null) {
checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), false);
}
if (cachedConstructor == null) {
if (this == Class.class) {
throw new IllegalAccessException(
"Can not call newInstance() on the Class for java.lang.Class"
);
}
try {
Class<?>[] empty = {};
final Constructor<T> c = getConstructor0(empty, Member.DECLARED);
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<Void>() {
public Void run() {
c.setAccessible(true);
return null;
}
});
cachedConstructor = c;
} catch (NoSuchMethodException e) {
throw (InstantiationException)
new InstantiationException(getName()).initCause(e);
}
}
Constructor<T> tmpConstructor = cachedConstructor;
int modifiers = tmpConstructor.getModifiers();
if (!Reflection.quickCheckMemberAccess(this, modifiers)) {
Class<?> caller = Reflection.getCallerClass();
if (newInstanceCallerCache != caller) {
Reflection.ensureMemberAccess(caller, this, null, modifiers);
newInstanceCallerCache = caller;
}
}
// Run constructor
try {
return tmpConstructor.newInstance((Object[])null);
} catch (InvocationTargetException e) {
Unsafe.getUnsafe().throwException(e.getTargetException());
// Not reached
return null;
}
}
创建此 Class 对象所表示的类的一个新实例。如同用一个带有一个空参数列表的 new
表达式实例化该类。如果该类尚未初始化,则初始化这个类。
public String getName() {
String name = this.name;
if (name == null)
this.name = name = getName0();
return name;
}
private transient String name;
private native String getName0();
返回Class对象实例所表示的类或接口名(全路径名)。
@CallerSensitive
public ClassLoader getClassLoader() {
ClassLoader cl = getClassLoader0();
if (cl == null)
return null;
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
ClassLoader.checkClassLoaderPermission(cl, Reflection.getCallerClass());
}
return cl;
}
ClassLoader getClassLoader0() { return classLoader; }
private final ClassLoader classLoader;
返回该类的加载器,引导加载器类返回的是null ;基本类型或者void都会返回Null。
public Package getPackage() {
return Package.getPackage(this);
}
返回此类的包名。
public Class<?>[] getInterfaces() {
ReflectionData<T> rd = reflectionData();
if (rd == null) {
return getInterfaces0();
} else {
Class<?>[] interfaces = rd.interfaces;
if (interfaces == null) {
interfaces = getInterfaces0();
rd.interfaces = interfaces;
}
return interfaces.clone();
}
}
private native Class<?>[] getInterfaces0();
返回此实例对象所表示类或接口所实现的接口。