Eclipse JDT tutorial – Java Search Engine – A Simple Working Example

This article belongs to Eclipse JDT Tutorial Series.

JDT Java Search Engine provides a function to quick search Java projects in the workspace for Java elements, such as method references, field declarations, implementors of an interface, etc.

There are mainly 4 steps involved with a search:

  1. Create a search pattern
  2. Create a search scope
  3. Define result collector
  4. Start Search

The example below follows those steps. There are more options for each of those steps, see the reference to find out more flexibilities.

public Object execute(ExecutionEvent event) throws ExecutionException {
 
	// step 1: Create a search pattern
	// search methods having "abcde" as name
	SearchPattern pattern = SearchPattern.createPattern("abcde",
			IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS,
			SearchPattern.R_EXACT_MATCH);
 
	// step 2: Create search scope
	// IJavaSearchScope scope = SearchEngine.createJavaSearchScope(packages);
	IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
 
	// step3: define a result collector
	SearchRequestor requestor = new SearchRequestor() {
		public void acceptSearchMatch(SearchMatch match) {
			System.out.println(match.getElement());
		}
	};
 
	// step4: start searching
	SearchEngine searchEngine = new SearchEngine();
	try {
		searchEngine.search(pattern, new SearchParticipant[] { SearchEngine
						.getDefaultSearchParticipant() }, scope, requestor,
						null);
	} catch (CoreException e) {
		e.printStackTrace();
	}
 
	return null;
}

Result:

abcde() (not open) [in test [in test.java [in [in src [in test]]]]]

You can create a search scope by using method createJavaSearchScope, then you need to get the necessary parameters.

public Object execute2(ExecutionEvent event) throws ExecutionException {
 
	IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
	IProject[] projects = root.getProjects();
 
	for (IProject project : projects) {
		IJavaProject javaProject = JavaCore.create(project);
		IPackageFragment[] packages = null;
		try {
			packages = javaProject.getPackageFragments();
		} catch (JavaModelException e) {
			e.printStackTrace();
		}
 
		// step 1: Create a search pattern
		SearchPattern pattern = SearchPattern.createPattern("abcde",
				IJavaSearchConstants.METHOD,
				IJavaSearchConstants.DECLARATIONS,
				SearchPattern.R_EXACT_MATCH);
 
		// step 2: Create search scope
		 IJavaSearchScope scope = SearchEngine.createJavaSearchScope(packages);
 
		// step3: define a result collector
		SearchRequestor requestor = new SearchRequestor() {
			public void acceptSearchMatch(SearchMatch match) {
				System.out.println(match.getElement());
			}
		};
 
		// step4: start searching
		SearchEngine searchEngine = new SearchEngine();
		try {
			searchEngine.search(pattern,
					new SearchParticipant[] { SearchEngine
							.getDefaultSearchParticipant() }, scope,
					requestor, null);
		} catch (CoreException e) {
			System.out.println("exception");
			e.printStackTrace();
		}
 
	}
 
	return null;
}

References:

1. Doc for Java search engine
2. Tutorial from EclipseCon

Leave a Comment