Eclipse 中的 ASTVisitor

发布于 2024-08-03 06:41:43 字数 939 浏览 8 评论 0原文

我正在学习如何为项目创建 ASTVisitor,因此我开始使用 Eclipse,它有一个全面的 API。

我已经从该网站下载了打包示例项目: http://www.eclipse.org/articles/ Article.php?file=Article-JavaCodeManipulation_AST/index.html

但我意识到代码没有 main() 方法,但我能够运行程序!有人知道为什么吗? [qn 1]

然而,这些代码似乎过于编译而无法使用,并且执行基本操作的说明也不是很清楚。

于是我也接着看了一下这个: http://www.vogella.de/articles/EclipsePreferences/article.html

有人知道如何编写一个 main 方法来调用execute(ExecutionEvent event)吗? -- 提供示例代码片段吗? [qn 2] 我不知道如何使用 ExecutionEvent...

我不断收到“线程“main”java.lang.IllegalStateException 中的异常:工作区已关闭”。即使我删除执行事件的参数(还需要删除扩展部分)或使用我的 main.c 中的部分代码。看起来它与 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot() 中的“ResourcesPlugin”有关。有人知道为什么吗? [问题3]

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_AST/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 something 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]

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

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

发布评论

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

评论(2

别靠近我心 2024-08-10 06:41:43

我没有足够的信心断然地说您需要编写一个插件才能使用 ASTVisitor,但这是最简单的方法。找到一些简单的插件代码,它可以创建一个按钮或可以按下并开始运行代码的东西。插件开发环境中有一个示例插件可以执行此操作(如果没有,请下载适当版本的 Eclipse)。

创建插件项目:

要创建插件项目,请转到包资源管理器并右键单击或打开文件菜单。选择顶部项目“新...”,这将打开一个子菜单,您可以在其中选择“其他...”。
“新建”对话框打开。打开“插件开发”文件夹,选择“插件项目”。

alt text

单击“下一步”几次,输入必要的信息,直到到达以下页面:

< img src="https://i620.photobucket.com/albums/tt281/NomeN/samples.png" alt="alt text">

这里第一个示例“Hello World”在第二级 Eclipse 中创建一个菜单。通过转到新项目的plugin.xml 来运行第二级eclipse,转到选项卡概述。

您将看到如下页面:

alt text

单击“启动 Eclipse 应用程序”,然后另一个 Eclipse 将启动并运行您的新插件。单击第二级 Eclipse 中的示例操作按钮可以看到一些甜蜜的操作。

现在您可以继续将操作代码替换为您想要的任何内容。在本例中创建并启动访问操作。

要创建 ASTVisitor:

创建一个扩展 ASTVisitor 的类,以便您可以添加自己的代码。

  • preVisit(ASTNode) 对 AST 进行前序遍历
  • postVisit(ASTNode) 对 AST 进行后序遍历
  • Visit(...) 检查特定节点

向您的访问者添加类似这样的内容:

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);
}

现在将代码添加到您的按钮按下操作中,以创建您的访问者并调用此函数:

MyVisitor mv = new MyVisitor();
mv.startVisit(...);

获取 IFile (这里您需要成为一个插件,否则您将收到所报告的错误) :

ResourcesPlugin.getWorkspace().getRoot().getProject(...).getFile(...);

将省略号替换为适当的项目和文件名(字符串)。

现在,每次按下按钮时,文件都会被访问,并且您输入的代码将在适当的时间执行(在访问前或访问后打印出每个 ASTNode 是非常有见地的)。

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).

Creating a plugin project:

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".

alt text

Click next a few times, input the necessary information until you reach the following page:

alt text

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.

You'll see a page like this:

alt text

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.

Now you can go ahead and replace the action code with whatever you want. In this case creating and starting a visit operation.

To create an ASTVisitor:

Create a class that extends ASTVisitor so you can add your own code.

  • preVisit(ASTNode) does a preorder traversal of the AST
  • postVisit(ASTNode) does a postorder traversal of the AST
  • visit(...) inspects a specific node

Add something like this to your visitor:

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);
}

Now add code to your button press action that creates your visitor and calls this function:

MyVisitor mv = new MyVisitor();
mv.startVisit(...);

To get an IFile (here you'll need to be a plug-in or you'll get the error you have reported) :

ResourcesPlugin.getWorkspace().getRoot().getProject(...).getFile(...);

Replace the appropriate project and file names (strings) for the ellipsis.

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).

我们只是彼此的过ke 2024-08-10 06:41:43

您的项目应该是一个 Eclipse 插件项目,而不是一个带有 main() 方法的简单 Java 项目。

这里有一个建议:看看 AST View 工具。它是一个旨在检查 AST 的工具。使用源代码,运行它,调试它,设置断点,修改代码,看看它是如何工作的。

另外,请确保您拥有所有 JDT 源。有很多 ASTVisitors 的例子(只要确保不使用任何内部方法)。

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).

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