Skip to content

Hot reload of normal class files

Property changes

java
public class User {
    
    private String userId; 
    
    private String name; 
    private String userName; 
    
    private Integer userAge; 
}
  • You can use the new userId attribute.
  • Remove the name attribute, and use the new userName attribute.
  • You can use the new userAge attribute.

Method changes

java
public class UserUtils {

    public static String getUserName(UserVO userVO) {
        return userVO.getName();
    }
    
    public static User getDefaultUser() { 
    public static User genDefaultUser () { 
        User user = new User();
        user.setUserId(0); 
        user.setUserId(Id.next()); 
        user.setName("default"); 
        user.setUserName("default"); 
        user.setUserAge(0);
        return user;
    }
}
  • You can use the new getUserName method.
  • Remove the getUserDefaultUser method, and use the new genDefaultUser method.

Static information changes

Support static variables, static final variables, and static code blocks.

java
public class StaticClass {
    
    private static String var1 = "debug"; 
    private static String var1 = "tools"; 
    
    private static final String var2 = "debug"; 
    private static final String var2 = "tools"; 
    
    private static String var3;
    
    static {
        var3 = "debug"; 
        var3 = "tools"; 
    }
    
}

After being overloaded, the values of variables var1, var2, and var3 are changed.

TIP

Hot reloading of static variables will re-execute the class's clinit method, so hot reloading will overwrite the runtime value with the value initialized at compile time.

If the following changes are not overridden, var1 will not change from String to Integer.

java
private static String var1 = "debug"; 
private static Integer var1 = 666; 

However, in the following scenario, var1 will add data at runtime. If you overwrite it, you will lose the runtime data.

java
private static Map<String, Long> var1 = new HashMap<>();

Therefore, DebugTools will overwrite runtime data by default, but you can prevent overwriting of field runtime data using the following method.

Method 1: Specify in the static code library that the variable should only be assigned a value when it is empty, such as:

java
private static Map<String, Long> var1;

static {
    // This way, executing this code block when calling the clinit method will not overwrite runtime data.
    if (var1 == null) {
        var1 = new HashMap<>();
    }
}

Method 2: Configure in plugin

When the mouse hovers over a field, you can add configuration options via the right-click menu to ignore the field during hot reload, so that the runtime value is not overridden by the compile-time value. After adding, an icon will appear at the top of the line, and clicking it will remove the field from the ignore list.

hotswap_ignores_this_field.png

Adding configuration options to the page allows users to write and switch between different configuration files.

setting_hotswap_ignores_field.png

The file content is a regular conf file; both class#fieldname and class.fieldname are acceptable, and comments are supported (# and ;).

hotswap_ignores_field_conf.png

Enumeration class information changes.

java
public enum StatusEnum {
    
    A1, 
    A22, 
    A2, 
    A3, 
}

After the change, there are only two enumeration values, A2 and A3.

java
public enum StatusEnum {

    A1(1, "a"), 
    A1(1, "A"), 
    A22(22, "BB"), 
    A2(2, "B"), 
    A3(3, "C"), 
    ;
    private final Integer code;

    private final String name;

    public StatusEnum(Integer code, String name) {
        this.code = code;
        this.name = name;
    }

    public Integer getCode() {
        return code;
    }

    public String getName() {
        return name;
    }
    
    public StatusEnum of(Integer code) { 
        for (StatusEnum statusEnum : StatusEnum.values()) { 
            if (statusEnum.getCode().equals(code)) { 
                return statusEnum; 
            } 
        } 
        return code; 
    }
}

The content will take effect after being changed.

Internal class changes

java
public class UserVO {
    
    private String userName;
    
    private List<RoleVO> userRoles;
    
    public static class RoleVO {
        
        private Long roleId;  
        
        private String name;  
        private String roleName;  

        private Integer status; 
        
        public String getName() { 
            return name; 
        public String getRoleName() { 
            return roleName; 
        }
        
        public String getStatusName() { 
            return status == 1 ? "正常" : "禁用"; 
        } 
        
    }
    
    private List<AuthVO> userAuths; 
    
    public class AuthVO { 
        
        private String authName; 
        
    } 
}
  • Added userAuths attribute.
  • Modified RoleVO internal class information for normal use.
  • Added AuthVO internal class information for normal use.

Abstract class

Turn on hot reload, and when the abstract class is modified, all subclasses will take effect.

java
public abstract class User {

    public String getUserName() { 
        return "default"; 
    } 
}

public class Debug extends User {

}

public class Tools extends User {

}

After hot reloading User, both Debug and Tools can use the getUserName() method.

Interface

When the default method of an interface is modified, all implementation classes will take effect.

java
public interface User {

    default String getUserName() { 
        return "default"; 
    } 
}

public class Debug implements User {

}

public class Tools implements User {

}

After hot reloading User, both Debug and Tools can use the getUserName() method.