From 57be9a4d9f7c48d6186d93e9db1a5d9f5b8623ea Mon Sep 17 00:00:00 2001 From: Adriano Machado <60320+ammachado@users.noreply.github.com> Date: Fri, 23 Feb 2024 01:14:08 -0500 Subject: [PATCH] Fixing swallowed compilation errors due to package name / path mismatch when using `dependsOn`. --- .../src/main/java/org/openrewrite/java/JavaParser.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/rewrite-java/src/main/java/org/openrewrite/java/JavaParser.java b/rewrite-java/src/main/java/org/openrewrite/java/JavaParser.java index 3f8dc7b09ad..f7d63f65300 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/JavaParser.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/JavaParser.java @@ -356,7 +356,7 @@ public B dependsOn(Collection inputs) { public B dependsOn(@Language("java") String... inputsAsStrings) { this.dependsOn = Arrays.stream(inputsAsStrings) - .map(Input::fromString) + .map(input -> Input.fromString(resolveSourcePathFromSourceText(Paths.get(""), input), input)) .collect(toList()); return (B) this; } @@ -417,6 +417,10 @@ public Builder
clone() { @Override default Path sourcePathFromSourceText(Path prefix, String sourceCode) { + return resolveSourcePathFromSourceText(prefix, sourceCode); + } + + static Path resolveSourcePathFromSourceText(Path prefix, String sourceCode) { Pattern packagePattern = Pattern.compile("^package\\s+([^;]+);"); Pattern classPattern = Pattern.compile("(class|interface|enum|record)\\s*(<[^>]*>)?\\s+(\\w+)");