从JAVA Source AST中删除所有Methodinvocation

时间:2014-12-16 06:04:44

标签: java eclipse abstract-syntax-tree eclipse-jdt

您好我发现所有Methodinvocation都带有ASTVisitor并且会使用ASTRewrite删除所有内容但是当使用“for循环”删除方法时,只删除永远调用事件的第一个方法调用。我想问题的原因不刷新AST,CompiliationUnit,ICompilationUnit或ASTRewrite,但我不知道何时以及如何刷新,以便在第一个事件中删除所有。

static void createAST(ICompilationUnit unit) throws JavaModelException {
    // now create the AST for the ICompilationUnits
    CompilationUnit parse = parse(unit);
    MethodVisitor visitor = new MethodVisitor();
    parse.accept(visitor);
    for (MethodInvocation metInv : visitor.getMethods1()) {
        try {
            //if declarated by user. 
            if (metInv.resolveMethodBinding().getDeclaringClass().isFromSource()) {
                methodInvocRemove(unit, metInv);
            }

        } catch (Exception e) {
            System.out.println("EXCEPOTION:" + e.getMessage());
        }
    }
}

此方法用于删除所有MethodInvocations。

private static void methodInvocRemove(ICompilationUnit unit, MethodInvocation met) {
    try {
        IProject project = unit.getJavaProject().getProject();
        CompilationUnit astRoot = parse(unit);
        // create a ASTRewrite
        AST ast = astRoot.getAST();
        ASTRewrite rewriter = ASTRewrite.create(met.getParent().getParent().getAST());
        rewriter.remove(met.getName().getParent(), null);
        TextEdit edits;
        edits = rewriter.rewriteAST();
        Document document = new Document(unit.getSource());
        edits.apply(document);
        // this is the code for adding statements
        unit.getBuffer().setContents(document.get());
        unit.getBuffer().close();
    } catch (MalformedTreeException e) {
        System.out.println("EXP!!!" + e.getMessage());
    } catch (BadLocationException e) {
        System.out.println("EXP!!!" + e.getMessage());
    } catch (JavaModelException e) {
        System.out.println("EXP!!!" + e.getMessage());
    } catch (IllegalArgumentException e) {
        System.out.println("EXP!!!" + e.getMessage());
    }
}

此方法用于使用ASTVisitor进行查找方法调用。在Eclipse上的每个事件之后JDT都会调用这个方法。

public static void findMethod(IProject project) {
    try {
        if (project.isNatureEnabled("org.eclipse.jdt.core.javanature")) {
            IPackageFragment[] packages = JavaCore.create(project).getPackageFragments();
            for (IPackageFragment myPack : packages) {
                if (myPack.getKind() == IPackageFragmentRoot.K_SOURCE) {
                    for (ICompilationUnit unit : myPack.getCompilationUnits()) {
                        CompilationUnit parse = parse(unit);
                    }
                }
            }
        }
    } catch (CoreException e) {
        System.out.println("EXP!!!"+e.getMessage());
    }
}

用于解析和修改的Java文件的示例代码。

public class MethodInvoc {

private void invoc1()
{
    MethodDeclar object1 = new MethodDeclar();
    System.out.println(object1.getStr());
}

private void invoc2() {
    MethodDeclar object2 = new MethodDeclar();
    System.out.println(object2.getStr());
}

private void invoc4()
{
    MethodDeclar object3 = new MethodDeclar();
    System.out.println(object3.getStr());
}
}

调用事件并运行methodInvocRemove以上代码更改为:

 public class MethodInvoc {

private void invoc1()
{
    MethodDeclar object1 = new MethodDeclar();
    System.out.println();
}

private void invoc2() {
    MethodDeclar object2 = new MethodDeclar();
    System.out.println(object2.getStr());//not change this :( 
}

private void invoc4()
{
    MethodDeclar object3 = new MethodDeclar();
    System.out.println(object3.getStr());//not change this :( 
}
}

1 个答案:

答案 0 :(得分:1)

您需要根据ASTRewrite缓存CompilationUnit,使用MethodInvocation删除所有ASTRewrite.remove(...)个节点,然后仅应用所有修改一次。

所以在伪代码中:

ASTRewrite rewriter = ASTRewrite.create(astRoot.getAST());
for (MethodInvocation metInv : getAllMethodInvocationsToRemove()) {
    rewriter.remove(metInv, null);
}
unit.applyTextEdit(rewriter.rewriteAST(), new NullProgressMonitor());

要应用rewriter中的修改,我已经有了很好的使用此代码的经验(其中unitICompilationUnit的实例),而不是使用Document:< / p>

unit.applyTextEdit(rewriter.rewriteAST(), new NullProgressMonitor());

此外,您可能希望查看此信息,并使用ICompilationUnit.becomeWorkingCopy以及ICompilationUnit.commitWorkingCopy来修改您的更改:Eclipse AST not changing files which are not opened in eclipse