Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 37756 - Move static method refactoring should help with called methods [refactoring]
Summary: Move static method refactoring should help with called methods [refactoring]
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 2.1   Edit
Hardware: PC Windows 2000
: P2 normal (vote)
Target Milestone: 3.0 M4   Edit
Assignee: Markus Keller CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2003-05-16 08:38 EDT by Erich Gamma CLA
Modified: 2003-10-08 06:39 EDT (History)
2 users (show)

See Also:


Attachments
Fix 1 (1.67 KB, patch)
2003-08-22 11:15 EDT, Markus Keller CLA
no flags Details | Diff
Fix 2 (8.28 KB, patch)
2003-09-01 10:45 EDT, Markus Keller CLA
no flags Details | Diff
Tests for Fix 2 (2.16 KB, patch)
2003-09-01 10:46 EDT, Markus Keller CLA
no flags Details | Diff
Fix 3 (28.27 KB, patch)
2003-09-03 09:47 EDT, Markus Keller CLA
no flags Details | Diff
Tests for Fix 3 (13.70 KB, patch)
2003-09-03 09:49 EDT, Markus Keller CLA
no flags Details | Diff
Fix 4 (1.05 KB, patch)
2003-09-08 11:03 EDT, Markus Keller CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Erich Gamma CLA 2003-05-16 08:38:13 EDT
feedback from thoughtworks:
----
I had a set of 3 co-dependant methods on a class and I wanted to move that 
functionality off into an external static class. I had to manually make them 
all static, and then do a Refactor-Move on each method one by one. However, 
this actually didn't work because the first method was private, and the 
refactoring didn't solve it. In the end I fixed most of it manually.
----
We should provide the same support for dependent methods as
we do in pull-up
Comment 1 Markus Keller CLA 2003-08-22 11:15:53 EDT
Created attachment 5832 [details]
Fix 1

OK, this is only a small first step ...

- Take this class:
package codep;
public class A {
	private static int fCount;
	public static int getCount() {
		return fCount;
	}
}

- Try to move fCount to some other class, click Preview>
- A dialog warns about possible errors. Click <Back
- Click Preview> again ... Boom!

The problem was that MoveStaticMethodRefactoring#checkInput didn't expect to be
called more than once.
Comment 2 Markus Keller CLA 2003-08-29 06:11:56 EDT
This is a test class:
public class A {
	private static int fCount;
	private static void setCount(int count) { fCount= count; }
	private static void incCount(int inc) { setCount(getCount() + inc); }
	private static int internalGetCount() { return fCount;}
	public static int getCount() { return internalGetCount(); }

	public int nonstaticGetCount() { return getCount(); }
	private static int fIndependent; 
}

Selecting the first 5 members in the Outline and calling Refactor>Move works.
However, clicking "Preview" shows some warnings that moved members will not be
visible after move (which doesn't hinder me to press "Continue").

Providing "the same functionality as for pull up" would mean to show a big
dialog with all static members and opportunity to move members or to increase
their visibility. This is IMO unrequired UI bloat.

The attached Fix 1 is still valid.
Comment 3 Dirk Baeumer CLA 2003-09-01 06:13:14 EDT
Release the patch.

Markus, I agree with our comment 2. Can you please confirm that if we move to 
private method that reference each other we don't see any warning. 

If we do, can you provide a patch.
Comment 4 Markus Keller CLA 2003-09-01 10:45:49 EDT
Created attachment 5925 [details]
Fix 2

Fix 2 adds more thorough visibility checks. Warning messages now appear only if
references from outside of the moved members will cause visibility errors.

test21 covered this case. It revealed another issue which is also fixed: The
order of static members is important for fields. Field initializers can only
referenced *after* they are defined. The fix preserves the order of moved
members.

I couldn't find out what was wrong with test22 (except for a typo), so I
reactivated it.
Comment 5 Markus Keller CLA 2003-09-01 10:46:16 EDT
Created attachment 5926 [details]
Tests for Fix 2
Comment 6 Dirk Baeumer CLA 2003-09-02 11:00:41 EDT
Markus, why do you do the sorting in check activation ? I think it can be 
moved to checkInput.
Comment 7 Markus Keller CLA 2003-09-02 12:27:38 EDT
I was afraid of having different orderings in fMembersToMove and
fMemberBindings. But there doesn't seem to be a direct relationship between
them, so we can put it into checkActivation().
Comment 8 Markus Keller CLA 2003-09-03 09:47:18 EDT
Created attachment 5954 [details]
Fix 3

This patch contains fixes for several bugs:

Bug 37756:
- Better visibility checks:
All real problems should now be detected and flagged as ERROR. There are no
WARNING messages any more.
Improved message text includes visibility status of enclosing type, which could
as well be the reason for visibility errors.

Bug 28022:
- Enabled moving "public static final" fields between classes and interfaces
(with appropriate error checking)

Bug 42383:
- Moving field declarations with multiple variables is now FATAL. I had to move
the invocation of getASTMembers() into checkActivation(), because we need
FieldDeclarations from the AST to find those which declare multiple variables.
Sorting is still done inside checkActivation(), but that's less of an
efficiency problem now, since it uses getStartPosition() from the AST rather
than from JavaElements.

Others:
- Solved problems with ! workingCopy.equals(primary) causing some invocations
to fail (e.g. when the target class contained references to moving members).
Comment 9 Markus Keller CLA 2003-09-03 09:49:20 EDT
Created attachment 5955 [details]
Tests for Fix 3
Comment 10 Dirk Baeumer CLA 2003-09-03 13:18:10 EDT
Released the patch. One minor improvement: getASTMembers could break out of 
the loop if a fatal error got created. In this case the refactoring will not 
work.
Comment 11 Dirk Baeumer CLA 2003-09-03 13:33:38 EDT
Released patch as well.
Comment 12 Markus Keller CLA 2003-09-08 11:03:05 EDT
Created attachment 6016 [details]
Fix 4

> getASTMembers could break out of the loop if a fatal error got created.

I didn't do it because I wanted to show all errors if there are multiple.
But it's useless, since RefactoringErrorDialogUtil doesn't show the
JavaStatusContexts and therefore all messages look the same.
Comment 13 Dirk Baeumer CLA 2003-09-09 05:31:54 EDT
Released patch and marked PR as fixed.
Comment 14 Dani Megert CLA 2003-10-08 06:29:33 EDT
start verifiying...
Comment 15 Dani Megert CLA 2003-10-08 06:39:01 EDT
Verified using I20031007 + plugin-export for I20031008