From 9da1345ef28b62d0304bda2200d860c27c9a81a0 Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Wed, 11 Jan 2023 13:18:47 -0500 Subject: [PATCH] Improve: Unify Representing J.Binary.Type printing (#2630) --- .../org/openrewrite/java/JavaPrinter.java | 61 +------------------ .../ControlFlowVisualizationVisitor.java | 43 +------------ .../java/org/openrewrite/java/tree/J.java | 47 +++++++------- 3 files changed, 27 insertions(+), 124 deletions(-) diff --git a/rewrite-java/src/main/java/org/openrewrite/java/JavaPrinter.java b/rewrite-java/src/main/java/org/openrewrite/java/JavaPrinter.java index f6ad07d2c57..ada2061f423 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/JavaPrinter.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/JavaPrinter.java @@ -254,66 +254,7 @@ public J visitAssignmentOperation(AssignmentOperation assignOp, PrintOutputCaptu @Override public J visitBinary(Binary binary, PrintOutputCapture

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); diff --git a/rewrite-java/src/main/java/org/openrewrite/java/controlflow/ControlFlowVisualizationVisitor.java b/rewrite-java/src/main/java/org/openrewrite/java/controlflow/ControlFlowVisualizationVisitor.java index 33bcdd51aaf..333a70d7265 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/controlflow/ControlFlowVisualizationVisitor.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/controlflow/ControlFlowVisualizationVisitor.java @@ -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; } diff --git a/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java b/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java index 1220cbe8f38..3768e7a6a3b 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java @@ -648,26 +648,29 @@ public List 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() { @@ -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(); @@ -3427,8 +3430,8 @@ public List getAllAnnotations() { @Override public String toString() { return "MethodDeclaration{" + - (getMethodType() == null ? "unknown" : getMethodType()) + - "}"; + (getMethodType() == null ? "unknown" : getMethodType()) + + "}"; } @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)