views:

422

answers:

2

Hi,

I am learning how to do an ASTVisitor for a project, so I started using Eclipse, which has a comprehensive API for that.

I have downloaded the Packed Example Project from this website: http://www.eclipse.org/articles/article.php?file=Article-JavaCodeManipulation%5FAST/index.html

But I realised that the codes do not have a main() method, yet I am able to run the program! Anyone knows why? [qn 1]

However the codes seems too compilated to work with, and the instructions for doing sth basic are not very clear.

So I also went on to look at this: http://www.vogella.de/articles/EclipsePreferences/article.html

Anyone knows how can I write a main method to invoke execute(ExecutionEvent event)? -- provide a sample code snippet? [qn 2] I don't know how do use ExecutionEvent...

I kept getting "Exception in thread "main" java.lang.IllegalStateException: Workspace is closed." even if I remove the parameter for Execution Event (also need to remove the extends part) or use part of the code in my main. It seems that it has to do with "ResourcesPlugin" in "IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();". Anyone knows why? [qn 3]

Thank you!

A: 

Your project should be an Eclipse plug-in project, not a simple Java project with a main() method.

Here's a suggestion: take a look at the AST View tool. It's a tool designed to inspect the AST. Work with the source, run it, debug it, put breakpoints, modify the code, see how it works.

Also, make sure you have all the JDT sources. There are many examples of ASTVisitors (just make sure not to use any internal methods).

zvikico
+1  A: 
NomeN
But how do I create an IFile? I still need "IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();" right? But doing that will get me "Exception in thread "main" java.lang.IllegalStateException: Workspace is closed." Do I need to create a plugin project in eclipse to do that? Cos currently I am using main to run. How do I create a plugin project?
yeeen