1 11 package org.eclipse.jdt.core.dom; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 18 import org.eclipse.core.runtime.IProgressMonitor; 19 import org.eclipse.core.runtime.OperationCanceledException; 20 import org.eclipse.jdt.core.ICompilationUnit; 21 import org.eclipse.jdt.core.IJavaElement; 22 import org.eclipse.jdt.core.IJavaProject; 23 import org.eclipse.jdt.core.JavaModelException; 24 import org.eclipse.jdt.core.WorkingCopyOwner; 25 import org.eclipse.jdt.core.compiler.CategorizedProblem; 26 import org.eclipse.jdt.core.compiler.CharOperation; 27 import org.eclipse.jdt.internal.compiler.CompilationResult; 28 import org.eclipse.jdt.internal.compiler.Compiler; 29 import org.eclipse.jdt.internal.compiler.DefaultErrorHandlingPolicies; 30 import org.eclipse.jdt.internal.compiler.ICompilerRequestor; 31 import org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy; 32 import org.eclipse.jdt.internal.compiler.IProblemFactory; 33 import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration; 34 import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; 35 import org.eclipse.jdt.internal.compiler.env.AccessRestriction; 36 import org.eclipse.jdt.internal.compiler.env.INameEnvironment; 37 import org.eclipse.jdt.internal.compiler.env.ISourceType; 38 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; 39 import org.eclipse.jdt.internal.compiler.lookup.Binding; 40 import org.eclipse.jdt.internal.compiler.lookup.ExtraCompilerModifiers; 41 import org.eclipse.jdt.internal.compiler.lookup.PackageBinding; 42 import org.eclipse.jdt.internal.compiler.parser.Parser; 43 import org.eclipse.jdt.internal.compiler.problem.AbortCompilation; 44 import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory; 45 import org.eclipse.jdt.internal.compiler.problem.ProblemReporter; 46 import org.eclipse.jdt.internal.compiler.util.HashtableOfObject; 47 import org.eclipse.jdt.internal.compiler.util.HashtableOfObjectToInt; 48 import org.eclipse.jdt.internal.compiler.util.Messages; 49 import org.eclipse.jdt.internal.core.BinaryMember; 50 import org.eclipse.jdt.internal.core.CancelableNameEnvironment; 51 import org.eclipse.jdt.internal.core.CancelableProblemFactory; 52 import org.eclipse.jdt.internal.core.JavaProject; 53 import org.eclipse.jdt.internal.core.NameLookup; 54 import org.eclipse.jdt.internal.core.SourceRefElement; 55 import org.eclipse.jdt.internal.core.SourceTypeElementInfo; 56 import org.eclipse.jdt.internal.core.util.BindingKeyResolver; 57 import org.eclipse.jdt.internal.core.util.CommentRecorderParser; 58 import org.eclipse.jdt.internal.core.util.DOMFinder; 59 60 class CompilationUnitResolver extends Compiler { 61 62 63 static class IntArrayList { 64 public int[] list = new int[5]; 65 public int length = 0; 66 public void add(int i) { 67 if (this.list.length == this.length) { 68 System.arraycopy(this.list, 0, this.list = new int[this.length*2], 0, this.length); 69 } 70 this.list[this.length++] = i; 71 } 72 } 73 74 78 HashtableOfObject requestedSources; 79 80 84 HashtableOfObject requestedKeys; 85 86 DefaultBindingResolver.BindingTables bindingTables; 87 88 boolean hasCompilationAborted; 89 90 private IProgressMonitor monitor; 91 92 127 public CompilationUnitResolver( 128 INameEnvironment environment, 129 IErrorHandlingPolicy policy, 130 CompilerOptions compilerOptions, 131 ICompilerRequestor requestor, 132 IProblemFactory problemFactory, 133 IProgressMonitor monitor) { 134 135 super(environment, policy, compilerOptions, requestor, problemFactory); 136 this.hasCompilationAborted = false; 137 this.monitor =monitor; 138 } 139 140 143 public void accept(ISourceType[] sourceTypes, PackageBinding packageBinding, AccessRestriction accessRestriction) { 144 SourceTypeElementInfo sourceType = (SourceTypeElementInfo) sourceTypes[0]; 147 accept((org.eclipse.jdt.internal.compiler.env.ICompilationUnit) sourceType.getHandle().getCompilationUnit(), accessRestriction); 148 } 149 150 154 protected void beginToCompile(org.eclipse.jdt.internal.compiler.env.ICompilationUnit[] sourceUnits, String [] bindingKeys) { 155 int sourceLength = sourceUnits.length; 156 int keyLength = bindingKeys.length; 157 int maxUnits = sourceLength + keyLength; 158 this.totalUnits = 0; 159 this.unitsToProcess = new CompilationUnitDeclaration[maxUnits]; 160 int index = 0; 161 162 this.requestedSources = new HashtableOfObject(); 164 for (int i = 0; i < sourceLength; i++) { 165 org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit = sourceUnits[i]; 166 CompilationUnitDeclaration parsedUnit; 167 CompilationResult unitResult = 168 new CompilationResult(sourceUnit, index++, maxUnits, this.options.maxProblemsPerUnit); 169 try { 170 if (options.verbose) { 171 this.out.println( 172 Messages.bind(Messages.compilation_request, 173 new String [] { 174 String.valueOf(index++ + 1), 175 String.valueOf(maxUnits), 176 new String (sourceUnit.getFileName()) 177 })); 178 } 179 if (this.totalUnits < this.parseThreshold) { 181 parsedUnit = this.parser.parse(sourceUnit, unitResult); 182 } else { 183 parsedUnit = this.parser.dietParse(sourceUnit, unitResult); 184 } 185 this.lookupEnvironment.buildTypeBindings(parsedUnit, null ); 187 addCompilationUnit(sourceUnit, parsedUnit); 188 this.requestedSources.put(unitResult.getFileName(), sourceUnit); 189 worked(1); 190 } finally { 191 sourceUnits[i] = null; } 193 } 194 195 this.requestedKeys = new HashtableOfObject(); 197 for (int i = 0; i < keyLength; i++) { 198 BindingKeyResolver resolver = new BindingKeyResolver(bindingKeys[i], this, this.lookupEnvironment); 199 resolver.parse(true); 200 CompilationUnitDeclaration parsedUnit = resolver.hasTypeName() ? resolver.getCompilationUnitDeclaration() : null; 203 if (parsedUnit != null) { 204 char[] fileName = parsedUnit.compilationResult.getFileName(); 205 Object existing = this.requestedKeys.get(fileName); 206 if (existing == null) 207 this.requestedKeys.put(fileName, resolver); 208 else if (existing instanceof ArrayList ) 209 ((ArrayList ) existing).add(resolver); 210 else { 211 ArrayList list = new ArrayList (); 212 list.add(existing); 213 list.add(resolver); 214 this.requestedKeys.put(fileName, list); 215 } 216 217 } else { 218 char[] key = resolver.hasTypeName() 219 ? resolver.getKey().toCharArray() : CharOperation.concatWith(resolver.compoundName(), '.'); this.requestedKeys.put(key, resolver); 222 } 223 worked(1); 224 } 225 226 lookupEnvironment.completeTypeBindings(); 228 } 229 230 IBinding createBinding(String key) { 231 if (this.bindingTables == null) 232 throw new RuntimeException ("Cannot be called outside ASTParser#createASTs(...)"); BindingKeyResolver keyResolver = new BindingKeyResolver(key, this, this.lookupEnvironment); 234 Binding compilerBinding = keyResolver.getCompilerBinding(); 235 if (compilerBinding == null) return null; 236 DefaultBindingResolver resolver = new DefaultBindingResolver(this.lookupEnvironment, null, this.bindingTables, false); 237 return resolver.getBinding(compilerBinding); 238 } 239 240 public static CompilationUnit convert(CompilationUnitDeclaration compilationUnitDeclaration, char[] source, int apiLevel, Map options, boolean needToResolveBindings, WorkingCopyOwner owner, DefaultBindingResolver.BindingTables bindingTables, int flags, IProgressMonitor monitor) { 241 BindingResolver resolver = null; 242 AST ast = AST.newAST(apiLevel); 243 ast.setDefaultNodeFlag(ASTNode.ORIGINAL); 244 CompilationUnit compilationUnit = null; 245 ASTConverter converter = new ASTConverter(options, needToResolveBindings, monitor); 246 if (needToResolveBindings) { 247 resolver = new DefaultBindingResolver(compilationUnitDeclaration.scope, owner, bindingTables, (flags & ICompilationUnit.ENABLE_BINDINGS_RECOVERY) != 0); 248 ast.setFlag(flags | AST.RESOLVED_BINDINGS); 249 } else { 250 resolver = new BindingResolver(); 251 ast.setFlag(flags); 252 } 253 ast.setBindingResolver(resolver); 254 converter.setAST(ast); 255 compilationUnit = converter.convert(compilationUnitDeclaration, source); 256 compilationUnit.setLineEndTable(compilationUnitDeclaration.compilationResult.getLineSeparatorPositions()); 257 ast.setDefaultNodeFlag(0); 258 ast.setOriginalModificationCount(ast.modificationCount()); 259 return compilationUnit; 260 } 261 262 protected static CompilerOptions getCompilerOptions(Map options, boolean statementsRecovery) { 263 CompilerOptions compilerOptions = new CompilerOptions(options); 264 compilerOptions.performMethodsFullRecovery = statementsRecovery; 265 compilerOptions.performStatementsRecovery = statementsRecovery; 266 compilerOptions.parseLiteralExpressionsAsConstants = false; 267 compilerOptions.storeAnnotations = true ; 268 return compilerOptions; 269 } 270 273 protected static IErrorHandlingPolicy getHandlingPolicy() { 274 275 return new IErrorHandlingPolicy() { 277 public boolean stopOnFirstError() { 278 return false; 279 } 280 public boolean proceedOnErrors() { 281 return false; } 283 }; 284 } 285 286 289 protected static ICompilerRequestor getRequestor() { 290 return new ICompilerRequestor() { 291 public void acceptResult(CompilationResult compilationResult) { 292 } 294 }; 295 } 296 297 300 public void initializeParser() { 301 this.parser = new CommentRecorderParser(this.problemReporter, false); 302 } 303 public void process(CompilationUnitDeclaration unit, int i) { 304 char[] fileName = unit.compilationResult.getFileName(); 306 if (!this.requestedKeys.containsKey(fileName) && !this.requestedSources.containsKey(fileName)) 307 super.process(unit, i); 308 } 309 312 protected void handleInternalException( 313 Throwable internalException, 314 CompilationUnitDeclaration unit, 315 CompilationResult result) { 316 super.handleInternalException(internalException, unit, result); 317 if (unit != null) { 318 removeUnresolvedBindings(unit); 319 } 320 } 321 322 325 protected void handleInternalException( 326 AbortCompilation abortException, 327 CompilationUnitDeclaration unit) { 328 super.handleInternalException(abortException, unit); 329 if (unit != null) { 330 removeUnresolvedBindings(unit); 331 } 332 this.hasCompilationAborted = true; 333 } 334 335 public static void parse(ICompilationUnit[] compilationUnits, ASTRequestor astRequestor, int apiLevel, Map options, int flags, IProgressMonitor monitor) { 336 try { 337 CompilerOptions compilerOptions = new CompilerOptions(options); 338 Parser parser = new CommentRecorderParser( 339 new ProblemReporter( 340 DefaultErrorHandlingPolicies.proceedWithAllProblems(), 341 compilerOptions, 342 new DefaultProblemFactory()), 343 false); 344 int length = compilationUnits.length; 345 if (monitor != null) monitor.beginTask("", length); for (int i = 0; i < length; i++) { 347 org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit = (org.eclipse.jdt.internal.compiler.env.ICompilationUnit) compilationUnits[i]; 348 CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, compilerOptions.maxProblemsPerUnit); 349 CompilationUnitDeclaration compilationUnitDeclaration = parser.dietParse(sourceUnit, compilationResult); 350 351 if (compilationUnitDeclaration.ignoreMethodBodies) { 352 compilationUnitDeclaration.ignoreFurtherInvestigation = true; 353 continue; 355 } 356 357 org.eclipse.jdt.internal.compiler.ast.TypeDeclaration[] types = compilationUnitDeclaration.types; 360 if (types != null) { 361 for (int j = types.length; --j >= 0;) 362 types[j].parseMethod(parser, compilationUnitDeclaration); 363 } 364 365 CompilationUnit node = convert(compilationUnitDeclaration, parser.scanner.getSource(), apiLevel, options, false, null, null, flags , monitor); 367 node.setTypeRoot(compilationUnits[i]); 368 369 astRequestor.acceptAST(compilationUnits[i], node); 371 372 if (monitor != null) monitor.worked(1); 373 } 374 } finally { 375 if (monitor != null) monitor.done(); 376 } 377 } 378 379 public static CompilationUnitDeclaration parse( 380 org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit, 381 NodeSearcher nodeSearcher, 382 Map settings, 383 int flags) { 384 if (sourceUnit == null) { 385 throw new IllegalStateException (); 386 } 387 CompilerOptions compilerOptions = new CompilerOptions(settings); 388 boolean statementsRecovery = (flags & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0; 389 compilerOptions.performMethodsFullRecovery = statementsRecovery; 390 compilerOptions.performStatementsRecovery = statementsRecovery; 391 Parser parser = new CommentRecorderParser( 392 new ProblemReporter( 393 DefaultErrorHandlingPolicies.proceedWithAllProblems(), 394 compilerOptions, 395 new DefaultProblemFactory()), 396 false); 397 CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, compilerOptions.maxProblemsPerUnit); 398 CompilationUnitDeclaration compilationUnitDeclaration = parser.dietParse(sourceUnit, compilationResult); 399 400 if (compilationUnitDeclaration.ignoreMethodBodies) { 401 compilationUnitDeclaration.ignoreFurtherInvestigation = true; 402 return null; 404 } 405 406 if (nodeSearcher != null) { 407 char[] source = parser.scanner.getSource(); 408 int searchPosition = nodeSearcher.position; 409 if (searchPosition < 0 || searchPosition > source.length) { 410 return compilationUnitDeclaration; 412 } 413 414 compilationUnitDeclaration.traverse(nodeSearcher, compilationUnitDeclaration.scope); 415 416 org.eclipse.jdt.internal.compiler.ast.ASTNode node = nodeSearcher.found; 417 if (node == null) { 418 return compilationUnitDeclaration; 419 } 420 421 org.eclipse.jdt.internal.compiler.ast.TypeDeclaration enclosingTypeDeclaration = nodeSearcher.enclosingType; 422 423 if (node instanceof AbstractMethodDeclaration) { 424 ((AbstractMethodDeclaration)node).parseStatements(parser, compilationUnitDeclaration); 425 } else if (enclosingTypeDeclaration != null) { 426 if (node instanceof org.eclipse.jdt.internal.compiler.ast.Initializer) { 427 ((org.eclipse.jdt.internal.compiler.ast.Initializer) node).parseStatements(parser, enclosingTypeDeclaration, compilationUnitDeclaration); 428 } else { 429 ((org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)node).parseMethod(parser, compilationUnitDeclaration); 430 } 431 } 432 } else { 433 org.eclipse.jdt.internal.compiler.ast.TypeDeclaration[] types = compilationUnitDeclaration.types; 436 if (types != null) { 437 for (int i = types.length; --i >= 0;) 438 types[i].parseMethod(parser, compilationUnitDeclaration); 439 } 440 } 441 return compilationUnitDeclaration; 442 } 443 444 public static void resolve( 445 ICompilationUnit[] compilationUnits, 446 String [] bindingKeys, 447 ASTRequestor requestor, 448 int apiLevel, 449 Map options, 450 IJavaProject javaProject, 451 WorkingCopyOwner owner, 452 int flags, 453 IProgressMonitor monitor) { 454 455 CancelableNameEnvironment environment = null; 456 CancelableProblemFactory problemFactory = null; 457 try { 458 if (monitor != null) { 459 int amountOfWork = (compilationUnits.length + bindingKeys.length) * 2; monitor.beginTask("", amountOfWork); } 462 environment = new CancelableNameEnvironment(((JavaProject) javaProject), owner, monitor); 463 problemFactory = new CancelableProblemFactory(monitor); 464 CompilationUnitResolver resolver = 465 new CompilationUnitResolver( 466 environment, 467 getHandlingPolicy(), 468 getCompilerOptions(options, (flags & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0), 469 getRequestor(), 470 problemFactory, 471 monitor); 472 473 resolver.resolve(compilationUnits, bindingKeys, requestor, apiLevel, options, owner, flags); 474 if (NameLookup.VERBOSE) { 475 System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInSourcePackage: " + environment.nameLookup.timeSpentInSeekTypesInSourcePackage + "ms"); System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInBinaryPackage: " + environment.nameLookup.timeSpentInSeekTypesInBinaryPackage + "ms"); } 478 } catch (JavaModelException e) { 479 parse(compilationUnits, requestor, apiLevel, options, flags, monitor); 481 } finally { 482 if (monitor != null) monitor.done(); 483 if (environment != null) { 484 environment.monitor = null; } 486 if (problemFactory != null) { 487 problemFactory.monitor = null; } 489 } 490 } 491 public static CompilationUnitDeclaration resolve( 492 org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit, 493 IJavaProject javaProject, 494 NodeSearcher nodeSearcher, 495 Map options, 496 WorkingCopyOwner owner, 497 int flags, 498 IProgressMonitor monitor) throws JavaModelException { 499 500 CompilationUnitDeclaration unit = null; 501 CancelableNameEnvironment environment = null; 502 CancelableProblemFactory problemFactory = null; 503 CompilationUnitResolver resolver = null; 504 try { 505 environment = new CancelableNameEnvironment(((JavaProject)javaProject), owner, monitor); 506 problemFactory = new CancelableProblemFactory(monitor); 507 resolver = 508 new CompilationUnitResolver( 509 environment, 510 getHandlingPolicy(), 511 getCompilerOptions(options, (flags & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0), 512 getRequestor(), 513 problemFactory, 514 monitor); 515 516 unit = 517 resolver.resolve( 518 null, sourceUnit, 520 nodeSearcher, 521 true, true, true); if (resolver.hasCompilationAborted) { 525 CompilationUnitDeclaration unitDeclaration = parse(sourceUnit, nodeSearcher, options, flags); 528 final int problemCount = unit.compilationResult.problemCount; 529 if (problemCount != 0) { 530 unitDeclaration.compilationResult.problems = new CategorizedProblem[problemCount]; 531 System.arraycopy(unit.compilationResult.problems, 0, unitDeclaration.compilationResult.problems, 0, problemCount); 532 unitDeclaration.compilationResult.problemCount = problemCount; 533 } 534 return unitDeclaration; 535 } 536 if (NameLookup.VERBOSE) { 537 System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInSourcePackage: " + environment.nameLookup.timeSpentInSeekTypesInSourcePackage + "ms"); System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInBinaryPackage: " + environment.nameLookup.timeSpentInSeekTypesInBinaryPackage + "ms"); } 540 return unit; 541 } finally { 542 if (environment != null) { 543 environment.monitor = null; } 545 if (problemFactory != null) { 546 problemFactory.monitor = null; } 548 } 558 } 559 public static IBinding[] resolve( 560 final IJavaElement[] elements, 561 int apiLevel, 562 Map compilerOptions, 563 IJavaProject javaProject, 564 WorkingCopyOwner owner, 565 int flags, 566 IProgressMonitor monitor) { 567 568 final int length = elements.length; 569 final HashMap sourceElementPositions = new HashMap (); int cuNumber = 0; 571 final HashtableOfObjectToInt binaryElementPositions = new HashtableOfObjectToInt(); for (int i = 0; i < length; i++) { 573 IJavaElement element = elements[i]; 574 if (!(element instanceof SourceRefElement)) 575 throw new IllegalStateException (element + " is not part of a compilation unit or class file"); Object cu = element.getAncestor(IJavaElement.COMPILATION_UNIT); 577 if (cu != null) { 578 IntArrayList intList = (IntArrayList) sourceElementPositions.get(cu); 580 if (intList == null) { 581 sourceElementPositions.put(cu, intList = new IntArrayList()); 582 cuNumber++; 583 } 584 intList.add(i); 585 } else { 586 try { 588 String key = ((BinaryMember) element).getKey(true); 589 binaryElementPositions.put(key, i); 590 } catch (JavaModelException e) { 591 throw new IllegalArgumentException (element + " does not exist"); } 593 } 594 } 595 ICompilationUnit[] cus = new ICompilationUnit[cuNumber]; 596 sourceElementPositions.keySet().toArray(cus); 597 598 int bindingKeyNumber = binaryElementPositions.size(); 599 String [] bindingKeys = new String [bindingKeyNumber]; 600 binaryElementPositions.keysToArray(bindingKeys); 601 602 class Requestor extends ASTRequestor { 603 IBinding[] bindings = new IBinding[length]; 604 public void acceptAST(ICompilationUnit source, CompilationUnit ast) { 605 IntArrayList intList = (IntArrayList) sourceElementPositions.get(source); 607 for (int i = 0; i < intList.length; i++) { 608 final int index = intList.list[i]; 609 SourceRefElement element = (SourceRefElement) elements[index]; 610 DOMFinder finder = new DOMFinder(ast, element, true); 611 try { 612 finder.search(); 613 } catch (JavaModelException e) { 614 throw new IllegalArgumentException (element + " does not exist"); } 616 this.bindings[index] = finder.foundBinding; 617 } 618 } 619 public void acceptBinding(String bindingKey, IBinding binding) { 620 int index = binaryElementPositions.get(bindingKey); 621 this.bindings[index] = binding; 622 } 623 } 624 Requestor requestor = new Requestor(); 625 resolve(cus, bindingKeys, requestor, apiLevel, compilerOptions, javaProject, owner, flags, monitor); 626 return requestor.bindings; 627 } 628 632 public void removeUnresolvedBindings(CompilationUnitDeclaration compilationUnitDeclaration) { 633 final org.eclipse.jdt.internal.compiler.ast.TypeDeclaration[] types = compilationUnitDeclaration.types; 634 if (types != null) { 635 for (int i = 0, max = types.length; i < max; i++) { 636 removeUnresolvedBindings(types[i]); 637 } 638 } 639 } 640 private void removeUnresolvedBindings(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration type) { 641 final org.eclipse.jdt.internal.compiler.ast.TypeDeclaration[] memberTypes = type.memberTypes; 642 if (memberTypes != null) { 643 for (int i = 0, max = memberTypes.length; i < max; i++){ 644 removeUnresolvedBindings(memberTypes[i]); 645 } 646 } 647 if (type.binding != null && (type.binding.modifiers & ExtraCompilerModifiers.AccUnresolved) != 0) { 648 type.binding = null; 649 } 650 651 final org.eclipse.jdt.internal.compiler.ast.FieldDeclaration[] fields = type.fields; 652 if (fields != null) { 653 for (int i = 0, max = fields.length; i < max; i++){ 654 if (fields[i].binding != null && (fields[i].binding.modifiers & ExtraCompilerModifiers.AccUnresolved) != 0) { 655 fields[i].binding = null; 656 } 657 } 658 } 659 660 final AbstractMethodDeclaration[] methods = type.methods; 661 if (methods != null) { 662 for (int i = 0, max = methods.length; i < max; i++){ 663 if (methods[i].binding != null && (methods[i].binding.modifiers & ExtraCompilerModifiers.AccUnresolved) != 0) { 664 methods[i].binding = null; 665 } 666 } 667 } 668 } 669 670 private void resolve(ICompilationUnit[] compilationUnits, String [] bindingKeys, ASTRequestor astRequestor, int apiLevel, Map compilerOptions, WorkingCopyOwner owner, int flags) { 671 672 astRequestor.compilationUnitResolver = this; 674 this.bindingTables = new DefaultBindingResolver.BindingTables(); 675 CompilationUnitDeclaration unit = null; 676 int i = 0; 677 try { 678 int length = compilationUnits.length; 679 org.eclipse.jdt.internal.compiler.env.ICompilationUnit[] sourceUnits = new org.eclipse.jdt.internal.compiler.env.ICompilationUnit[length]; 680 System.arraycopy(compilationUnits, 0, sourceUnits, 0, length); 681 beginToCompile(sourceUnits, bindingKeys); 682 for (; i < this.totalUnits; i++) { 684 if (this.requestedSources.size() == 0 && this.requestedKeys.size() == 0) { 685 for (; i < this.totalUnits; i++) { 689 this.unitsToProcess[i].cleanUp(); 690 this.unitsToProcess[i] = null; 691 } 692 break; 693 } 694 unit = this.unitsToProcess[i]; 695 try { 696 super.process(unit, i); 698 char[] fileName = unit.compilationResult.getFileName(); 700 ICompilationUnit source = (ICompilationUnit) this.requestedSources.get(fileName); 701 if (source != null) { 702 CompilationResult compilationResult = unit.compilationResult; 704 org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit = compilationResult.compilationUnit; 705 char[] contents = sourceUnit.getContents(); 706 AST ast = AST.newAST(apiLevel); 707 ast.setFlag(flags | AST.RESOLVED_BINDINGS); 708 ast.setDefaultNodeFlag(ASTNode.ORIGINAL); 709 ASTConverter converter = new ASTConverter(compilerOptions, true, this.monitor); 710 BindingResolver resolver = new DefaultBindingResolver(unit.scope, owner, this.bindingTables, (flags & ICompilationUnit.ENABLE_BINDINGS_RECOVERY) != 0); 711 ast.setBindingResolver(resolver); 712 converter.setAST(ast); 713 CompilationUnit compilationUnit = converter.convert(unit, contents); 714 compilationUnit.setTypeRoot(source); 715 compilationUnit.setLineEndTable(compilationResult.getLineSeparatorPositions()); 716 ast.setDefaultNodeFlag(0); 717 ast.setOriginalModificationCount(ast.modificationCount()); 718 719 astRequestor.acceptAST(source, compilationUnit); 721 722 worked(1); 723 } 724 725 Object key = this.requestedKeys.get(fileName); 727 if (key instanceof BindingKeyResolver) { 728 reportBinding(key, astRequestor, owner, unit); 729 worked(1); 730 } else if (key instanceof ArrayList ) { 731 Iterator iterator = ((ArrayList ) key).iterator(); 732 while (iterator.hasNext()) { 733 reportBinding(iterator.next(), astRequestor, owner, unit); 734 worked(1); 735 } 736 } 737 738 this.requestedSources.removeKey(fileName); 740 this.requestedKeys.removeKey(fileName); 741 } finally { 742 unit.cleanUp(); 744 } 745 this.unitsToProcess[i] = null; this.requestor.acceptResult(unit.compilationResult.tagAsAccepted()); 747 } 748 749 DefaultBindingResolver resolver = new DefaultBindingResolver(this.lookupEnvironment, owner, this.bindingTables, (flags & ICompilationUnit.ENABLE_BINDINGS_RECOVERY) != 0); 751 Object [] keys = this.requestedKeys.valueTable; 752 for (int j = 0, keysLength = keys.length; j < keysLength; j++) { 753 BindingKeyResolver keyResolver = (BindingKeyResolver) keys[j]; 754 if (keyResolver == null) continue; 755 Binding compilerBinding = keyResolver.getCompilerBinding(); 756 IBinding binding = compilerBinding == null ? null : resolver.getBinding(compilerBinding); 757 astRequestor.acceptBinding(((BindingKeyResolver) this.requestedKeys.valueTable[j]).getKey(), binding); 759 worked(1); 760 } 761 } catch (OperationCanceledException e) { 762 throw e; 763 } catch (AbortCompilation e) { 764 this.handleInternalException(e, unit); 765 } catch (Error e) { 766 this.handleInternalException(e, unit, null); 767 throw e; } catch (RuntimeException e) { 769 this.handleInternalException(e, unit, null); 770 throw e; } finally { 772 astRequestor.compilationUnitResolver = null; 774 } 775 } 776 777 private void reportBinding(Object key, ASTRequestor astRequestor, WorkingCopyOwner owner, CompilationUnitDeclaration unit) { 778 BindingKeyResolver keyResolver = (BindingKeyResolver) key; 779 Binding compilerBinding = keyResolver.getCompilerBinding(); 780 if (compilerBinding != null) { 781 DefaultBindingResolver resolver = new DefaultBindingResolver(unit.scope, owner, this.bindingTables, false); 782 IBinding binding = resolver.getBinding(compilerBinding); 783 if (binding != null) 784 astRequestor.acceptBinding(keyResolver.getKey(), binding); 785 } 786 } 787 788 private CompilationUnitDeclaration resolve( 789 CompilationUnitDeclaration unit, 790 org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit, 791 NodeSearcher nodeSearcher, 792 boolean verifyMethods, 793 boolean analyzeCode, 794 boolean generateCode) { 795 796 try { 797 798 if (unit == null) { 799 this.parseThreshold = 0; beginToCompile(new org.eclipse.jdt.internal.compiler.env.ICompilationUnit[] { sourceUnit }); 802 unit = this.unitsToProcess[0]; 804 } else { 805 this.lookupEnvironment.buildTypeBindings(unit, null ); 807 808 this.lookupEnvironment.completeTypeBindings(); 810 } 811 812 if (nodeSearcher == null) { 813 this.parser.getMethodBodies(unit); } else { 815 int searchPosition = nodeSearcher.position; 816 char[] source = sourceUnit.getContents(); 817 int length = source.length; 818 if (searchPosition >= 0 && searchPosition <= length) { 819 unit.traverse(nodeSearcher, unit.scope); 820 821 org.eclipse.jdt.internal.compiler.ast.ASTNode node = nodeSearcher.found; 822 823 this.parser.scanner.setSource(source, unit.compilationResult); 824 825 if (node != null) { 826 org.eclipse.jdt.internal.compiler.ast.TypeDeclaration enclosingTypeDeclaration = nodeSearcher.enclosingType; 827 if (node instanceof AbstractMethodDeclaration) { 828 ((AbstractMethodDeclaration)node).parseStatements(this.parser, unit); 829 } else if (enclosingTypeDeclaration != null) { 830 if (node instanceof org.eclipse.jdt.internal.compiler.ast.Initializer) { 831 ((org.eclipse.jdt.internal.compiler.ast.Initializer) node).parseStatements(this.parser, enclosingTypeDeclaration, unit); 832 } else if (node instanceof org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) { 833 ((org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)node).parseMethod(this.parser, unit); 834 } 835 } 836 } 837 } 838 } 839 840 if (unit.scope != null) { 841 unit.scope.faultInTypes(); 843 if (unit.scope != null && verifyMethods) { 844 unit.scope.verifyMethods(this.lookupEnvironment.methodVerifier()); 847 } 848 unit.resolve(); 850 851 if (analyzeCode) unit.analyseCode(); 853 854 if (generateCode) unit.generateCode(); 856 } 857 if (this.unitsToProcess != null) this.unitsToProcess[0] = null; this.requestor.acceptResult(unit.compilationResult.tagAsAccepted()); 859 return unit; 860 } catch (AbortCompilation e) { 861 this.handleInternalException(e, unit); 862 return unit == null ? this.unitsToProcess[0] : unit; 863 } catch (Error e) { 864 this.handleInternalException(e, unit, null); 865 throw e; } catch (RuntimeException e) { 867 this.handleInternalException(e, unit, null); 868 throw e; } finally { 870 } 878 } 879 882 public CompilationUnitDeclaration resolve( 883 org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit, 884 boolean verifyMethods, 885 boolean analyzeCode, 886 boolean generateCode) { 887 888 return resolve( 889 null, 890 sourceUnit, 891 null, 892 verifyMethods, 893 analyzeCode, 894 generateCode); 895 } 896 897 900 public CompilationUnitDeclaration resolve( 901 CompilationUnitDeclaration unit, 902 org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit, 903 boolean verifyMethods, 904 boolean analyzeCode, 905 boolean generateCode) { 906 907 return resolve( 908 unit, 909 sourceUnit, 910 null, 911 verifyMethods, 912 analyzeCode, 913 generateCode); 914 } 915 916 private void worked(int work) { 917 if (this.monitor != null) { 918 if (this.monitor.isCanceled()) 919 throw new OperationCanceledException(); 920 this.monitor.worked(work); 921 } 922 } 923 } 924 | Popular Tags |