KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > SelectionRequestor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.jdt.core.Flags;
16 import org.eclipse.jdt.core.ICompilationUnit;
17 import org.eclipse.jdt.core.IField;
18 import org.eclipse.jdt.core.IJavaElement;
19 import org.eclipse.jdt.core.IMethod;
20 import org.eclipse.jdt.core.IPackageFragment;
21 import org.eclipse.jdt.core.ISourceRange;
22 import org.eclipse.jdt.core.IType;
23 import org.eclipse.jdt.core.ITypeParameter;
24 import org.eclipse.jdt.core.JavaModelException;
25 import org.eclipse.jdt.core.Signature;
26 import org.eclipse.jdt.core.compiler.*;
27 import org.eclipse.jdt.internal.codeassist.ISelectionRequestor;
28 import org.eclipse.jdt.internal.codeassist.SelectionEngine;
29 import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration;
30 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
31 import org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
32 import org.eclipse.jdt.internal.compiler.lookup.LocalTypeBinding;
33 import org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding;
34 import org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
35 import org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding;
36 import org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding;
37 import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
38 import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
39 import org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding;
40 import org.eclipse.jdt.internal.core.util.HandleFactory;
41 import org.eclipse.jdt.internal.core.util.Util;
42
43 /**
44  * Implementation of <code>ISelectionRequestor</code> to assist with
45  * code resolve in a compilation unit. Translates names to elements.
46  */

47 public class SelectionRequestor implements ISelectionRequestor {
48     /*
49      * The name lookup facility used to resolve packages
50      */

51     protected NameLookup nameLookup;
52
53     /*
54      * The compilation unit or class file we are resolving in
55      */

56     protected Openable openable;
57
58     /*
59      * The collection of resolved elements.
60      */

61     protected IJavaElement[] elements = JavaElement.NO_ELEMENTS;
62     protected int elementIndex = -1;
63
64     protected HandleFactory handleFactory = new HandleFactory();
65
66 /**
67  * Creates a selection requestor that uses that given
68  * name lookup facility to resolve names.
69  *
70  * Fix for 1FVXGDK
71  */

72 public SelectionRequestor(NameLookup nameLookup, Openable openable) {
73     super();
74     this.nameLookup = nameLookup;
75     this.openable = openable;
76 }
77 private void acceptBinaryMethod(
78         IType type,
79         IMethod method,
80         char[] uniqueKey,
81         boolean isConstructor) {
82     try {
83         if(!isConstructor || ((JavaElement)method).getSourceMapper() == null) {
84             if (uniqueKey != null) {
85                 ResolvedBinaryMethod resolvedMethod = new ResolvedBinaryMethod(
86                         (JavaElement)method.getParent(),
87                         method.getElementName(),
88                         method.getParameterTypes(),
89                         new String JavaDoc(uniqueKey));
90                 resolvedMethod.occurrenceCount = method.getOccurrenceCount();
91                 method = resolvedMethod;
92             }
93
94             addElement(method);
95             if(SelectionEngine.DEBUG){
96                 System.out.print("SELECTION - accept method("); //$NON-NLS-1$
97
System.out.print(method.toString());
98                 System.out.println(")"); //$NON-NLS-1$
99
}
100         } else {
101             ISourceRange range = method.getSourceRange();
102             if (range.getOffset() != -1 && range.getLength() != 0 ) {
103                 if (uniqueKey != null) {
104                     ResolvedBinaryMethod resolvedMethod = new ResolvedBinaryMethod(
105                             (JavaElement)method.getParent(),
106                             method.getElementName(),
107                             method.getParameterTypes(),
108                             new String JavaDoc(uniqueKey));
109                     resolvedMethod.occurrenceCount = method.getOccurrenceCount();
110                     method = resolvedMethod;
111                 }
112                 addElement(method);
113                 if(SelectionEngine.DEBUG){
114                     System.out.print("SELECTION - accept method("); //$NON-NLS-1$
115
System.out.print(method.toString());
116                     System.out.println(")"); //$NON-NLS-1$
117
}
118             } else {
119                 // no range was actually found, but a method was originally given -> default constructor
120
addElement(type);
121                 if(SelectionEngine.DEBUG){
122                     System.out.print("SELECTION - accept type("); //$NON-NLS-1$
123
System.out.print(type.toString());
124                     System.out.println(")"); //$NON-NLS-1$
125
}
126             }
127         }
128     } catch (JavaModelException e) {
129         // an exception occurs, return nothing
130
}
131 }
132 /**
133  * Resolve the binary method
134  *
135  * fix for 1FWFT6Q
136  */

137 protected void acceptBinaryMethod(
138         IType type,
139         char[] selector,
140         char[][] parameterPackageNames,
141         char[][] parameterTypeNames,
142         String JavaDoc[] parameterSignatures,
143         char[][] typeParameterNames,
144         char[][][] typeParameterBoundNames,
145         char[] uniqueKey,
146         boolean isConstructor) {
147     IMethod method= type.getMethod(new String JavaDoc(selector), parameterSignatures);
148
149     if (method.exists()) {
150         if (typeParameterNames != null && typeParameterNames.length != 0) {
151             IMethod[] methods = type.findMethods(method);
152             if (methods.length > 1) {
153                 for (int i = 0; i < methods.length; i++) {
154                     if (areTypeParametersCompatible(methods[i], typeParameterNames, typeParameterBoundNames)) {
155                         acceptBinaryMethod(type, method, uniqueKey, isConstructor);
156                     }
157                 }
158                 return;
159             }
160         }
161         acceptBinaryMethod(type, method, uniqueKey, isConstructor);
162     }
163 }
164 /**
165  * Resolve the type.
166  */

167 public void acceptType(char[] packageName, char[] typeName, int modifiers, boolean isDeclaration, char[] uniqueKey, int start, int end) {
168     int acceptFlags = 0;
169     int kind = modifiers & (ClassFileConstants.AccInterface|ClassFileConstants.AccEnum|ClassFileConstants.AccAnnotation);
170     switch (kind) {
171         case ClassFileConstants.AccAnnotation:
172         case ClassFileConstants.AccAnnotation|ClassFileConstants.AccInterface:
173             acceptFlags = NameLookup.ACCEPT_ANNOTATIONS;
174             break;
175         case ClassFileConstants.AccEnum:
176             acceptFlags = NameLookup.ACCEPT_ENUMS;
177             break;
178         case ClassFileConstants.AccInterface:
179             acceptFlags = NameLookup.ACCEPT_INTERFACES;
180             break;
181         default:
182             acceptFlags = NameLookup.ACCEPT_CLASSES;
183             break;
184     }
185     IType type = null;
186     if(isDeclaration) {
187         type = resolveTypeByLocation(packageName, typeName, acceptFlags, start, end);
188     } else {
189         type = resolveType(packageName, typeName, acceptFlags);
190         if(type != null ) {
191             String JavaDoc key = uniqueKey == null ? type.getKey() : new String JavaDoc(uniqueKey);
192             if(type.isBinary()) {
193                 ResolvedBinaryType resolvedType = new ResolvedBinaryType((JavaElement)type.getParent(), type.getElementName(), key);
194                 resolvedType.occurrenceCount = type.getOccurrenceCount();
195                 type = resolvedType;
196             } else {
197                 ResolvedSourceType resolvedType = new ResolvedSourceType((JavaElement)type.getParent(), type.getElementName(), key);
198                 resolvedType.occurrenceCount = type.getOccurrenceCount();
199                 type = resolvedType;
200             }
201         }
202     }
203
204     if (type != null) {
205         addElement(type);
206         if(SelectionEngine.DEBUG){
207             System.out.print("SELECTION - accept type("); //$NON-NLS-1$
208
System.out.print(type.toString());
209             System.out.println(")"); //$NON-NLS-1$
210
}
211     }
212 }
213 /**
214  * @see ISelectionRequestor#acceptError
215  */

216 public void acceptError(CategorizedProblem error) {
217     // do nothing
218
}
219 /**
220  * Resolve the field.
221  */

222 public void acceptField(char[] declaringTypePackageName, char[] declaringTypeName, char[] name, boolean isDeclaration, char[] uniqueKey, int start, int end) {
223     if(isDeclaration) {
224         IType type= resolveTypeByLocation(declaringTypePackageName, declaringTypeName,
225                 NameLookup.ACCEPT_ALL,
226                 start, end);
227         if(type != null) {
228             try {
229                 IField[] fields = type.getFields();
230                 for (int i = 0; i < fields.length; i++) {
231                     IField field = fields[i];
232                     ISourceRange range = field.getNameRange();
233                     if(range.getOffset() <= start
234                             && range.getOffset() + range.getLength() >= end
235                             && field.getElementName().equals(new String JavaDoc(name))) {
236                         addElement(fields[i]);
237                         if(SelectionEngine.DEBUG){
238                             System.out.print("SELECTION - accept field("); //$NON-NLS-1$
239
System.out.print(field.toString());
240                             System.out.println(")"); //$NON-NLS-1$
241
}
242                         return; // only one method is possible
243
}
244                 }
245             } catch (JavaModelException e) {
246                 return;
247             }
248         }
249     } else {
250         IType type= resolveType(declaringTypePackageName, declaringTypeName, NameLookup.ACCEPT_ALL);
251         if (type != null) {
252             IField field= type.getField(new String JavaDoc(name));
253             if (field.exists()) {
254                 if (uniqueKey != null) {
255                     if(field.isBinary()) {
256                         ResolvedBinaryField resolvedField = new ResolvedBinaryField(
257                                 (JavaElement)field.getParent(),
258                                 field.getElementName(),
259                                 new String JavaDoc(uniqueKey));
260                         resolvedField.occurrenceCount = field.getOccurrenceCount();
261                         field = resolvedField;
262                     } else {
263                         ResolvedSourceField resolvedField = new ResolvedSourceField(
264                                 (JavaElement)field.getParent(),
265                                 field.getElementName(),
266                                 new String JavaDoc(uniqueKey));
267                         resolvedField.occurrenceCount = field.getOccurrenceCount();
268                         field = resolvedField;
269                     }
270                 }
271                 addElement(field);
272                 if(SelectionEngine.DEBUG){
273                     System.out.print("SELECTION - accept field("); //$NON-NLS-1$
274
System.out.print(field.toString());
275                     System.out.println(")"); //$NON-NLS-1$
276
}
277             }
278         }
279     }
280 }
281 public void acceptLocalField(FieldBinding fieldBinding) {
282     IJavaElement res;
283     if(fieldBinding.declaringClass instanceof ParameterizedTypeBinding) {
284         LocalTypeBinding localTypeBinding = (LocalTypeBinding)((ParameterizedTypeBinding)fieldBinding.declaringClass).genericType();
285         res = findLocalElement(localTypeBinding.sourceStart());
286     } else {
287         SourceTypeBinding typeBinding = (SourceTypeBinding)fieldBinding.declaringClass;
288         res = findLocalElement(typeBinding.sourceStart());
289     }
290     if (res != null && res.getElementType() == IJavaElement.TYPE) {
291         IType type = (IType) res;
292         IField field= type.getField(new String JavaDoc(fieldBinding.name));
293         if (field.exists()) {
294             char[] uniqueKey = fieldBinding.computeUniqueKey();
295             if(field.isBinary()) {
296                 ResolvedBinaryField resolvedField = new ResolvedBinaryField(
297                         (JavaElement)field.getParent(),
298                         field.getElementName(),
299                         new String JavaDoc(uniqueKey));
300                 resolvedField.occurrenceCount = field.getOccurrenceCount();
301                 field = resolvedField;
302             } else {
303                 ResolvedSourceField resolvedField = new ResolvedSourceField(
304                         (JavaElement)field.getParent(),
305                         field.getElementName(),
306                         new String JavaDoc(uniqueKey));
307                 resolvedField.occurrenceCount = field.getOccurrenceCount();
308                 field = resolvedField;
309             }
310             addElement(field);
311             if(SelectionEngine.DEBUG){
312                 System.out.print("SELECTION - accept field("); //$NON-NLS-1$
313
System.out.print(field.toString());
314                 System.out.println(")"); //$NON-NLS-1$
315
}
316         }
317     }
318 }
319 public void acceptLocalMethod(MethodBinding methodBinding) {
320     IJavaElement res = findLocalElement(methodBinding.sourceStart());
321     if(res != null) {
322         if(res.getElementType() == IJavaElement.METHOD) {
323             IMethod method = (IMethod) res;
324
325             char[] uniqueKey = methodBinding.computeUniqueKey();
326             if(method.isBinary()) {
327                 ResolvedBinaryMethod resolvedRes = new ResolvedBinaryMethod(
328                         (JavaElement)res.getParent(),
329                         method.getElementName(),
330                         method.getParameterTypes(),
331                         new String JavaDoc(uniqueKey));
332                 resolvedRes.occurrenceCount = method.getOccurrenceCount();
333                 res = resolvedRes;
334             } else {
335                 ResolvedSourceMethod resolvedRes = new ResolvedSourceMethod(
336                         (JavaElement)res.getParent(),
337                         method.getElementName(),
338                         method.getParameterTypes(),
339                         new String JavaDoc(uniqueKey));
340                 resolvedRes.occurrenceCount = method.getOccurrenceCount();
341                 res = resolvedRes;
342             }
343             addElement(res);
344             if(SelectionEngine.DEBUG){
345                 System.out.print("SELECTION - accept method("); //$NON-NLS-1$
346
System.out.print(res.toString());
347                 System.out.println(")"); //$NON-NLS-1$
348
}
349         } else if(methodBinding.selector == TypeConstants.INIT && res.getElementType() == IJavaElement.TYPE) {
350             // it's a default constructor
351
res = ((JavaElement)res).resolved(methodBinding.declaringClass);
352             addElement(res);
353             if(SelectionEngine.DEBUG){
354                 System.out.print("SELECTION - accept type("); //$NON-NLS-1$
355
System.out.print(res.toString());
356                 System.out.println(")"); //$NON-NLS-1$
357
}
358         }
359     }
360 }
361 public void acceptLocalType(TypeBinding typeBinding) {
362     IJavaElement res = null;
363     if(typeBinding instanceof ParameterizedTypeBinding) {
364         LocalTypeBinding localTypeBinding = (LocalTypeBinding)((ParameterizedTypeBinding)typeBinding).genericType();
365         res = findLocalElement(localTypeBinding.sourceStart());
366     } else if(typeBinding instanceof SourceTypeBinding) {
367         res = findLocalElement(((SourceTypeBinding)typeBinding).sourceStart());
368     }
369     if(res != null && res.getElementType() == IJavaElement.TYPE) {
370         res = ((JavaElement)res).resolved(typeBinding);
371         addElement(res);
372         if(SelectionEngine.DEBUG){
373             System.out.print("SELECTION - accept type("); //$NON-NLS-1$
374
System.out.print(res.toString());
375             System.out.println(")"); //$NON-NLS-1$
376
}
377     }
378 }
379 public void acceptLocalTypeParameter(TypeVariableBinding typeVariableBinding) {
380     IJavaElement res;
381     if(typeVariableBinding.declaringElement instanceof ParameterizedTypeBinding) {
382         LocalTypeBinding localTypeBinding = (LocalTypeBinding)((ParameterizedTypeBinding)typeVariableBinding.declaringElement).genericType();
383         res = findLocalElement(localTypeBinding.sourceStart());
384     } else {
385         SourceTypeBinding typeBinding = (SourceTypeBinding)typeVariableBinding.declaringElement;
386         res = findLocalElement(typeBinding.sourceStart());
387     }
388     if (res != null && res.getElementType() == IJavaElement.TYPE) {
389         IType type = (IType) res;
390         ITypeParameter typeParameter = type.getTypeParameter(new String JavaDoc(typeVariableBinding.sourceName));
391         if (typeParameter.exists()) {
392             addElement(typeParameter);
393             if(SelectionEngine.DEBUG){
394                 System.out.print("SELECTION - accept type parameter("); //$NON-NLS-1$
395
System.out.print(typeParameter.toString());
396                 System.out.println(")"); //$NON-NLS-1$
397
}
398         }
399     }
400 }
401 public void acceptLocalMethodTypeParameter(TypeVariableBinding typeVariableBinding) {
402     MethodBinding methodBinding = (MethodBinding)typeVariableBinding.declaringElement;
403     IJavaElement res = findLocalElement(methodBinding.sourceStart());
404     if(res != null && res.getElementType() == IJavaElement.METHOD) {
405         IMethod method = (IMethod) res;
406
407         ITypeParameter typeParameter = method.getTypeParameter(new String JavaDoc(typeVariableBinding.sourceName));
408         if (typeParameter.exists()) {
409             addElement(typeParameter);
410             if(SelectionEngine.DEBUG){
411                 System.out.print("SELECTION - accept type parameter("); //$NON-NLS-1$
412
System.out.print(typeParameter.toString());
413                 System.out.println(")"); //$NON-NLS-1$
414
}
415         }
416     }
417 }
418 public void acceptLocalVariable(LocalVariableBinding binding) {
419     LocalDeclaration local = binding.declaration;
420     IJavaElement parent = findLocalElement(local.sourceStart); // findLocalElement() cannot find local variable
421
IJavaElement localVar = null;
422     if(parent != null) {
423         localVar = new LocalVariable(
424                 (JavaElement)parent,
425                 new String JavaDoc(local.name),
426                 local.declarationSourceStart,
427                 local.declarationSourceEnd,
428                 local.sourceStart,
429                 local.sourceEnd,
430                 Util.typeSignature(local.type));
431     }
432     if (localVar != null) {
433         addElement(localVar);
434         if(SelectionEngine.DEBUG){
435             System.out.print("SELECTION - accept local variable("); //$NON-NLS-1$
436
System.out.print(localVar.toString());
437             System.out.println(")"); //$NON-NLS-1$
438
}
439     }
440 }
441 /**
442  * Resolve the method
443  */

444 public void acceptMethod(
445         char[] declaringTypePackageName,
446         char[] declaringTypeName,
447         String JavaDoc enclosingDeclaringTypeSignature,
448         char[] selector,
449         char[][] parameterPackageNames,
450         char[][] parameterTypeNames,
451         String JavaDoc[] parameterSignatures,
452         char[][] typeParameterNames,
453         char[][][] typeParameterBoundNames,
454         boolean isConstructor,
455         boolean isDeclaration,
456         char[] uniqueKey,
457         int start,
458         int end) {
459     IJavaElement[] previousElement = this.elements;
460     int previousElementIndex = this.elementIndex;
461     this.elements = JavaElement.NO_ELEMENTS;
462     this.elementIndex = -1;
463
464     if(isDeclaration) {
465         IType type = resolveTypeByLocation(declaringTypePackageName, declaringTypeName,
466                 NameLookup.ACCEPT_ALL,
467                 start, end);
468
469         if(type != null) {
470             this.acceptMethodDeclaration(type, selector, start, end);
471         }
472     } else {
473         IType type = resolveType(declaringTypePackageName, declaringTypeName,
474             NameLookup.ACCEPT_ALL);
475         // fix for 1FWFT6Q
476
if (type != null) {
477             if (type.isBinary()) {
478
479                 // need to add a paramater for constructor in binary type
480
IType declaringDeclaringType = type.getDeclaringType();
481
482                 boolean isStatic = false;
483                 try {
484                     isStatic = Flags.isStatic(type.getFlags());
485                 } catch (JavaModelException e) {
486                     // isStatic == false
487
}
488
489                 if(declaringDeclaringType != null && isConstructor && !isStatic) {
490                     int length = parameterPackageNames.length;
491                     System.arraycopy(parameterPackageNames, 0, parameterPackageNames = new char[length+1][], 1, length);
492                     System.arraycopy(parameterTypeNames, 0, parameterTypeNames = new char[length+1][], 1, length);
493                     System.arraycopy(parameterSignatures, 0, parameterSignatures = new String JavaDoc[length+1], 1, length);
494
495                     parameterPackageNames[0] = declaringDeclaringType.getPackageFragment().getElementName().toCharArray();
496                     parameterTypeNames[0] = declaringDeclaringType.getTypeQualifiedName().toCharArray();
497                     parameterSignatures[0] = Signature.getTypeErasure(enclosingDeclaringTypeSignature);
498                 }
499
500                 acceptBinaryMethod(type, selector, parameterPackageNames, parameterTypeNames, parameterSignatures, typeParameterNames, typeParameterBoundNames, uniqueKey, isConstructor);
501             } else {
502                 acceptSourceMethod(type, selector, parameterPackageNames, parameterTypeNames, parameterSignatures, typeParameterNames, typeParameterBoundNames, uniqueKey);
503             }
504         }
505     }
506
507     if(previousElementIndex > -1) {
508         int elementsLength = this.elementIndex + previousElementIndex + 2;
509         if(elementsLength > this.elements.length) {
510             System.arraycopy(this.elements, 0, this.elements = new IJavaElement[elementsLength * 2 + 1], 0, this.elementIndex + 1);
511         }
512         System.arraycopy(previousElement, 0, this.elements, this.elementIndex + 1, previousElementIndex + 1);
513         this.elementIndex += previousElementIndex + 1;
514     }
515 }
516 /**
517  * Resolve the package
518  */

519 public void acceptPackage(char[] packageName) {
520     IPackageFragment[] pkgs = this.nameLookup.findPackageFragments(new String JavaDoc(packageName), false);
521     if (pkgs != null) {
522         for (int i = 0, length = pkgs.length; i < length; i++) {
523             addElement(pkgs[i]);
524             if(SelectionEngine.DEBUG){
525                 System.out.print("SELECTION - accept package("); //$NON-NLS-1$
526
System.out.print(pkgs[i].toString());
527                 System.out.println(")"); //$NON-NLS-1$
528
}
529         }
530     }
531 }
532 /**
533  * Resolve the source method
534  *
535  * fix for 1FWFT6Q
536  */

537 protected void acceptSourceMethod(
538         IType type,
539         char[] selector,
540         char[][] parameterPackageNames,
541         char[][] parameterTypeNames,
542         String JavaDoc[] parameterSignatures,
543         char[][] typeParameterNames,
544         char[][][] typeParameterBoundNames,
545         char[] uniqueKey) {
546
547     String JavaDoc name = new String JavaDoc(selector);
548     IMethod[] methods = null;
549     try {
550         methods = type.getMethods();
551         for (int i = 0; i < methods.length; i++) {
552             if (methods[i].getElementName().equals(name)
553                     && methods[i].getParameterTypes().length == parameterTypeNames.length) {
554                 IMethod method = methods[i];
555                 if (uniqueKey != null) {
556                     ResolvedSourceMethod resolvedMethod = new ResolvedSourceMethod(
557                         (JavaElement)method.getParent(),
558                         method.getElementName(),
559                         method.getParameterTypes(),
560                         new String JavaDoc(uniqueKey));
561                     resolvedMethod.occurrenceCount = method.getOccurrenceCount();
562                     method = resolvedMethod;
563                 }
564                 addElement(method);
565             }
566         }
567     } catch (JavaModelException e) {
568         return;
569     }
570
571     // if no matches, nothing to report
572
if (this.elementIndex == -1) {
573         // no match was actually found, but a method was originally given -> default constructor
574
addElement(type);
575         if(SelectionEngine.DEBUG){
576             System.out.print("SELECTION - accept type("); //$NON-NLS-1$
577
System.out.print(type.toString());
578             System.out.println(")"); //$NON-NLS-1$
579
}
580         return;
581     }
582
583     // if there is only one match, we've got it
584
if (this.elementIndex == 0) {
585         if(SelectionEngine.DEBUG){
586             System.out.print("SELECTION - accept method("); //$NON-NLS-1$
587
System.out.print(this.elements[0].toString());
588             System.out.println(")"); //$NON-NLS-1$
589
}
590         return;
591     }
592
593     // more than one match - must match simple parameter types
594
IJavaElement[] matches = this.elements;
595     int matchesIndex = this.elementIndex;
596     this.elements = JavaElement.NO_ELEMENTS;
597     this.elementIndex = -1;
598     for (int i = 0; i <= matchesIndex; i++) {
599         IMethod method= (IMethod)matches[i];
600         String JavaDoc[] signatures = method.getParameterTypes();
601         boolean match= true;
602         for (int p = 0; p < signatures.length; p++) {
603             String JavaDoc simpleName= Signature.getSimpleName(Signature.toString(Signature.getTypeErasure(signatures[p])));
604             char[] simpleParameterName = CharOperation.lastSegment(parameterTypeNames[p], '.');
605             if (!simpleName.equals(new String JavaDoc(simpleParameterName))) {
606                 match = false;
607                 break;
608             }
609         }
610
611         if (match && !areTypeParametersCompatible(method, typeParameterNames, typeParameterBoundNames)) {
612             match = false;
613         }
614
615         if (match) {
616             addElement(method);
617             if(SelectionEngine.DEBUG){
618                 System.out.print("SELECTION - accept method("); //$NON-NLS-1$
619
System.out.print(method.toString());
620                 System.out.println(")"); //$NON-NLS-1$
621
}
622         }
623     }
624
625 }
626 protected void acceptMethodDeclaration(IType type, char[] selector, int start, int end) {
627     String JavaDoc name = new String JavaDoc(selector);
628     IMethod[] methods = null;
629     try {
630         methods = type.getMethods();
631         for (int i = 0; i < methods.length; i++) {
632             ISourceRange range = methods[i].getNameRange();
633             if(range.getOffset() <= start
634                     && range.getOffset() + range.getLength() >= end
635                     && methods[i].getElementName().equals(name)) {
636                 addElement(methods[i]);
637                 if(SelectionEngine.DEBUG){
638                     System.out.print("SELECTION - accept method("); //$NON-NLS-1$
639
System.out.print(this.elements[0].toString());
640                     System.out.println(")"); //$NON-NLS-1$
641
}
642                 return; // only one method is possible
643
}
644         }
645     } catch (JavaModelException e) {
646         return;
647     }
648
649     // no match was actually found
650
addElement(type);
651     if(SelectionEngine.DEBUG){
652         System.out.print("SELECTION - accept type("); //$NON-NLS-1$
653
System.out.print(type.toString());
654         System.out.println(")"); //$NON-NLS-1$
655
}
656     return;
657 }
658 public void acceptTypeParameter(char[] declaringTypePackageName, char[] declaringTypeName, char[] typeParameterName, boolean isDeclaration, int start, int end) {
659     IType type;
660     if(isDeclaration) {
661         type = resolveTypeByLocation(declaringTypePackageName, declaringTypeName,
662                 NameLookup.ACCEPT_ALL,
663                 start, end);
664     } else {
665         type = resolveType(declaringTypePackageName, declaringTypeName,
666                 NameLookup.ACCEPT_ALL);
667     }
668
669     if(type != null) {
670         ITypeParameter typeParameter = type.getTypeParameter(new String JavaDoc(typeParameterName));
671         if(typeParameter == null) {
672             addElement(type);
673             if(SelectionEngine.DEBUG){
674                 System.out.print("SELECTION - accept type("); //$NON-NLS-1$
675
System.out.print(type.toString());
676                 System.out.println(")"); //$NON-NLS-1$
677
}
678         } else {
679             addElement(typeParameter);
680             if(SelectionEngine.DEBUG){
681                 System.out.print("SELECTION - accept type parameter("); //$NON-NLS-1$
682
System.out.print(typeParameter.toString());
683                 System.out.println(")"); //$NON-NLS-1$
684
}
685         }
686     }
687 }
688 public void acceptMethodTypeParameter(char[] declaringTypePackageName, char[] declaringTypeName, char[] selector,int selectorStart, int selectorEnd, char[] typeParameterName, boolean isDeclaration, int start, int end) {
689     IType type = resolveTypeByLocation(declaringTypePackageName, declaringTypeName,
690             NameLookup.ACCEPT_ALL,
691             selectorStart, selectorEnd);
692
693     if(type != null) {
694         IMethod method = null;
695
696         String JavaDoc name = new String JavaDoc(selector);
697         IMethod[] methods = null;
698
699         try {
700             methods = type.getMethods();
701             done : for (int i = 0; i < methods.length; i++) {
702                 ISourceRange range = methods[i].getNameRange();
703                 if(range.getOffset() >= selectorStart
704                         && range.getOffset() + range.getLength() <= selectorEnd
705                         && methods[i].getElementName().equals(name)) {
706                     method = methods[i];
707                     break done;
708                 }
709             }
710         } catch (JavaModelException e) {
711             //nothing to do
712
}
713
714         if(method == null) {
715             addElement(type);
716             if(SelectionEngine.DEBUG){
717                 System.out.print("SELECTION - accept type("); //$NON-NLS-1$
718
System.out.print(type.toString());
719                 System.out.println(")"); //$NON-NLS-1$
720
}
721         } else {
722             ITypeParameter typeParameter = method.getTypeParameter(new String JavaDoc(typeParameterName));
723             if(typeParameter == null) {
724                 addElement(method);
725                 if(SelectionEngine.DEBUG){
726                     System.out.print("SELECTION - accept method("); //$NON-NLS-1$
727
System.out.print(method.toString());
728                     System.out.println(")"); //$NON-NLS-1$
729
}
730             } else {
731                 addElement(typeParameter);
732                 if(SelectionEngine.DEBUG){
733                     System.out.print("SELECTION - accept method type parameter("); //$NON-NLS-1$
734
System.out.print(typeParameter.toString());
735                     System.out.println(")"); //$NON-NLS-1$
736
}
737             }
738         }
739     }
740 }
741 /*
742  * Adds the given element to the list of resolved elements.
743  */

744 protected void addElement(IJavaElement element) {
745     int elementLength = this.elementIndex + 1;
746     if (elementLength == this.elements.length) {
747         System.arraycopy(this.elements, 0, this.elements = new IJavaElement[(elementLength*2) + 1], 0, elementLength);
748     }
749     this.elements[++this.elementIndex] = element;
750 }
751 private boolean areTypeParametersCompatible(IMethod method, char[][] typeParameterNames, char[][][] typeParameterBoundNames) {
752     try {
753         ITypeParameter[] typeParameters = method.getTypeParameters();
754         int length1 = typeParameters == null ? 0 : typeParameters.length;
755         int length2 = typeParameterNames == null ? 0 : typeParameterNames.length;
756         if (length1 != length2) {
757             return false;
758         } else {
759             for (int j = 0; j < length1; j++) {
760                 ITypeParameter typeParameter = typeParameters[j];
761                 String JavaDoc typeParameterName = typeParameter.getElementName();
762                 if (!typeParameterName.equals(new String JavaDoc(typeParameterNames[j]))) {
763                     return false;
764                 }
765
766                 String JavaDoc[] bounds = typeParameter.getBounds();
767                 int boundCount = typeParameterBoundNames[j] == null ? 0 : typeParameterBoundNames[j].length;
768
769                 if (bounds.length != boundCount) {
770                     return false;
771                 } else {
772                     for (int k = 0; k < boundCount; k++) {
773                         String JavaDoc simpleName = Signature.getSimpleName(bounds[k]);
774                         int index = simpleName.indexOf('<');
775                         if (index != -1) {
776                             simpleName = simpleName.substring(0, index);
777                         }
778                         if (!simpleName.equals(new String JavaDoc(typeParameterBoundNames[j][k]))) {
779                             return false;
780                         }
781                     }
782                 }
783             }
784         }
785     } catch (JavaModelException e) {
786         return false;
787     }
788     return true;
789 }
790 /*
791  * findLocalElement() cannot find local variable
792  */

793 protected IJavaElement findLocalElement(int pos) {
794     IJavaElement res = null;
795     if(this.openable instanceof ICompilationUnit) {
796         ICompilationUnit cu = (ICompilationUnit) this.openable;
797         try {
798             res = cu.getElementAt(pos);
799         } catch (JavaModelException e) {
800             // do nothing
801
}
802     } else if (this.openable instanceof ClassFile) {
803         ClassFile cf = (ClassFile) this.openable;
804         try {
805              res = cf.getElementAtConsideringSibling(pos);
806         } catch (JavaModelException e) {
807             // do nothing
808
}
809     }
810     return res;
811 }
812 /**
813  * Returns the resolved elements.
814  */

815 public IJavaElement[] getElements() {
816     int elementLength = this.elementIndex + 1;
817     if (this.elements.length != elementLength) {
818         System.arraycopy(this.elements, 0, this.elements = new IJavaElement[elementLength], 0, elementLength);
819     }
820     return this.elements;
821 }
822 /**
823  * Resolve the type
824  */

825 protected IType resolveType(char[] packageName, char[] typeName, int acceptFlags) {
826
827     IType type= null;
828
829     if (this.openable instanceof CompilationUnit && ((CompilationUnit)this.openable).isWorkingCopy()) {
830         CompilationUnit wc = (CompilationUnit) this.openable;
831         try {
832             if(((packageName == null || packageName.length == 0) && wc.getPackageDeclarations().length == 0) ||
833                 (!(packageName == null || packageName.length == 0) && wc.getPackageDeclaration(new String JavaDoc(packageName)).exists())) {
834
835                 char[][] compoundName = CharOperation.splitOn('.', typeName);
836                 if(compoundName.length > 0) {
837                     type = wc.getType(new String JavaDoc(compoundName[0]));
838                     for (int i = 1, length = compoundName.length; i < length; i++) {
839                         type = type.getType(new String JavaDoc(compoundName[i]));
840                     }
841                 }
842
843                 if(type != null && !type.exists()) {
844                     type = null;
845                 }
846             }
847         }catch (JavaModelException e) {
848             // type is null
849
}
850     }
851
852     if(type == null) {
853         IPackageFragment[] pkgs = this.nameLookup.findPackageFragments(
854             (packageName == null || packageName.length == 0) ? IPackageFragment.DEFAULT_PACKAGE_NAME : new String JavaDoc(packageName),
855             false);
856         // iterate type lookup in each package fragment
857
for (int i = 0, length = pkgs == null ? 0 : pkgs.length; i < length; i++) {
858             type= this.nameLookup.findType(new String JavaDoc(typeName), pkgs[i], false, acceptFlags, true/*consider secondary types*/);
859             if (type != null) break;
860         }
861         if (type == null) {
862             String JavaDoc pName= IPackageFragment.DEFAULT_PACKAGE_NAME;
863             if (packageName != null) {
864                 pName = new String JavaDoc(packageName);
865             }
866             if (this.openable != null && this.openable.getParent().getElementName().equals(pName)) {
867                 // look inside the type in which we are resolving in
868
String JavaDoc tName= new String JavaDoc(typeName);
869                 tName = tName.replace('.','$');
870                 IType[] allTypes= null;
871                 try {
872                     ArrayList JavaDoc list = this.openable.getChildrenOfType(IJavaElement.TYPE);
873                     allTypes = new IType[list.size()];
874                     list.toArray(allTypes);
875                 } catch (JavaModelException e) {
876                     return null;
877                 }
878                 for (int i= 0; i < allTypes.length; i++) {
879                     if (allTypes[i].getTypeQualifiedName().equals(tName)) {
880                         return allTypes[i];
881                     }
882                 }
883             }
884         }
885     }
886     return type;
887 }
888 protected IType resolveTypeByLocation(char[] packageName, char[] typeName, int acceptFlags, int start, int end) {
889
890     IType type= null;
891
892     // TODO (david) post 3.0 should remove isOpen check, and investigate reusing ICompilationUnit#getElementAt. may need to optimize #getElementAt to remove recursions
893
if (this.openable instanceof CompilationUnit && ((CompilationUnit)this.openable).isOpen()) {
894         CompilationUnit wc = (CompilationUnit) this.openable;
895         try {
896             if(((packageName == null || packageName.length == 0) && wc.getPackageDeclarations().length == 0) ||
897                 (!(packageName == null || packageName.length == 0) && wc.getPackageDeclaration(new String JavaDoc(packageName)).exists())) {
898
899                 char[][] compoundName = CharOperation.splitOn('.', typeName);
900                 if(compoundName.length > 0) {
901
902                     IType[] tTypes = wc.getTypes();
903                     int i = 0;
904                     int depth = 0;
905                     done : while(i < tTypes.length) {
906                         ISourceRange range = tTypes[i].getSourceRange();
907                         if(range.getOffset() <= start
908                                 && range.getOffset() + range.getLength() >= end
909                                 && tTypes[i].getElementName().equals(new String JavaDoc(compoundName[depth]))) {
910                             if(depth == compoundName.length - 1) {
911                                 type = tTypes[i];
912                                 break done;
913                             }
914                             tTypes = tTypes[i].getTypes();
915                             i = 0;
916                             depth++;
917                             continue done;
918                         }
919                         i++;
920                     }
921                 }
922
923                 if(type != null && !type.exists()) {
924                     type = null;
925                 }
926             }
927         }catch (JavaModelException e) {
928             // type is null
929
}
930     }
931
932     if(type == null) {
933         IPackageFragment[] pkgs = this.nameLookup.findPackageFragments(
934             (packageName == null || packageName.length == 0) ? IPackageFragment.DEFAULT_PACKAGE_NAME : new String JavaDoc(packageName),
935             false);
936         // iterate type lookup in each package fragment
937
for (int i = 0, length = pkgs == null ? 0 : pkgs.length; i < length; i++) {
938             type= this.nameLookup.findType(new String JavaDoc(typeName), pkgs[i], false, acceptFlags, true/*consider secondary types*/);
939             if (type != null) break;
940         }
941         if (type == null) {
942             String JavaDoc pName= IPackageFragment.DEFAULT_PACKAGE_NAME;
943             if (packageName != null) {
944                 pName = new String JavaDoc(packageName);
945             }
946             if (this.openable != null && this.openable.getParent().getElementName().equals(pName)) {
947                 // look inside the type in which we are resolving in
948
String JavaDoc tName= new String JavaDoc(typeName);
949                 tName = tName.replace('.','$');
950                 IType[] allTypes= null;
951                 try {
952                     ArrayList JavaDoc list = this.openable.getChildrenOfType(IJavaElement.TYPE);
953                     allTypes = new IType[list.size()];
954                     list.toArray(allTypes);
955                 } catch (JavaModelException e) {
956                     return null;
957                 }
958                 for (int i= 0; i < allTypes.length; i++) {
959                     if (allTypes[i].getTypeQualifiedName().equals(tName)) {
960                         return allTypes[i];
961                     }
962                 }
963             }
964         }
965     }
966     return type;
967 }
968 }
969
Popular Tags