运行 Eclipse 插件

发布于 2024-08-03 11:42:06 字数 918 浏览 4 评论 0原文

如何在此处的资源 [1] 下运行插件项目: http://www.eclipse.org/articles/article.php?file=Article-JavaCodeManipulation%5FAST/index.html

如果我没记错的话,项目起点在这里 公共类 ASTArticleActionDelegate 实现 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);
    }
}

我知道我应该将它作为 Eclipse 应用程序运行,但是之后我应该做什么才能看到一些东西?我只看到一个 Eclipse 应用程序启动了,没有其他任何东西,没有按钮或任何东西!

我搜索“IObjectActionDelegate”,它似乎与上下文菜单有关,当我右键单击某些内容时,我应该看到一些内容(IStructuredSelection - 树结构?)?但我在上下文菜单中看不出有什么区别!

请让我知道一个查看该项目正在运行的方法的示例,以便我能够使用它。

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 something? I only see an Eclipse application started, and nothing else, no button or anything!

I search for "IObjectActionDelegate" and it seems like it has something to do with context menu, which is I should see something when I right click on something (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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东走西顾 2024-08-10 11:42:06

测试此 AST 项目示例的正确方法 (net.sourceforge .earticleast.app_1.0.0.zip_1.0.0.zip) 的作用是:

  • 解压缩该包
  • 在当前 Eclipse 工作区中导入该包中的项目
  • 右键单击​​该项目并选择“调试为 > ; Eclipse 应用程序”

(注意“调试为”,以便能够在第一个 Eclipse 实例中设置断点)

启动第二个 Eclipse 后,您可以:

  • 转到“帮助/Anout Eclipse SDK”,单击“安装详细信息”,然后单击“插件”并在顶部看到插件“抽象语法树文章,示例应用程序插件”,id“net.sourceforge.earticleast.app
  • 导入第二个 Eclipse 的新工作区中的任何项目实例(例如,您可以重新导入 net.sourceforge.earticleast.app 项目!)
  • 右键单击任何类,然后在上下文菜单中看到一个自定义条目:“Ast 文章:移动声明”(检测矛盾的变量声明并将它们移动到正确位置的操作)

所以现在几乎所有东西都已准备就绪,可以测试这些 AST 操作。

最后一件事:创建一个能够突出显示那些变量声明重写的 Java 单元编译。

在导入的项目(无论是什么)中创建一个包 test,其中包含类:

package test;

public class Test {

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

}

右键单击该类并选择“Ast 文章:移动声明”:请参阅源立即重写为:

package test;

public class Test {

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

}

从 Eclipse 的第一个实例中,您可以在以下位置设置一些断点:

  • ASTArticleMoveVariableDeclaration:run()
  • AbstractManipulator:manipulate(final CompilationUnit unit, CollectionManagers )

看看神奇发生在哪里。

“移动声明”情况的其他情况是:

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

被重写为:

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

最后,有一个更高级的移动,即:

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' 已被正确删除。但是,请注意剩下的“i = 3”:这是因为新的声明节点“int i = 3”是在之后添加的。 i = 3' 而不是替换它。

经过一番调试后,结果发现 ASTRewriteBasedManipulator:addNewVariableDeclaration() 忘记删除初始化程序“i=3”,它应该用声明“int”替换我= 3 '。

只需在此方法的末尾添加:

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

即可开始。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文