Skip to content

Commit

Permalink
Add getKeyword() to J.Binary.Type and J.Unary.Type
Browse files Browse the repository at this point in the history
Similar to #2630 but without adding any Java fields which would increase the size of the LST.
  • Loading branch information
knutwannheden committed Feb 20, 2023
1 parent bc7aa0b commit b89ad00
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 127 deletions.
92 changes: 8 additions & 84 deletions rewrite-java/src/main/java/org/openrewrite/java/JavaPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,66 +251,7 @@ public J visitAssignmentOperation(AssignmentOperation assignOp, PrintOutputCaptu

@Override
public J visitBinary(Binary binary, PrintOutputCapture<P> p) {
String keyword = "";
switch (binary.getOperator()) {
case Addition:
keyword = "+";
break;
case Subtraction:
keyword = "-";
break;
case Multiplication:
keyword = "*";
break;
case Division:
keyword = "/";
break;
case Modulo:
keyword = "%";
break;
case LessThan:
keyword = "<";
break;
case GreaterThan:
keyword = ">";
break;
case LessThanOrEqual:
keyword = "<=";
break;
case GreaterThanOrEqual:
keyword = ">=";
break;
case Equal:
keyword = "==";
break;
case NotEqual:
keyword = "!=";
break;
case BitAnd:
keyword = "&";
break;
case BitOr:
keyword = "|";
break;
case BitXor:
keyword = "^";
break;
case LeftShift:
keyword = "<<";
break;
case RightShift:
keyword = ">>";
break;
case UnsignedRightShift:
keyword = ">>>";
break;
case Or:
keyword = "||";
break;
case And:
keyword = "&&";
break;
}
String keyword = binary.getOperator().getKeyword();
beforeSyntax(binary, Space.Location.BINARY_PREFIX, p);
visit(binary.getLeft(), p);
visitSpace(binary.getPadding().getOperator().getBefore(), Space.Location.BINARY_OPERATOR, p);
Expand Down Expand Up @@ -1012,39 +953,22 @@ public J visitUnary(Unary unary, PrintOutputCapture<P> p) {
beforeSyntax(unary, Space.Location.UNARY_PREFIX, p);
switch (unary.getOperator()) {
case PreIncrement:
p.append("++");
visit(unary.getExpression(), p);
break;
case PreDecrement:
p.append("--");
case Positive:
case Negative:
case Complement:
case Not:
p.append(unary.getOperator().getKeyword());
visit(unary.getExpression(), p);
break;
case PostIncrement:
visit(unary.getExpression(), p);
visitSpace(unary.getPadding().getOperator().getBefore(), Space.Location.UNARY_OPERATOR, p);
p.append("++");
break;
case PostDecrement:
visit(unary.getExpression(), p);
visitSpace(unary.getPadding().getOperator().getBefore(), Space.Location.UNARY_OPERATOR, p);
p.append("--");
p.append(unary.getOperator().getKeyword());
break;
case Positive:
p.append("+");
visit(unary.getExpression(), p);
break;
case Negative:
p.append("-");
visit(unary.getExpression(), p);
break;
case Complement:
p.append("~");
visit(unary.getExpression(), p);
break;
case Not:
default:
p.append("!");
visit(unary.getExpression(), p);
throw new IllegalArgumentException("Unknown unary operator: " + unary.getOperator());
}
afterSyntax(unary, p);
return unary;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,48 +169,7 @@ private String labelDescription(J j) {
private static String labelTag(J j) {
if (j instanceof J.Binary) {
J.Binary binary = (J.Binary) j;
switch (binary.getOperator()) {
case And:
return "&&";
case Or:
return "||";
case Addition:
return "+";
case Subtraction:
return "-";
case Multiplication:
return "*";
case Division:
return "/";
case Modulo:
return "%";
case LessThan:
return "<";
case LessThanOrEqual:
return "<=";
case GreaterThan:
return ">";
case GreaterThanOrEqual:
return ">=";
case Equal:
return "==";
case NotEqual:
return "!=";
case BitAnd:
return "&";
case BitOr:
return "|";
case BitXor:
return "^";
case LeftShift:
return "<<";
case RightShift:
return ">>";
case UnsignedRightShift:
return ">>>";
default:
throw new IllegalStateException("Unexpected value: " + binary.getOperator());
}
return binary.getOperator().getKeyword();
}
return null;
}
Expand Down
48 changes: 47 additions & 1 deletion rewrite-java/src/main/java/org/openrewrite/java/tree/J.java
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,32 @@ public enum Type {
RightShift,
UnsignedRightShift,
Or,
And
And;

public String getKeyword() {
switch (this) {
case Addition: return "+";
case Subtraction: return "-";
case Multiplication: return "*";
case Division: return "/";
case Modulo: return "%";
case LessThan: return "<";
case GreaterThan: return ">";
case LessThanOrEqual: return "<=";
case GreaterThanOrEqual: return ">=";
case Equal: return "==";
case NotEqual: return "!=";
case BitAnd: return "&";
case BitOr: return "|";
case BitXor: return "^";
case LeftShift: return "<<";
case RightShift: return ">>";
case UnsignedRightShift: return ">>>";
case Or: return "||";
case And: return "&&";
default: throw new IllegalStateException("Unexpected value: " + this);
}
}
}

public Padding getPadding() {
Expand Down Expand Up @@ -5332,6 +5357,27 @@ public boolean isModifying() {
return false;
}
}

public String getKeyword() {
switch (this) {
case PreIncrement:
case PostIncrement:
return "++";
case PreDecrement:
case PostDecrement:
return "--";
case Positive:
return "+";
case Negative:
return "-";
case Complement:
return "~";
case Not:
return "!";
default:
throw new IllegalStateException("Unexpected value: " + this);
}
}
}

public Padding getPadding() {
Expand Down

0 comments on commit b89ad00

Please sign in to comment.