Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(doc): Handles comments in CtNewArray and in CtConditional #590

Merged
merged 1 commit into from
Apr 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 42 additions & 6 deletions src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -819,15 +819,37 @@ public <T> void visitCtClass(CtClass<T> ctClass) {

public <T> void visitCtConditional(CtConditional<T> conditional) {
enterCtExpression(conditional);
scan(conditional.getCondition());
CtExpression<Boolean> condition = conditional.getCondition();
if (!(condition instanceof CtStatement)) {
printComment(condition, CommentOffset.BEFORE);
}
scan(condition);
if (!(condition instanceof CtStatement)) {
printComment(condition, CommentOffset.AFTER);
}
write(" ? ");
scan(conditional.getThenExpression());
CtExpression<T> thenExpression = conditional.getThenExpression();
if (!(thenExpression instanceof CtStatement)) {
printComment(thenExpression, CommentOffset.BEFORE);
}
scan(thenExpression);
if (!(thenExpression instanceof CtStatement)) {
printComment(thenExpression, CommentOffset.AFTER);
}
write(" : ");

CtExpression<T> elseExpression = conditional.getElseExpression();
boolean isAssign = false;
if ((isAssign = conditional.getElseExpression() instanceof CtAssignment)) {
if ((isAssign = elseExpression instanceof CtAssignment)) {
write("(");
}
scan(conditional.getElseExpression());
if (!(elseExpression instanceof CtStatement)) {
printComment(elseExpression, CommentOffset.BEFORE);
}
scan(elseExpression);
if (!(elseExpression instanceof CtStatement)) {
printComment(elseExpression, CommentOffset.AFTER);
}
if (isAssign) {
write(")");
}
Expand Down Expand Up @@ -1614,7 +1636,8 @@ private List<CtComment> getComments(CtElement e, CommentOffset offset) {
}
continue;
}
if (offset == CommentOffset.BEFORE && comment.getPosition().getLine() <= e.getPosition().getLine()) {
if (offset == CommentOffset.BEFORE && (comment.getPosition().getLine() < e.getPosition().getLine()
|| e.getPosition().getSourceEnd() >= comment.getPosition().getSourceEnd())) {
commentsToPrint.add(comment);
} else if (offset == CommentOffset.AFTER && comment.getPosition().getSourceStart() >= e.getPosition().getSourceEnd()) {
commentsToPrint.add(comment);
Expand Down Expand Up @@ -1700,7 +1723,14 @@ public <T> void visitCtNewArray(CtNewArray<T> newArray) {
for (int i = 0; ref instanceof CtArrayTypeReference; i++) {
write("[");
if (newArray.getDimensionExpressions().size() > i) {
scan(newArray.getDimensionExpressions().get(i));
CtExpression<Integer> e = newArray.getDimensionExpressions().get(i);
if (!(e instanceof CtStatement)) {
printComment(e, CommentOffset.BEFORE);
}
scan(e);
if (!(e instanceof CtStatement)) {
printComment(e, CommentOffset.AFTER);
}
}
write("]");
ref = ((CtArrayTypeReference) ref).getComponentType();
Expand All @@ -1709,8 +1739,14 @@ public <T> void visitCtNewArray(CtNewArray<T> newArray) {
if (newArray.getDimensionExpressions().size() == 0) {
write("{ ");
for (CtExpression e : newArray.getElements()) {
if (!(e instanceof CtStatement)) {
printComment(e, CommentOffset.BEFORE);
}
scan(e);
write(" , ");
if (!(e instanceof CtStatement)) {
printComment(e, CommentOffset.AFTER);
}
}
if (newArray.getElements().size() > 0) {
removeLastChar();
Expand Down
25 changes: 17 additions & 8 deletions src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtCase;
import spoon.reflect.code.CtComment;
import spoon.reflect.code.CtConditional;
import spoon.reflect.code.CtIf;
import spoon.reflect.code.CtNewArray;
import spoon.reflect.code.CtStatement;
Expand Down Expand Up @@ -53,7 +54,7 @@
* The comment builder that will insert all element of a CompilationUnitDeclaration into the Spoon AST
*/
@SuppressWarnings("unchecked")
public class JDTCommentBuilder {
class JDTCommentBuilder {

private static final Logger LOGGER = Logger.getLogger(JDTCommentBuilder.class);

Expand All @@ -69,7 +70,7 @@ public class JDTCommentBuilder {
* @param declarationUnit the declaration unit
* @param factory the Spoon AST
*/
public JDTCommentBuilder(CompilationUnitDeclaration declarationUnit, Factory factory) {
JDTCommentBuilder(CompilationUnitDeclaration declarationUnit, Factory factory) {
this.declarationUnit = declarationUnit;
if (declarationUnit.comments == null) {
return;
Expand Down Expand Up @@ -147,13 +148,12 @@ private CtElement addCommentToNear(final CtComment comment, final Collection<CtE
if (element instanceof CtComment) {
continue;
}
if (element.getPosition().getLine() == comment.getPosition().getLine()) {
element.addComment(comment);
return element;
}
final boolean isAfter = element.getPosition().getSourceEnd() < comment.getPosition().getSourceStart();
int distance = Math.abs(element.getPosition().getSourceStart() - comment.getPosition().getSourceEnd());
boolean isAfter = element.getPosition().getSourceEnd() < comment.getPosition().getSourceStart();
if (distance < smallDistance && !isAfter) {
if (isAfter) {
distance = Math.abs(element.getPosition().getSourceEnd() - comment.getPosition().getSourceStart());
}
if (distance < smallDistance && (!isAfter || element.getPosition().getEndLine() == comment.getPosition().getLine())) {
best = element;
smallDistance = distance;
}
Expand Down Expand Up @@ -215,6 +215,15 @@ public <T> void visitCtConstructor(CtConstructor<T> e) {
e.addComment(comment);
}

@Override
public <T> void visitCtConditional(CtConditional<T> e) {
List<CtElement> elements = new ArrayList<CtElement>();
elements.add(e.getElseExpression());
elements.add(e.getThenExpression());
elements.add(e.getCondition());
addCommentToNear(comment, elements);
}

@Override
public <T> void visitCtBinaryOperator(CtBinaryOperator<T> e) {
List<CtElement> elements = new ArrayList<CtElement>();
Expand Down
28 changes: 26 additions & 2 deletions src/test/java/spoon/test/comment/CommentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
import org.junit.Test;
import spoon.Launcher;
import spoon.reflect.code.CtComment;
import spoon.reflect.code.CtConditional;
import spoon.reflect.code.CtConstructorCall;
import spoon.reflect.code.CtDo;
import spoon.reflect.code.CtFor;
import spoon.reflect.code.CtIf;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.code.CtLocalVariable;
import spoon.reflect.code.CtNewArray;
import spoon.reflect.code.CtReturn;
import spoon.reflect.code.CtSwitch;
import spoon.reflect.code.CtSynchronized;
import spoon.reflect.code.CtTry;
import spoon.reflect.declaration.CtAnonymousExecutable;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtConstructor;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtParameter;
Expand All @@ -27,6 +30,7 @@
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class CommentTest {
Expand Down Expand Up @@ -69,14 +73,15 @@ public void testInLineComment() {

List<CtComment> comments = type.getElements(new TypeFilter<CtComment>(CtComment.class));
// verify that the number of comment present in the AST is correct
assertEquals(50, comments.size());
assertEquals(57, comments.size());

// verify that all comments present in the AST is printed
for (CtComment comment : comments) {
if (comment.getCommentType() == CtComment.CommentType.FILE) {
// the header of the file is not printed with the toString
continue;
}
assertNotNull(comment.getParent());
assertTrue(comment.toString() + ":" + comment.getParent() + " is not printed", strType.contains(comment.toString()));
}

Expand Down Expand Up @@ -197,7 +202,25 @@ public void testInLineComment() {
+ " // comment in synchronized" + newLine
+ "}", ctSynchronized.toString());

CtReturn ctReturn = m1.getBody().getStatement(10);
CtLocalVariable ctLocalVariable1 = m1.getBody().getStatement(10);
CtConditional ctConditional = (CtConditional) ctLocalVariable1.getDefaultExpression();
assertEquals(createFakeComment(f, "comment after condition CtConditional"), ctConditional.getCondition().getComments().get(0));
assertEquals(createFakeComment(f, "comment before then CtConditional"), ctConditional.getThenExpression().getComments().get(0));
assertEquals(createFakeComment(f, "comment after then CtConditional"), ctConditional.getThenExpression().getComments().get(1));
assertEquals(createFakeComment(f, "comment before else CtConditional"), ctConditional.getElseExpression().getComments().get(0));
assertEquals(createFakeComment(f, "comment after else CtConditional"), ctLocalVariable1.getComments().get(0));
assertEquals("java.lang.Double dou = i == 1// comment after condition CtConditional" + newLine
+ " ? // comment before then CtConditional" + newLine
+ "null// comment after then CtConditional" + newLine
+ " : // comment before else CtConditional" + newLine
+ "new java.lang.Double((j / ((double)((i - 1)))))", ctLocalVariable1.toString());

CtNewArray ctNewArray = (CtNewArray) ((CtLocalVariable) m1.getBody().getStatement(11)).getDefaultExpression();
CtElement arrayValue = (CtElement) ctNewArray.getElements().get(0);
assertEquals(createFakeComment(f, "comment before array value"), arrayValue.getComments().get(0));
assertEquals(createFakeComment(f, "comment after array value"), arrayValue.getComments().get(1));

CtReturn ctReturn = m1.getBody().getStatement(12);
assertEquals(createFakeComment(f, "comment return"), ctReturn.getComments().get(0));
assertEquals("// comment return" + newLine
+ "return ", ctReturn.toString());
Expand Down Expand Up @@ -239,6 +262,7 @@ public void testBlockComment() {
// the header of the file is not printed with the toString
continue;
}
assertNotNull(comment.getParent());
assertTrue(comment.toString() + ":" + comment.getParent() + " is not printed", strType.contains(comment.toString()));
}

Expand Down
14 changes: 14 additions & 0 deletions src/test/java/spoon/test/comment/testclasses/InlineComment.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ public void m1() {
// comment in synchronized
}

Double dou =
(i == 1) // comment after condition CtConditional
?
// comment before then CtConditional
null // comment after then CtConditional
:
// comment before else CtConditional
new Double(j / (double) (i - 1)); // comment after else CtConditional
int[] arr = new int[] {
// comment before array value
1, // comment after array value
2,
3
};
// comment return
return;
}
Expand Down