Skip to content

Commit

Permalink
Some comments
Browse files Browse the repository at this point in the history
Co-authored-by: Hannes Greule <SirYwell@users.noreply.github.com>
  • Loading branch information
2 people authored and algomaster99 committed Nov 17, 2022
1 parent b82443b commit 5579eec
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
32 changes: 25 additions & 7 deletions src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import spoon.reflect.code.CtTypeAccess;
import spoon.reflect.code.CtTypePattern;
import spoon.reflect.code.CtUnaryOperator;
import spoon.reflect.code.CtVariableAccess;
import spoon.reflect.code.CtVariableRead;
import spoon.reflect.code.CtVariableWrite;
import spoon.reflect.code.CtWhile;
Expand Down Expand Up @@ -296,7 +297,7 @@ protected void enterCtExpression(CtExpression<?> e) {
elementPrinterHelper.writeComment(e, CommentOffset.BEFORE);
}
getPrinterHelper().mapLine(e, sourceCompilationUnit);
if (shouldSetBracket(e)) {
if (shouldSetBracketAroundExpressionAndCast(e)) {
context.parenthesedExpression.push(e);
printer.writeSeparator("(");
}
Expand All @@ -305,12 +306,29 @@ protected void enterCtExpression(CtExpression<?> e) {
printer.writeSeparator("(");
scan(r);
printer.writeSeparator(")").writeSpace();
if (!isMinimizeRoundBrackets()) {
printer.writeSeparator("(");
context.parenthesedExpression.push(e);
}
}
if (shouldSetBracketAroundCastTarget(e)) {
printer.writeSeparator("(");
context.parenthesedExpression.push(e);
}
}
}

private boolean shouldSetBracketAroundCastTarget(CtExpression<?> expr) {
if (!isMinimizeRoundBrackets()) {
return true;
}

if (expr instanceof CtTargetedExpression) {
return false;
}
if (expr instanceof CtLiteral) {
return false;
}
if (expr instanceof CtVariableAccess) {
return false;
}
return true;
}

/**
Expand Down Expand Up @@ -401,12 +419,12 @@ public DefaultJavaPrettyPrinter scan(CtElement e) {
return this;
}

private boolean shouldSetBracket(CtExpression<?> e) {
private boolean shouldSetBracketAroundExpressionAndCast(CtExpression<?> e) {
if (isMinimizeRoundBrackets()) {
RoundBracketAnalyzer.EncloseInRoundBrackets requiresBrackets =
RoundBracketAnalyzer.requiresRoundBrackets(e);
if (requiresBrackets != RoundBracketAnalyzer.EncloseInRoundBrackets.UNKNOWN) {
return requiresBrackets == RoundBracketAnalyzer.EncloseInRoundBrackets.YES && e.getTypeCasts().isEmpty();
return requiresBrackets == RoundBracketAnalyzer.EncloseInRoundBrackets.YES || !e.getTypeCasts().isEmpty();
}
if (e.isParentInitialized() && e.getParent() instanceof CtTargetedExpression && ((CtTargetedExpression) e.getParent()).getTarget() == e) {
return e instanceof CtVariableRead<?> && !e.getTypeCasts().isEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ public class DefaultJavaPrettyPrinterTest {
"1 | 2 & 3",
"(1 | 2) & 3",
"1 | 2 ^ 3",
"(1 | 2) ^ 3"
"(1 | 2) ^ 3",
"((int) (1 + 2)) * 3",
"(int) (int) (1 + 1)",
})
public void testParenOptimizationCorrectlyPrintsParenthesesForExpressions(String rawExpression) {
// contract: When input expressions are minimally parenthesized, pretty-printed output
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/spoon/test/eval/EvalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void testDoNotSimplifyCasts() throws Exception {
CtBlock<?> b = type.getMethodsByName("testDoNotSimplifyCasts").get(0).getBody();
assertEquals(1, b.getStatements().size());
b = b.partiallyEvaluate();
assertEquals("return ((U) ((java.lang.Object) (spoon.test.eval.testclasses.ToEvaluate.castTarget(element).getClass())))", b.getStatements().get(0).toString());
assertEquals("return ((U) (java.lang.Object) (spoon.test.eval.testclasses.ToEvaluate.castTarget(element).getClass()))", b.getStatements().get(0).toString());
}

@Test
Expand Down

0 comments on commit 5579eec

Please sign in to comment.