【问题标题】:Remove All Methodinvocation from JAVA Source AST从 JAVA 源 AST 中删除所有方法调用
【发布时间】:2014-12-18 17:41:58
【问题描述】:

您好,我找到了所有 MethodinvocationASTVisitor 并将删除所有 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 :( 
}
}

【问题讨论】:

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


    【解决方案1】:

    您需要为每个CompilationUnit 缓存您的ASTRewrite,删除所有带有ASTRewrite.remove(...)MethodInvocation 节点,然后只应用一次所有编辑。

    所以在伪代码中:

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

    要在rewriter 中应用编辑,我对这段代码(其中unitICompilationUnit 的一个实例)有很好的经验,而不是使用Document

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

    您可能还想查看这篇文章,并使用ICompilationUnit.becomeWorkingCopyICompilationUnit.commitWorkingCopy 来包装您的更改:Eclipse AST not changing files which are not opened in eclipse

    【讨论】:

      猜你喜欢
      • 2018-12-23
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 2012-12-15
      • 1970-01-01
      • 2020-07-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多