openrewrite / rewrite

Automated mass refactoring of source code.
https://docs.openrewrite.org
Apache License 2.0
2.09k stars 312 forks source link

BlockStatementTemplateGenerator templated lambda stub does not compile #2224

Closed pway99 closed 1 year ago

pway99 commented 1 year ago

Problem

Issue discovered with src/main/java/org/springframework/data/repository/query/Parameters.java

Expected behavior

Describe what you expected to see.

Example diff

        this.pageableIndex = pageableIndexTemp;
        this.sortIndex = sortIndexTemp;
        this.dynamicProjectionIndex = dynamicProjectionTemp;
-       this.bindable = Lazy.of(() -> (S) this);
+       this.bindable = Lazy.of(/*~~(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)
+  ...)~~>*/() -> (S) this);
    }

    private S getBindable() {

Recipes in example diff:

pway99 commented 1 year ago

This issue is most likely caused by a large number of type related issues associated with a rather large stub.

The snippet below from the Parameters file referenced in this issue works fine in the ReplaceLambdaWithMethodReferenceTest.

import java.util.function.Supplier;
class A<S> {
    class Lazy<T> implements Supplier<T> { }
    Lazy binder;
    void m() {
        this.binder = Lazy.of(() -> (S) this);
    }
}