Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从JAVA源AST中删除所有Methodinvocation_Java_Eclipse_Abstract Syntax Tree_Eclipse Jdt - Fatal编程技术网

从JAVA源AST中删除所有Methodinvocation

从JAVA源AST中删除所有Methodinvocation,java,eclipse,abstract-syntax-tree,eclipse-jdt,Java,Eclipse,Abstract Syntax Tree,Eclipse Jdt,Hi i find all with并将移除all with,但当对移除方法使用“for loop”时,仅移除任何调用事件的第一个方法调用。我猜问题的原因是没有刷新AST、CompliationUnit、ICompilationUnit或,但我不知道何时以及如何在第一个事件中刷新以删除所有 static void createAST(ICompilationUnit unit) throws JavaModelException { // now create the AST for t

Hi i find all with并将移除all with,但当对移除方法使用“for loop”时,仅移除任何调用事件的第一个方法调用。我猜问题的原因是没有刷新AST、CompliationUnit、ICompilationUnit或,但我不知道何时以及如何在第一个事件中刷新以删除所有

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());
        }
    }
}
此方法用于删除所有MethodInvocation

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调用find方法。在EclipseJDT上的每个事件之后,调用此方法

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());
}
}
调用事件并运行MethodInvocre后,将上述代码更改为:

 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 :( 
}
}

您需要按照
编译单元缓存
ASTRewrite
,使用
ASTRewrite删除所有
MethodInvocation
节点。删除(…)
,然后只应用一次所有编辑

所以在伪代码中:

ASTRewrite rewriter = ASTRewrite.create(astRoot.getAST());
for (MethodInvocation metInv : getAllMethodInvocationsToRemove()) {
    rewriter.remove(metInv, null);
}
unit.applyTextEdit(rewriter.rewriteAST(), new NullProgressMonitor());
为了应用
rewriter
中的编辑,我对这段代码(其中
unit
ICompilationUnit
的一个实例)有很好的经验,而不是使用
文档

unit.applyTextEdit(rewriter.rewriteAST(), new NullProgressMonitor());
此外,您可能还想看看这篇文章,并使用
ICompilationUnit.becomeWorkingCopy
以及
ICompilationUnit.commitworkcopy
包装您的更改: