Summary
Redeclaring a method resets the value of variables that depend on the previous method implementation.
Steps to Reproduce
Execute the following snippet:
int f() { return 1; }
var v = f();
int f() { return 2; }
v;
Expected Behavior
The value of v should remain unchanged after f() is redeclared:
v was initialized using the original implementation of f(), so redefining f() should not retroactively re-evaluate or reset v.
jshell cli tool handles this.
Actual Behavior
After redeclaring f(), evaluating v returns:
The original value is lost.
Root Cause
When f() is redeclared, JShell recompiles both the method wrapper and dependent variable wrapper. JJava does not support in-place redefinitions, so JShell falls back to replacement wrapper classes.
The replacement wrapper for v is loaded with its static backing field in the default state. Since the variable initializer is not re-executed during dependent recompilation, v becomes the default value instead of retaining its original value.
Possible Solution
Implement true in-place class redefinition (jshell approach) by providing a real ExecutionControl.redefine() implementation so existing wrapper classes retain their identity and static field values while receiving updated method bytecode.
Summary
Redeclaring a method resets the value of variables that depend on the previous method implementation.
Steps to Reproduce
Execute the following snippet:
Expected Behavior
The value of
vshould remain unchanged afterf()is redeclared:vwas initialized using the original implementation off(), so redefiningf()should not retroactively re-evaluate or resetv.Actual Behavior
After redeclaring
f(), evaluatingvreturns:The original value is lost.
Root Cause
When
f()is redeclared, JShell recompiles both the method wrapper and dependent variable wrapper. JJava does not support in-place redefinitions, so JShell falls back to replacement wrapper classes.The replacement wrapper for
vis loaded with its static backing field in the default state. Since the variable initializer is not re-executed during dependent recompilation,vbecomes the default value instead of retaining its original value.Possible Solution
Implement true in-place class redefinition (
jshellapproach) by providing a realExecutionControl.redefine()implementation so existing wrapper classes retain their identity and static field values while receiving updated method bytecode.