Skip to content

Commit

Permalink
feature: add CtComment#getRawContent() (#2746)
Browse files Browse the repository at this point in the history
  • Loading branch information
pvojtechovsky authored and monperrus committed Nov 7, 2018
1 parent f0dd561 commit fdd3250
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/main/java/spoon/reflect/code/CtComment.java
Expand Up @@ -18,6 +18,7 @@

import spoon.reflect.annotations.PropertyGetter;
import spoon.reflect.annotations.PropertySetter;
import spoon.support.DerivedProperty;

import static spoon.reflect.path.CtRole.COMMENT_TYPE;
import static spoon.reflect.path.CtRole.COMMENT_CONTENT;
Expand Down Expand Up @@ -68,6 +69,16 @@ enum CommentType {
@PropertySetter(role = COMMENT_CONTENT)
<E extends CtComment> E setContent(String content);

/**
* @return the original raw comment from the source file including comment prefix and suffix, indentation (including TABs) original EOLs,
* based on the attached position object (the returned value is "derived" from the position).
* If the file pointed to in the position object does not exist on disk anymore,
* then the empty string "" is returned
* Note: the call of {@link #setContent(String)} doesn't influence the returned value, only the value of the position object.
*/
@DerivedProperty
String getRawContent();

/**
* Get the type of the comment
* @return the comment type
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/spoon/support/reflect/code/CtCommentImpl.java
Expand Up @@ -19,6 +19,8 @@
import spoon.reflect.annotations.MetamodelPropertyField;
import spoon.reflect.code.CtComment;
import spoon.reflect.code.CtJavaDoc;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.declaration.CtCompilationUnit;
import spoon.reflect.path.CtRole;
import spoon.reflect.visitor.CtVisitor;

Expand All @@ -45,6 +47,19 @@ public String getContent() {
return content;
}

@Override
public String getRawContent() {
SourcePosition pos = getPosition();
CtCompilationUnit cu = pos.getCompilationUnit();
if (cu != null) {
String source = cu.getOriginalSourceCode();
if (source != null) {
return source.substring(pos.getSourceStart(), pos.getSourceEnd() + 1);
}
}
return "";
}

@Override
public <E extends CtComment> E setContent(String content) {
getFactory().getEnvironment().getModelChangeListener().onObjectUpdate(this, CtRole.COMMENT_CONTENT, content, this.content);
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/spoon/test/comment/CommentTest.java
Expand Up @@ -1069,6 +1069,31 @@ public void testCommentAssociationAndPrettyPrint() {
}

@Test
public void testCommentGetRawContent() {
Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/resources/comment/JavaDocComment.java");
launcher.getEnvironment().setCommentEnabled(true);
launcher.run();

CtClass<?> type = (CtClass<?>) launcher.getFactory().Type().get("spoon.test.comment.testclasses.JavaDocComment");
//contract: getContent always returns cleaned comment content with \n as EOL
assertEquals("JavaDoc test class.\n" +
"\n" +
"Long description", type.getComments().get(0).getContent());
// contract: return the full original comment with prefix and suffix, incl. the original EOL (\r as EOL here)
assertEquals("/**\r" +
" * JavaDoc test class.\r" +
" *\r" +
" * Long description\r" +
" *\r" +
" * @deprecated\r" +
" * @since 1.3\r" +
" * @author Thomas Durieux\r" +
" * @version 1.0\r" +
" */", type.getComments().get(0).getRawContent());
}

@Test
public void testEmptyStatementComments() {
//contract: model building should not produce NPE, comments should exist
Launcher launcher = new Launcher();
Expand Down

0 comments on commit fdd3250

Please sign in to comment.