How to use IJavaElement
I am developing a search plugin where i am using IPackageFragment to specify the package in which th开发者_C百科e search is to be carried out . I am not able to use the IPackageFragment to create IJavaSearchScope . Please help me by providing an explanation with suitable example .
Thanks
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(); }
精彩评论