views:

152

answers:

1

How do I run the plugin project under Resources [1] here: http://www.eclipse.org/articles/article.php?file=Article-JavaCodeManipulation%5FAST/index.html

If I am not wrong, the project starting point is here public class ASTArticleActionDelegate implements IObjectActionDelegate -> public void run(IAction action)

public void run(IAction action) {
    if (selection instanceof IStructuredSelection) {
     ICompilationUnit lwUnit = (ICompilationUnit) ((IStructuredSelection) selection).getFirstElement();
     createActionExecutable(action.getId()).run(lwUnit);
    }
}

I know I should run it as an Eclipse Application, but what should I do after that to see sth? I only see an Eclipse application started, and nothing else, no button or anything!

I search for "IObjectActionDelegate" and it seems like it has sth to do with context menu, which is i should see sth when I right click on sth (IStructuredSelection - tree structure?)? But I see no difference in the context menu! :'(

Just let me know an example of a way to see that this project is running, so that I would be able to use it.

Pls help, thanks!

+1  A: 

The proper way to test this AST project example (net.sourceforge.earticleast.app_1.0.0.zip_1.0.0.zip) is to:

  • unzip that package
  • import the project within that package in your current eclipse workspace
  • right-click on the project and select "Debug As > Eclipse Application"

(Note the "Debug As", to be able to set breakpoint within your first eclipse instance)

Once the second eclipse is launched, you can:

  • go to Help/Anout Eclipse SDK, click on "installation details", click "Plugins" and see right at the top the plugin "Abstract Syntax Tree Article, Example Application Plugin", id "net.sourceforge.earticleast.app"
  • Import any project in that new workspace of that second eclipse instance (you can for instance re-importe the net.sourceforge.earticleast.app project!)
  • right-click on any class and see a custom entry in the contextual menu: "Ast article: Move Declaration" (the action to detect contradicting variable declarations and to move them to their correct place)

So now almost everything is in place to test those AST manipulation.

One last thing: create a Java Unit compilation able to highlights those variable declarations rewrites.

Create in your imported project (whatever it is) a package test, with the class:

package test;

public class Test {

    static {
     int i = 2;
     System.out.println("test");
     System.out.println(i);
    }

}

Right-click on that class and select "Ast article: Move Declaration": see the source being instantly rewritten as:

package test;

public class Test {

    static {
     System.out.println("test");
     int i = 2;
     System.out.println(i);
    }

}

From the first instance of the eclipse, you can set up some breakpoints in:

  • ASTArticleMoveVariableDeclaration:run()
  • AbstractManipulator:manipulate(final CompilationUnit unit, Collection<VariableBindingManager> managers)

to see where the magic is happening.

The other cases of "Move Declaration" cases are:

static {
 int i = 2;
 System.out.println("test");
 try
 {
  System.out.println(i);   
 }
 catch(Exception e)
 {
  System.out.println(i);   
 }
}

which get rewritten as:

static {
 System.out.println("test");
 int i = 2;
 try
 {
  System.out.println(i);   
 }
 catch(Exception e)
 {
  System.out.println(i);   
 }
}

Finally, there is a more advanced move which is:

package test;

public class Test {

    static {
     int i = 2;
     i = 3;
     System.out.println(i);
    }

}

package test;

public class Test {

    static {
     i = 3;
     int i = 3;
     System.out.println(i);
    }

}

'int i = 2' has been correctly removed. However, note the 'i = 3' which is left: that is because the new declaration node 'int i = 3 is added after 'i = 3' instead of replacing it.

After some debugging, it turns out ASTRewriteBasedManipulator:addNewVariableDeclaration() forgets to remove the initializer 'i=3' which it is supposed to replaced with the declaration 'int i = 3'.

Just add at the end of this method:

 rewrite.remove(manager.getInitializer().getParent().getParent(), null);

and you are good to go.

VonC
Thank you for u effort! I have found another bug as well... "int i = 2;System.out.println("test"); System.out.println(i);int j = 2;System.out.println("test"); System.out.println(j);"Only the 1st one is changed, not the 2nd one. Trying with more of these will yield more strange result. Hard to debug as the eclipse application with the plugin will hang, then I can't see where the program is running. Then when I debug I am supposed to be able to see what is in a variable, but I can't understand those things listed there. Not sure if u can provide me some tips in debugging.
yeeen
The tip when you are debugging AST manipulation is to first let it run without breakpoint, then, in the second eclipse where the source has just been rewritten, do some *Undo* (CTRL+Z) to see what kind of rewrite has been done and in what order. Than you can set breakpoints and check if the list of rewrite events reflects what you saw in the undos, and debug why those events are set in that order.
VonC