Skip to content

Redeclaring a method resets dependent variable values #128

Description

@m-dzianishchyts

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:

1

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:

0

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions