Skip to content

Commit ea61b2f

Browse files
committed
URLConstructorToURICreate: Don't fail on lambda parameters
1 parent 6d18cd7 commit ea61b2f

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/main/java/org/openrewrite/java/migrate/net/URLConstructorToURICreate.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.openrewrite.TreeVisitor;
2323
import org.openrewrite.analysis.constantfold.ConstantFold;
2424
import org.openrewrite.analysis.util.CursorUtil;
25+
import org.openrewrite.internal.RecipeRunException;
2526
import org.openrewrite.java.JavaParser;
2627
import org.openrewrite.java.JavaTemplate;
2728
import org.openrewrite.java.JavaVisitor;
@@ -86,9 +87,14 @@ public J visitNewClass(J.NewClass nc, ExecutionContext ctx) {
8687
} else if (arg instanceof J.Identifier &&
8788
TypeUtils.isOfType(arg.getType(), JavaType.Primitive.String)) {
8889
// find constant value of the identifier
89-
return CursorUtil.findCursorForTree(getCursor(), arg)
90-
.bind(c -> ConstantFold.findConstantLiteralValue(c, String.class))
91-
.toNull();
90+
try {
91+
return CursorUtil.findCursorForTree(getCursor(), arg)
92+
.bind(c -> ConstantFold.findConstantLiteralValue(c, String.class))
93+
.toNull();
94+
} catch (RecipeRunException e) {
95+
// `ConstantFold` does not support lambdas
96+
return null;
97+
}
9298
} else {
9399
// null indicates no path extractable
94100
return null;

src/test/java/org/openrewrite/java/migrate/net/URLConstructorToURICreateTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,25 @@ void urlConstructor() {
7979
);
8080
}
8181

82+
@Test
83+
void lambdaParameter() {
84+
rewriteRun(
85+
//language=java
86+
java(
87+
"""
88+
import java.net.URL;
89+
import java.util.function.Consumer;
90+
91+
class Test {
92+
Consumer<String> foo() {
93+
return (url) -> System.out.println(new URL(url));
94+
}
95+
}
96+
"""
97+
)
98+
);
99+
}
100+
82101
@Test
83102
void removeUrlImport() {
84103
rewriteRun(

0 commit comments

Comments
 (0)