Skip to content

Instantly share code, notes, and snippets.

@nbhusare
Created October 8, 2020 10:06
Show Gist options
  • Save nbhusare/137199b9ca7ae686e80caf2a8fc295fc to your computer and use it in GitHub Desktop.
Save nbhusare/137199b9ca7ae686e80caf2a8fc295fc to your computer and use it in GitHub Desktop.
Finding References of a given field in the workspace
List<QuerySpecification> queries = new ArrayList<>();
ICompilationUnit compilationUnit = (ICompilationUnit) ((IStructuredSelection) currentSelection).getFirstElement();
for (IJavaElement javaElement : compilationUnit.getChildren()) {
if (javaElement instanceof SourceType) {
IField[] fields = ((SourceType) javaElement).getFields();
for (IJavaElement field : fields) {
queries.add(createQuery(field));
}
}
}
JavaSearchQuery javaSearchQuery = new JavaSearchQuery(queries);
javaSearchQuery.run(new NullProgressMonitor());
JavaSearchResult searchResult = (JavaSearchResult) javaSearchQuery.getSearchResult();
private ElementQuerySpecification createQuery(IJavaElement element) {
JavaSearchScopeFactory factory = JavaSearchScopeFactory.getInstance();
IJavaSearchScope scope = factory.createWorkspaceScope(true);
String description = factory.getWorkspaceScopeDescription(true);
return new ElementQuerySpecification(element, IJavaSearchConstants.REFERENCES, scope, description);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment