KickJava   Java API By Example, From Geeks To Geeks.

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


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

11 package org.eclipse.jdt.internal.core;
12
13 import org.eclipse.jdt.core.ICompletionRequestor;
14 import org.eclipse.jdt.core.IMethod;
15 import org.eclipse.jdt.core.IType;
16 import org.eclipse.jdt.core.JavaModelException;
17 import org.eclipse.jdt.core.Signature;
18 import org.eclipse.jdt.core.compiler.*;
19 import org.eclipse.jdt.core.compiler.IProblem;
20 import org.eclipse.jdt.internal.codeassist.CompletionEngine;
21 import org.eclipse.jdt.internal.codeassist.IExtendedCompletionRequestor;
22
23 public class CompletionRequestorWrapper implements IExtendedCompletionRequestor {
24     private static Object JavaDoc NO_ATTACHED_SOURCE = new Object JavaDoc();
25     
26     static final char[] ARG = "arg".toCharArray(); //$NON-NLS-1$
27
static final char[] ARG0 = "arg0".toCharArray(); //$NON-NLS-1$
28
static final char[] ARG1 = "arg1".toCharArray(); //$NON-NLS-1$
29
static final char[] ARG2 = "arg2".toCharArray(); //$NON-NLS-1$
30
static final char[] ARG3 = "arg3".toCharArray(); //$NON-NLS-1$
31
static final char[][] ARGS1 = new char[][]{ARG0};
32     static final char[][] ARGS2 = new char[][]{ARG0, ARG1};
33     static final char[][] ARGS3 = new char[][]{ARG0, ARG1, ARG2};
34     static final char[][] ARGS4 = new char[][]{ARG0, ARG1, ARG2, ARG3};
35     
36     public CompletionEngine completionEngine;
37     ICompletionRequestor clientRequestor;
38     NameLookup nameLookup;
39     
40 public CompletionRequestorWrapper(ICompletionRequestor clientRequestor, NameLookup nameLookup){
41     this.clientRequestor = clientRequestor;
42     this.nameLookup = nameLookup;
43 }
44 public void acceptAnonymousType(char[] superTypePackageName,char[] superTypeName,char[][] parameterPackageNames,char[][] parameterTypeNames,char[][] parameterNames,char[] completionName,int modifiers,int completionStart,int completionEnd, int relevance){
45     if(parameterNames == null)
46         parameterNames = findMethodParameterNames(superTypePackageName, superTypeName, superTypeName, parameterPackageNames, parameterTypeNames);
47
48     if(CompletionEngine.DEBUG) {
49         printDebug("acceptAnonymousType", new String JavaDoc[]{ //$NON-NLS-1$
50
String.valueOf(superTypePackageName),
51             String.valueOf(superTypeName),
52             String.valueOf(parameterPackageNames),
53             String.valueOf(parameterTypeNames),
54             String.valueOf(parameterNames),
55             String.valueOf(completionName),
56             String.valueOf(modifiers),
57             String.valueOf(completionStart),
58             String.valueOf(completionEnd),
59             String.valueOf(relevance)
60         });
61     }
62     this.clientRequestor.acceptAnonymousType(superTypePackageName, superTypeName, parameterPackageNames, parameterTypeNames, parameterNames, completionName, modifiers, completionStart, completionEnd, relevance);
63 }
64 /**
65  * See ICompletionRequestor
66  */

67 public void acceptClass(char[] packageName, char[] className, char[] completionName, int modifiers, int completionStart, int completionEnd, int relevance) {
68     
69     if(CompletionEngine.DEBUG) {
70         printDebug("acceptClass", new String JavaDoc[]{ //$NON-NLS-1$
71
String.valueOf(packageName),
72             String.valueOf(className),
73             String.valueOf(completionName),
74             String.valueOf(modifiers),
75             String.valueOf(completionStart),
76             String.valueOf(completionEnd),
77             String.valueOf(relevance)
78         });
79     }
80     this.clientRequestor.acceptClass(packageName, className, completionName, modifiers, completionStart, completionEnd, relevance);
81 }
82 /**
83  * See ICompletionRequestor
84  */

85 public void acceptError(IProblem error) {
86     
87     if(CompletionEngine.DEBUG) {
88         System.out.print("COMPLETION - acceptError("); //$NON-NLS-1$
89
System.out.print(error);
90         System.out.println(")"); //$NON-NLS-1$
91
}
92     this.clientRequestor.acceptError(error);
93 }
94 /**
95  * See ICompletionRequestor
96  */

97 public void acceptField(char[] declaringTypePackageName, char[] declaringTypeName, char[] name, char[] typePackageName, char[] typeName, char[] completionName, int modifiers, int completionStart, int completionEnd, int relevance) {
98     
99     if(CompletionEngine.DEBUG) {
100         printDebug("acceptField", new String JavaDoc[]{ //$NON-NLS-1$
101
String.valueOf(declaringTypePackageName),
102             String.valueOf(declaringTypeName),
103             String.valueOf(name),
104             String.valueOf(typePackageName),
105             String.valueOf(typeName),
106             String.valueOf(completionName),
107             String.valueOf(modifiers),
108             String.valueOf(completionStart),
109             String.valueOf(completionEnd),
110             String.valueOf(relevance)
111         });
112     }
113     this.clientRequestor.acceptField(declaringTypePackageName, declaringTypeName, name, typePackageName, typeName, completionName, modifiers, completionStart, completionEnd, relevance);
114 }
115 /**
116  * See ICompletionRequestor
117  */

118 public void acceptInterface(char[] packageName, char[] interfaceName, char[] completionName, int modifiers, int completionStart, int completionEnd, int relevance) {
119     
120     if(CompletionEngine.DEBUG) {
121         printDebug("acceptInterface", new String JavaDoc[]{ //$NON-NLS-1$
122
String.valueOf(packageName),
123             String.valueOf(interfaceName),
124             String.valueOf(completionName),
125             String.valueOf(modifiers),
126             String.valueOf(completionStart),
127             String.valueOf(completionEnd),
128             String.valueOf(relevance)
129         });
130     }
131     this.clientRequestor.acceptInterface(packageName, interfaceName, completionName, modifiers, completionStart, completionEnd, relevance);
132 }
133 /**
134  * See ICompletionRequestor
135  */

136 public void acceptKeyword(char[] keywordName, int completionStart, int completionEnd, int relevance) {
137     
138     if(CompletionEngine.DEBUG) {
139         printDebug("acceptKeyword", new String JavaDoc[]{ //$NON-NLS-1$
140
String.valueOf(keywordName),
141             String.valueOf(completionStart),
142             String.valueOf(completionEnd),
143             String.valueOf(relevance)
144         });
145     }
146     this.clientRequestor.acceptKeyword(keywordName, completionStart, completionEnd, relevance);
147 }
148 /**
149  * See ICompletionRequestor
150  */

151 public void acceptLabel(char[] labelName, int completionStart, int completionEnd, int relevance) {
152     
153     if(CompletionEngine.DEBUG) {
154         printDebug("acceptLabel", new String JavaDoc[]{ //$NON-NLS-1$
155
String.valueOf(labelName),
156             String.valueOf(completionStart),
157             String.valueOf(completionEnd),
158             String.valueOf(relevance)
159         });
160     }
161     this.clientRequestor.acceptLabel(labelName, completionStart, completionEnd, relevance);
162 }
163 /**
164  * See ICompletionRequestor
165  */

166 public void acceptLocalVariable(char[] name, char[] typePackageName, char[] typeName, int modifiers, int completionStart, int completionEnd, int relevance) {
167     
168     if(CompletionEngine.DEBUG) {
169         printDebug("acceptLocalVariable", new String JavaDoc[]{ //$NON-NLS-1$
170
String.valueOf(name),
171             String.valueOf(typePackageName),
172             String.valueOf(typeName),
173             String.valueOf(modifiers),
174             String.valueOf(completionStart),
175             String.valueOf(completionEnd),
176             String.valueOf(relevance)
177         });
178     }
179     this.clientRequestor.acceptLocalVariable(name, typePackageName, typeName, modifiers, completionStart, completionEnd, relevance);
180 }
181 /**
182  * See ICompletionRequestor
183  */

184 public void acceptMethod(char[] declaringTypePackageName, char[] declaringTypeName, char[] selector, char[][] parameterPackageNames, char[][] parameterTypeNames, char[][] parameterNames, char[] returnTypePackageName, char[] returnTypeName, char[] completionName, int modifiers, int completionStart, int completionEnd, int relevance) {
185     if(parameterNames == null)
186         parameterNames = findMethodParameterNames(declaringTypePackageName, declaringTypeName, selector, parameterPackageNames, parameterTypeNames);
187
188     if(CompletionEngine.DEBUG) {
189         printDebug("acceptMethod", new String JavaDoc[]{ //$NON-NLS-1$
190
String.valueOf(declaringTypePackageName),
191             String.valueOf(declaringTypeName),
192             String.valueOf(selector),
193             String.valueOf(parameterPackageNames),
194             String.valueOf(parameterTypeNames),
195             String.valueOf(parameterNames),
196             String.valueOf(returnTypePackageName),
197             String.valueOf(returnTypeName),
198             String.valueOf(completionName),
199             String.valueOf(modifiers),
200             String.valueOf(completionStart),
201             String.valueOf(completionEnd),
202             String.valueOf(relevance)
203         });
204     }
205     this.clientRequestor.acceptMethod(declaringTypePackageName, declaringTypeName, selector, parameterPackageNames, parameterTypeNames, parameterNames, returnTypePackageName, returnTypeName, completionName, modifiers, completionStart, completionEnd, relevance);
206 }
207 /**
208  * See ICompletionRequestor
209  */

210 public void acceptMethodDeclaration(char[] declaringTypePackageName, char[] declaringTypeName, char[] selector, char[][] parameterPackageNames, char[][] parameterTypeNames, char[][] parameterNames, char[] returnTypePackageName, char[] returnTypeName, char[] completionName, int modifiers, int completionStart, int completionEnd, int relevance) {
211     if(parameterNames == null) {
212         int length = parameterTypeNames.length;
213         
214         parameterNames = findMethodParameterNames(declaringTypePackageName, declaringTypeName, selector, parameterPackageNames, parameterTypeNames);
215         
216         StringBuffer JavaDoc completion = new StringBuffer JavaDoc(completionName.length);
217             
218         int start = 0;
219         int end = CharOperation.indexOf('%', completionName);
220
221         completion.append(CharOperation.subarray(completionName, start, end));
222         
223         for(int i = 0 ; i < length ; i++){
224             completion.append(parameterNames[i]);
225             start = end + 1;
226             end = CharOperation.indexOf('%', completionName, start);
227             if(end > -1){
228                 completion.append(CharOperation.subarray(completionName, start, end));
229             } else {
230                 completion.append(CharOperation.subarray(completionName, start, completionName.length));
231             }
232         }
233         
234         completionName = completion.toString().toCharArray();
235     }
236     
237     if(CompletionEngine.DEBUG) {
238         printDebug("acceptMethodDeclaration", new String JavaDoc[]{ //$NON-NLS-1$
239
String.valueOf(declaringTypePackageName),
240             String.valueOf(declaringTypeName),
241             String.valueOf(selector),
242             String.valueOf(parameterPackageNames),
243             String.valueOf(parameterTypeNames),
244             String.valueOf(parameterNames),
245             String.valueOf(returnTypePackageName),
246             String.valueOf(returnTypeName),
247             String.valueOf(completionName),
248             String.valueOf(modifiers),
249             String.valueOf(completionStart),
250             String.valueOf(completionEnd),
251             String.valueOf(relevance)
252         });
253     }
254     this.clientRequestor.acceptMethodDeclaration(declaringTypePackageName, declaringTypeName, selector, parameterPackageNames, parameterTypeNames, parameterNames, returnTypePackageName, returnTypeName, completionName, modifiers, completionStart, completionEnd, relevance);
255 }
256 /**
257  * See ICompletionRequestor
258  */

259 public void acceptModifier(char[] modifierName, int completionStart, int completionEnd, int relevance) {
260     
261     if(CompletionEngine.DEBUG) {
262         printDebug("acceptModifier", new String JavaDoc[]{ //$NON-NLS-1$
263
String.valueOf(modifierName),
264             String.valueOf(completionStart),
265             String.valueOf(completionEnd),
266             String.valueOf(relevance)
267         });
268     }
269     this.clientRequestor.acceptModifier(modifierName, completionStart, completionEnd, relevance);
270 }
271 /**
272  * See ICompletionRequestor
273  */

274 public void acceptPackage(char[] packageName, char[] completionName, int completionStart, int completionEnd, int relevance) {
275     
276     if(CompletionEngine.DEBUG) {
277         printDebug("acceptPackage", new String JavaDoc[]{ //$NON-NLS-1$
278
String.valueOf(packageName),
279             String.valueOf(completionName),
280             String.valueOf(completionStart),
281             String.valueOf(completionEnd),
282             String.valueOf(relevance)
283         });
284     }
285     this.clientRequestor.acceptPackage(packageName, completionName, completionStart, completionEnd, relevance);
286 }
287 /**
288  * See ICompletionRequestor
289  */

290 public void acceptType(char[] packageName, char[] typeName, char[] completionName, int completionStart, int completionEnd, int relevance) {
291     
292     if(CompletionEngine.DEBUG) {
293         printDebug("acceptType", new String JavaDoc[]{ //$NON-NLS-1$
294
String.valueOf(packageName),
295             String.valueOf(typeName),
296             String.valueOf(completionName),
297             String.valueOf(completionStart),
298             String.valueOf(completionEnd),
299             String.valueOf(relevance)
300         });
301     }
302     this.clientRequestor.acceptType(packageName, typeName, completionName, completionStart, completionEnd, relevance);
303 }
304 public void acceptVariableName(char[] typePackageName, char[] typeName, char[] name, char[] completionName, int completionStart, int completionEnd, int relevance){
305     
306     if(CompletionEngine.DEBUG) {
307         printDebug("acceptVariableName", new String JavaDoc[]{ //$NON-NLS-1$
308
String.valueOf(typePackageName),
309             String.valueOf(typeName),
310             String.valueOf(name),
311             String.valueOf(completionName),
312             String.valueOf(completionStart),
313             String.valueOf(completionEnd),
314             String.valueOf(relevance)
315         });
316     }
317     this.clientRequestor.acceptVariableName(typePackageName, typeName, name, completionName, completionStart, completionEnd, relevance);
318 }
319 private char[][] findMethodParameterNames(char[] declaringTypePackageName, char[] declaringTypeName, char[] selector, char[][] parameterPackageNames, char[][] parameterTypeNames){
320     char[][] parameterNames = null;
321     int length = parameterTypeNames.length;
322     
323     char[] typeName = CharOperation.concat(declaringTypePackageName,declaringTypeName,'.');
324     Object JavaDoc cachedType = this.completionEngine.typeCache.get(typeName);
325     
326     IType type = null;
327     if(cachedType != null) {
328         if(cachedType != NO_ATTACHED_SOURCE && cachedType instanceof BinaryType) {
329             type = (BinaryType)cachedType;
330         }
331     } else {
332         type = this.nameLookup.findType(new String JavaDoc(typeName), false, NameLookup.ACCEPT_CLASSES & NameLookup.ACCEPT_INTERFACES);
333         if(type instanceof BinaryType){
334             if(((BinaryType)type).getSourceMapper() != null) {
335                 this.completionEngine.typeCache.put(typeName, type);
336             } else {
337                 this.completionEngine.typeCache.put(typeName, NO_ATTACHED_SOURCE);
338                 type = null;
339             }
340         } else {
341             type = null;
342         }
343     }
344     
345     if(type != null) {
346         String JavaDoc[] args = new String JavaDoc[length];
347         for(int i = 0; i< length ; i++){
348             char[] parameterType = CharOperation.concat(parameterPackageNames[i],parameterTypeNames[i],'.');
349             args[i] = Signature.createTypeSignature(parameterType,true);
350         }
351         IMethod method = type.getMethod(new String JavaDoc(selector),args);
352         try{
353             parameterNames = new char[length][];
354             String JavaDoc[] params = method.getParameterNames();
355             for(int i = 0; i< length ; i++){
356                 parameterNames[i] = params[i].toCharArray();
357             }
358         } catch(JavaModelException e){
359             parameterNames = null;
360         }
361     }
362     // default parameters name
363
if(parameterNames == null) {
364         switch (length) {
365             case 0 :
366                 parameterNames = new char[length][];
367                 break;
368             case 1 :
369                 parameterNames = ARGS1;
370                 break;
371             case 2 :
372                 parameterNames = ARGS2;
373                 break;
374             case 3 :
375                 parameterNames = ARGS3;
376                 break;
377             case 4 :
378                 parameterNames = ARGS4;
379                 break;
380             default :
381                 parameterNames = new char[length][];
382                 for (int i = 0; i < length; i++) {
383                     parameterNames[i] = CharOperation.concat(ARG, String.valueOf(i).toCharArray());
384                 }
385                 break;
386         }
387         
388     }
389     return parameterNames;
390 }
391 public void acceptPotentialMethodDeclaration(char[] declaringTypePackageName,
392         char[] declaringTypeName, char[] selector, int completionStart,
393         int completionEnd, int relevance) {
394     if(this.clientRequestor instanceof IExtendedCompletionRequestor) {
395         if(CompletionEngine.DEBUG) {
396             printDebug("acceptPotentialMethodDeclaration", new String JavaDoc[]{ //$NON-NLS-1$
397
String.valueOf(declaringTypePackageName),
398                 String.valueOf(declaringTypeName),
399                 String.valueOf(selector),
400                 String.valueOf(completionStart),
401                 String.valueOf(completionEnd),
402                 String.valueOf(relevance)
403             });
404         }
405         
406         ((IExtendedCompletionRequestor)this.clientRequestor).acceptPotentialMethodDeclaration(
407             declaringTypePackageName,
408             declaringTypeName,
409             selector,
410             completionStart,
411             completionEnd,
412             relevance);
413     }
414 }
415
416 private void printDebug(String JavaDoc header, String JavaDoc[] param){
417     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
418     buffer.append("COMPLETION - "); //$NON-NLS-1$
419
buffer.append(header);
420     buffer.append("(");//$NON-NLS-1$
421

422     for (int i = 0; i < param.length; i++) {
423         if(i != 0)
424             buffer.append(", ");//$NON-NLS-1$
425
buffer.append(param[i]);
426     }
427
428     buffer.append(")");//$NON-NLS-1$
429
System.out.println(buffer.toString());
430 }
431 }
432
Popular Tags