Created
August 6, 2010 16:28
-
-
Save rylan/511576 to your computer and use it in GitHub Desktop.
Fully qualified name to ICompilationUnit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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