/** Returns the matching nodes that are in the given range in the source order. */
  protected ASTNode[] matchingNodes(int start, int end) {
    ArrayList nodes = null;
    Object[] keyTable = this.matchingNodes.keyTable;
    for (int i = 0, l = keyTable.length; i < l; i++) {
      ASTNode node = (ASTNode) keyTable[i];
      if (node != null && start <= node.sourceStart && node.sourceEnd <= end) {
        if (nodes == null) nodes = new ArrayList();
        nodes.add(node);
      }
    }
    if (nodes == null) return null;

    ASTNode[] result = new ASTNode[nodes.size()];
    nodes.toArray(result);

    // sort nodes by source starts
    Util.Comparer comparer =
        new Util.Comparer() {
          public int compare(Object o1, Object o2) {
            return ((ASTNode) o1).sourceStart - ((ASTNode) o2).sourceStart;
          }
        };
    Util.sort(result, comparer);
    return result;
  }
Esempio n. 2
0
 public String toString() {
   StringBuffer result = new StringBuffer("JavaSearchScope on "); // $NON-NLS-1$
   if (this.elements != null) {
     result.append("["); // $NON-NLS-1$
     for (int i = 0, length = this.elements.size(); i < length; i++) {
       JavaElement element = (JavaElement) this.elements.get(i);
       result.append("\n\t"); // $NON-NLS-1$
       result.append(element.toStringWithAncestors());
     }
     result.append("\n]"); // $NON-NLS-1$
   } else {
     if (this.pathsCount == 0) {
       result.append("[empty scope]"); // $NON-NLS-1$
     } else {
       result.append("["); // $NON-NLS-1$
       String[] paths = new String[this.relativePaths.length];
       int index = 0;
       for (int i = 0; i < this.relativePaths.length; i++) {
         String path = this.relativePaths[i];
         if (path == null) continue;
         String containerPath;
         if (ExternalFoldersManager.isInternalPathForExternalFolder(
             new Path(this.containerPaths[i]))) {
           Object target = JavaModel.getWorkspaceTarget(new Path(this.containerPaths[i]));
           containerPath = ((IFolder) target).getLocation().toOSString();
         } else {
           containerPath = this.containerPaths[i];
         }
         if (path.length() > 0) {
           paths[index++] = containerPath + '/' + path;
         } else {
           paths[index++] = containerPath;
         }
       }
       System.arraycopy(paths, 0, paths = new String[index], 0, index);
       Util.sort(paths);
       for (int i = 0; i < index; i++) {
         result.append("\n\t"); // $NON-NLS-1$
         result.append(paths[i]);
       }
       result.append("\n]"); // $NON-NLS-1$
     }
   }
   return result.toString();
 }
  /*
   * Update projects references so that the build order is consistent with the classpath
   */
  public void updateProjectReferencesIfNecessary() throws JavaModelException {

    String[] oldRequired =
        this.oldResolvedClasspath == null
            ? CharOperation.NO_STRINGS
            : this.project.projectPrerequisites(this.oldResolvedClasspath);
    IClasspathEntry[] newResolvedClasspath = this.project.getResolvedClasspath();
    String[] newRequired = this.project.projectPrerequisites(newResolvedClasspath);
    final IProject projectResource = this.project.getProject();

    try {
      IProject[] projectReferences = projectResource.getDescription().getDynamicReferences();

      HashSet oldReferences = new HashSet(projectReferences.length);
      for (int i = 0; i < projectReferences.length; i++) {
        String projectName = projectReferences[i].getName();
        oldReferences.add(projectName);
      }
      HashSet newReferences = (HashSet) oldReferences.clone();

      for (int i = 0; i < oldRequired.length; i++) {
        String projectName = oldRequired[i];
        newReferences.remove(projectName);
      }
      for (int i = 0; i < newRequired.length; i++) {
        String projectName = newRequired[i];
        newReferences.add(projectName);
      }

      Iterator iter;
      int newSize = newReferences.size();

      checkIdentity:
      {
        if (oldReferences.size() == newSize) {
          iter = newReferences.iterator();
          while (iter.hasNext()) {
            if (!oldReferences.contains(iter.next())) {
              break checkIdentity;
            }
          }
          return;
        }
      }
      String[] requiredProjectNames = new String[newSize];
      int index = 0;
      iter = newReferences.iterator();
      while (iter.hasNext()) {
        requiredProjectNames[index++] = (String) iter.next();
      }
      Util.sort(requiredProjectNames); // ensure that if changed, the order is consistent

      final IProject[] requiredProjectArray = new IProject[newSize];
      IWorkspaceRoot wksRoot = projectResource.getWorkspace().getRoot();
      for (int i = 0; i < newSize; i++) {
        requiredProjectArray[i] = wksRoot.getProject(requiredProjectNames[i]);
      }

      // ensure that a scheduling rule is used so that the project description is not modified by
      // another thread while we update it
      // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=214981
      // also ensure that if no change (checkIdentify block returned above) we don't reach here
      // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=241751
      IWorkspace workspace = projectResource.getWorkspace();
      ISchedulingRule rule =
          workspace
              .getRuleFactory()
              .modifyRule(projectResource); // scheduling rule for modifying the project
      IWorkspaceRunnable runnable =
          new IWorkspaceRunnable() {
            public void run(IProgressMonitor monitor) throws CoreException {
              IProjectDescription description = projectResource.getDescription();
              description.setDynamicReferences(requiredProjectArray);
              projectResource.setDescription(description, IResource.AVOID_NATURE_CONFIG, null);
            }
          };
      workspace.run(runnable, rule, IWorkspace.AVOID_UPDATE, null);
    } catch (CoreException e) {
      if (!ExternalJavaProject.EXTERNAL_PROJECT_NAME.equals(this.project.getElementName()))
        throw new JavaModelException(e);
    }
  }
  public char[][][] collect() throws JavaModelException {
    if (this.type != null) {
      // Collect the paths of the cus that are in the hierarchy of the given type
      this.result = new char[1][][];
      this.resultIndex = 0;
      JavaProject javaProject = (JavaProject) this.type.getJavaProject();
      this.locator.initialize(javaProject, 0);
      try {
        if (this.type.isBinary()) {
          BinaryTypeBinding binding = this.locator.cacheBinaryType(this.type, null);
          if (binding != null) collectSuperTypeNames(binding);
        } else {
          ICompilationUnit unit = this.type.getCompilationUnit();
          SourceType sourceType = (SourceType) this.type;
          boolean isTopLevelOrMember = sourceType.getOuterMostLocalContext() == null;
          CompilationUnitDeclaration parsedUnit = buildBindings(unit, isTopLevelOrMember);
          if (parsedUnit != null) {
            TypeDeclaration typeDecl = new ASTNodeFinder(parsedUnit).findType(this.type);
            if (typeDecl != null && typeDecl.binding != null)
              collectSuperTypeNames(typeDecl.binding);
          }
        }
      } catch (AbortCompilation e) {
        // problem with classpath: report inacurrate matches
        return null;
      }
      if (this.result.length > this.resultIndex)
        System.arraycopy(
            this.result, 0, this.result = new char[this.resultIndex][][], 0, this.resultIndex);
      return this.result;
    }

    // Collect the paths of the cus that declare a type which matches declaringQualification +
    // declaringSimpleName
    String[] paths = this.getPathsOfDeclaringType();
    if (paths == null) return null;

    // Create bindings from source types and binary types and collect super type names of the type
    // declaration
    // that match the given declaring type
    Util.sort(paths); // sort by projects
    JavaProject previousProject = null;
    this.result = new char[1][][];
    this.resultIndex = 0;
    for (int i = 0, length = paths.length; i < length; i++) {
      try {
        Openable openable = this.locator.handleFactory.createOpenable(paths[i], this.locator.scope);
        if (openable == null) continue; // outside classpath

        IJavaProject project = openable.getJavaProject();
        if (!project.equals(previousProject)) {
          previousProject = (JavaProject) project;
          this.locator.initialize(previousProject, 0);
        }
        if (openable instanceof ICompilationUnit) {
          ICompilationUnit unit = (ICompilationUnit) openable;
          CompilationUnitDeclaration parsedUnit =
              buildBindings(
                  unit, true /*only toplevel and member types are visible to the focus type*/);
          if (parsedUnit != null)
            parsedUnit.traverse(new TypeDeclarationVisitor(), parsedUnit.scope);
        } else if (openable instanceof IClassFile) {
          IClassFile classFile = (IClassFile) openable;
          BinaryTypeBinding binding = this.locator.cacheBinaryType(classFile.getType(), null);
          if (matches(binding)) collectSuperTypeNames(binding);
        }
      } catch (AbortCompilation e) {
        // ignore: continue with next element
      } catch (JavaModelException e) {
        // ignore: continue with next element
      }
    }
    if (this.result.length > this.resultIndex)
      System.arraycopy(
          this.result, 0, this.result = new char[this.resultIndex][][], 0, this.resultIndex);
    return this.result;
  }