Skip to content

Commit

Permalink
Don't call ExecutionContext#putMessage()
Browse files Browse the repository at this point in the history
Avoid calling `ExecutionContext#putMessage()` and instead use cursor message passing.
  • Loading branch information
knutwannheden committed Nov 23, 2023
1 parent e9190fa commit d9c38f1
Showing 1 changed file with 7 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,28 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
MethodMatcher enumEquals = new MethodMatcher("java.lang.Enum equals(java.lang.Object)");
return Preconditions.check(new UsesMethod<>(enumEquals), new JavaVisitor<ExecutionContext>() {
@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;
Expand Down

0 comments on commit d9c38f1

Please sign in to comment.