public ElementHandle<TypeElement> getElementHandle(Project p, final String fqn)
{
FileObject projRoot = p.getProjectDirectory();
// actually, should find srcRoot using p.getLookup().lookup(Sources.class) and iterating over the SourceGroups.
// This is cheating, but illustrative...
FileObject srcRoot = projRoot.getFileObject("src").getFileObject("java");
ClasspathInfo ci = ClasspathInfo.create(srcRoot);
JavaSource js = JavaSource.create(ci);
Searcher searcher = new Searcher(fqn);
try
{
js.runUserActionTask(searcher, true);
}
catch (IOException ex)
{
// kvetch
}
if (searcher.handle == null)
{
throw new IllegalArgumentException("Cannot find class: " + fqn);
}
}
private class Searcher implements CancellableTask<CompilationController>
{
private String fqn;
ElementHandle<TypeElement> handle;
Searcher(String fqn)
{
this.fqn = fqn;
}
public void cancel() {}
public void run(CompilationController info) throws Exception {
TypeElement te = info.getElements().getTypeElement(fqn);
handle = ElementHandle.create(te);
}
}