redhat-developer / redhat-developer/vscode-java

Inline Method leaves unqualified instance method call inside local class when inlining into static context

Open
#4,464 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
TypeScript
Stars
2.3k
Forks
546
Avg merge
20h 1m
Merged PRs (30d)
11

Description

Description

Inline Method produces uncompilable code when inlining an instance method that contains a local class into the static method main.

Before refactoring, the local class H is declared inside the instance method f(). The call helper() inside H.g() is valid because it is implicitly bound to the enclosing I01 instance.

After applying Inline Method to the invocation obj.f(), the local class H is moved into the static method main. However, the call inside H.g() remains:

return helper();

At this point, there is no implicit I01.this available in the static context. The call should be qualified with the original receiver, for example:

return obj.helper();

The refactored code no longer compiles.

Code before refactoring

public class I01 {
    public static void main(String[] args) {
        I01 obj = new I01();

        // Refactoring operation: Inline Method
        // Target invocation: obj.f()
        int result = obj.f();

        System.out.println(result);
    }

    int helper() {
        return 7;
    }

    int f() {
        class H {
            int g() {
                return helper();
            }
        }

        return H.class.getSimpleName().length();
    }
}

Code after refactoring

public class I01 {
    public static void main(String[] args) {
        I01 obj = new I01();

        class H {
            int g() {
                return helper();
            }
        }

        // Refactoring operation: Inline Method
        // Target invocation: obj.f()
        int result = H.class.getSimpleName().length();

        System.out.println(result);
    }

    int helper() {
        return 7;
    }

    int f() {
        class H {
            int g() {
                return helper();
            }
        }

        return H.class.getSimpleName().length();
    }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Inline Method implementation and reproduce the issue using the I01 example, focusing on local classes moved into static main contexts. Trace how the original obj.f() receiver is handled inside H.g(); done means the generated helper() call is qualified with the receiver and the refactored Java code compiles.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.