title: Using Search Engine to implement call hierarchy (getting all the methods that invoke a certain method) in Java/eclipse
body: <p>I have a Java Project named &quot;javaProject&quot; which has two classes Hello and ReferenceHello.</p>
  <p><img src="https://i.stack.imgur.com/tzsMX.png" alt="enter image description here" /></p>
  <p>Hello.java</p>
  <pre><code>package com.prosseek.test;
  
  public class Hello {
      public void world()
      {
          System.out.println(&quot;Hello world&quot;);
      }
      
      public void referWorld()
      {
          world();
      }
  }
  </code></pre>
  <p>ReferenceHello.java</p>
  <pre><code>package com.prosseek.test;
  
  public class ReferenceHello {
      public void referWorld()
      {
          Hello h = new Hello();
          h.world();
      }
  }
  </code></pre>
  <p>I can find all the methods that references (or invokes) Hello.world() using Search/Java dialog box.</p>
  <p><img src="https://i.stack.imgur.com/o84tg.png" alt="enter image description here" /></p>
  <p>I'd like to get the same result programmatically using <code>Search Engine</code>. This is the code that I came up with, however it returns nothing.</p>
  <pre><code>public void testIt() throws CoreException {
      String projectName = &quot;javaProject&quot;;
      IJavaProject javaProject = JavaProjectHelper.createJavaProject(projectName);
      
      String targetMethodName = &quot;world&quot;;
      SearchPattern pattern = SearchPattern.createPattern(
              targetMethodName, 
              IJavaSearchConstants.METHOD, 
              IJavaSearchConstants.REFERENCES,
              SearchPattern.R_CASE_SENSITIVE // &lt;--- ????
              );
      
      boolean includeReferencedProjects = false;
      IJavaElement[] javaProjects = new IJavaElement[] {javaProject};
      IJavaSearchScope scope = SearchEngine.createJavaSearchScope(javaProjects, includeReferencedProjects); // &lt;--- ????
  
      SearchRequestor requestor = new SearchRequestor() {
          @Override
          public void acceptSearchMatch(SearchMatch match)
                  throws CoreException {
              System.out.println(match.getElement());
          }
      };
      SearchEngine searchEngine = new SearchEngine();
      searchEngine.search(
              pattern, 
              new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant()}, 
              scope, 
              requestor, 
              null);      
  }
  </code></pre>
  <ul>
  <li>What might be wrong in my search code?</li>
  <li>Is there other way to get the call hierarchy in a eclipse plugin programmatically?</li>
  </ul>
  <h2>ADDED</h2>
  <p>The search code works fine, the issue was with my wrong use of <a href="http://alvinalexander.com/java/jwarehouse/eclipse/org.eclipse.jdt.debug.tests/test-plugin/org/eclipse/jdt/debug/testplugin/JavaProjectHelper.java.shtml" rel="nofollow noreferrer">JavaProjectHelper.createJavaProject</a>. I should have opened the existing java project, not creating one with the same name. As a result, .metadata was broken and nothing was searched.</p>
  <p>With my new getJavaProject method, everything works fine now.</p>
  <pre><code>private IJavaProject getJavaProject(String projectName) throws CoreException
  {
      IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
      IProject project= root.getProject(projectName);
      if (!project.exists()) {
          project.create(null);
      } else {
          project.refreshLocal(IResource.DEPTH_INFINITE, null);
      }
      
      if (!project.isOpen()) {
          project.open(null);
      }
      
      IJavaProject jproject= JavaCore.create(project);
      
      return jproject;    
  }
  </code></pre>