KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > eval > EvaluationContextWrapper


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.eval;
12
13 import java.util.Locale JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.jdt.core.*;
18 import org.eclipse.jdt.core.ICompilationUnit;
19 import org.eclipse.jdt.core.IImportDeclaration;
20 import org.eclipse.jdt.core.IJavaElement;
21 import org.eclipse.jdt.core.IJavaModelStatusConstants;
22 import org.eclipse.jdt.core.IJavaProject;
23 import org.eclipse.jdt.core.IType;
24 import org.eclipse.jdt.core.JavaModelException;
25 import org.eclipse.jdt.core.compiler.IProblem;
26 import org.eclipse.jdt.core.eval.ICodeSnippetRequestor;
27 import org.eclipse.jdt.core.eval.IEvaluationContext;
28 import org.eclipse.jdt.core.eval.IGlobalVariable;
29 import org.eclipse.jdt.internal.compiler.IProblemFactory;
30 import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
31 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
32 import org.eclipse.jdt.internal.core.*;
33 import org.eclipse.jdt.internal.core.BinaryType;
34 import org.eclipse.jdt.internal.core.ClassFile;
35 import org.eclipse.jdt.internal.core.JavaModelStatus;
36 import org.eclipse.jdt.internal.core.JavaProject;
37 import org.eclipse.jdt.internal.core.SelectionRequestor;
38 import org.eclipse.jdt.internal.core.SourceMapper;
39 import org.eclipse.jdt.internal.core.builder.NameEnvironment;
40 import org.eclipse.jdt.internal.core.builder.ProblemFactory;
41 import org.eclipse.jdt.internal.eval.EvaluationContext;
42 import org.eclipse.jdt.internal.eval.GlobalVariable;
43 import org.eclipse.jdt.internal.eval.IRequestor;
44 import org.eclipse.jdt.internal.eval.InstallException;
45
46 /**
47  * A wrapper around the infrastructure evaluation context.
48  */

49 public class EvaluationContextWrapper implements IEvaluationContext {
50     protected EvaluationContext context;
51     protected JavaProject project;
52 /**
53  * Creates a new wrapper around the given infrastructure evaluation context
54  * and project.
55  */

56 public EvaluationContextWrapper(EvaluationContext context, JavaProject project) {
57     this.context = context;
58     this.project = project;
59 }
60 /**
61  * @see org.eclipse.jdt.core.eval.IEvaluationContext#allVariables()
62  */

63 public IGlobalVariable[] allVariables() {
64     GlobalVariable[] vars = this.context.allVariables();
65     int length = vars.length;
66     GlobalVariableWrapper[] result = new GlobalVariableWrapper[length];
67     for (int i = 0; i < length; i++) {
68         result[i] = new GlobalVariableWrapper(vars[i]);
69     }
70     return result;
71 }
72 /**
73  * Checks to ensure that there is a previously built state.
74  */

75 protected void checkBuilderState() {
76     
77     return;
78 }
79 /**
80  * @see org.eclipse.jdt.core.eval.IEvaluationContext#codeComplete(String, int, ICompletionRequestor)
81  * @deprecated
82  */

83 public void codeComplete(String JavaDoc codeSnippet, int position, ICompletionRequestor requestor) throws JavaModelException {
84     codeComplete(codeSnippet, position, requestor, DefaultWorkingCopyOwner.PRIMARY);
85 }
86 /**
87  * @see org.eclipse.jdt.core.eval.IEvaluationContext#codeComplete(String, int, ICompletionRequestor, WorkingCopyOwner)
88  * @deprecated
89  */

90 public void codeComplete(String JavaDoc codeSnippet, int position, ICompletionRequestor requestor, WorkingCopyOwner owner) throws JavaModelException {
91     if (requestor == null) {
92         throw new IllegalArgumentException JavaDoc("Completion requestor cannot be null"); //$NON-NLS-1$
93
}
94     codeComplete(codeSnippet, position, new org.eclipse.jdt.internal.codeassist.CompletionRequestorWrapper(requestor), owner);
95 }
96 /**
97  * @see org.eclipse.jdt.core.eval.IEvaluationContext#codeComplete(String, int, CompletionRequestor)
98  */

99 public void codeComplete(String JavaDoc codeSnippet, int position, CompletionRequestor requestor) throws JavaModelException {
100     codeComplete(codeSnippet, position, requestor, DefaultWorkingCopyOwner.PRIMARY);
101 }
102 /**
103  * @see org.eclipse.jdt.core.eval.IEvaluationContext#codeComplete(String, int, CompletionRequestor, WorkingCopyOwner)
104  */

105 public void codeComplete(String JavaDoc codeSnippet, int position, CompletionRequestor requestor, WorkingCopyOwner owner) throws JavaModelException {
106     SearchableEnvironment environment = this.project.newSearchableNameEnvironment(owner);
107     this.context.complete(
108         codeSnippet.toCharArray(),
109         position,
110         environment,
111         requestor,
112         this.project.getOptions(true),
113         this.project
114     );
115 }
116 /**
117  * @see org.eclipse.jdt.core.eval.IEvaluationContext#codeSelect(String, int, int)
118  */

119 public IJavaElement[] codeSelect(String JavaDoc codeSnippet, int offset, int length) throws JavaModelException {
120     return codeSelect(codeSnippet, offset, length, DefaultWorkingCopyOwner.PRIMARY);
121 }
122 /**
123  * @see org.eclipse.jdt.core.eval.IEvaluationContext#codeSelect(String, int, int, WorkingCopyOwner)
124  */

125 public IJavaElement[] codeSelect(String JavaDoc codeSnippet, int offset, int length, WorkingCopyOwner owner) throws JavaModelException {
126     SearchableEnvironment environment = this.project.newSearchableNameEnvironment(owner);
127     SelectionRequestor requestor= new SelectionRequestor(environment.nameLookup, null); // null because there is no need to look inside the code snippet itself
128
this.context.select(
129         codeSnippet.toCharArray(),
130         offset,
131         offset + length - 1,
132         environment,
133         requestor,
134         this.project.getOptions(true)
135     );
136     return requestor.getElements();
137 }
138 /**
139  * @see org.eclipse.jdt.core.eval.IEvaluationContext#deleteVariable(IGlobalVariable)
140  */

141 public void deleteVariable(IGlobalVariable variable) {
142     if (variable instanceof GlobalVariableWrapper) {
143         GlobalVariableWrapper wrapper = (GlobalVariableWrapper)variable;
144         this.context.deleteVariable(wrapper.variable);
145     } else {
146         throw new Error JavaDoc("Unknown implementation of IGlobalVariable"); //$NON-NLS-1$
147
}
148 }
149 /**
150  * @see IEvaluationContext#evaluateCodeSnippet(String, String[], String[], int[], IType, boolean, boolean, ICodeSnippetRequestor, IProgressMonitor)
151  */

152 public void evaluateCodeSnippet(
153     String JavaDoc codeSnippet,
154     String JavaDoc[] localVariableTypeNames,
155     String JavaDoc[] localVariableNames,
156     int[] localVariableModifiers,
157     IType declaringType,
158     boolean isStatic,
159     boolean isConstructorCall,
160     ICodeSnippetRequestor requestor,
161     IProgressMonitor progressMonitor) throws org.eclipse.jdt.core.JavaModelException {
162         
163     checkBuilderState();
164     
165     int length = localVariableTypeNames.length;
166     char[][] varTypeNames = new char[length][];
167     for (int i = 0; i < length; i++){
168         varTypeNames[i] = localVariableTypeNames[i].toCharArray();
169     }
170
171     length = localVariableNames.length;
172     char[][] varNames = new char[length][];
173     for (int i = 0; i < length; i++){
174         varNames[i] = localVariableNames[i].toCharArray();
175     }
176
177     Map JavaDoc options = this.project.getOptions(true);
178     // transfer the imports of the IType to the evaluation context
179
if (declaringType != null) {
180         // retrieves the package statement
181
this.context.setPackageName(declaringType.getPackageFragment().getElementName().toCharArray());
182         ICompilationUnit compilationUnit = declaringType.getCompilationUnit();
183         if (compilationUnit != null) {
184             // retrieves the import statement
185
IImportDeclaration[] imports = compilationUnit.getImports();
186             int importsLength = imports.length;
187             if (importsLength != 0) {
188                 char[][] importsNames = new char[importsLength][];
189                 for (int i = 0; i < importsLength; i++) {
190                     importsNames[i] = imports[i].getElementName().toCharArray();
191                 }
192                 this.context.setImports(importsNames);
193                 // turn off import complaints for implicitly added ones
194
options.put(CompilerOptions.OPTION_ReportUnusedImport, CompilerOptions.IGNORE);
195             }
196         } else {
197             // try to retrieve imports from the source
198
SourceMapper sourceMapper = ((ClassFile) declaringType.getClassFile()).getSourceMapper();
199             if (sourceMapper != null) {
200                 char[][] imports = sourceMapper.getImports((BinaryType) declaringType);
201                 if (imports != null) {
202                     this.context.setImports(imports);
203                     // turn off import complaints for implicitly added ones
204
options.put(CompilerOptions.OPTION_ReportUnusedImport, CompilerOptions.IGNORE);
205                 }
206             }
207         }
208     }
209     INameEnvironment environment = null;
210     try {
211         this.context.evaluate(
212             codeSnippet.toCharArray(),
213             varTypeNames,
214             varNames,
215             localVariableModifiers,
216             declaringType == null? null : declaringType.getFullyQualifiedName().toCharArray(),
217             isStatic,
218             isConstructorCall,
219             environment = getBuildNameEnvironment(),
220             options,
221             getInfrastructureEvaluationRequestor(requestor),
222             getProblemFactory());
223     } catch (InstallException e) {
224         handleInstallException(e);
225     } finally {
226         if (environment != null) environment.cleanup();
227     }
228 }
229 /**
230  * @see IEvaluationContext#evaluateCodeSnippet(String, ICodeSnippetRequestor, IProgressMonitor)
231  */

232 public void evaluateCodeSnippet(String JavaDoc codeSnippet, ICodeSnippetRequestor requestor, IProgressMonitor progressMonitor) throws JavaModelException {
233
234     checkBuilderState();
235     INameEnvironment environment = null;
236     try {
237         this.context.evaluate(
238             codeSnippet.toCharArray(),
239             environment = getBuildNameEnvironment(),
240             this.project.getOptions(true),
241             getInfrastructureEvaluationRequestor(requestor),
242             getProblemFactory());
243     } catch (InstallException e) {
244         handleInstallException(e);
245     } finally {
246         if (environment != null) environment.cleanup();
247     }
248 }
249 /**
250  * @see IEvaluationContext#evaluateVariable(IGlobalVariable, ICodeSnippetRequestor, IProgressMonitor)
251  */

252 public void evaluateVariable(IGlobalVariable variable, ICodeSnippetRequestor requestor, IProgressMonitor progressMonitor) throws JavaModelException {
253
254     checkBuilderState();
255     INameEnvironment environment = null;
256     try {
257         this.context.evaluateVariable(
258             ((GlobalVariableWrapper)variable).variable,
259             environment = getBuildNameEnvironment(),
260             this.project.getOptions(true),
261             getInfrastructureEvaluationRequestor(requestor),
262             getProblemFactory());
263     } catch (InstallException e) {
264         handleInstallException(e);
265     } finally {
266         if (environment != null) environment.cleanup();
267     }
268 }
269 /**
270  * Returns a name environment for the last built state.
271  */

272 protected INameEnvironment getBuildNameEnvironment() {
273     return new NameEnvironment(getProject());
274 }
275 public char[] getVarClassName() {
276     return this.context.getVarClassName();
277 }
278
279 /**
280  * @see org.eclipse.jdt.core.eval.IEvaluationContext#getImports()
281  */

282 public String JavaDoc[] getImports() {
283     char[][] imports = this.context.getImports();
284     int length = imports.length;
285     String JavaDoc[] result = new String JavaDoc[length];
286     for (int i = 0; i < length; i++) {
287         result[i] = new String JavaDoc(imports[i]);
288     }
289     return result;
290 }
291 /**
292  * Returns the infrastructure evaluation context.
293  */

294 public EvaluationContext getInfrastructureEvaluationContext() {
295     return this.context;
296 }
297 /**
298  * Returns a new infrastructure evaluation requestor instance.
299  */

300 protected IRequestor getInfrastructureEvaluationRequestor(ICodeSnippetRequestor requestor) {
301     return new RequestorWrapper(requestor);
302 }
303 /**
304  * @see org.eclipse.jdt.core.eval.IEvaluationContext#getPackageName()
305  */

306 public String JavaDoc getPackageName() {
307     return new String JavaDoc(this.context.getPackageName());
308 }
309 /**
310  * Returns the problem factory to be used during evaluation.
311  */

312 protected IProblemFactory getProblemFactory() {
313     return ProblemFactory.getProblemFactory(Locale.getDefault());
314 }
315 /**
316  * @see org.eclipse.jdt.core.eval.IEvaluationContext#getProject()
317  */

318 public IJavaProject getProject() {
319     return this.project;
320 }
321 /**
322  * Handles an install exception by throwing a Java Model exception.
323  */

324 protected void handleInstallException(InstallException e) throws JavaModelException {
325     throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.EVALUATION_ERROR, e.toString()));
326 }
327 /**
328  * @see org.eclipse.jdt.core.eval.IEvaluationContext#newVariable(String, String, String)
329  */

330 public IGlobalVariable newVariable(String JavaDoc typeName, String JavaDoc name, String JavaDoc initializer) {
331     GlobalVariable newVar =
332         this.context.newVariable(
333             typeName.toCharArray(),
334             name.toCharArray(),
335             (initializer == null) ?
336                 null :
337                 initializer.toCharArray());
338     return new GlobalVariableWrapper(newVar);
339 }
340 /**
341  * @see org.eclipse.jdt.core.eval.IEvaluationContext#setImports(String[])
342  */

343 public void setImports(String JavaDoc[] imports) {
344     int length = imports.length;
345     char[][] result = new char[length][];
346     for (int i = 0; i < length; i++) {
347         result[i] = imports[i].toCharArray();
348     }
349     this.context.setImports(result);
350 }
351 /**
352  * @see org.eclipse.jdt.core.eval.IEvaluationContext#setPackageName(String)
353  */

354 public void setPackageName(String JavaDoc packageName) {
355     this.context.setPackageName(packageName.toCharArray());
356 }
357 /**
358  * @see IEvaluationContext#validateImports(ICodeSnippetRequestor)
359  */

360 public void validateImports(ICodeSnippetRequestor requestor) {
361     
362     checkBuilderState();
363     INameEnvironment environment = null;
364     try {
365         this.context.evaluateImports(
366             environment = getBuildNameEnvironment(),
367             getInfrastructureEvaluationRequestor(requestor),
368             getProblemFactory());
369     } finally {
370         if (environment != null) environment.cleanup();
371     }
372 }
373 /**
374  * @see IEvaluationContext#codeComplete(String, int, ICodeCompletionRequestor)
375  * @deprecated - use codeComplete(String, int, ICompletionRequestor) instead
376  */

377 public void codeComplete(String JavaDoc codeSnippet, int position, final org.eclipse.jdt.core.ICodeCompletionRequestor requestor) throws JavaModelException {
378
379     if (requestor == null){
380         codeComplete(codeSnippet, position, (ICompletionRequestor)null);
381         return;
382     }
383     codeComplete(
384         codeSnippet,
385         position,
386         new ICompletionRequestor(){
387             public void acceptAnonymousType(char[] superTypePackageName,char[] superTypeName,char[][] parameterPackageNames,char[][] parameterTypeNames,char[][] parameterNames,char[] completionName,int modifiers,int completionStart,int completionEnd, int relevance) {
388                 // implements interface method
389
}
390             public void acceptClass(char[] packageName, char[] className, char[] completionName, int modifiers, int completionStart, int completionEnd, int relevance) {
391                 requestor.acceptClass(packageName, className, completionName, modifiers, completionStart, completionEnd);
392             }
393             public void acceptError(IProblem error) {
394                 // was disabled in 1.0
395
}
396             
397             public void acceptField(char[] declaringTypePackageName, char[] declaringTypeName, char[] name, char[] typePackageName, char[] typeName, char[] completionName, int modifiers, int completionStart, int completionEnd, int relevance) {
398                 requestor.acceptField(declaringTypePackageName, declaringTypeName, name, typePackageName, typeName, completionName, modifiers, completionStart, completionEnd);
399             }
400             public void acceptInterface(char[] packageName,char[] interfaceName,char[] completionName,int modifiers,int completionStart,int completionEnd, int relevance) {
401                 requestor.acceptInterface(packageName, interfaceName, completionName, modifiers, completionStart, completionEnd);
402             }
403             public void acceptKeyword(char[] keywordName,int completionStart,int completionEnd, int relevance){
404                 requestor.acceptKeyword(keywordName, completionStart, completionEnd);
405             }
406             public void acceptLabel(char[] labelName,int completionStart,int completionEnd, int relevance){
407                 requestor.acceptLabel(labelName, completionStart, completionEnd);
408             }
409             public void acceptLocalVariable(char[] name,char[] typePackageName,char[] typeName,int modifiers,int completionStart,int completionEnd, int relevance){
410                 // ignore
411
}
412             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){
413                 // skip parameter names
414
requestor.acceptMethod(declaringTypePackageName, declaringTypeName, selector, parameterPackageNames, parameterTypeNames, returnTypePackageName, returnTypeName, completionName, modifiers, completionStart, completionEnd);
415             }
416             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){
417                 // ignore
418
}
419             public void acceptModifier(char[] modifierName,int completionStart,int completionEnd, int relevance){
420                 requestor.acceptModifier(modifierName, completionStart, completionEnd);
421             }
422             public void acceptPackage(char[] packageName,char[] completionName,int completionStart,int completionEnd, int relevance){
423                 requestor.acceptPackage(packageName, completionName, completionStart, completionEnd);
424             }
425             public void acceptType(char[] packageName,char[] typeName,char[] completionName,int completionStart,int completionEnd, int relevance){
426                 requestor.acceptType(packageName, typeName, completionName, completionStart, completionEnd);
427             }
428             public void acceptVariableName(char[] typePackageName,char[] typeName,char[] name,char[] completionName,int completionStart,int completionEnd, int relevance){
429                 // ignore
430
}
431         });
432 }
433 }
434
Popular Tags