From fd37bfc914475018bd377b55ccb94e37700b3de3 Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Mon, 20 Feb 2023 15:16:26 +0100 Subject: [PATCH] Add `getKeyword()` to `J.Binary`, `J.Unary`, and `J.AssignmentOperation` Similar to #2630 but without adding any Java fields which would increase the size of the LST. --- .../org/openrewrite/java/JavaPrinter.java | 131 ++---------------- .../ControlFlowVisualizationVisitor.java | 43 +----- .../java/org/openrewrite/java/tree/J.java | 79 ++++++++++- 3 files changed, 87 insertions(+), 166 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 267eb00e88d..58f1adb7906 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/JavaPrinter.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/JavaPrinter.java @@ -204,46 +204,10 @@ public J visitAssignment(Assignment assignment, PrintOutputCapture

p) { @Override public J visitAssignmentOperation(AssignmentOperation assignOp, PrintOutputCapture

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; @@ -251,70 +215,10 @@ 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; - } 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; @@ -1012,39 +916,22 @@ public J visitUnary(Unary unary, PrintOutputCapture

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; 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 574be0e7bc4..e1e2869622e 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 @@ -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() { @@ -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() { @@ -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() {