2 years ago

#14046

test-img

Alexander Kalinowski

Unexpected behaviour when writing modifications by ASTNode.copySubtrees() to file

I am writing some Builder pattern insertion code and part of that involves copying fields from the top-level class to an autogenerated, nested Builder class. To do so, I call ASTNode.copySubtrees() to replicate those top-level class fields and then remove any final modifiers from the (supposedly deep!) copies. However, when writing changes back to source code file via CompilationUnit.rewrite() and TextEdit.apply(), I discovered that the final modifiers are not removed in the source code.

Playing around with it some more, I further discovered that the changes are made correctly in AST - just not correctly translated into changes of the source code: passing the top-level class declaration to System.out.println() right before rewrite() reveals that the final modifiers have been removed in memory correctly; still, they do not get removed from the source code.

The workaround that I am currently using is to copy the field subtrees to a temporary AST and from there to the nested Builder class, which works like a charm.

Am I missing something here or can someone confirm or deny if this is a (known?) bug in the JDT/Eclipse?

java

eclipse

abstract-syntax-tree

eclipse-jdt

0 Answers

Your Answer

Accepted video resources