|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +categories: [Java] |
| 4 | +description: none |
| 5 | +keywords: Java |
| 6 | +--- |
| 7 | +# Java源码Class类 |
| 8 | + |
| 9 | +## |
| 10 | +我们来看下Class类的源码,源码太多了,挑了几个重点: |
| 11 | +``` |
| 12 | +public final class Class<T> implements java.io.Serializable, |
| 13 | + GenericDeclaration, |
| 14 | + Type, |
| 15 | + AnnotatedElement { |
| 16 | + private static final int ANNOTATION= 0x00002000; |
| 17 | + private static final int ENUM = 0x00004000; |
| 18 | + private static final int SYNTHETIC = 0x00001000; |
| 19 | + |
| 20 | + private static native void registerNatives(); |
| 21 | + static { |
| 22 | + registerNatives(); |
| 23 | + } |
| 24 | + |
| 25 | + /* |
| 26 | + * Private constructor. Only the Java Virtual Machine creates Class objects. |
| 27 | + * This constructor is not used and prevents the default constructor being |
| 28 | + * generated. |
| 29 | + */ |
| 30 | + private Class(ClassLoader loader) { //私有化的 构造器 |
| 31 | + // Initialize final field for classLoader. The initialization value of non-null |
| 32 | + // prevents future JIT optimizations from assuming this final field is null. |
| 33 | + classLoader = loader; |
| 34 | + } |
| 35 | + ... |
| 36 | + |
| 37 | + // reflection data that might get invalidated when JVM TI RedefineClasses() is called |
| 38 | + private static class ReflectionData<T> { |
| 39 | + volatile Field[] declaredFields;//字段 |
| 40 | + volatile Field[] publicFields; |
| 41 | + volatile Method[] declaredMethods;//方法 |
| 42 | + volatile Method[] publicMethods; |
| 43 | + volatile Constructor<T>[] declaredConstructors;//构造器 |
| 44 | + volatile Constructor<T>[] publicConstructors; |
| 45 | + // Intermediate results for getFields and getMethods |
| 46 | + volatile Field[] declaredPublicFields; |
| 47 | + volatile Method[] declaredPublicMethods; |
| 48 | + volatile Class<?>[] interfaces;//接口 |
| 49 | + |
| 50 | + // Value of classRedefinedCount when we created this ReflectionData instance |
| 51 | + final int redefinedCount; |
| 52 | + |
| 53 | + ReflectionData(int redefinedCount) { |
| 54 | + this.redefinedCount = redefinedCount; |
| 55 | + } |
| 56 | + } |
| 57 | + ... |
| 58 | + //注释数据 |
| 59 | + private volatile transient AnnotationData annotationData; |
| 60 | + |
| 61 | + private AnnotationData annotationData() { |
| 62 | + while (true) { // retry loop |
| 63 | + AnnotationData annotationData = this.annotationData; |
| 64 | + int classRedefinedCount = this.classRedefinedCount; |
| 65 | + if (annotationData != null && |
| 66 | + annotationData.redefinedCount == classRedefinedCount) { |
| 67 | + return annotationData; |
| 68 | + } |
| 69 | + // null or stale annotationData -> optimistically create new instance |
| 70 | + AnnotationData newAnnotationData = createAnnotationData(classRedefinedCount); |
| 71 | + // try to install it |
| 72 | + if (Atomic.casAnnotationData(this, annotationData, newAnnotationData)) { |
| 73 | + // successfully installed new AnnotationData |
| 74 | + return newAnnotationData; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + ... |
| 79 | +``` |
| 80 | +Class类的构造方法是private, 只有JVM能创建Class实例 ,我们开发人员 是无法创建Class实例的,JVM在构造Class对象时,需要传入一个 类加载器 。 |
| 81 | + |
| 82 | +类也是可以用来存储数据的,Class类就像 普通类的模板 一样,用来保存“类所有相关信息”的类 。 |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
0 commit comments