From d9c38f1ccb3f8dd4af29cd3d2e5e1d93d8af72bf Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Thu, 23 Nov 2023 21:26:13 +0100 Subject: [PATCH] Don't call `ExecutionContext#putMessage()` Avoid calling `ExecutionContext#putMessage()` and instead use cursor message passing. --- .../CompareEnumsWithEqualityOperator.java | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/openrewrite/staticanalysis/CompareEnumsWithEqualityOperator.java b/src/main/java/org/openrewrite/staticanalysis/CompareEnumsWithEqualityOperator.java index 1e95fcca9..a8a1d3221 100644 --- a/src/main/java/org/openrewrite/staticanalysis/CompareEnumsWithEqualityOperator.java +++ b/src/main/java/org/openrewrite/staticanalysis/CompareEnumsWithEqualityOperator.java @@ -53,30 +53,28 @@ public TreeVisitor getVisitor() { MethodMatcher enumEquals = new MethodMatcher("java.lang.Enum equals(java.lang.Object)"); return Preconditions.check(new UsesMethod<>(enumEquals), new JavaVisitor() { @Override - public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) { - J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, executionContext); + public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { + J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, ctx); if (enumEquals.matches(m) && m.getSelect() != null) { Cursor parent = getCursor().dropParentUntil(is -> is instanceof J.Unary || is instanceof J.Block); boolean isNot = parent.getValue() instanceof J.Unary && ((J.Unary) parent.getValue()).getOperator() == J.Unary.Type.Not; if (isNot) { - executionContext.putMessage("REMOVE_UNARY_NOT", parent.getValue()); + parent.putMessage("REMOVE_UNARY_NOT", parent.getValue()); } String code = "#{any()} " + (isNot ? "!=" : "==") + " #{any()}"; return autoFormat(JavaTemplate .builder(code) - .contextSensitive() .build() - .apply(updateCursor(m), m.getCoordinates().replace(), m.getSelect(), m.getArguments().get(0)), executionContext); + .apply(updateCursor(m), m.getCoordinates().replace(), m.getSelect(), m.getArguments().get(0)), ctx); } return m; } @Override - public J visitUnary(J.Unary unary, ExecutionContext executionContext) { - J j = super.visitUnary(unary, executionContext); + public J visitUnary(J.Unary unary, ExecutionContext ctx) { + J j = super.visitUnary(unary, ctx); J.Unary asUnary = (J.Unary) j; - if (executionContext.getMessage("REMOVE_UNARY_NOT") instanceof J.Unary && - asUnary.equals(executionContext.getMessage("REMOVE_UNARY_NOT"))) { + if (asUnary.equals(getCursor().pollMessage("REMOVE_UNARY_NOT"))) { return asUnary.getExpression().unwrap().withPrefix(asUnary.getPrefix()); } return j;