Skip to content

Commit

Permalink
Improve: Unify Representing J.Binary.Type printing (#2630)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLLeitschuh authored Jan 11, 2023
1 parent c0ea0ae commit 9da1345
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 124 deletions.
61 changes: 1 addition & 60 deletions rewrite-java/src/main/java/org/openrewrite/java/JavaPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,66 +254,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
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
47 changes: 25 additions & 22 deletions rewrite-java/src/main/java/org/openrewrite/java/tree/J.java
Original file line number Diff line number Diff line change
Expand Up @@ -648,26 +648,29 @@ public List<J> getSideEffects() {
return sideEffects;
}

@AllArgsConstructor(access = AccessLevel.PRIVATE)
public enum Type {
Addition,
Subtraction,
Multiplication,
Division,
Modulo,
LessThan,
GreaterThan,
LessThanOrEqual,
GreaterThanOrEqual,
Equal,
NotEqual,
BitAnd,
BitOr,
BitXor,
LeftShift,
RightShift,
UnsignedRightShift,
Or,
And
Addition("+"),
Subtraction("-"),
Multiplication("*"),
Division("/"),
Modulo("%"),
LessThan("<"),
GreaterThan(">"),
LessThanOrEqual("<="),
GreaterThanOrEqual(">="),
Equal("=="),
NotEqual("!="),
BitAnd("&"),
BitOr("|"),
BitXor("^"),
LeftShift("<<"),
RightShift(">>"),
UnsignedRightShift(">>>"),
Or("||"),
And("&&");
@Getter
final String keyword;
}

public Padding getPadding() {
Expand Down Expand Up @@ -2607,7 +2610,7 @@ private String getTypeName(J.FieldAccess type) {
String name = part.getSimpleName();
if (part.getTarget() instanceof J.Identifier) {
typeName.insert(0, ((Identifier) part.getTarget()).getSimpleName() +
"." + name);
"." + name);
break;
} else {
part = (FieldAccess) part.getTarget();
Expand Down Expand Up @@ -3427,8 +3430,8 @@ public List<J.Annotation> getAllAnnotations() {
@Override
public String toString() {
return "MethodDeclaration{" +
(getMethodType() == null ? "unknown" : getMethodType()) +
"}";
(getMethodType() == null ? "unknown" : getMethodType()) +
"}";
}

@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
Expand Down

0 comments on commit 9da1345

Please sign in to comment.