| Summary: | [extract method] "Extract Method" into an enclosing class fails when the inner class has a method of the same name | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| Product: | [Eclipse Project] JDT | Reporter: | Missing name <harely> | ||||||
| Component: | UI | Assignee: | Deepak Azad <deepakazad> | ||||||
| Status: | VERIFIED FIXED | QA Contact: | |||||||
| Severity: | minor | ||||||||
| Priority: | P3 | CC: | daniel_megert, deepakazad, markus.kell.r | ||||||
| Version: | 3.7 | Flags: | markus.kell.r:
review+
|
||||||
| Target Milestone: | 3.7 M1 | ||||||||
| Hardware: | PC | ||||||||
| OS: | Windows XP | ||||||||
| Whiteboard: | |||||||||
| Attachments: |
|
||||||||
(In reply to comment #0) > 3. > The refactoring fails with message "Method 'g' already exists in type 'B'." True, but on clicking continue it produces the following result which has a compilation error public class A { private class B { private void g() {} private int f() { return g(); // compilation error here } } private int g() { return 2+3; } } Created attachment 172861 [details]
fix + test
The problem was that we always checked for the new method name in the same type itself instead of the destinaiton type - see ExtractMethodAnalyzer#checkInput(...)
Markus, the patch looks good?
The patch does not work if the expression is inside an anonymous, e.g. extract
"2 + 3" to g in A here:
import java.util.concurrent.Callable;
public class A {
private class B {
private void g() {}
private int f() {
return new Callable<Integer>() {
public Integer call() {
return 2 + 3;
}
}.call();
}
}
}
Is there a special reason why you didn't use
ITypeBinding type= ASTNodes.getEnclosingType(destination)
in ExtractMethodAnalyzer#checkInput(RefactoringStatus, String, ASTNode)? I always prefer less code and less changes in working code.
Created attachment 173166 [details] fix + tests (In reply to comment #3) > The patch does not work if the expression is inside an anonymous, The patch fails when there is more than one level of nesting, and also when the inner class is a static class. Fixed all these issues. (In reply to comment #4) > Created an attachment (id=173166) [details] [diff] > fix + tests Fixed in HEAD. . Looks good. Filed and fixed bug 318609 for a problem with enum types. Verified in I20100802-1800. |
Build Identifier: 20100218-1602 When using "Extract Method" on an expression in an inner class to extract it into a method in the enclosing class, and the name of the new method is the same as the name of some method of the inner class, the refactoring fails although there is no name clash. Reproducible: Always Steps to Reproduce: 1. package sandbox; import java.util.HashSet; import java.util.Set; public class A { private class B { private void g() {} private int f() { return 2 + 3; } } } 2. Use Extract Method on the expression "2 + 3" choosing the function name g and destination type A. 3. The refactoring fails with message "Method 'g' already exists in type 'B'."