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:
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:
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();
}
}
Description
Inline Methodproduces uncompilable code when inlining an instance method that contains a local class into the static methodmain.Before refactoring, the local class
His declared inside the instance methodf(). The callhelper()insideH.g()is valid because it is implicitly bound to the enclosingI01instance.After applying
Inline Methodto the invocationobj.f(), the local classHis moved into the static methodmain. However, the call insideH.g()remains:At this point, there is no implicit
I01.thisavailable in the static context. The call should be qualified with the original receiver, for example:The refactored code no longer compiles.
Code before refactoring
Code after refactoring