View | Details | Raw Unified | Return to bug 140971
Collapse All | Expand All

(-)core extension/org/eclipse/jdt/internal/corext/fix/AddUnimplementedMethodsOperation.java (-1 / +6 lines)
Lines 11-16 Link Here
11
11
12
package org.eclipse.jdt.internal.corext.fix;
12
package org.eclipse.jdt.internal.corext.fix;
13
13
14
import java.util.Arrays;
15
14
import org.eclipse.core.runtime.Assert;
16
import org.eclipse.core.runtime.Assert;
15
import org.eclipse.core.runtime.CoreException;
17
import org.eclipse.core.runtime.CoreException;
16
18
Lines 36-41 Link Here
36
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFix.CompilationUnitRewriteOperation;
38
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFix.CompilationUnitRewriteOperation;
37
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
39
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
38
import org.eclipse.jdt.internal.corext.util.Messages;
40
import org.eclipse.jdt.internal.corext.util.Messages;
41
import org.eclipse.jdt.internal.corext.util.MethodsSourcePositionComparator;
39
42
40
import org.eclipse.jdt.ui.JavaElementLabels;
43
import org.eclipse.jdt.ui.JavaElementLabels;
41
44
Lines 153-158 Link Here
153
		if (binding == null)
156
		if (binding == null)
154
			return new IMethodBinding[0];
157
			return new IMethodBinding[0];
155
158
156
		return StubUtility2.getUnimplementedMethods(binding, implementAbstractsOfInput);
159
		IMethodBinding[] unimplementedMethods= StubUtility2.getUnimplementedMethods(binding, implementAbstractsOfInput);
160
		Arrays.sort(unimplementedMethods, new MethodsSourcePositionComparator(binding));
161
		return unimplementedMethods;
157
	}
162
	}
158
}
163
}
(-)core extension/org/eclipse/jdt/internal/corext/codemanipulation/AddUnimplementedMethodsOperation.java (+4 lines)
Lines 11-16 Link Here
11
package org.eclipse.jdt.internal.corext.codemanipulation;
11
package org.eclipse.jdt.internal.corext.codemanipulation;
12
12
13
import java.util.ArrayList;
13
import java.util.ArrayList;
14
import java.util.Arrays;
14
import java.util.List;
15
import java.util.List;
15
16
16
import org.eclipse.core.runtime.CoreException;
17
import org.eclipse.core.runtime.CoreException;
Lines 41-46 Link Here
41
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext;
42
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext;
42
43
43
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
44
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
45
import org.eclipse.jdt.internal.corext.util.MethodsSourcePositionComparator;
44
46
45
import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings;
47
import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings;
46
48
Lines 193-198 Link Here
193
				methodsToImplement= StubUtility2.getUnimplementedMethods(currTypeBinding);
195
				methodsToImplement= StubUtility2.getUnimplementedMethods(currTypeBinding);
194
			}
196
			}
195
197
198
			Arrays.sort(methodsToImplement, new MethodsSourcePositionComparator(fType));
199
196
			ImportRewriteContext context= null;
200
			ImportRewriteContext context= null;
197
			int insertionPosition= fInsertPos;
201
			int insertionPosition= fInsertPos;
198
			if (insertionPosition == -1 && fASTRoot.types().size() > 0) {
202
			if (insertionPosition == -1 && fASTRoot.types().size() > 0) {
(-)core extension/org/eclipse/jdt/internal/corext/codemanipulation/AddDelegateMethodsOperation.java (+4 lines)
Lines 11-16 Link Here
11
package org.eclipse.jdt.internal.corext.codemanipulation;
11
package org.eclipse.jdt.internal.corext.codemanipulation;
12
12
13
import java.util.ArrayList;
13
import java.util.ArrayList;
14
import java.util.Arrays;
14
import java.util.List;
15
import java.util.List;
15
16
16
import org.eclipse.core.runtime.Assert;
17
import org.eclipse.core.runtime.Assert;
Lines 40-45 Link Here
40
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
41
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
41
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
42
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
42
43
44
import org.eclipse.jdt.internal.corext.util.DelegateEntryComparator;
43
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
45
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
44
46
45
import org.eclipse.jdt.ui.CodeStyleConfiguration;
47
import org.eclipse.jdt.ui.CodeStyleConfiguration;
Lines 173-178 Link Here
173
175
174
				ContextSensitiveImportRewriteContext context= new ContextSensitiveImportRewriteContext(fASTRoot, typeDecl.getStartPosition(), importRewrite);
176
				ContextSensitiveImportRewriteContext context= new ContextSensitiveImportRewriteContext(fASTRoot, typeDecl.getStartPosition(), importRewrite);
175
177
178
				Arrays.sort(fDelegatesToCreate, new DelegateEntryComparator());
179
176
				for (int i= 0; i < fDelegatesToCreate.length; i++) {
180
				for (int i= 0; i < fDelegatesToCreate.length; i++) {
177
					IMethodBinding delegateMethod= fDelegatesToCreate[i].delegateMethod;
181
					IMethodBinding delegateMethod= fDelegatesToCreate[i].delegateMethod;
178
					IVariableBinding field= fDelegatesToCreate[i].field;
182
					IVariableBinding field= fDelegatesToCreate[i].field;
(-)core extension/org/eclipse/jdt/internal/corext/codemanipulation/AddGetterSetterOperation.java (-18 / +25 lines)
Lines 10-15 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.jdt.internal.corext.codemanipulation;
11
package org.eclipse.jdt.internal.corext.codemanipulation;
12
12
13
import java.util.Arrays;
14
import java.util.HashSet;
15
import java.util.Set;
16
13
import org.eclipse.core.runtime.Assert;
17
import org.eclipse.core.runtime.Assert;
14
import org.eclipse.core.runtime.CoreException;
18
import org.eclipse.core.runtime.CoreException;
15
import org.eclipse.core.runtime.IProgressMonitor;
19
import org.eclipse.core.runtime.IProgressMonitor;
Lines 308-335 Link Here
308
			}
312
			}
309
313
310
			fSkipAllExisting= (fSkipExistingQuery == null);
314
			fSkipAllExisting= (fSkipExistingQuery == null);
311
			if (!fSort) {
315
			
312
				for (int index= 0; index < fAccessorFields.length; index++) {
316
			Set accessors = new HashSet(Arrays.asList(fAccessorFields));
313
					generateGetterMethod(fAccessorFields[index], listRewriter);
317
			Set getters = new HashSet(Arrays.asList(fGetterFields));
314
					generateSetterMethod(fAccessorFields[index], astRewrite, listRewriter);
318
			Set setters= new HashSet(Arrays.asList(fSetterFields));
319
			IField[] fields= fType.getFields();
320
			for (int i= 0; i < fields.length; i++) {
321
				if (accessors.contains(fields[i])) {
322
					generateGetterMethod(fields[i], listRewriter);
323
					generateSetterMethod(fields[i], astRewrite, listRewriter);
324
					monitor.worked(1);
325
					if (monitor.isCanceled()) {
326
						throw new OperationCanceledException();
327
					}
328
				} else if (getters.contains(fields[i])) {
329
					generateGetterMethod(fields[i], listRewriter);
330
					monitor.worked(1);
331
					if (monitor.isCanceled()) {
332
						throw new OperationCanceledException();
333
					}
334
				} else if (setters.contains(fields[i])) {
335
					generateSetterMethod(fields[i], astRewrite, listRewriter);
315
					monitor.worked(1);
336
					monitor.worked(1);
316
					if (monitor.isCanceled()) {
337
					if (monitor.isCanceled()) {
317
						throw new OperationCanceledException();
338
						throw new OperationCanceledException();
318
					}
339
					}
319
				}
320
			}
321
			for (int index= 0; index < fGetterFields.length; index++) {
322
				generateGetterMethod(fGetterFields[index], listRewriter);
323
				monitor.worked(1);
324
				if (monitor.isCanceled()) {
325
					throw new OperationCanceledException();
326
				}
327
			}
328
			for (int index= 0; index < fSetterFields.length; index++) {
329
				generateSetterMethod(fSetterFields[index], astRewrite, listRewriter);
330
				monitor.worked(1);
331
				if (monitor.isCanceled()) {
332
					throw new OperationCanceledException();
333
				}
340
				}
334
			}
341
			}
335
			fEdit= astRewrite.rewriteAST();
342
			fEdit= astRewrite.rewriteAST();
(-)core (+72 lines)
Added Link Here
1
package org.eclipse.jdt.internal.corext.util;
2
3
import java.util.Comparator;
4
5
import org.eclipse.jdt.core.IField;
6
import org.eclipse.jdt.core.IMethod;
7
import org.eclipse.jdt.core.ISourceRange;
8
import org.eclipse.jdt.core.JavaModelException;
9
import org.eclipse.jdt.core.dom.IVariableBinding;
10
11
import org.eclipse.jdt.internal.corext.codemanipulation.AddDelegateMethodsOperation.DelegateEntry;
12
13
/**
14
 * A comparator which sorts instances of DelegateEntry according to order in which delegate methods
15
 * should be generated in a type. More formally, if <code>T</code> is a class and <code>d1</code>
16
 * and <code>d2</code> are two DelegateEntries representing delegate methods which are about to be
17
 * added to <code>T</code> then according to this comparator <code>d1</code> is less than
18
 * <code>d2</code> iff one of following holds:
19
 * <ul>
20
 * <li><code>d1</code> and <code>d2</code> represent methods delegated to the same field of
21
 * <code>T</code>, that field's type has a source attachment and method of <code>d1</code> appears
22
 * in that source before method of <code>d2</code></li>
23
 * <li><code>d1</code> and <code>d2</code> represent methods delegated to the same field of
24
 * <code>T</code>, that field's type doesn't have a source attachment and name of method of
25
 * <code>d1</code> alphabetically precedes name of method of <code>d2</code></li>
26
 * <li><code>d1</code> and <code>d2</code> represent methods delegated to different fields
27
 * <code>f1</code> and <code>f2</code>; field <code>f1</code> is declared before <code>f2</code> in
28
 * type <code>T</code></li>
29
 * </ul>
30
 */
31
public class DelegateEntryComparator implements Comparator {
32
33
	public DelegateEntryComparator() {
34
	}
35
36
	public int compare(Object o1, Object o2) {
37
		if (!(o1 instanceof DelegateEntry)) {
38
			return -1;
39
		}
40
		if (!(o2 instanceof DelegateEntry)) {
41
			return -1;
42
		}
43
		DelegateEntry firstEntry= (DelegateEntry)o1;
44
		DelegateEntry secondEntry= (DelegateEntry)o2;
45
		IVariableBinding firstVariable= firstEntry.field;
46
		IVariableBinding secondVariable= secondEntry.field;
47
48
		if (firstVariable.equals(secondVariable)) {
49
			try {
50
				IMethod firstMethod= (IMethod)firstEntry.delegateMethod.getJavaElement();
51
				IMethod secondMethod= (IMethod)secondEntry.delegateMethod.getJavaElement();
52
				ISourceRange firstSourceRange= firstMethod.getSourceRange();
53
				ISourceRange secondSourceRange= secondMethod.getSourceRange();
54
				if (firstSourceRange == null || secondSourceRange == null) {
55
					return firstMethod.getElementName().compareTo(secondMethod.getElementName());
56
				} else {
57
					return firstSourceRange.getOffset() - secondSourceRange.getOffset();
58
				}
59
			} catch (JavaModelException e) {
60
				return -1;
61
			}
62
		}
63
64
		IField firstField= (IField)firstVariable.getJavaElement();
65
		IField secondField= (IField)secondVariable.getJavaElement();
66
		try {
67
			return firstField.getSourceRange().getOffset() - secondField.getSourceRange().getOffset();
68
		} catch (JavaModelException e) {
69
			return -1;
70
		}
71
	}
72
}
(-)core (+120 lines)
Added Link Here
1
package org.eclipse.jdt.internal.corext.util;
2
3
import java.util.Comparator;
4
5
import org.eclipse.jdt.core.IMethod;
6
import org.eclipse.jdt.core.ISourceRange;
7
import org.eclipse.jdt.core.JavaModelException;
8
import org.eclipse.jdt.core.dom.IMethodBinding;
9
import org.eclipse.jdt.core.dom.ITypeBinding;
10
11
/**
12
 * A comparator which sorts methods (IMethodBinding) of a type passed as constructor argument,
13
 * according to their order in source files. More formally, if <code>m1</code> and <code>m2</code>
14
 * are methods of type <code>T</code> then according to this comparator <code>m1</code> is less than
15
 * <code>m2</code> iff one of following holds:
16
 * <ul>
17
 * <li><code>m1</code> and <code>m2</code> are defined in the same type (<code>T</code> or any
18
 * supertype of <code>T</code>), that type has a source attachment and <code>m1</code> appears
19
 * before <code>m2</code> in source of that type</li>
20
 * <li><code>m1</code> and <code>m2</code> are defined in the same type (<code>T</code> or any
21
 * supertype of <code>T</code>), that type doesn't have a source attachment and name of
22
 * <code>m1</code> alphabetically precedes name of <code>m2</code></li>
23
 * <li><code>m1</code> is defined in <code>T</code> and <code>m2</code> is defined in any supertype
24
 * of <code>T</code></li>
25
 * <li><code>m1</code> is defined in a superclass of <code>T</code> and <code>m2</code> is defined
26
 * in a superinterface of <code>T</code></li>
27
 * <li><code>m1</code> and <code>m2</code> are defined in different superclasses of <code>T</code>
28
 * and a class which defines <code>m2</code> extends class which defines <code>m1</code>
29
 * <li><code>m1</code> and <code>m2</code> are defined in different superinterfaces of
30
 * <code>T</code> and an interface which defines <code>m1</code> appears before an interface which
31
 * defines <code>m2</code> in <code>implements</code> clause of declaration of type <code>T</code></li>
32
 * </ul>
33
 */
34
public class MethodsSourcePositionComparator implements Comparator {
35
36
	private final ITypeBinding fTypeBinding;
37
38
	public MethodsSourcePositionComparator(ITypeBinding typeBinding) {
39
		if (typeBinding == null) {
40
			throw new IllegalArgumentException();
41
		}
42
		fTypeBinding= typeBinding;
43
	}
44
45
	public int compare(Object o1, Object o2) {
46
		if (!(o1 instanceof IMethodBinding)) {
47
			return -1;
48
		}
49
		if (!(o2 instanceof IMethodBinding)) {
50
			return -1;
51
		}
52
		IMethodBinding firstMethodBinding= (IMethodBinding)o1;
53
		IMethodBinding secondMethodBinding= (IMethodBinding)o2;
54
		ITypeBinding firstMethodType= firstMethodBinding.getDeclaringClass();
55
		ITypeBinding secondMethodType= secondMethodBinding.getDeclaringClass();
56
57
		if (firstMethodType.equals(secondMethodType)) {
58
			return compareInTheSameType(firstMethodBinding, secondMethodBinding);
59
		}
60
61
		if (firstMethodType.equals(fTypeBinding)) {
62
			return -1;
63
		}
64
		if (secondMethodType.equals(fTypeBinding)) {
65
			return 1;
66
		}
67
68
		ITypeBinding type= fTypeBinding;
69
		int count= 0, firstCount= -1, secondCount= -1;
70
		while ((type= type.getSuperclass()) != null) {
71
			if (firstMethodType.equals(type)) {
72
				firstCount= count;
73
			}
74
			if (secondMethodType.equals(type)) {
75
				secondCount= count;
76
			}
77
			count++;
78
		}
79
		if (firstCount != -1 && secondCount != -1) {
80
			return -(firstCount - secondCount);
81
		}
82
		if (firstCount != -1 && secondCount == -1) {
83
			return -1;
84
		}
85
		if (firstCount == -1 && secondCount != -1) {
86
			return 1;
87
		}
88
89
		ITypeBinding[] interfaces= fTypeBinding.getInterfaces();
90
		for (int i= 0; i < interfaces.length; i++) {
91
			if (firstMethodType.equals(interfaces[i])) {
92
				return -1;
93
			}
94
			if (secondMethodType.equals(interfaces[i])) {
95
				return 1;
96
			}
97
		}
98
		return 0;
99
	}
100
101
	private int compareInTheSameType(IMethodBinding firstMethodBinding, IMethodBinding secondMethodBinding) {
102
		try {
103
			IMethod firstMethod= (IMethod)firstMethodBinding.getJavaElement();
104
			IMethod secondMethod= (IMethod)secondMethodBinding.getJavaElement();
105
			if (firstMethod == null || secondMethod == null) {
106
				return -1;
107
			}
108
			ISourceRange firstSourceRange= firstMethod.getSourceRange();
109
			ISourceRange secondSourceRange= secondMethod.getSourceRange();
110
111
			if (firstSourceRange == null || secondSourceRange == null) {
112
				return firstMethod.getElementName().compareTo(secondMethod.getElementName());
113
			} else {
114
				return firstSourceRange.getOffset() - secondSourceRange.getOffset();
115
			}
116
		} catch (JavaModelException e) {
117
			return -1;
118
		}
119
	}
120
}

Return to bug 140971