Class
类是 Java 中的一个关键类,它代表了一个类的运行时对象。Class
类的实例包含了该类的结构信息,例如类的字段、方法、构造函数等。以下是 JDK 8 中 Class
类的部分源代码:
public final class Class<T> implements Serializable, GenericDeclaration, Type, AnnotatedElement {
private static final long serialVersionUID = 3206093459760846163L;
private static native void registerNatives();
static {
registerNatives();
}
private Class() {}
public String getName() {
return (isPrimitive() ? getName() : ("L" + getName() + ";")).replace('.', '/');
}
public ClassLoader getClassLoader() {
return getClassLoader0();
}
private native ClassLoader getClassLoader0();
public Class<?> getSuperclass() {
return (Class<?>) getGenericSuperclass();
}
public Type getGenericSuperclass() {
if (isInterface()) {
return null;
} else {
return getGenericSuperclass0();
}
}
private native Type getGenericSuperclass0();
public Class<?>[] getInterfaces() {
return (Class<?>[]) getInterfaces0();
}
private native Class<?>[] getInterfaces0();
public Type[] getGenericInterfaces() {
return (Type[]) getGenericInterfaces0();
}
private native Type[] getGenericInterfaces0();
public Class<?> getComponentType() {
return null;
}
public int getModifiers() {
return getModifiers0();
}
private native int getModifiers0();
public boolean isPrimitive() {
return isPrimitive0();
}
private native boolean isPrimitive0();
public boolean isArray() {
return getComponentType() != null;
}
public boolean isInterface() {
return (getModifiers() & Modifier.INTERFACE) != 0;
}
public boolean isInstance(Object obj) {
return isInstance(obj, this);
}
public boolean isAssignableFrom(Class<?> cls) {
return isAssignableFrom(this, cls);
}
public boolean isAssignableFrom(Class<?> cls1, Class<?> cls2) {
return (cls1 == cls2) || cls1.isAssignableFrom(cls2);
}
public Object newInstance() throws InstantiationException, IllegalAccessException {
if (System.getSecurityManager() != null) {
throw new SecurityException();
}
return newInstance0();
}
private native Object newInstance0() throws InstantiationException, IllegalAccessException;
public String toString() {
return (isInterface() ? "interface " : (isPrimitive() ? "" : "class ")) + getName();
}
public String toGenericString() {
return (isInterface() ? "interface " : (isPrimitive() ? "" : "class ")) + getName();
}
public T cast(Object obj) {
if (obj != null && !isInstance(obj)) {
throw new ClassCastException(cannotCastMsg(obj));
}
return (T) obj;
}
private String cannotCastMsg(Object obj) {
return "Cannot cast " + obj.getClass().getName() + " to " + getName();
}
public Class<?> asSubclass(Class<?> clazz) {
if (!clazz.isAssignableFrom(this)) {
throw new ClassCastException(toString() + " cannot be cast to " + clazz.getName());
}
return this;
}
public <U> Class<? extends U> asSubclass(Class<U> clazz) {
if (!clazz.isAssignableFrom(this)) {
throw new ClassCastException(toString() + " cannot be cast to " + clazz.getName());
}
return (Class<? extends U>) this;
}
public boolean desiredAssertionStatus() {
return desiredAssertionStatus0(this);
}
private static native boolean desiredAssertionStatus0(Class<?> clazz);
public ClassValue<T> classValue() {
return new ClassValue<T>() {
@Override
protected T computeValue(Class<?> type) {
return null;
}
};
}
public TypeVariable<Class<T>>[] getTypeParameters() {
return (TypeVariable<Class<T>>[]) new TypeVariable<?>[0];
}
public boolean isAnnotation() {
return (getModifiers() & Modifier.ANNOTATION) != 0;
}
public boolean isSynthetic() {
return (getModifiers() & Modifier.SYNTHETIC) != 0;
}
public boolean isEnum() {
return (getModifiers() & Modifier.ENUM) != 0;
}
public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
return null;
}
public Annotation[] getAnnotations() {
return new Annotation[0];
}
public Annotation[] getDeclaredAnnotations() {
return new Annotation[0];
}
public boolean isLocalClass() {
return (getModifiers() & (Modifier.STATIC | Modifier.PRIVATE | Modifier.PROTECTED)) == 0 &&
getEnclosingMethod() == null;
}
public boolean isMemberClass() {
return getEnclosingClass() != null;
}
public Class<?> getDeclaringClass() {
return getDeclaringClass0();
}
private native Class<?> getDeclaringClass0();
public Class<?> getEnclosingClass() {
return getEnclosingClass0();
}
private native Class<?> getEnclosingClass0();
public Constructor<?> getEnclosingConstructor() throws SecurityException {
return null;
}
public Method getEnclosingMethod() throws SecurityException {
return null;
}
public String getSimpleName() {
return "";
}
public Module getModule() {
return null;
}