You have a fine example in this article Find unused methods:
The IPackageFragment
is used to get any Java element you want (like methods)
public static List<MethodInformation> calculate(IJavaProject project) {
List<MethodInformation> list = new ArrayList<MethodInformation>();
try {
if (project.isOpen()) {
IPackageFragment[] packages = project.getPackageFragments();
// parse(JavaCore.create(project));
for (IPackageFragment mypackage : packages) {
if (mypackage.getKind() == IPackageFragmentRoot.K_SOURCE) {
for (ICompilationUnit unit : mypackage.getCompilationUnits()) {
IType[] types = unit.getTypes();
for (int i = 0; i < types.length; i++) {
IType type = types[i];
IMethod[] methods = type.getMethods();
And the IJavaSearchScope
is created from the SearchEngine
, and used with elements found through the IPackageFragment
elements.
private static int performIMethodSearch(IMethod method) throws CoreException {
SearchPattern pattern = SearchPattern.createPattern(method, IJavaSearchConstants.REFERENCES);
IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
MySearchRequestor requestor = new MySearchRequestor();
SearchEngine searchEngine = new SearchEngine();
searchEngine.search( pattern,
new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() },
scope, requestor, null);
return requestor.getNumberOfCalls();
}