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 |
} |