HotswapProjects / HotswapAgent

Java unlimited redefinition of classes at runtime.
GNU General Public License v2.0
2.32k stars 491 forks source link

final static variable values duplicates when hot swap #549

Open UyCode opened 4 months ago

UyCode commented 4 months ago

for example, if I have a:

public class Test {
  private final static List<String> test = new ArrayList<>();
  {
      test.add("1");
  }
  @GetMapping("/test")
  public List<String> getTestList() {
    return test;
  }
}

if add new item like test.add("2"); and let the agent hot swap, then I would get: "1", "1", "2"

I think it because of the static, that agent not cleaned up before hot swap

skybber commented 4 months ago

ClassInitPlugin copies method and instruments it to initialize new static fields. Old static instances are not recreated, so in your case HA method adds additional values to test field. It is unsolvable to keep old static instance of test (it is kept by dcevm not HA) and apply only addition of items that are new in new variant of method. Another approach is to recreate instance after redefinition, but there is next problem with modifications out of method - not possible to track it in HA.