Skip to content

Commit 4ae2ddc

Browse files
author
emmanue1
committed
Optimize ternary operator
1 parent 9cfcf7d commit 4ae2ddc

File tree

3 files changed

+20
-23
lines changed

3 files changed

+20
-23
lines changed

src/main/java/org/jd/core/v1/model/token/TextToken.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class TextToken implements Token {
3838
public static final TextToken SEMICOLON_SPACE = new TextToken("; ");
3939
public static final TextToken VARARGS = new TextToken("... ");
4040
public static final TextToken VERTICALLINE = new TextToken("|");
41+
public static final TextToken EXCLAMATION = new TextToken("!");
4142

4243
protected final String text;
4344

src/main/java/org/jd/core/v1/service/fragmenter/javasyntaxtojavafragment/visitor/ExpressionVisitor.java

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -558,44 +558,40 @@ public void visit(SuperExpression expression) {
558558
public void visit(TernaryOperatorExpression expression) {
559559
tokens.addLineNumberToken(expression.getCondition());
560560

561-
if (expression.getCondition().getPriority() > 3) {
562-
tokens.add(TextToken.LEFTROUNDBRACKET);
563-
expression.getCondition().accept(this);
564-
tokens.add(TextToken.RIGHTROUNDBRACKET);
565-
} else {
566-
expression.getCondition().accept(this);
567-
}
568-
569-
if ((expression.getType() == PrimitiveType.TYPE_BOOLEAN) &&
570-
(expression.getExpressionTrue().getClass() == BooleanExpression.class) &&
561+
if ((expression.getExpressionTrue().getClass() == BooleanExpression.class) &&
571562
(expression.getExpressionFalse().getClass() == BooleanExpression.class)) {
572563

573564
BooleanExpression be1 = (BooleanExpression)expression.getExpressionTrue();
574565
BooleanExpression be2 = (BooleanExpression)expression.getExpressionFalse();
575566

576567
if (be1.isTrue() && be2.isFalse()) {
568+
printTernaryOperatorExpression(expression.getCondition());
577569
return;
578570
}
579-
}
580-
581-
tokens.add(TextToken.SPACE_QUESTION_SPACE);
582571

583-
if (expression.getExpressionTrue().getPriority() > 3) {
584-
tokens.add(TextToken.LEFTROUNDBRACKET);
585-
expression.getExpressionTrue().accept(this);
586-
tokens.add(TextToken.RIGHTROUNDBRACKET);
587-
} else {
588-
expression.getExpressionTrue().accept(this);
572+
if (be1.isFalse() && be2.isTrue()) {
573+
tokens.add(TextToken.EXCLAMATION);
574+
tokens.add(TextToken.LEFTROUNDBRACKET);
575+
expression.getCondition().accept(this);
576+
tokens.add(TextToken.RIGHTROUNDBRACKET);
577+
return;
578+
}
589579
}
590580

581+
printTernaryOperatorExpression(expression.getCondition());
582+
tokens.add(TextToken.SPACE_QUESTION_SPACE);
583+
printTernaryOperatorExpression(expression.getExpressionTrue());
591584
tokens.add(TextToken.SPACE_COLON_SPACE);
585+
printTernaryOperatorExpression(expression.getExpressionFalse());
586+
}
592587

593-
if (expression.getExpressionFalse().getPriority() > 3) {
588+
protected void printTernaryOperatorExpression(Expression expression) {
589+
if (expression.getPriority() > 3) {
594590
tokens.add(TextToken.LEFTROUNDBRACKET);
595-
expression.getExpressionFalse().accept(this);
591+
expression.accept(this);
596592
tokens.add(TextToken.RIGHTROUNDBRACKET);
597593
} else {
598-
expression.getExpressionFalse().accept(this);
594+
expression.accept(this);
599595
}
600596
}
601597

src/test/java/org/jd/core/v1/ClassFileToJavaSourceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,7 @@ public void testJdk118TernaryOperator() throws Exception {
10511051
assertTrue(source.matches(PatternMaker.make(": 15]", "\"1\"")));
10521052
assertTrue(source.matches(PatternMaker.make(": 16]", "\"2\";")));
10531053
assertTrue(source.matches(PatternMaker.make(": 24]", "s = (s == null) ? ((s == null) ? \"1\" : \"2\") : ((s == null) ? \"3\" : \"4\");")));
1054-
assertTrue(source.matches(PatternMaker.make(": 34]", "return (s != s || time < time) ? false : true;")));
1054+
assertTrue(source.matches(PatternMaker.make(": 34]", "return !(s != s || time < time);")));
10551055
assertTrue(source.matches(PatternMaker.make(": 40]", "if ((s1 == null) ? (s2 == null) : s1.equals(s2))")));
10561056
assertTrue(source.matches(PatternMaker.make(": 60]", "if ((s1 == null) ? (s2 == null) : s1.equals(s2))")));
10571057
assertTrue(source.matches(PatternMaker.make(": 71]", "if ((s1 == null) ? false : (s1.length() > 0))")));

0 commit comments

Comments
 (0)