1 11 package org.eclipse.jdt.internal.core.eval; 12 13 import java.util.Locale ; 14 import java.util.Map ; 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 49 public class EvaluationContextWrapper implements IEvaluationContext { 50 protected EvaluationContext context; 51 protected JavaProject project; 52 56 public EvaluationContextWrapper(EvaluationContext context, JavaProject project) { 57 this.context = context; 58 this.project = project; 59 } 60 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 75 protected void checkBuilderState() { 76 77 return; 78 } 79 83 public void codeComplete(String codeSnippet, int position, ICompletionRequestor requestor) throws JavaModelException { 84 codeComplete(codeSnippet, position, requestor, DefaultWorkingCopyOwner.PRIMARY); 85 } 86 90 public void codeComplete(String codeSnippet, int position, ICompletionRequestor requestor, WorkingCopyOwner owner) throws JavaModelException { 91 if (requestor == null) { 92 throw new IllegalArgumentException ("Completion requestor cannot be null"); } 94 codeComplete(codeSnippet, position, new org.eclipse.jdt.internal.codeassist.CompletionRequestorWrapper(requestor), owner); 95 } 96 99 public void codeComplete(String codeSnippet, int position, CompletionRequestor requestor) throws JavaModelException { 100 codeComplete(codeSnippet, position, requestor, DefaultWorkingCopyOwner.PRIMARY); 101 } 102 105 public void codeComplete(String 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 119 public IJavaElement[] codeSelect(String codeSnippet, int offset, int length) throws JavaModelException { 120 return codeSelect(codeSnippet, offset, length, DefaultWorkingCopyOwner.PRIMARY); 121 } 122 125 public IJavaElement[] codeSelect(String codeSnippet, int offset, int length, WorkingCopyOwner owner) throws JavaModelException { 126 SearchableEnvironment environment = this.project.newSearchableNameEnvironment(owner); 127 SelectionRequestor requestor= new SelectionRequestor(environment.nameLookup, null); 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 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 ("Unknown implementation of IGlobalVariable"); } 148 } 149 152 public void evaluateCodeSnippet( 153 String codeSnippet, 154 String [] localVariableTypeNames, 155 String [] 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 options = this.project.getOptions(true); 178 if (declaringType != null) { 180 this.context.setPackageName(declaringType.getPackageFragment().getElementName().toCharArray()); 182 ICompilationUnit compilationUnit = declaringType.getCompilationUnit(); 183 if (compilationUnit != null) { 184 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 options.put(CompilerOptions.OPTION_ReportUnusedImport, CompilerOptions.IGNORE); 195 } 196 } else { 197 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 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 232 public void evaluateCodeSnippet(String 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 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 272 protected INameEnvironment getBuildNameEnvironment() { 273 return new NameEnvironment(getProject()); 274 } 275 public char[] getVarClassName() { 276 return this.context.getVarClassName(); 277 } 278 279 282 public String [] getImports() { 283 char[][] imports = this.context.getImports(); 284 int length = imports.length; 285 String [] result = new String [length]; 286 for (int i = 0; i < length; i++) { 287 result[i] = new String (imports[i]); 288 } 289 return result; 290 } 291 294 public EvaluationContext getInfrastructureEvaluationContext() { 295 return this.context; 296 } 297 300 protected IRequestor getInfrastructureEvaluationRequestor(ICodeSnippetRequestor requestor) { 301 return new RequestorWrapper(requestor); 302 } 303 306 public String getPackageName() { 307 return new String (this.context.getPackageName()); 308 } 309 312 protected IProblemFactory getProblemFactory() { 313 return ProblemFactory.getProblemFactory(Locale.getDefault()); 314 } 315 318 public IJavaProject getProject() { 319 return this.project; 320 } 321 324 protected void handleInstallException(InstallException e) throws JavaModelException { 325 throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.EVALUATION_ERROR, e.toString())); 326 } 327 330 public IGlobalVariable newVariable(String typeName, String name, String 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 343 public void setImports(String [] 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 354 public void setPackageName(String packageName) { 355 this.context.setPackageName(packageName.toCharArray()); 356 } 357 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 377 public void codeComplete(String 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 } 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 } 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 } 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 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 } 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 } 431 }); 432 } 433 } 434 | Popular Tags |