Skip to content

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

Description

@zmjjcxy8

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();
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions