簡體   English   中英

JavaParser不更新源文件

[英]JavaParser doesn't update source file

我正在使用JavaParser並遵循其Wiki。 問題是即使我更改方法的名稱並向其添加參數,該文件也不會更新。 換句話說,不會保存更改。 當我在System.out.println中更改了CompilationUnit ,它會根據更改打印它,但這些更改根本不會影響源文件。

有什么像CompilationUnit.update()或我錯過了什么?

我從Wiki中使用的例子:

    files_list = FilePicker.chooseAndGetJavaFiles();

    if (files_list == null || files_list.isEmpty()) {
        Errors.showError(Errors.COMMENT_GENERATOR_FILELIST_NULL_OR_EMPTY);
    } else {

        CompilationUnit cu = null;
        FileInputStream in = new FileInputStream(files_list.get(0));
        try {
            cu = JavaParser.parse(in);
        } catch (ParseException ex) {
            Logger.getLogger(CommentGenerator.class.getName()).log(Level.SEVERE, null, ex);
        } finally{
            in.close();
        }
        new MethodChangerVisitor().visit(cu,null);

        System.out.println(cu.toString());
    }
}

private static class MethodChangerVisitor extends VoidVisitorAdapter{

    @Override
    public void visit(MethodDeclaration n, Object arg) {
       // change the name of the method to upper case
        n.setName(n.getName().toUpperCase());

        // create the new parameter
        Parameter newArg = ASTHelper.createParameter(ASTHelper.INT_TYPE, "value");

        // add the parameter to the method
        ASTHelper.addParameter(n, newArg);

    }


}

編輯:這是解決方案; 添加下線;

Files.write(new File("Modified.java").toPath(), cu.toString(), StandardCharsets.UTF_8);

更改下方線以使用特殊字符(例如“ş,ö,ü...)

cu = JavaParser.parse(files_list.get(0));

cu = JavaParser.parse(files_list.get(0),"UTF-8");

既然你已經有了字符串表示,那么這個:

Files.write(new File("Modified.java").toPath(), cu.toString(), StandardCharsets.UTF_8);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM