Skip to content

Instantly share code, notes, and snippets.

@rylan
Created August 6, 2010 16:28
Show Gist options
  • Save rylan/511576 to your computer and use it in GitHub Desktop.
Save rylan/511576 to your computer and use it in GitHub Desktop.
Fully qualified name to ICompilationUnit
/* Uses the fully qualified type name to search the Eclipse Workspace
* to find the type's ICompilationUnit
*/
public class FindICompilationUnit {
public ICompilationUnit unitLookup(String name){
ICompilationUnit unit = null;
SearchPattern sp = SearchPattern.createPattern(name, IJavaSearchConstants.TYPE, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_FULL_MATCH);
SearchEngine se = new SearchEngine();
UnitRequestor ur = new UnitRequestor();
SearchParticipant[] part = {SearchEngine.getDefaultSearchParticipant()};
try {
se.search(sp, part, SearchEngine.createWorkspaceScope(), ur, new NullProgressMonitor());
} catch (CoreException e) {
e.printStackTrace();
}
if(ur.getIJavaElement() != null)
return (ICompilationUnit) ur.getIJavaElement().getAncestor(IJavaElement.COMPILATION_UNIT);
else
return null;
}
class UnitRequestor extends SearchRequestor {
IJavaElement ije = null;
public void acceptSearchMatch(SearchMatch match) throws CoreException {
if (match instanceof TypeDeclarationMatch) {
if(match.getElement() instanceof IJavaElement)
ije = (IJavaElement)((TypeDeclarationMatch)match).getElement();
}
}
public IJavaElement getIJavaElement(){
return ije;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment