Skip to content

Commit

Permalink
fixed(prettyprint): parentheses are missing when pretty-pretting some…
Browse files Browse the repository at this point in the history
… ternary expressions (#927)
  • Loading branch information
arnobl authored and monperrus committed Nov 3, 2016
1 parent 40eae7f commit e44ef7b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
import spoon.reflect.declaration.CtParameter;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.CtTypeParameter;
import spoon.reflect.declaration.CtVariable;
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.declaration.ParentNotInitializedException;
import spoon.reflect.reference.CtArrayTypeReference;
Expand Down Expand Up @@ -543,11 +544,12 @@ public <T> void visitCtConditional(CtConditional<T> conditional) {
if (!(condition instanceof CtStatement)) {
elementPrinterHelper.writeComment(condition, CommentOffset.BEFORE);
}
boolean parent = false;
boolean parent;
try {
parent = (conditional.getParent() instanceof CtAssignment);
parent = conditional.getParent() instanceof CtAssignment || conditional.getParent() instanceof CtVariable;
} catch (ParentNotInitializedException ex) {
// nothing if we have no parent
parent = false;
}
if (parent) {
printer.write("(");
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/spoon/test/comment/CommentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public void testInLineComment() {
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
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
import spoon.compiler.SpoonCompiler;
import spoon.compiler.SpoonResource;
import spoon.compiler.SpoonResourceHelper;
import spoon.reflect.code.CtCodeSnippetStatement;
import spoon.reflect.code.CtConstructorCall;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.code.CtStatement;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtTypeReference;
Expand All @@ -19,11 +20,11 @@
import spoon.reflect.visitor.Query;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.JavaOutputProcessor;
import spoon.support.compiler.SnippetCompilationError;
import spoon.test.prettyprinter.testclasses.AClass;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -285,4 +286,16 @@ public void importsFromMultipleTypesSupported() {
));
assertTrue(printer.getResult().contains("import java.util.ArrayList;"));
}

@Test
public void testTernaryParenthesesOnLocalVariable() {
// Spooning the code snippet
Launcher launcher = new Launcher();
CtCodeSnippetStatement snippet = launcher.getFactory().Code().createCodeSnippetStatement(
"final int foo = (new Object() instanceof Object ? new Object().equals(null) : new Object().equals(new Object())) ? 0 : new Object().hashCode();");
CtStatement compile = snippet.compile();
// Pretty-printing the Spooned code snippet and compiling the resulting code.
snippet = launcher.getFactory().Code().createCodeSnippetStatement(compile.toString());
assertEquals(compile, snippet.compile());
}
}

0 comments on commit e44ef7b

Please sign in to comment.