gpt4 book ai didi

java - 使用 Eclipse AST 将元素添加到 AST 的特定位置

转载 作者:太空宇宙 更新时间:2023-11-04 08:23:33 27 4
gpt4 key购买 nike

更新2:再次感谢@deepak-azad,我设法解决了我的问题:这里是主代码的链接:https://gist.github.com/1714641

更新:感谢@deepak-azad,我补充了代码,但仍然无法工作。

我正在尝试使用 EclipseJDT 在 Java 中检测源文件。我的主要目标是添加一些语句“x();”在每个变量声明下方。

例如:从此:

int a = 10;

对此:

int a = 10;
method();

我能够创建 ast 并创建一个类来扩展访问方法以获取代码中的所有变量声明:

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;

public class VDS extends ASTVisitor {
List<VariableDeclarationStatement> vds = new ArrayList<VariableDeclarationStatement>();

@Override
public boolean visit(VariableDeclarationStatement node) {
vds.add(node);
return super.visit(node);
}

public List<VariableDeclarationStatement> getVDS() {
return vds;
}
}

我测试了它,它可以很好地捕获每个变量,但不能插入新节点,使用如下所示:

VDS vds = new VDS();
unit2.accept(vds); // unit2 is the compilation unit
System.out.println("Variables :" + vds.getVDS().size());

for(VariableDeclarationStatement vds_p : vds.getVDS()){
System.out.println(vds_p.toString())

List<Statement> a = ((Block) vds_.getParent()).statements();
a.add(e);//e is the artificial statement

ListRewrite lrw = astRewrite.getListRewrite(vds_p.getParent().BLock.STATEMENTS_PROPERTY);
lrw.insertAfter(e,vds_p,null);

}

实际写下的代码是(这部分工作我使用任意注释进行了测试)

IDocument document2;
ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager(); // get the buffer manager
IPath path = unit2.getJavaElement().getPath(); // unit: instance of CompilationUnit
try {
bufferManager.connect(path, null);
ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path);
// retrieve the buffer
document2 = textFileBuffer.getDocument();
textFileBuffer.commit(null /* ProgressMonitor */, false /* Overwrite */);

} finally{ bufferManager.disconnect(path, null);}
TextEdit edits = unit2.rewrite(document2, null);
edits.apply(document2);
write2File(document2);

我对层次结构如何工作有点困惑,并且基于每个变量语句属于一个 block ,每个 block 属于一个方法这一事实,我得出了一个想法,但仍然不知道如何在 ListRewrite 元素中处理这个问题。

我一直在阅读关于 eclipse 中的 ast,但所有问题都与代码的创建有关,而不是与编辑过程有关

当我执行此操作时,我收到如下非法参数异常:

java.lang.IllegalArgumentException
at org.eclipse.jdt.core.dom.ASTNode.checkNewChild(ASTNode.java:1905)
at org.eclipse.jdt.core.dom.ASTNode$NodeList.add(ASTNode.java:1269)
at java.util.AbstractList.add(AbstractList.java:91)
at plugin.astsimple.handlers.SampleHandler.execute(SampleHandler.java:109)
at org.eclipse.ui.internal.handlers.HandlerProxy.execute(HandlerProxy.java:293)
at org.eclipse.core.commands.Command.executeWithChecks(Command.java:476)
...

其中我的代码的第 109 行是将 e 添加到 a

a.add(e);

谢谢!

最佳答案

每个“ block ”由一个语句“列表”组成,因此您需要使用“org.eclipse.jdt.core.dom.rewrite.ListRewrite.insertAfter(ASTNode, ASTNode, TextEditGroup)”。您可以在 org.eclipse.jdt.ui 插件中查找此方法的用法示例。

关于java - 使用 Eclipse AST 将元素添加到 AST 的特定位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9059980/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com