|
19 | 19 | import org.intellij.lang.annotations.Language; |
20 | 20 | import org.jspecify.annotations.Nullable; |
21 | 21 | import org.openrewrite.*; |
| 22 | +import org.openrewrite.internal.ToBeRemoved; |
22 | 23 | import org.openrewrite.java.internal.JavaTypeCache; |
23 | 24 | import org.openrewrite.java.internal.parser.JavaParserClasspathLoader; |
24 | 25 | import org.openrewrite.java.internal.parser.RewriteClasspathJarClasspathLoader; |
@@ -111,6 +112,15 @@ static List<Path> filterArtifacts(String artifactName, List<Path> runtimeClasspa |
111 | 112 | return artifacts; |
112 | 113 | } |
113 | 114 |
|
| 115 | + /** |
| 116 | + * Load artifacts from packaged resources. This is useful for loading dependencies which are not on the recipe |
| 117 | + * execution classpath, or where you need to load multiple different versions of the same artifact. |
| 118 | + * Supports both {@link TypeTable} `classpath.tsv.gz` and packaged resource jars in `META-INF/rewrite/classpath/`. |
| 119 | + * |
| 120 | + * @param ctx The execution context to use for loading resources. |
| 121 | + * @param artifactNamesWithVersions artifact prefix to match, e.g. "guava" or "guava-31" for a specific version. |
| 122 | + * @return A list of paths to the located artifacts. |
| 123 | + */ |
114 | 124 | static List<Path> dependenciesFromResources(ExecutionContext ctx, String... artifactNamesWithVersions) { |
115 | 125 | if (artifactNamesWithVersions.length == 0) { |
116 | 126 | return emptyList(); |
@@ -304,19 +314,43 @@ public B addClasspathEntry(Path entry) { |
304 | 314 | return (B) this; |
305 | 315 | } |
306 | 316 |
|
| 317 | + /** |
| 318 | + * Sets the classpath from runtime classpath dependencies of the process constructing the parser. Predates |
| 319 | + * {@link #classpathFromResources(ExecutionContext, String...)}, with the latter preferred to limit dependencies |
| 320 | + * needed on the recipe runtime classpath, as the runtime classpath may differ in say the CLI or Platform. |
| 321 | + * <p> |
| 322 | + * This is suitable, for example, when writing tests that may require older versions of dependencies, |
| 323 | + * without needing to add them to the recipe runtime classpath through resources. |
| 324 | + * |
| 325 | + * @return A list of paths to jars on the runtime classpath of the process constructing the parser. |
| 326 | + */ |
307 | 327 | public B classpath(String... artifactNames) { |
308 | 328 | this.artifactNames = Arrays.asList(artifactNames); |
309 | 329 | this.classpath = emptyList(); |
310 | 330 | return (B) this; |
311 | 331 | } |
312 | 332 |
|
| 333 | + /** |
| 334 | + * Load artifacts from packaged resources. This is useful for loading dependencies which are not on the recipe |
| 335 | + * execution classpath, or where you need to load multiple different versions of the same artifact. |
| 336 | + * Supports both {@link TypeTable} `classpath.tsv.gz` and packaged resource jars in `META-INF/rewrite/classpath/`. |
| 337 | + * |
| 338 | + * @param ctx The execution context to use for loading resources. |
| 339 | + * @param classpath artifact prefix to match, e.g. "guava" or "guava-31" for a specific version. |
| 340 | + * @return A list of paths to the located artifacts. |
| 341 | + */ |
313 | 342 | @SuppressWarnings({"UnusedReturnValue", "unused"}) |
314 | 343 | public B classpathFromResources(ExecutionContext ctx, String... classpath) { |
315 | 344 | this.artifactNames = emptyList(); |
316 | 345 | this.classpath = dependenciesFromResources(ctx, classpath); |
317 | 346 | return (B) this; |
318 | 347 | } |
319 | 348 |
|
| 349 | + /** |
| 350 | + * @deprecated prefer {@link #classpath} and {@link #classpathFromResources(ExecutionContext, String...)}. |
| 351 | + */ |
| 352 | + @Deprecated |
| 353 | + @ToBeRemoved(after = "2025-12-31", reason = "Use classpath or classpathFromResources instead.") |
320 | 354 | public B classpath(byte[]... classpath) { |
321 | 355 | this.classBytesClasspath = Arrays.asList(classpath); |
322 | 356 | return (B) this; |
|
0 commit comments