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

fixed(prettyprint): parentheses are missing when pretty-pretting some ternary expressions #927

Merged
merged 3 commits into from
Nov 3, 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
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());
}
}