Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I am not confident enough to categorically say you need to be writing a plugin to use the ASTVisitor, but it is the easiest way. Find some simple plugin code that creates a button or something you can press and start the run of your code. There is a sample plugin in the Plugin Development Environment that does just this (download the appropriate version of Eclipse if you do not have this).</p> <p><strong>Creating a plugin project:</strong></p> <p>To create a plugin project got to your package explorer and right click or open the file menu. Select the top item "new..." this opens a submenu where you selct "other...". The "new" dialog is opened. Open the folder "plug-in development" and select "plug-in project".</p> <p><img src="https://i620.photobucket.com/albums/tt281/NomeN/newProject.png" alt="alt text"></p> <p>Click next a few times, input the necessary information until you reach the following page:</p> <p><img src="https://i620.photobucket.com/albums/tt281/NomeN/samples.png" alt="alt text"></p> <p>Here the first sample "Hello World" creates a menu in your 2nd level eclipse. Run the 2nd level eclipse by going to the plugin.xml of the new project, go to the tab overview.</p> <p>You'll see a page like this:</p> <p><img src="https://i620.photobucket.com/albums/tt281/NomeN/manifest.png" alt="alt text"></p> <p>click "launch an eclipse application", and another eclipse will be started with your new plugin up and running. Click the sample action button in this 2nd level eclipse to see some sweet action. </p> <p>Now you can go ahead and replace the action code with whatever you want. In this case creating and starting a visit operation.</p> <p><strong>To create an ASTVisitor:</strong></p> <p>Create a class that extends ASTVisitor so you can add your own code.</p> <ul> <li>preVisit(ASTNode) does a preorder traversal of the AST</li> <li>postVisit(ASTNode) does a postorder traversal of the AST</li> <li>visit(...) inspects a specific node</li> </ul> <p>Add something like this to your visitor:</p> <pre><code>private void startVisit(IFile file) { ICompilationUnit icu = JavaCore.createCompilationUnitFrom(file); ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setResolveBindings(true); parser.setSource(icu); CompilationUnit cu = (CompilationUnit) parser.createAST(null); cu.accept(this); } </code></pre> <p>Now add code to your button press action that creates your visitor and calls this function:</p> <pre><code>MyVisitor mv = new MyVisitor(); mv.startVisit(...); </code></pre> <p><strong>To get an IFile</strong> <em>(here you'll need to be a plug-in or you'll get the error you have reported)</em> <strong>:</strong></p> <pre><code>ResourcesPlugin.getWorkspace().getRoot().getProject(...).getFile(...); </code></pre> <p>Replace the appropriate project and file names (strings) for the ellipsis.</p> <p>Now every time you press the button the file gets visited, and the code you entered will be executed at the appropriate times (it is very insightfull to just print out every ASTNode in pre or postvisit).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload