예제 #1
0
  public void findIndexMatches(
      Index index,
      IndexQueryRequestor requestor,
      SearchParticipant participant,
      IJavaSearchScope scope,
      IProgressMonitor progressMonitor)
      throws IOException {
    if (progressMonitor != null && progressMonitor.isCanceled())
      throw new OperationCanceledException();

    resetQuery();
    SimpleSet intersectedNames = null;
    try {
      index.startQuery();
      do {
        SearchPattern pattern = currentPattern();
        EntryResult[] entries = pattern.queryIn(index);
        if (entries == null) return;

        SearchPattern decodedResult = pattern.getBlankPattern();
        SimpleSet newIntersectedNames = new SimpleSet(3);
        for (int i = 0, l = entries.length; i < l; i++) {
          if (progressMonitor != null && progressMonitor.isCanceled())
            throw new OperationCanceledException();

          EntryResult entry = entries[i];
          decodedResult.decodeIndexKey(entry.getWord());
          if (pattern.matchesDecodedKey(decodedResult)) {
            String[] names = entry.getDocumentNames(index);
            if (intersectedNames != null) {
              for (int j = 0, n = names.length; j < n; j++)
                if (intersectedNames.includes(names[j])) newIntersectedNames.add(names[j]);
            } else {
              for (int j = 0, n = names.length; j < n; j++) newIntersectedNames.add(names[j]);
            }
          }
        }

        if (newIntersectedNames.elementSize == 0) return;
        intersectedNames = newIntersectedNames;
      } while (hasNextQuery());
    } finally {
      index.stopQuery();
    }

    String containerPath = index.containerPath;
    char separator = index.separator;
    Object[] names = intersectedNames.values;
    for (int i = 0, l = names.length; i < l; i++)
      if (names[i] != null)
        acceptMatch(
            (String) names[i],
            containerPath,
            separator,
            null /*no pattern*/,
            requestor,
            participant,
            scope); // AndPatterns cannot provide the decoded result
  }
예제 #2
0
 /*
  * Create the list of focused jars or projects.
  */
 private static IJavaElement[] getFocusedElementsAndTypes(
     SearchPattern pattern, IJavaElement focusElement, ObjectVector superTypes)
     throws JavaModelException {
   if (pattern instanceof MethodPattern) {
     // For method pattern, it needs to walk along the focus type super hierarchy
     // and add jars/projects of all the encountered types.
     IType type = (IType) pattern.focus.getAncestor(IJavaElement.TYPE);
     MethodPattern methodPattern = (MethodPattern) pattern;
     String selector = new String(methodPattern.selector);
     int parameterCount = methodPattern.parameterCount;
     ITypeHierarchy superHierarchy = type.newSupertypeHierarchy(null);
     IType[] allTypes = superHierarchy.getAllSupertypes(type);
     int length = allTypes.length;
     SimpleSet focusSet = new SimpleSet(length + 1);
     if (focusElement != null) focusSet.add(focusElement);
     for (int i = 0; i < length; i++) {
       IMethod[] methods = allTypes[i].getMethods();
       int mLength = methods.length;
       for (int m = 0; m < mLength; m++) {
         if (parameterCount == methods[m].getNumberOfParameters()
             && methods[m].getElementName().equals(selector)) {
           IPackageFragmentRoot root =
               (IPackageFragmentRoot) allTypes[i].getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
           IJavaElement element = root.isArchive() ? root : root.getParent();
           focusSet.add(element);
           if (superTypes != null) superTypes.add(allTypes[i]);
           break;
         }
       }
     }
     // Rebuilt a contiguous array
     IJavaElement[] focuses = new IJavaElement[focusSet.elementSize];
     Object[] values = focusSet.values;
     int count = 0;
     for (int i = values.length; --i >= 0; ) {
       if (values[i] != null) {
         focuses[count++] = (IJavaElement) values[i];
       }
     }
     return focuses;
   }
   if (focusElement == null) return new IJavaElement[0];
   return new IJavaElement[] {focusElement};
 }
  protected void reportDeclaration(
      MethodBinding methodBinding, MatchLocator locator, SimpleSet knownMethods)
      throws CoreException {
    ReferenceBinding declaringClass = methodBinding.declaringClass;
    IType type = locator.lookupType(declaringClass);
    if (type == null) return; // case of a secondary type

    // Report match for binary
    if (type.isBinary()) {
      IMethod method = null;
      TypeBinding[] parameters = methodBinding.original().parameters;
      int parameterLength = parameters.length;
      char[][] parameterTypes = new char[parameterLength][];
      for (int i = 0; i < parameterLength; i++) {
        char[] typeName = parameters[i].qualifiedSourceName();
        for (int j = 0, dim = parameters[i].dimensions(); j < dim; j++) {
          typeName = CharOperation.concat(typeName, new char[] {'[', ']'});
        }
        parameterTypes[i] = typeName;
      }
      method = locator.createBinaryMethodHandle(type, methodBinding.selector, parameterTypes);
      if (method == null || knownMethods.addIfNotIncluded(method) == null) return;

      IResource resource = type.getResource();
      if (resource == null) resource = type.getJavaProject().getProject();
      IBinaryType info =
          locator.getBinaryInfo(
              (org.eclipse.jdt.internal.core.ClassFile) type.getClassFile(), resource);
      locator.reportBinaryMemberDeclaration(
          resource, method, methodBinding, info, SearchMatch.A_ACCURATE);
      return;
    }

    // When source is available, report match if method is found in the declaring type
    IResource resource = type.getResource();
    if (declaringClass instanceof ParameterizedTypeBinding)
      declaringClass = ((ParameterizedTypeBinding) declaringClass).genericType();
    ClassScope scope = ((SourceTypeBinding) declaringClass).scope;
    if (scope != null) {
      TypeDeclaration typeDecl = scope.referenceContext;
      AbstractMethodDeclaration methodDecl = typeDecl.declarationOf(methodBinding.original());
      if (methodDecl != null) {
        // Create method handle from method declaration
        String methodName = new String(methodBinding.selector);
        Argument[] arguments = methodDecl.arguments;
        int length = arguments == null ? 0 : arguments.length;
        String[] parameterTypes = new String[length];
        for (int i = 0; i < length; i++) {
          char[][] typeName = arguments[i].type.getParameterizedTypeName();
          parameterTypes[i] =
              Signature.createTypeSignature(CharOperation.concatWith(typeName, '.'), false);
        }
        IMethod method = type.getMethod(methodName, parameterTypes);
        if (method == null || knownMethods.addIfNotIncluded(method) == null) return;

        // Create and report corresponding match
        int offset = methodDecl.sourceStart;
        this.match =
            new MethodDeclarationMatch(
                method,
                SearchMatch.A_ACCURATE,
                offset,
                methodDecl.sourceEnd - offset + 1,
                locator.getParticipant(),
                resource);
        locator.report(this.match);
      }
    }
  }
예제 #4
0
  /*
   *  Compute the list of paths which are keying index files.
   */
  private void initializeIndexLocations() {
    IPath[] projectsAndJars = this.searchScope.enclosingProjectsAndJars();
    IndexManager manager = JavaModelManager.getIndexManager();
    SimpleSet locations = new SimpleSet();
    IJavaElement focus = MatchLocator.projectOrJarFocus(this.pattern);
    if (focus == null) {
      for (int i = 0; i < projectsAndJars.length; i++) {
        IPath path = projectsAndJars[i];
        Object target = JavaModel.getTarget(path, false /*don't check existence*/);
        if (target instanceof IFolder) // case of an external folder
        path = ((IFolder) target).getFullPath();
        locations.add(manager.computeIndexLocation(path));
      }
    } else {
      try {
        // See whether the state builder might be used to reduce the number of index locations

        // find the projects from projectsAndJars that see the focus then walk those projects
        // looking for the jars from projectsAndJars
        int length = projectsAndJars.length;
        JavaProject[] projectsCanSeeFocus = new JavaProject[length];
        SimpleSet visitedProjects = new SimpleSet(length);
        int projectIndex = 0;
        SimpleSet externalLibsToCheck = new SimpleSet(length);
        ObjectVector superTypes = new ObjectVector();
        IJavaElement[] focuses = getFocusedElementsAndTypes(this.pattern, focus, superTypes);
        char[][][] focusQualifiedNames = null;
        boolean isAutoBuilding = ResourcesPlugin.getWorkspace().getDescription().isAutoBuilding();
        if (isAutoBuilding && focus instanceof IJavaProject) {
          focusQualifiedNames = getQualifiedNames(superTypes);
        }
        IJavaModel model = JavaModelManager.getJavaModelManager().getJavaModel();
        for (int i = 0; i < length; i++) {
          IPath path = projectsAndJars[i];
          JavaProject project = (JavaProject) getJavaProject(path, model);
          if (project != null) {
            visitedProjects.add(project);
            if (canSeeFocus(focuses, project, focusQualifiedNames)) {
              locations.add(manager.computeIndexLocation(path));
              projectsCanSeeFocus[projectIndex++] = project;
            }
          } else {
            externalLibsToCheck.add(path);
          }
        }
        for (int i = 0; i < projectIndex && externalLibsToCheck.elementSize > 0; i++) {
          IClasspathEntry[] entries = projectsCanSeeFocus[i].getResolvedClasspath();
          for (int j = entries.length; --j >= 0; ) {
            IClasspathEntry entry = entries[j];
            if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
              IPath path = entry.getPath();
              if (externalLibsToCheck.remove(path) != null) {
                Object target = JavaModel.getTarget(path, false /*don't check existence*/);
                if (target instanceof IFolder) // case of an external folder
                path = ((IFolder) target).getFullPath();
                locations.add(manager.computeIndexLocation(path));
              }
            }
          }
        }
        // jar files can be included in the search scope without including one of the projects that
        // references them, so scan all projects that have not been visited
        if (externalLibsToCheck.elementSize > 0) {
          IJavaProject[] allProjects = model.getJavaProjects();
          for (int i = 0, l = allProjects.length;
              i < l && externalLibsToCheck.elementSize > 0;
              i++) {
            JavaProject project = (JavaProject) allProjects[i];
            if (!visitedProjects.includes(project)) {
              IClasspathEntry[] entries = project.getResolvedClasspath();
              for (int j = entries.length; --j >= 0; ) {
                IClasspathEntry entry = entries[j];
                if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                  IPath path = entry.getPath();
                  if (externalLibsToCheck.remove(path) != null) {
                    Object target = JavaModel.getTarget(path, false /*don't check existence*/);
                    if (target instanceof IFolder) // case of an external folder
                    path = ((IFolder) target).getFullPath();
                    locations.add(manager.computeIndexLocation(path));
                  }
                }
              }
            }
          }
        }
      } catch (JavaModelException e) {
        // ignored
      }
    }

    this.indexLocations = new IPath[locations.elementSize];
    Object[] values = locations.values;
    int count = 0;
    for (int i = values.length; --i >= 0; )
      if (values[i] != null) this.indexLocations[count++] = (IPath) values[i];
  }