|
|
Re: Get IJavaElement with package and class name [message #903466 is a reply to message #901548] |
Thu, 23 August 2012 13:53  |
Eclipse User |
|
|
|
You can use JDT's search engine. Here is an example:
IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
SearchEngine engine = new SearchEngine();
SearchPattern pattern = SearchPattern.createPattern("some.Clazz",
IJavaSearchConstants.TYPE, IJavaSearchConstants.DECLARATIONS,
SearchPattern.R_EXACT_MATCH);
SearchRequestor requestor = new SearchRequestor() {
public void acceptSearchMatch(final SearchMatch match) throws CoreException {
TypeDeclarationMatch typeMatch = (TypeDeclarationMatch) match;
IJavaElement type = (IJavaElement) typeMatch.getElement();
IFile file = (IFile) match.getResource();
// do something
}
};
try {
engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, requestor, new NullProgressMonitor());
} catch (final CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
You only have to replace the string some.Clazz with your package and class name. But keep in mind that there may be more than one match if the class is defined in multiple projects (since workspace scope). If you know the project in advance, you can narrow the search scope. And hopefully only get one match.
|
|
|
Powered by
FUDForum. Page generated in 0.08646 seconds