Skip to content

Commit b3434f6

Browse files
Document JavaParser.Builder classpath options (#5926)
* Document `JavaParser.Builder` classpath options * Update reference specifically for builder methods * Mark `classpath(byte[]... classpath)` as deprecated * Apply suggestions from code review Co-authored-by: Jacob van Lingen <[email protected]> --------- Co-authored-by: Jacob van Lingen <[email protected]>
1 parent 380c942 commit b3434f6

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

rewrite-java/src/main/java/org/openrewrite/java/JavaParser.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.intellij.lang.annotations.Language;
2020
import org.jspecify.annotations.Nullable;
2121
import org.openrewrite.*;
22+
import org.openrewrite.internal.ToBeRemoved;
2223
import org.openrewrite.java.internal.JavaTypeCache;
2324
import org.openrewrite.java.internal.parser.JavaParserClasspathLoader;
2425
import org.openrewrite.java.internal.parser.RewriteClasspathJarClasspathLoader;
@@ -111,6 +112,15 @@ static List<Path> filterArtifacts(String artifactName, List<Path> runtimeClasspa
111112
return artifacts;
112113
}
113114

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+
*/
114124
static List<Path> dependenciesFromResources(ExecutionContext ctx, String... artifactNamesWithVersions) {
115125
if (artifactNamesWithVersions.length == 0) {
116126
return emptyList();
@@ -304,19 +314,43 @@ public B addClasspathEntry(Path entry) {
304314
return (B) this;
305315
}
306316

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+
*/
307327
public B classpath(String... artifactNames) {
308328
this.artifactNames = Arrays.asList(artifactNames);
309329
this.classpath = emptyList();
310330
return (B) this;
311331
}
312332

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+
*/
313342
@SuppressWarnings({"UnusedReturnValue", "unused"})
314343
public B classpathFromResources(ExecutionContext ctx, String... classpath) {
315344
this.artifactNames = emptyList();
316345
this.classpath = dependenciesFromResources(ctx, classpath);
317346
return (B) this;
318347
}
319348

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.")
320354
public B classpath(byte[]... classpath) {
321355
this.classBytesClasspath = Arrays.asList(classpath);
322356
return (B) this;

0 commit comments

Comments
 (0)