Skip to content

Commit

Permalink
Add getKeyword() to J.Binary, J.Unary, and J.AssignmentOperation
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 fd37bfc
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 166 deletions.
131 changes: 9 additions & 122 deletions rewrite-java/src/main/java/org/openrewrite/java/JavaPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,117 +204,21 @@ public J visitAssignment(Assignment assignment, PrintOutputCapture<P> p) {

@Override
public J visitAssignmentOperation(AssignmentOperation assignOp, PrintOutputCapture<P> p) {
String keyword = "";
switch (assignOp.getOperator()) {
case Addition:
keyword = "+=";
break;
case Subtraction:
keyword = "-=";
break;
case Multiplication:
keyword = "*=";
break;
case Division:
keyword = "/=";
break;
case Modulo:
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;
}
beforeSyntax(assignOp, Space.Location.ASSIGNMENT_OPERATION_PREFIX, p);
visit(assignOp.getVariable(), p);
visitSpace(assignOp.getPadding().getOperator().getBefore(), Space.Location.ASSIGNMENT_OPERATION_OPERATOR, p);
p.append(keyword);
p.append(assignOp.getOperator().getKeyword());
visit(assignOp.getAssignment(), p);
afterSyntax(assignOp, p);
return assignOp;
}

@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;
}
beforeSyntax(binary, Space.Location.BINARY_PREFIX, p);
visit(binary.getLeft(), p);
visitSpace(binary.getPadding().getOperator().getBefore(), Space.Location.BINARY_OPERATOR, p);
p.append(keyword);
p.append(binary.getOperator().getKeyword());
visit(binary.getRight(), p);
afterSyntax(binary, p);
return binary;
Expand Down Expand Up @@ -1012,39 +916,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("--");
break;
case Positive:
p.append("+");
visit(unary.getExpression(), p);
break;
case Negative:
p.append("-");
visit(unary.getExpression(), p);
p.append(unary.getOperator().getKeyword());
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
79 changes: 77 additions & 2 deletions rewrite-java/src/main/java/org/openrewrite/java/tree/J.java
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,36 @@ public enum Type {
Multiplication,
RightShift,
Subtraction,
UnsignedRightShift
UnsignedRightShift;

public String getKeyword() {
switch (this) {
case Addition:
return "+=";
case Subtraction:
return "-=";
case Multiplication:
return "*=";
case Division:
return "/=";
case Modulo:
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: " + this);
}
}
}

public Padding getPadding() {
Expand Down Expand Up @@ -667,7 +696,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 +5386,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 fd37bfc

Please sign in to comment.