1313 *******************************************************************************/
1414package org .eclipse .jdt .internal .codeassist ;
1515
16+ import java .util .List ;
17+
18+ import org .eclipse .jdt .core .Flags ;
19+ import org .eclipse .jdt .core .IMethod ;
20+ import org .eclipse .jdt .core .JavaModelException ;
1621import org .eclipse .jdt .core .dom .IMethodBinding ;
1722import org .eclipse .jdt .core .dom .ITypeBinding ;
1823import org .eclipse .jdt .internal .compiler .ast .ASTNode ;
@@ -26,8 +31,10 @@ class DOMCompletionEngineBuilder {
2631 private static final String EXTENDS = "extends" ; //$NON-NLS-1$
2732 private static final String THROWS = "throws" ; //$NON-NLS-1$
2833 private static final String SUPER = "super" ; //$NON-NLS-1$
34+
35+ private static final String JAVA_LANG_PKG = "java.lang." ;
2936
30- static void createMethod (IMethodBinding methodBinding , StringBuilder completion ) {
37+ static void createMethod (IMethodBinding methodBinding , StringBuilder completion , ITypeBinding currentType , List < ITypeBinding > importedTypes , String currentPackage ) {
3138
3239 // Modifiers
3340 // flush uninteresting modifiers
@@ -47,14 +54,14 @@ static void createMethod(IMethodBinding methodBinding, StringBuilder completion)
4754 completion .append (',' );
4855 completion .append (' ' );
4956 }
50- createTypeVariable (typeVariableBindings [i ], completion );
57+ createTypeVariable (typeVariableBindings [i ], completion , currentType , importedTypes , currentPackage );
5158 }
5259 completion .append ('>' );
5360 completion .append (' ' );
5461 }
5562
5663 // Return type
57- createType (methodBinding .getReturnType (), completion );
64+ createType (methodBinding .getReturnType (), completion , currentType , importedTypes , currentPackage );
5865 completion .append (' ' );
5966
6067 // Selector (name)
@@ -64,14 +71,19 @@ static void createMethod(IMethodBinding methodBinding, StringBuilder completion)
6471
6572 // Parameters
6673 ITypeBinding [] parameterTypes = methodBinding .getParameterTypes ();
67- String [] parameterNames = methodBinding .getParameterNames ();
74+ String [] parameterNames ;
75+ try {
76+ parameterNames = ((IMethod )methodBinding .getJavaElement ()).getParameterNames ();
77+ } catch (JavaModelException e ) {
78+ parameterNames = methodBinding .getParameterNames ();
79+ }
6880 int length = parameterTypes .length ;
6981 for (int i = 0 ; i < length ; i ++) {
7082 if (i != 0 ) {
7183 completion .append (',' );
7284 completion .append (' ' );
7385 }
74- createType (parameterTypes [i ], completion );
86+ createType (parameterTypes [i ], completion , currentType , importedTypes , currentPackage );
7587 completion .append (' ' );
7688 if (parameterNames != null ) {
7789 completion .append (parameterNames [i ]);
@@ -94,42 +106,42 @@ static void createMethod(IMethodBinding methodBinding, StringBuilder completion)
94106 completion .append (' ' );
95107 completion .append (',' );
96108 }
97- createType (exceptions [i ], completion );
109+ createType (exceptions [i ], completion , currentType , importedTypes , currentPackage );
98110 }
99111 }
100112 }
101113
102- static void createType (ITypeBinding type , StringBuilder completion ) {
114+ static void createType (ITypeBinding type , StringBuilder completion , ITypeBinding currentType , List < ITypeBinding > importedTypes , String currentPackage ) {
103115 if (type .isWildcardType () || type .isIntersectionType ()) {
104116 completion .append ('?' );
105117 if (type .isUpperbound () && type .getBound () != null ) {
106118 completion .append (' ' );
107119 completion .append (EXTENDS );
108120 completion .append (' ' );
109- createType (type .getBound (), completion );
121+ createType (type .getBound (), completion , currentType , importedTypes , currentPackage );
110122 if (type .getTypeBounds () != null ) {
111123 for (ITypeBinding bound : type .getTypeBounds ()) {
112124 completion .append (' ' );
113125 completion .append ('&' );
114126 completion .append (' ' );
115- createType (bound , completion );
127+ createType (bound , completion , currentType , importedTypes , currentPackage );
116128 }
117129 }
118130 } else if (type .getBound () != null ) {
119131 completion .append (' ' );
120132 completion .append (SUPER );
121133 completion .append (' ' );
122- createType (type .getBound (), completion );
134+ createType (type .getBound (), completion , currentType , importedTypes , currentPackage );
123135 }
124136 } else if (type .isArray ()) {
125- createType (type .getElementType (), completion );
137+ createType (type .getElementType (), completion , currentType , importedTypes , currentPackage );
126138 int dim = type .getDimensions ();
127139 for (int i = 0 ; i < dim ; i ++) {
128140 completion .append ("[]" ); //$NON-NLS-1$
129141 }
130142 } else if (type .isParameterizedType ()) {
131143 if (type .isMember ()) {
132- createType (type .getDeclaringClass (), completion );
144+ createType (type .getDeclaringClass (), completion , currentType , importedTypes , currentPackage );
133145 completion .append ('.' );
134146 completion .append (type .getName ());
135147 } else {
@@ -141,24 +153,46 @@ static void createType(ITypeBinding type, StringBuilder completion) {
141153 for (int i = 0 , length = typeArguments .length ; i < length ; i ++) {
142154 if (i != 0 )
143155 completion .append (',' );
144- createType (typeArguments [i ], completion );
156+ createType (typeArguments [i ], completion , currentType , importedTypes , currentPackage );
145157 }
146158 completion .append ('>' );
147159 }
148160 } else {
149- completion .append (type .getQualifiedName ());
161+ boolean appended = false ;
162+ for (ITypeBinding importedType : importedTypes ) {
163+ if (importedType .getKey ().equals (type .getErasure ().getKey ())) {
164+ completion .append (type .getName ());
165+ appended = true ;
166+ break ;
167+ }
168+ }
169+ ITypeBinding firstMatchingInheritedMemberType = getFirstMatchingInheritedMemberType (currentType , type .getName ());
170+ if (firstMatchingInheritedMemberType != null && firstMatchingInheritedMemberType .getKey ().equals (type .getKey ())) {
171+ completion .append (type .getName ());
172+ appended = true ;
173+ }
174+ if (!appended ) {
175+ String qualifiedName = type .getQualifiedName ();
176+ String packageName = type .getPackage () != null ? type .getPackage ().getName () : "" ;
177+ if (qualifiedName .startsWith (JAVA_LANG_PKG )) {
178+ qualifiedName = qualifiedName .substring (JAVA_LANG_PKG .length ());
179+ } else if (!packageName .isEmpty () && currentPackage .startsWith (packageName )) {
180+ qualifiedName = qualifiedName .substring (packageName .length () + 1 );
181+ }
182+ completion .append (qualifiedName );
183+ }
150184 }
151185 }
152186
153- static void createTypeVariable (ITypeBinding typeVariable , StringBuilder completion ) {
187+ static void createTypeVariable (ITypeBinding typeVariable , StringBuilder completion , ITypeBinding currentType , List < ITypeBinding > importedTypes , String currentPackage ) {
154188 completion .append (typeVariable .getName ());
155189
156190 if (typeVariable .getSuperclass () != null
157191 && typeVariable .getTypeBounds ()[0 ].getKey ().equals (typeVariable .getSuperclass ().getKey ())) {
158192 completion .append (' ' );
159193 completion .append (EXTENDS );
160194 completion .append (' ' );
161- createType (typeVariable .getSuperclass (), completion );
195+ createType (typeVariable .getSuperclass (), completion , currentType , importedTypes , currentPackage );
162196 }
163197 if (typeVariable .getInterfaces () != null ) {
164198 if (!typeVariable .getTypeBounds ()[0 ].getKey ().equals (typeVariable .getSuperclass ().getKey ())) {
@@ -172,9 +206,25 @@ static void createTypeVariable(ITypeBinding typeVariable, StringBuilder completi
172206 completion .append (EXTENDS );
173207 completion .append (' ' );
174208 }
175- createType (typeVariable .getInterfaces ()[i ], completion );
209+ createType (typeVariable .getInterfaces ()[i ], completion , currentType , importedTypes , currentPackage );
176210 }
177211 }
178212 }
179213
214+ static ITypeBinding getFirstMatchingInheritedMemberType (ITypeBinding typeBinding , String typeName ) {
215+ return getFirstMatchingInheritedMemberType (typeBinding , typeName , true );
216+ }
217+
218+ static ITypeBinding getFirstMatchingInheritedMemberType (ITypeBinding typeBinding , String typeName , boolean canUsePrivate ) {
219+ for (ITypeBinding memberType : typeBinding .getDeclaredTypes ()) {
220+ if (typeName .equals (memberType .getName ()) && (canUsePrivate || !Flags .isPrivate (memberType .getModifiers ()))) {
221+ return memberType ;
222+ }
223+ }
224+ if (typeBinding .getSuperclass () != null ) {
225+ return getFirstMatchingInheritedMemberType (typeBinding .getSuperclass (), typeName , false );
226+ }
227+ return null ;
228+ }
229+
180230}
0 commit comments