openrewrite / rewrite-static-analysis

OpenRewrite recipes for identifying and fixing static analysis issues.
Apache License 2.0
27 stars 43 forks source link

ReplaceLambdaWithMethodReference attempts to transform lambda with multiple statements and JavaTemplate error #21

Open pway99 opened 1 year ago

pway99 commented 1 year ago

Problem

ReplaceLambdaWithMethodReference should not transform lamda having multiple statements

    private void respondToProduce(TopicPartition tp, Errors error, long offset) {
        client.respond(
            request -> request instanceof ProduceRequest,
            produceResponse(tp, offset, error, 0)
        );

    }

https://github.com/apache/kafka/blob/bc95aa21169b8b5b9b8a4b609e88cc125157234b/clients/src/test/java/org/apache/kafka/clients/producer/internals/SenderTest.java#L2860-L2866

Example diff


private void respondToProduce(TopicPartition tp, Errors error, long offset) {
  client.respond(
-            request -> request instanceof ProduceRequest,
+            /*~~(Recipe failed with an exception.
+java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
+  java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
+  java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
+  java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)
+  java.base/java.util.Objects.checkIndex(Objects.java:359)
+  java.base/java.util.ArrayList.get(ArrayList.java:427)
+  org.openrewrite.java.internal.template.JavaTemplateParser.compileTemplate(JavaTemplateParser.java:247)
+  org.openrewrite.java.internal.template.JavaTemplateParser.parseBlockStatements(JavaTemplateParser.java:166)
+  org.openrewrite.java.JavaTemplate$2.maybeReplaceStatement(JavaTemplate.java:482)
+  ...)~~>*/request -> request instanceof ProduceRequest,
      produceResponse(tp, offset, error, 0)
  );

also check this diff:

      context.deliverRequest(context.fetchRequest(epoch, voter2, 1L, epoch, 500));
context.client.poll();
assertTrue(context.channel.drainSendQueue().stream()
-            .noneMatch(msg -> msg.data() instanceof FetchResponseData));
+            .noneMatch(/*~~(Recipe failed with an exception.
+java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
+  java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
+  java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
+  java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)
+  java.base/java.util.Objects.checkIndex(Objects.java:359)
+  java.base/java.util.ArrayList.get(ArrayList.java:427)
+  org.openrewrite.java.internal.template.JavaTemplateParser.compileTemplate(JavaTemplateParser.java:247)
+  org.openrewrite.java.internal.template.JavaTemplateParser.parseBlockStatements(JavaTemplateParser.java:166)
+  org.openrewrite.java.JavaTemplate$2.maybeReplaceStatement(JavaTemplate.java:482)
+  ...)~~>*/msg -> msg.data() instanceof FetchResponseData));

// Now we get a BeginEpoch from the other voter and become a follower
context.deliverRequest(context.beginEpochRequest(epoch + 1, voter3));


#### Recipes in example diff:
- `org.openrewrite.java.cleanup.ReplaceLambdaWithMethodReference`

### References:
- [View original result](https://public.moderne.io/results/1osvJ/details/eyJfX3R5cGVuYW1lIjoiR2l0SHViUmVwb3NpdG9yeSIsImlkIjoiR2l0SHViUmVwb3NpdG9yeX5+Z2l0aHViLmNvbX5+YXBhY2hlL2thZmthfn50cnVuayIsIm9yaWdpbiI6ImdpdGh1Yi5jb20iLCJwYXRoIjoiYXBhY2hlL2thZmthIiwiYnJhbmNoIjoidHJ1bmsiLCJvcmdhbml6YXRpb24iOiJhcGFjaGUiLCJuYW1lIjoia2Fma2EiLCJpbmdlc3RlZCI6bnVsbH0=)
- Recipe ID: `org.openrewrite.java.cleanup.ReplaceLambdaWithMethodReference`
- Recipe Name: `undefined`
- Repository: `apache/kafka/trunk`
- Created at Thu Oct 13 2022 10:35:30 GMT-0700 (Pacific Daylight Time)
koppor commented 1 year ago

Similar issue here:

-                           .filtered(Bindings.createObjectBinding(() -> this::showNode, filter));
+                           .filtered(Bindings.createObjectBinding(this::/*__TEMPLATE_STOP__*/
+                <error>, filter));