JDK 源码:Class

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;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值