问题

当更新了类之后,怎么样在生产环境判断类或对象中的字段,方法有没有被正常更新呢?这个时候可以使用Arthas的sc命令来查找JVM中已经加载的类

操作

  • sc -d com.ct.hear.entity.*
    通过这个命令可知,实体类加载了两个对象,Article和MessageLog
[arthas@4967]$ sc -d com.ct.hear.entity.*
 class-info        com.ct.hear.entity.Article                                                                                                                                                                    
 code-source       file:/data/1data/1data-project-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/                                                                                                                         
 name              com.ct.hear.entity.Article                                                                                                                                                                    
 isInterface       false                                                                                                                                                                                         
 isAnnotation      false                                                                                                                                                                                         
 isEnum            false                                                                                                                                                                                         
 isAnonymousClass  false                                                                                                                                                                                         
 isArray           false                                                                                                                                                                                         
 isLocalClass      false                                                                                                                                                                                         
 isMemberClass     false                                                                                                                                                                                         
 isPrimitive       false                                                                                                                                                                                         
 isSynthetic       false                                                                                                                                                                                         
 simple-name       Article                                                                                                                                                                                       
 modifier          public                                                                                                                                                                                        
 annotation        io.swagger.annotations.ApiModel                                                                                                                                                               
 interfaces        java.io.Serializable                                                                                                                                                                          
 super-class       +-java.lang.Object                                                                                                                                                                            
 class-loader      +-org.springframework.boot.loader.LaunchedURLClassLoader@21b8d17c                                                                                                                             
                     +-sun.misc.Launcher$AppClassLoader@70dea4e                                                                                                                                                  
                       +-sun.misc.Launcher$ExtClassLoader@2038ae61                                                                                                                                               
 classLoaderHash   21b8d17c                                                                                                                                                                                      

 class-info        com.ct.hear.entity.MessageLog                                                                                                                                                                 
 code-source       file:/data/1data/1data-project-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/                                                                                                                         
 name              com.ct.hear.entity.MessageLog                                                                                                                                                                 
 isInterface       false                                                                                                                                                                                         
 isAnnotation      false                                                                                                                                                                                         
 isEnum            false                                                                                                                                                                                         
 isAnonymousClass  false                                                                                                                                                                                         
 isArray           false                                                                                                                                                                                         
 isLocalClass      false                                                                                                                                                                                         
 isMemberClass     false                                                                                                                                                                                         
 isPrimitive       false                                                                                                                                                                                         
 isSynthetic       false                                                                                                                                                                                         
 simple-name       MessageLog                                                                                                                                                                                    
 modifier          public                                                                                                                                                                                        
 annotation        io.swagger.annotations.ApiModel                                                                                                                                                               
 interfaces        java.io.Serializable                                                                                                                                                                          
 super-class       +-java.lang.Object                                                                                                                                                                            
 class-loader      +-org.springframework.boot.loader.LaunchedURLClassLoader@21b8d17c                                                                                                                             
                     +-sun.misc.Launcher$AppClassLoader@70dea4e                                                                                                                                                  
                       +-sun.misc.Launcher$ExtClassLoader@2038ae61                                                                                                                                               
 classLoaderHash   21b8d17c                                                                                                                                                                                      

Affect(row-cnt:2) cost in 17 ms.
  • sc -d *UserController | grep class
    sc命令还支持管道,使用这个命令来获取UserController的信息
[arthas@81]$ sc -d *UserController | grep class
 class-info        com.example.demo.arthas.user.UserController
 code-source       file:/root/demo-arthas-spring-boot.jar!/BOOT-INF/classes!/
 super-class       +-java.lang.Object
 class-loader      +-org.springframework.boot.loader.LaunchedURLClassLoader@728938a9
 classLoaderHash   728938a9
  • sc -d -f com.ct.hear.entity.*
    再使用这个命令获取到该Object下的Field
[arthas@4967]$ sc -d -f com.ct.hear.entity.Article
 class-info        com.ct.hear.entity.Article                                                                                                                                                                    
 code-source       file:/data/1data/1data-project-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/                                                                                                                         
 name              com.ct.hear.entity.Article                                                                                                                                                                    
 isInterface       false                                                                                                                                                                                         
 isAnnotation      false                                                                                                                                                                                         
 isEnum            false                                                                                                                                                                                         
 isAnonymousClass  false                                                                                                                                                                                         
 isArray           false                                                                                                                                                                                         
 isLocalClass      false                                                                                                                                                                                         
 isMemberClass     false                                                                                                                                                                                         
 isPrimitive       false                                                                                                                                                                                         
 isSynthetic       false                                                                                                                                                                                         
 simple-name       Article                                                                                                                                                                                       
 modifier          public                                                                                                                                                                                        
 annotation        io.swagger.annotations.ApiModel                                                                                                                                                               
 interfaces        java.io.Serializable                                                                                                                                                                          
 super-class       +-java.lang.Object                                                                                                                                                                            
 class-loader      +-org.springframework.boot.loader.LaunchedURLClassLoader@21b8d17c                                                                                                                             
                     +-sun.misc.Launcher$AppClassLoader@70dea4e                                                                                                                                                  
                       +-sun.misc.Launcher$ExtClassLoader@2038ae61                                                                                                                                               
 classLoaderHash   21b8d17c                                                                                                                                                                                      
 fields            name     serialVersionUID                                                                                                                                                                     
                   type     long                                                                                                                                                                                 
                   modifier final,private,static                                                                                                                                                                 
                   value    1                                                                                                                                                                                    
                                                                                                                                                                                                                 
                   name       id                                                                                                                                                                                 
                   type       java.lang.String                                                                                                                                                                   
                   modifier   private                                                                                                                                                                            
                   annotation com.baomidou.mybatisplus.annotation.TableId,io.swagger.annotations.ApiModelProperty                                                                                                
                                                                                                                                                                                                                 
                   name       title                                                                                                                                                                              
                   type       java.lang.String                                                                                                                                                                   
                   modifier   private                                                                                                                                                                            
                   annotation io.swagger.annotations.ApiModelProperty                                                                                                                                            
                                                                                                                                                                                                                 
                   name       summary                                                                                                                                                                            
                   type       java.lang.String                                                                                                                                                                   
                   modifier   private                                                                                                                                                                            
                   annotation io.swagger.annotations.ApiModelProperty                                                                                                                                            
                                                                                                                                                                                                                 
                   name       pcontent                                                                                                                                                                           
                   type       java.lang.String                                                                                                                                                                   
                   modifier   private                                                                                                                                                                            
                   annotation io.swagger.annotations.ApiModelProperty                                                                                                                                            
                                                                                                                                                                                                                 
                   name       emotion                                                                                                                                                                            
                   type       java.lang.Integer                                                                                                                                                                  
                   modifier   private                                                                                                                                                                            
                   annotation io.swagger.annotations.ApiModelProperty                                                                                                                                            
                                                                                                                                                                                                                 
                   name       pubTime                                                                                                                                                                            
                   type       java.lang.Long                                                                                                                                                                     
                   modifier   private                                                                                                                                                                            
                   annotation io.swagger.annotations.ApiModelProperty                                                                                                                                            
                                                                                                                                                                                                                 
                   name       createTime                                                                                                                                                                         
                   type       java.lang.Long                                                                                                                                                                     
                   modifier   private                                                                                                                                                                            
                   annotation io.swagger.annotations.ApiModelProperty                                                                                                                                            
                                                                                                                                                                                                                 
                   name       url                                                                                                                                                                                
                   type       java.lang.String                                                                                                                                                                   
                   modifier   private                                                                                                                                                                            
                   annotation io.swagger.annotations.ApiModelProperty                                                                                                                                            
                                                                                                                                                                                                                 
                   name       author                                                                                                                                                                             
                   type       java.lang.String                                                                                                                                                                   
                   modifier   private                                                                                                                                                                            
                   annotation io.swagger.annotations.ApiModelProperty                                                                                                                                            
                                                                                                                                                                                                                 
                   name       authorAddress                                                                                                                                                                      
                   type       java.lang.String                                                                                                                                                                   
                   modifier   private                                                                                                                                                                            
                   annotation io.swagger.annotations.ApiModelProperty                                                                                                                                            
                                                                                                                                                                                                                 
                   name       authorGender                                                                                                                                                                       
                   type       java.lang.String                                                                                                                                                                   
                   modifier   private                                                                                                                                                                            
                   annotation io.swagger.annotations.ApiModelProperty                                                                                                                                            
                                                                                                                                                                                                                 
                   name       mediaName                                                                                                                                                                          
                   type       java.lang.String                                                                                                                                                                   
                   modifier   private                                                                                                                                                                            
                   annotation io.swagger.annotations.ApiModelProperty                                                                                                                                            
                                                                                                                                                                                                                 
                   name       mediaType                                                                                                                                                                          
                   type       java.lang.String                                                                                                                                                                   
                   modifier   private                                                                                                                                                                            
                   annotation io.swagger.annotations.ApiModelProperty                                                                                                                                            
                                                                                                                                                                                                                 
                   name       mediaArea                                                                                                                                                                          
                   type       java.lang.String                                                                                                                                                                   
                   modifier   private                                                                                                                                                                            
                   annotation io.swagger.annotations.ApiModelProperty                                                                                                                                            
                                                                                                                                                                                                                 
                   name       coreWords                                                                                                                                                                          
                   type       java.lang.String                                                                                                                                                                   
                   modifier   private                                                                                                                                                                            
                   annotation io.swagger.annotations.ApiModelProperty                                                                                                                                            
                                                                                                                                                                                                                 
                   name       monitorWords                                                                                                                                                                       
                   type       java.lang.String                                                                                                                                                                   
                   modifier   private                                                                                                                                                                            
                   annotation io.swagger.annotations.ApiModelProperty                                                                                                                                            
                                                                                                                                                                                                                 
                   name       fromSource                                                                                                                                                                         
                   type       java.lang.String                                                                                                                                                                   
                   modifier   private                                                                                                                                                                            
                   annotation io.swagger.annotations.ApiModelProperty                                                                                                                                            
                                                                                                                                                                                                                 
                   name       readNum                                                                                                                                                                            
                   type       java.lang.Integer                                                                                                                                                                  
                   modifier   private                                                                                                                                                                            
                   annotation io.swagger.annotations.ApiModelProperty                                                                                                                                            
                                                                                                                                                                                                                 
                   name       commentNum                                                                                                                                                                         
                   type       java.lang.Integer                                                                                                                                                                  
                   modifier   private                                                                                                                                                                            
                   annotation io.swagger.annotations.ApiModelProperty                                                                                                                                            
                                                                                                                                                                                                                 
                   name       praiseNum                                                                                                                                                                          
                   type       java.lang.Integer                                                                                                                                                                  
                   modifier   private                                                                                                                                                                            
                   annotation io.swagger.annotations.ApiModelProperty                                                                                                                                            
                                                                                                                                                                                                                 
                   name       forwardNum                                                                                                                                                                         
                   type       java.lang.Integer                                                                                                                                                                  
                   modifier   private                                                                                                                                                                            
                   annotation io.swagger.annotations.ApiModelProperty                                                                                                                                            
                                                                                                                                                                                                                 
                   name       column                                                                                                                                                                             
                   type       java.lang.String                                                                                                                                                                   
                   modifier   private                                                                                                                                                                            
                   annotation io.swagger.annotations.ApiModelProperty,com.baomidou.mybatisplus.annotation.TableField                                                                                             
                                                                                                                                                                                                                 
                   name       topic                                                                                                                                                                              
                   type       java.lang.String                                                                                                                                                                   
                   modifier   private                                                                                                                                                                            
                   annotation io.swagger.annotations.ApiModelProperty                                                                                                                                            
                                                                                                                                                                                                                 
                   name       fansNum                                                                                                                                                                            
                   type       java.lang.Integer                                                                                                                                                                  
                   modifier   private                                                                                                                                                                            
                   annotation io.swagger.annotations.ApiModelProperty                                                                                                                                            
                                                                                                                                                                                                                 
                   name       verifyType                                                                                                                                                                         
                   type       java.lang.String                                                                                                                                                                   
                   modifier   private                                                                                                                                                                            
                   annotation io.swagger.annotations.ApiModelProperty                                                                                                                                            
                                                                                                                                                                                                                 
                   name       parentId                                                                                                                                                                           
                   type       java.lang.String                                                                                                                                                                   
                   modifier   private                                                                                                                                                                            
                   annotation io.swagger.annotations.ApiModelProperty                                                                                                                                            
                                                                                                                                                                                                                 

Affect(row-cnt:1) cost in 18 ms.

思考

如果一个类被多个类加载器(classLoader)加载,调用第一条命令会出现多个对象,这是因为对于JVM来说,不同的类加载器加载出的对象是肯定不同的;两个对象equals的前提,就是它们由相同的类加载器加载,可以移步到JVM篇章看详细解答