rewrite-static-analysis
rewrite-static-analysis copied to clipboard
ReplaceLambdaWithMethodReference attempts to transform lambda with multiple statements and JavaTemplate error
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)
Similar issue here:
- .filtered(Bindings.createObjectBinding(() -> this::showNode, filter));
+ .filtered(Bindings.createObjectBinding(this::/*__TEMPLATE_STOP__*/
+ <error>, filter));