/*
   * Ensures that searching takes the primary owner's working copies into account only if the working copy
   * is not saved.
   */
  public void testSearch3() throws CoreException {
    try {
      createFile("/P/Y.java", "");
      this.workingCopy = getCompilationUnit("P/Y.java");
      this.workingCopy.becomeWorkingCopy(null);

      // create type Y in working copy
      this.workingCopy.getBuffer().setContents("public class Y {}");
      this.workingCopy.makeConsistent(null);

      JavaSearchTests.JavaSearchResultCollector resultCollector =
          new JavaSearchTests.JavaSearchResultCollector();
      search(
          "Y",
          IJavaSearchConstants.TYPE,
          IJavaSearchConstants.DECLARATIONS,
          SearchEngine.createWorkspaceScope(),
          resultCollector);
      assertEquals("Y.java Y [Y]", resultCollector.toString());

      //	commit new type
      this.workingCopy.commitWorkingCopy(false, null);
      resultCollector = new JavaSearchTests.JavaSearchResultCollector();
      search(
          "Y",
          IJavaSearchConstants.TYPE,
          IJavaSearchConstants.DECLARATIONS,
          SearchEngine.createWorkspaceScope(),
          resultCollector);
      assertEquals("Y.java Y [Y]", resultCollector.toString());
    } finally {
      deleteFile("/P/Y.java");
    }
  }
  /*
   * Ensures that searching takes the owner's working copies into account.
   */
  public void testSearch2() throws CoreException {
    ICompilationUnit cu = getCompilationUnit("P/X.java");
    TestWorkingCopyOwner owner = new TestWorkingCopyOwner();
    this.workingCopy = cu.getWorkingCopy(owner, null);

    // remove type X
    this.workingCopy.getBuffer().setContents("");
    this.workingCopy.makeConsistent(null);

    SearchPattern pattern =
        SearchPattern.createPattern(
            "X",
            IJavaSearchConstants.TYPE,
            IJavaSearchConstants.DECLARATIONS,
            SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
    JavaSearchTests.JavaSearchResultCollector resultCollector =
        new JavaSearchTests.JavaSearchResultCollector();
    new SearchEngine(owner)
        .search(
            pattern,
            new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
            SearchEngine.createWorkspaceScope(),
            resultCollector,
            null);
    assertEquals(
        "", // should not find any in the owner's context
        resultCollector.toString());
  }
Example #3
0
 private void searchForTypesUsed(
     SearchEngine engine, IJavaElement parent, IType[] types, IJavaSearchScope scope)
     throws CoreException {
   for (int i = 0; i < types.length; i++) {
     if (types[i].isAnonymous()) continue;
     TypeReferenceSearchRequestor requestor = new TypeReferenceSearchRequestor();
     engine.search(
         SearchPattern.createPattern(types[i], IJavaSearchConstants.REFERENCES),
         new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
         scope,
         requestor,
         null);
     if (requestor.containMatches()) {
       TypeDeclarationSearchRequestor decRequestor = new TypeDeclarationSearchRequestor();
       engine.search(
           SearchPattern.createPattern(types[i], IJavaSearchConstants.DECLARATIONS),
           new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
           SearchEngine.createJavaSearchScope(new IJavaElement[] {parent}),
           decRequestor,
           null);
       Match match = decRequestor.getMatch();
       if (match != null) fSearchResult.addMatch(match);
     }
   }
 }
  /*
   * Ensures that searching takes the primary owner's working copies and the given working copies into account.
   * (regression test for bug 43300 SearchEngine(IWorkingCopy[] workingCopies) not backward compatible)
   */
  public void testSearch4() throws CoreException {
    ICompilationUnit primaryWorkingCopy = null;
    try {
      createFolder("P/p");
      createFile("/P/p/Y.java", "");
      primaryWorkingCopy = getCompilationUnit("P/p/Y.java");
      primaryWorkingCopy.becomeWorkingCopy(null);

      // create type Y in working copy
      primaryWorkingCopy.getBuffer().setContents("package p;\n" + "public class Y {\n" + "}");
      primaryWorkingCopy.makeConsistent(null);

      // create new working copy on X.java and add type X
      this.workingCopy = getCompilationUnit("P/p/X.java").getWorkingCopy(null);
      this.workingCopy.getBuffer().setContents("package p;\n" + "public class X {\n" + "}");
      this.workingCopy.makeConsistent(null);

      JavaSearchTests.JavaSearchResultCollector resultCollector =
          new JavaSearchTests.JavaSearchResultCollector();
      IJavaSearchScope scope =
          SearchEngine.createJavaSearchScope(new IJavaElement[] {primaryWorkingCopy.getParent()});
      SearchPattern pattern =
          SearchPattern.createPattern(
              "*",
              IJavaSearchConstants.TYPE,
              IJavaSearchConstants.DECLARATIONS,
              SearchPattern.R_PATTERN_MATCH | SearchPattern.R_CASE_SENSITIVE);
      new SearchEngine(new ICompilationUnit[] {this.workingCopy})
          .search(
              pattern,
              new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
              scope,
              resultCollector,
              null);
      assertEquals("p/X.java p.X [X]\n" + "p/Y.java p.Y [Y]", resultCollector.toString());

    } finally {
      if (primaryWorkingCopy != null) {
        primaryWorkingCopy.discardWorkingCopy();
      }
      deleteFile("/P/p/Y.java");
    }
  }
  /*
   * Ensures that searching takes the owner's working copies into account.
   */
  public void testSearch1() throws CoreException {
    ICompilationUnit cu = getCompilationUnit("P/Y.java");
    TestWorkingCopyOwner owner = new TestWorkingCopyOwner();
    this.workingCopy = cu.getWorkingCopy(owner, null);
    this.workingCopy.getBuffer().setContents("public class Y {\n" + "  X field;\n" + "}");
    this.workingCopy.makeConsistent(null);

    SearchPattern pattern =
        SearchPattern.createPattern(
            "X",
            IJavaSearchConstants.TYPE,
            IJavaSearchConstants.REFERENCES,
            SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
    JavaSearchTests.JavaSearchResultCollector resultCollector =
        new JavaSearchTests.JavaSearchResultCollector();
    new SearchEngine(owner)
        .search(
            pattern,
            new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
            SearchEngine.createWorkspaceScope(),
            resultCollector,
            null);
    assertEquals("Y.java Y.field [X]", resultCollector.toString());
  }
  protected String[] getPathsOfDeclaringType() {
    if (this.typeQualification == null && this.typeSimpleName == null) return null;

    final PathCollector pathCollector = new PathCollector();
    IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
    IndexManager indexManager = JavaModelManager.getIndexManager();
    SearchPattern searchPattern =
        new TypeDeclarationPattern(
            this.typeSimpleName != null
                ? null
                : this.typeQualification, // use the qualification only if no simple name
            null, // do find member types
            this.typeSimpleName,
            IIndexConstants.TYPE_SUFFIX,
            this.pattern.getMatchRule());
    IndexQueryRequestor searchRequestor =
        new IndexQueryRequestor() {
          public boolean acceptIndexMatch(
              String documentPath,
              SearchPattern indexRecord,
              SearchParticipant participant,
              AccessRuleSet access) {
            TypeDeclarationPattern record = (TypeDeclarationPattern) indexRecord;
            if (record.enclosingTypeNames
                != IIndexConstants.ONE_ZERO_CHAR) { // filter out local and anonymous classes
              pathCollector.acceptIndexMatch(documentPath, indexRecord, participant, access);
            }
            return true;
          }
        };

    indexManager.performConcurrentJob(
        new PatternSearchJob(searchPattern, new JavaSearchParticipant(), scope, searchRequestor),
        IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH,
        progressMonitor == null ? null : new SubProgressMonitor(progressMonitor, 100));
    return pathCollector.getPaths();
  }