繁体   English   中英

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

[英]Adding element into AST in specific position with Eclipse AST

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

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

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

例如:从此:

int a = 10;

对此:

int a = 10;
method();

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

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

我对层次结构的工作方式有些困惑,我想到了一个想法,因为每个变量语句都属于一个块,而每个块都属于一个方法,但是仍然不知道如何在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.add(e);

谢谢!

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

为了更好地了解AST中的层次结构是如何工作的,您应该使用AST视图插件-http: //www.eclipse.org/jdt/ui/astview/index.php

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM