Community
Participate
Working Groups
In a Javadoc, we I modify the Text content in a javadoc. The end-lines are missing in the output. For example, I have the orginale code: /** * @author eric * * Line1 * Line2 * @param arg1 */ After the modification or clone, when I rewite the code via Document document = new Document(targetSource); TextEdit mods = compilationUnit.rewrite(document, null); mods.apply(document); I get the following results: /** * @author eric Line1 Line2 * @param arg1 */ There are two issue: 1. one more space before the "eric"(rf: 88126) 2. end-lines are missing But in debug when I select the TagElement, it is displayed correctly. What I found is the following codes in ASTRewriteFlattener: public boolean visit(TagElement node) { Object tagName= getAttribute(node, TagElement.TAG_NAME_PROPERTY); if (tagName != null) { this.result.append((String) tagName); } List list= getChildList(node, TagElement.FRAGMENTS_PROPERTY); for (int i= 0; i < list.size(); i++) { if (i > 0 || tagName != null) { this.result.append(' '); } ASTNode curr= (ASTNode) list.get(i); if (curr instanceof TagElement) { this.result.append('{'); curr.accept(this); this.result.append('}'); } else { curr.accept(this); } } return false; } I guess it should be: public boolean visit(TagElement node) { Object tagName= getAttribute(node, TagElement.TAG_NAME_PROPERTY); if (tagName != null) { this.result.append((String) tagName); } List list= getChildList(node, TagElement.FRAGMENTS_PROPERTY); for (int i= 0; i < list.size(); i++) { if (i == 0) { if (tagName != null) this.result.append(' '); } else this.result.append("\n * ") ASTNode curr= (ASTNode) list.get(i); if (curr instanceof TagElement) { this.result.append('{'); curr.accept(this); this.result.append('}'); } else { curr.accept(this); } } return false; }
It seems that cloning a node looses all format information. The cloned node is similar to a node that you constructed yourself (with ast.newXY). The modifying rewrite approach doesn't give you the full possibilities, so I suggest you to use the describing approach -> ASTRewrite use createCopyTarget to copy a node (including all the modifications inside) or createMoveTarget to move. I'll discuss with David if cloning should do a copy
It seems we cannot use the ASTRewrite to copy a node into another CompilationUnit since the AST are different. I get an exception during the add: Caused by: java.lang.IllegalArgumentException at org.eclipse.jdt.core.dom.ASTNode.checkNewChild(ASTNode.java:1852) at org.eclipse.jdt.core.dom.ASTNode.preReplaceChild(ASTNode.java:1918) the exception is raised in the following codes: static void checkNewChild(ASTNode node, ASTNode newChild, boolean cycleCheck, Class nodeType) { if (newChild.ast != node.ast) { // new child is from a different AST throw new IllegalArgumentException(); }
That's correct, you can't copy to a different AST. In that case it's best to copy it in a string placeholder (ASTRewrite#createStringPlaceholder). That's the only way to not loose any special formatting. If you don't care about that, thing should work as soon as I fix the AST rewriter to correctly format Javadoc comments.
ASTRewrite rewrite = ASTRewrite.create(target.getAST()); value = (Javadoc) rewrite.createStringPlaceholder(value.toString(), ASTNode.JAVADOC); target.setJavadoc(value); Finally, i get an empty Javadoc: /** */
value.toString() doesn't work here, you have to get it from the source of the compilation unit (The AST doesn't know its source anymore). I know it's nasty, but after getting the comment from the source you have to remove the indents of the Javadoc comment as the string placeholder requires an unindented string.
In debug, I check the toString() content. I get a correct value. I have the same problem with a constant string: String content = "/** \n * test\n */"; value = (Javadoc) rewrite.createStringPlaceholder(content, ASTNode.JAVADOC);
what's the state here? as said, if you want to copy nodes to a different AST, it has to be done on a string basis. Please reopen if I missed a problem.
reopening to close properly as no further action is planned there.
no further action planned, assuming it is fine as reporter didn't comment in 10 months.