Created
October 8, 2020 10:06
-
-
Save nbhusare/137199b9ca7ae686e80caf2a8fc295fc to your computer and use it in GitHub Desktop.
Finding References of a given field in the workspace
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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