Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Source reference is lost when changing constructor #516

Open
Artur- opened this issue Feb 20, 2025 · 2 comments
Open

Source reference is lost when changing constructor #516

Artur- opened this issue Feb 20, 2025 · 2 comments

Comments

@Artur-
Copy link

Artur- commented Feb 20, 2025

Given a class like

@Route("")
public class HelloFlowView extends Div {

    public HelloFlowView() {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        for (int i = 0; i < stackTrace.length && i < 5; i++) {
            Div div = new Div();
            div.setText(stackTrace[i].toString());
            add(div);
        }
    }
}

you can see the stack trace for the constructor when opening the view:

java.base/java.lang.Thread.getStackTrace(Thread.java:2451)
com.example.application.views.helloflow.HelloFlowView.<init>(HelloFlowView.java:11)
java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)
java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)
java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486)

If you change something in the code and hotswap the changes, like add

add(new Span("Hello"));

at the end of the constructor, you see the same stack trace.

However, if you change the constructor to e.g.

public HelloFlowView(Span span) {

then the source information for the view Java file is lost and the stack trace becomes

java.base/java.lang.Thread.getStackTrace(Thread.java:2451)
com.example.application.views.helloflow.HelloFlowView.<init>(Unknown Source)
java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)
java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)
java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486)

This is problematic for us because we use the line information to figure out where in the source file certain things happen, to be able to edit the source file correctly.

Reproducible example in https://github.com/Artur-/test-update-constructor
Launch Application.java using hotswap agent, modify the constructor and hotswap

@skybber
Copy link
Collaborator

skybber commented Feb 20, 2025

Thanks for the report and the great example. I was able to reproduce the issue, but I haven't found a quick workaround. Interestingly, if you move the stack trace generation to a new method and call it from the constructor, the constructor's source mapping appears correctly. The issue needs more investigation.

@skybber
Copy link
Collaborator

skybber commented Feb 20, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants