From e6ee14f29882f401ad73498c41060e8f58d3c99c Mon Sep 17 00:00:00 2001 From: Martin Monperrus Date: Tue, 5 Mar 2019 23:40:07 +0100 Subject: [PATCH] feature(CtCommentImpl): add _setRawContent to be able to bypass cleanComment (#2889) --- .../support/reflect/code/CtCommentImpl.java | 11 +++++++++ .../java/spoon/test/comment/CommentTest.java | 23 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/main/java/spoon/support/reflect/code/CtCommentImpl.java b/src/main/java/spoon/support/reflect/code/CtCommentImpl.java index d872b8ec777..733bb0e9a15 100644 --- a/src/main/java/spoon/support/reflect/code/CtCommentImpl.java +++ b/src/main/java/spoon/support/reflect/code/CtCommentImpl.java @@ -69,6 +69,17 @@ public E setContent(String content) { return (E) this; } + /** + * FOR ADVANCED USAGE ONLY + * Set the comment content, without cleaning the comment, if the cleaning behavior to get a canonical version does not work for you. + * Does not ensure any AST contract such as calling the change listener + * You have to cast your comment to CtCommentImpl, it's not beautiful, but it's known :-) + */ + public E _setRawContent(String content) { + this.content = content; + return (E) this; + } + @Override public CommentType getCommentType() { return type; diff --git a/src/test/java/spoon/test/comment/CommentTest.java b/src/test/java/spoon/test/comment/CommentTest.java index 17bcbebad92..192c4d83b7c 100644 --- a/src/test/java/spoon/test/comment/CommentTest.java +++ b/src/test/java/spoon/test/comment/CommentTest.java @@ -64,6 +64,7 @@ import spoon.support.JavaOutputProcessor; import spoon.support.StandardEnvironment; import spoon.support.compiler.jdt.JDTSnippetCompiler; +import spoon.support.reflect.code.CtCommentImpl; import spoon.test.comment.testclasses.BlockComment; import spoon.test.comment.testclasses.Comment1; import spoon.test.comment.testclasses.Comment2; @@ -635,6 +636,28 @@ public void testBlockComment() { + "/* comment before name parameter */" + newLine + "int i) throws java.lang.Error, java.lang.Exception {" + newLine + "}", m2.toString()); + + // contract: one does not crash when setting a comment starting with '//' in a block comment + // https://github.com/INRIA/spoon/issues/2887 + CtComment ctComment = m2.getComments().get(0); + ctComment.setContent("// foo"); + assertEquals(CtComment.CommentType.BLOCK, ctComment.getCommentType()); + // it's a limitation, you cannot start with '' + assertEquals("/* foo */", ctComment.toString()); + + // workaround #1: the comment can start with ' //' + ctComment.setContent(" // foo"); + assertEquals(CtComment.CommentType.BLOCK, ctComment.getCommentType()); + // it's a limitation, you cannot start with '' + assertEquals("/* // foo */", ctComment.toString()); + + // workaround #2: one can cast and call '_setRawContent' + // without setting the comment field through reflection + ((CtCommentImpl) ctComment)._setRawContent("// foo"); + assertEquals(CtComment.CommentType.BLOCK, ctComment.getCommentType()); + // it's a limitation, you cannot start with '' + assertEquals("/* // foo */", ctComment.toString()); + } @Test