09、jvm源码解读 - enum WKID 枚举

源码中对于枚举类型WKID的使用

  static bool initialize_wk_klass(WKID id, int init_opt, TRAPS);
  static void initialize_wk_klasses_until(WKID limit_id, WKID &start_id, TRAPS);
  static void initialize_wk_klasses_through(WKID end_id, WKID &start_id, TRAPS) {
    int limit = (int)end_id + 1;
    initialize_wk_klasses_until((WKID) limit, start_id, THREAD);

这个是宏定义,类似于以下简码

int main() {
    enum EM{
        day1,b1=day1,
        day2,b2=day2,

    };
    std::cout << "day2的值为:" << std::endl;
    std::cout << EM::day2 << std::endl;
    std::cout << "b1 的值为:" << std::endl;
    std::cout << EM::b1 << std::endl;
    return 0;
}
打印结果
day2的值为:
1
b1 的值为:
0

能看出来,这个枚举非常特殊,

那么看WKID

enum WKID {
    NO_WKID = 0,
  /* well-known classes */                                                                                               
  Object_klass_knum,                                java_lang_Object_knum = Object_klass_knum,                                           
  String_klass_knum,                                java_lang_String_knum = String_klass_knum,                                           
  Class_klass_knum,                                 java_lang_Class_knum = Class_klass_knum,                                            
  Cloneable_klass_knum,                             java_lang_Cloneable_knum = Cloneable_klass_knum,                                        
  ClassLoader_klass_knum,                           java_lang_ClassLoader_knum = Cloneable_klass_knum,                                      
  Serializable_klass_knum,                          java_io_Serializable_knum = Serializable_klass_knum,                                       
  System_klass_knum,                                java_lang_System_knum = System_klass_knum,                                           
  Throwable_klass_knum,                             java_lang_Throwable_knum = Throwable_klass_knum,                                        
  Error_klass_knum,                                 java_lang_Error_knum = Error_klass_knum,                                            
  ThreadDeath_klass_knum,                           java_lang_ThreadDeath_knum = ThreadDeath_klass_knum,                                      
  Exception_klass_knum,                             java_lang_Exception_knum = Exception_klass_knum,,                                        
  RuntimeException_klass_knum,                      java_lang_RuntimeException_knum = RuntimeException_klass_knum,                                 
  SecurityManager_klass_knum,                       java_lang_SecurityManager_knum = SecurityManager_klass_knum,                                  
  ProtectionDomain_klass_knum,                      java_security_ProtectionDomain_knum = ProtectionDomain_klass_knum,                             
  AccessControlContext_klass_knum,                  java_security_AccessControlContext_knum = AccessControlContext_klass_knum,                         
  ClassNotFoundException_klass_knum,                java_lang_ClassNotFoundException_knum = ClassNotFoundException_klass_knum,                           
  NoClassDefFoundError_klass_knum,                  java_lang_NoClassDefFoundError_knum = NoClassDefFoundError_klass_knum,                             
  LinkageError_klass_knum,                          java_lang_LinkageError_knum = LinkageError_klass_knum,                                     
  ClassCastException_klass_knum,                    java_lang_ClassCastException_knum = ClassCastException_klass_knum,                               
  ArrayStoreException_klass_knum,                   java_lang_ArrayStoreException_knum = ArrayStoreException_klass_knum,                              
  VirtualMachineError_klass_knum,                   java_lang_VirtualMachineError_knum = VirtualMachineError_klass_knum,                              
  OutOfMemoryError_klass_knum,                      java_lang_OutOfMemoryError_knum = OutOfMemoryError_klass_knum,                                 
  StackOverflowError_klass_knum,                    java_lang_StackOverflowError_knum = StackOverflowError_klass_knum,                               
  IllegalMonitorStateException_klass_knum,          java_lang_IllegalMonitorStateException_knum = IllegalMonitorStateException_klass_knum,                     
  Reference_klass_knum,                             java_lang_ref_Reference_knum = Reference_klass_knum,                                    

  /* load ref klasses and set reference types */                                                                      
  SoftReference_klass_knum,                         java_lang_ref_SoftReference_knum = SoftReference_klass_knum,                                
  WeakReference_klass_knum,                         java_lang_ref_WeakReference_knum = WeakReference_klass_knum,                                
  FinalReference_klass_knum,                        java_lang_ref_FinalReference_knum = FinalReference_klass_knum,                               
  PhantomReference_klass_knum,                      java_lang_ref_PhantomReference_knum = PhantomReference_klass_knum,                             
  Finalizer_klass_knum,                             java_lang_ref_Finalizer_knum = Finalizer_klass_knum,                                    

  Thread_klass_knum,                                java_lang_Thread_knum = Thread_klass_knum,                                           
  ThreadGroup_klass_knum,                           java_lang_ThreadGroup_knum = ThreadGroup_klass_knum,                                      
  Properties_klass_knum,                            java_util_Properties_knum = Properties_klass_knum,                                       
  reflect_AccessibleObject_klass_knum,              java_lang_reflect_AccessibleObject_knum = reflect_AccessibleObject_klass_knum,                         
  reflect_Field_klass_knum,                         java_lang_reflect_Field_knum = reflect_Field_klass_knum,                                    
  reflect_Parameter_klass_knum,                     java_lang_reflect_Parameter_knum = reflect_Parameter_klass_knum,                                
  reflect_Method_klass_knum,                        java_lang_reflect_Method_knum = reflect_Method_klass_knum,                                   
  reflect_Constructor_klass_knum,                   java_lang_reflect_Constructor_knum = reflect_Constructor_klass_knum,                              

  /* NOTE: needed too early in bootstrapping process to have checks based on JDK version */                              
  /* Universe::is_gte_jdk14x_version( is not set up by this point. */                                                   
  /* It's okay if this turns out to be NULL in non-1.4 JDKs. */                                                          
  reflect_MagicAccessorImpl_klass_knum,             sun_reflect_MagicAccessorImpl_knum = reflect_MagicAccessorImpl_klass_knum,                              
  reflect_MethodAccessorImpl_klass_knum,            sun_reflect_MethodAccessorImpl_knum = reflect_MethodAccessorImpl_klass_knum,            
  reflect_ConstructorAccessorImpl_klass_knum,       sun_reflect_ConstructorAccessorImpl_knum = reflect_ConstructorAccessorImpl_klass_knum,       
  reflect_DelegatingClassLoader_klass_knum,         sun_reflect_DelegatingClassLoader_knum = reflect_DelegatingClassLoader_klass_knum,                          
  reflect_ConstantPool_klass_knum,                  sun_reflect_ConstantPool_knum = reflect_ConstantPool_klass_knum,                        
  reflect_UnsafeStaticFieldAccessorImpl_klass_knum, sun_reflect_UnsafeStaticFieldAccessorImpl_knum = reflect_UnsafeStaticFieldAccessorImpl_klass_knum,       
  reflect_CallerSensitive_klass_knum,               sun_reflect_CallerSensitive_knum = reflect_CallerSensitive_klass_knum,                                

  /* support for dynamic typing; it's OK if these are NULL in earlier JDKs */                                            
  DirectMethodHandle_klass_knum,                    java_lang_invoke_DirectMethodHandle_knum = DirectMethodHandle_klass_knum,                        
  MethodHandle_klass_knum,                          java_lang_invoke_MethodHandle_knum = MethodHandle_klass_knum,                       
  MemberName_klass_knum,                            java_lang_invoke_MemberName_knum = MemberName_klass_knum,                         
  MethodHandleNatives_klass_knum,                   java_lang_invoke_MethodHandleNatives_knum = MethodHandleNatives_klass_knum,                
  LambdaForm_klass_knum,                            java_lang_invoke_LambdaForm_knum = LambdaForm_klass_knum,                                
  MethodType_klass_knum,                            java_lang_invoke_MethodType_knum = MethodType_klass_knum,                         
  BootstrapMethodError_klass_knum,                  java_lang_BootstrapMethodError_knum = BootstrapMethodError_klass_knum,                      
  CallSite_klass_knum,                              java_lang_invoke_CallSite_knum = CallSite_klass_knum,                           
  ConstantCallSite_klass_knum,                      java_lang_invoke_ConstantCallSite_knum = ConstantCallSite_klass_knum,                   
  MutableCallSite_klass_knum,                       java_lang_invoke_MutableCallSite_knum = MutableCallSite_klass_knum,                    
  VolatileCallSite_klass_knum,                      java_lang_invoke_VolatileCallSite_knum = VolatileCallSite_klass_knum,                   
  /* Note: MethodHandle must be first, and VolatileCallSite last in group */                                             

  StringBuffer_klass_knum,                          java_lang_StringBuffer_knum = StringBuffer_klass_knum,                                     
  StringBuilder_klass_knum,                         java_lang_StringBuilder_knum = StringBuilder_klass_knum,                                    
  misc_Unsafe_klass_knum,                           sun_misc_Unsafe_knum = misc_Unsafe_klass_knum,                                            

  /* It's NULL in non-1.4 JDKs. */                                                                                       
  StackTraceElement_klass_knum,                     java_lang_StackTraceElement_knum = StackTraceElement_klass_knum,                                
  /* Universe::is_gte_jdk14x_version( is not set up by this point. */                                                   
  /* It's okay if this turns out to be NULL in non-1.4 JDKs. */                                                          
  nio_Buffer_klass_knum,                            java_nio_Buffer_knum = nio_Buffer_klass_knum,                                            

  /* load boxing klasses */                                                                                           
  Boolean_klass_knum,                               java_lang_Boolean_knum = Boolean_klass_knum,                                          
  Character_klass_knum,                             java_lang_Character_knum = Character_klass_knum,                                        
  Float_klass_knum,                                 java_lang_Float_knum = Float_klass_knum,                                            
  Double_klass_knum,                                java_lang_Double_knum = Double_klass_knum,                                           
  Byte_klass_knum,                                  java_lang_Byte_knum = Byte_klass_knum,                                             
  Short_klass_knum,                                 java_lang_Short_knum = Short_klass_knum,                                            
  Integer_klass_knum,                               java_lang_Integer_knum = Integer_klass_knum,                                          
  Long_klass_knum,                                  java_lang_Long_knum = Long_klass_knum,           //68                                  
  /*end*/
  WKID_LIMIT, //69

    FIRST_WKID = NO_WKID + 1
};

这个宏是之前是看不懂的,今天顺利的解决了,通过clion的replace R 功能,替换,先将代码写道一个cpp文件中,全部替换就行了

 public:
  enum WKID {
    NO_WKID = 0,

    #define WK_KLASS_ENUM(name, symbol, ignore_o) WK_KLASS_ENUM_NAME(name), WK_KLASS_ENUM_NAME(symbol) = WK_KLASS_ENUM_NAME(name),
    WK_KLASSES_DO(WK_KLASS_ENUM)
    #undef WK_KLASS_ENUM

    WKID_LIMIT,

    FIRST_WKID = NO_WKID + 1
  };

这里非常重要的是

#define WK_KLASS_ENUM(name, symbol, ignore_o) WK_KLASS_ENUM_NAME(name), WK_KLASS_ENUM_NAME(symbol) = WK_KLASS_ENUM_NAME(name),
//

这个一行看成两部分,用 空格作为区分