开发者

When developing an Eclipse plugin, how to get access to the project's Java Model and AST root node?

I am currently developing a plugin that'll make use of Eclipse's Java Model and Eclipse's Java Abstract Syntax Tree.

So what I am looking for is a way of getting though my plugin the Java Model root object and the current Java project's AST Root node:

public class SampleHandler extends Abstra开发者_如何学运维ctHandler {
    public Object execute(ExecutionEvent event) throws ExecutionException {     
        IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
                ??? how to get the current Java project Java Model? and the AST node?
        }
}

Thanks


I use the following to get the model:

public static IJavaModel prepareWorkspace() {
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    IWorkspaceRoot workspaceRoot = workspace.getRoot();
    IJavaModel javaModel = JavaCore.create(workspaceRoot);
    return javaModel;
}

You can find this and some other helpful Eclipse utility methods in EclipseUtils.java and EclipseSearchUtils.java.


You can get the selection from within your handler, and then you can decide what to do with it:

ISelection sel = HandlerUtil.getCurrentSelection(event);
if (sel instanceof IStructuredSelection) {
    // check to see if it's empty first, though
    Object obj = ((IStructuredSelection)sel).getFirstElement();
    // then have a look and see what your selection is.
}

If you have an IJavaElement, you can walk about the model until you find the point you are looking for. If you have an IFile/IResource, you can use some of the JavaCore methods to get to the java model.


See org.eclipse.jdt.core plugin and IJavaProject and JavaCore classes. Use JavaCore.create( [IProject] ) to get IJavaProject and go rom there.


Take a look at this...

http://code.google.com/a/eclipselabs.org/p/javadocasumlview/source/browse/trunk/JavadocAsUmlAPI/src/main/java/utils/CompilationUnitResolverHelper.java

and

http://code.google.com/a/eclipselabs.org/p/javadocasumlview/source/browse/trunk/JavadocAsUmlAPI/src/main/java/utils/CompilationUnitResolverCache.java

Basically

  • the selection context is mostly a compilation unit which itself is a ITypeRoot
  • look for ASTParser or SharedASTProvider for a good starting point

Good Luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜