How to get all the references of static field with JDT

后端 未结 2 874
礼貌的吻别
礼貌的吻别 2021-01-14 20:33

I found Java: Find all callers of a method – get all methods that call a particular method that gives a hint on how to find all the callers of a specific method.

The

相关标签:
2条回答
  • 2021-01-14 21:01

    You need to use JDT Core SearchEngine API for finding (references of) java elements.

    0 讨论(0)
  • 2021-01-14 21:03

    The example is base on the org.eclipse.jdt.internal.* classes, I think you don't need to take so much effort to do this, since JDT SearchEngine API is full-featured. In your case, the following code will be enough:

        IWorkspace workspace = ResourcesPlugin.getWorkspace();
        IWorkspaceRoot root = workspace.getRoot();
        IProject plainProject = root.getProject("some project");
        IJavaProject javaProject = JavaCore.create(plainProject);
        try {
            IType type = javaProject.findType("foo.bar.AnotherClass");
            IField field = type.getField("z");
            //IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
            IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { plainProject });
            SearchPattern searchParttern = SearchPattern.createPattern(field,
                    IJavaSearchConstants.REFERENCES);
            SearchRequestor requestor = new SearchRequestor() {
                @Override
                public void acceptSearchMatch(SearchMatch match) {
                    System.out.println(match.getElement());
                }
            };
            SearchEngine searchEngine = new SearchEngine();
            searchEngine.search(searchParttern,
                    new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope,
                    requestor, new NullProgressMonitor());
        } catch (Exception e) {
            // some exception handling you need to do
        }
    
    0 讨论(0)
提交回复
热议问题