2

I'm using JavaParser and following its Wiki. Problem is even though I change the method's name and add a parameter to it, the file doesn't update. In other words, changes are not saved. When I System.out.println the changed CompilationUnit, it prints it with changes, but those changes don't affect the source file at all.

Is there anything like CompilationUnit.update() or am I missing something ?

The example I've used from the 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);

    }


}

EDIT: Here is the solution; Add Below Line;

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

Change Below Line to use Special Characters as well(e.g "ş,ö,ü ...)

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

To

cu = JavaParser.parse(files_list.get(0),"UTF-8");
Nerzid
  • 457
  • 5
  • 15
  • 1
    Why do you expect a _parser_ to modify its data source? It shouldn't, especially since the source code might not come from a file but some other data source. And the parser cannot know how to modify an arbitrary InputStream. – Roland Illig Aug 10 '16 at 20:04
  • @RolandIllig I don't understand your statement here. If you look at the [JavaParser](https://github.com/javaparser/javaparser) page, it has a feature Modifiable. Also If it doesn't modify the source code, why there are methods like setName(), or setComment() ? – Nerzid Aug 10 '16 at 20:13
  • Silly me, I understand now, but question still remains, how do I change the source file ? – Nerzid Aug 10 '16 at 21:50
  • @Nerzid: Should be `cu = JavaParser.parse(files_list.get(0), StandardCharsets.UTF_8);` if I'm not mistaken? Your example with the string "UTF-8" doesn't work @ my side, the constant on the other hand does... – GeertVc Aug 05 '18 at 11:31

1 Answers1

5

Since you already have the string representation, what about this:

Files.write(new File("Modified.java").toPath(), cu.toString(), StandardCharsets.UTF_8);
Roland Illig
  • 40,703
  • 10
  • 88
  • 121
  • Thanks! This actually works, but here another problem, CompilationUnit doesn't recognize UTF-8 characters.(e.g chars like ö,ş.. become �) . – Nerzid Aug 10 '16 at 22:30
  • 2
    I've solved special character problem as well. If anyone has same issue, you must pass encoding argument to JavaParser as follows `CompilationUnit cu = JavaParser.parser(file, "UTF-8");` Thank you again @RolandIllig – Nerzid Aug 10 '16 at 22:37
  • I assumed your source files were encoded in UTF-8, as this is the most common encoding nowadays. Apparently I was wrong, so try to save the file with `StandardEncodings.ISO_8859_1`, which works well for the standard Windows encoding (codepage 1252). – Roland Illig Aug 10 '16 at 22:39
  • That's weird. When you read a file that is actually UTF-8 with a reader that interprets the file as ISO 8859-1, the character `ö` should come out as `ö` instead of the replacement character. – Roland Illig Aug 10 '16 at 22:43
  • 1
    Actually you were right about utf-8. Problem was related to CompilationUnit was getting the source file without using utf-8 encoding. :) – Nerzid Aug 10 '16 at 22:43