-
Notifications
You must be signed in to change notification settings - Fork 519
Expand file tree
/
Copy pathJavaParser.java
More file actions
495 lines (434 loc) · 20.7 KB
/
JavaParser.java
File metadata and controls
495 lines (434 loc) · 20.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
* Copyright 2020 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java;
import io.github.classgraph.ClassGraph;
import lombok.experimental.UtilityClass;
import org.intellij.lang.annotations.Language;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.internal.ToBeRemoved;
import org.openrewrite.java.internal.JavaTypeCache;
import org.openrewrite.java.internal.parser.RewriteClasspathJarClasspathLoader;
import org.openrewrite.java.internal.parser.TypeTable;
import org.openrewrite.java.marker.JavaSourceSet;
import org.openrewrite.java.tree.J;
import org.openrewrite.style.NamedStyles;
import java.io.ByteArrayInputStream;
import java.lang.reflect.Method;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
public interface JavaParser extends Parser {
/**
* Set to <code>true</code> on an {@link ExecutionContext} supplied to parsing to skip generation of
* type attribution from the class in {@link JavaSourceSet} marker.
*/
String SKIP_SOURCE_SET_TYPE_GENERATION = "org.openrewrite.java.skipSourceSetTypeGeneration";
static List<Path> runtimeClasspath() {
return RuntimeClasspathCache.getRuntimeClasspath();
}
/**
* Convenience utility for constructing a parser with binary dependencies on the runtime classpath of the process
* constructing the parser.
*
* @param artifactNames The "artifact name" of the dependency to look for. Artifact name is the artifact portion of
* group:artifact:version coordinates. For example, for Google's Guava (com.google.guava:guava:VERSION),
* the artifact name is "guava".
* @return A set of paths of jars on the runtime classpath matching the provided artifact names, to the extent such
* matching jars can be found.
*/
static List<Path> dependenciesFromClasspath(String... artifactNames) {
List<Path> runtimeClasspath = RuntimeClasspathCache.getRuntimeClasspath();
List<Path> artifacts = new ArrayList<>(artifactNames.length);
List<String> missingArtifactNames = new ArrayList<>(artifactNames.length);
for (String artifactName : artifactNames) {
List<Path> matchedArtifacts = filterArtifacts(artifactName, runtimeClasspath);
if (matchedArtifacts.isEmpty()) {
missingArtifactNames.add(artifactName);
} else {
artifacts.addAll(matchedArtifacts);
}
}
if (!missingArtifactNames.isEmpty()) {
String missing = missingArtifactNames.stream().sorted().collect(joining("', '", "'", "'"));
throw new IllegalArgumentException(String.format("Unable to find runtime dependencies beginning with: %s, classpath: %s",
missing, runtimeClasspath));
}
return artifacts;
}
/**
* Filters the classpath entries to find paths that match the given artifact name.
*
* @param artifactName The artifact name to search for.
* @param runtimeClasspath The list of classpath URIs to search within.
* @return List of Paths that match the artifact name.
*/
// VisibleForTesting
static List<Path> filterArtifacts(String artifactName, List<Path> runtimeClasspath) {
List<Path> artifacts = new ArrayList<>();
// Bazel automatically replaces '-' with '_' when generating jar files.
String normalizedArtifactName = artifactName.replace('-', '_');
Pattern jarPattern = Pattern.compile(String.format("(%s|%s)(?:-.*?)?\\.jar$", artifactName, normalizedArtifactName));
// In a multi-project IDE classpath, some classpath entries aren't jars
Pattern explodedPattern = Pattern.compile("/" + artifactName + "/");
for (Path cpEntry : runtimeClasspath) {
String cpEntryString = cpEntry.toString();
if (jarPattern.matcher(cpEntryString).find() ||
explodedPattern.matcher(cpEntryString).find() && cpEntry.toFile().isDirectory()) {
artifacts.add(cpEntry);
// Do not break because jarPattern matches "foo-bar-1.0.jar" and "foo-1.0.jar" to "foo"
}
}
return artifacts;
}
/**
* Load artifacts from packaged resources. This is useful for loading dependencies which are not on the recipe
* execution classpath, or where you need to load multiple different versions of the same artifact.
* Supports both {@link TypeTable} `classpath.tsv.gz` and packaged resource jars in `META-INF/rewrite/classpath/`.
*
* @param ctx The execution context to use for loading resources.
* @param artifactNamesWithVersions artifact prefix to match, e.g. "guava" or "guava-31" for a specific version.
* @return A list of paths to the located artifacts.
*/
static List<Path> dependenciesFromResources(ExecutionContext ctx, String... artifactNamesWithVersions) {
if (artifactNamesWithVersions.length == 0) {
return emptyList();
}
List<Path> artifacts = new ArrayList<>(artifactNamesWithVersions.length);
Set<String> missingArtifactNames = new LinkedHashSet<>(Arrays.asList(artifactNamesWithVersions));
List<String> availableArtifacts = new ArrayList<>();
TypeTable typeTable = TypeTable.fromClasspath(ctx, missingArtifactNames);
if (typeTable != null) {
for (Iterator<String> it = missingArtifactNames.iterator(); it.hasNext(); ) {
String missingArtifactName = it.next();
Path located = typeTable.load(missingArtifactName);
if (located != null) {
artifacts.add(located);
it.remove();
}
}
if (!missingArtifactNames.isEmpty()) {
availableArtifacts.addAll(typeTable.availableArtifacts());
}
}
if (!missingArtifactNames.isEmpty()) {
try (RewriteClasspathJarClasspathLoader classpathLoader = new RewriteClasspathJarClasspathLoader(ctx)) {
for (String missingArtifactName : new ArrayList<>(missingArtifactNames)) {
Path located = classpathLoader.load(missingArtifactName);
if (located != null) {
artifacts.add(located);
missingArtifactNames.remove(missingArtifactName);
}
}
if (!missingArtifactNames.isEmpty()) {
availableArtifacts.addAll(classpathLoader.availableArtifacts());
}
}
}
if (!missingArtifactNames.isEmpty()) {
String missing = missingArtifactNames.stream().sorted().collect(joining("', '", "'", "'"));
StringBuilder message = new StringBuilder();
message.append("Unable to find classpath resource dependencies beginning with: ").append(missing);
if (!availableArtifacts.isEmpty()) {
message.append(" in [").append(String.join(", ", availableArtifacts)).append(']');
}
throw new IllegalArgumentException(message.toString());
}
return artifacts;
}
/**
* Builds a Java parser with a language level equal to that of the JDK running this JVM process.
*/
static JavaParser.Builder<? extends JavaParser, ?> fromJavaVersion() {
return JdkParserBuilderCache.getBuilder();
}
@Override
default Stream<SourceFile> parse(ExecutionContext ctx, @Language("java") String... sources) {
return parseInputs(
Arrays.stream(sources)
.map(sourceFile -> new Input(
sourcePathFromSourceText(Paths.get(""), sourceFile), null,
() -> new ByteArrayInputStream(sourceFile.getBytes(getCharset(ctx))), true
))
.collect(toList()),
null,
ctx
);
}
@Override
default Stream<SourceFile> parse(@Language("java") String... sources) {
InMemoryExecutionContext ctx = new InMemoryExecutionContext();
return parse(ctx, sources);
}
@Override
default boolean accept(Path path) {
return path.toString().endsWith(".java") && !path.endsWith("module-info.java");
}
/**
* Clear any in-memory parser caches that may prevent re-parsing of classes with the same fully qualified name in
* different rounds
*/
@Override
JavaParser reset();
JavaParser reset(Collection<URI> uris);
/**
* Changes the classpath on the parser. Intended for use in multiple pass parsing, where we want to keep the
* compiler symbol table intact for type attribution on later parses, i.e. for maven multi-module projects.
*
* @param classpath new classpath to use
*/
void setClasspath(Collection<Path> classpath);
@SuppressWarnings("unchecked")
abstract class Builder<P extends JavaParser, B extends Builder<P, B>> extends Parser.Builder {
protected Collection<Path> classpath = emptyList();
protected Collection<String> artifactNames = emptyList();
protected Collection<byte[]> classBytesClasspath = emptyList();
protected JavaTypeCache javaTypeCache = new JavaTypeCache();
@Nullable
protected Collection<Input> dependsOn;
protected Charset charset = Charset.defaultCharset();
protected boolean logCompilationWarningsAndErrors = false;
protected final List<NamedStyles> styles = new ArrayList<>();
public Builder() {
super(J.CompilationUnit.class);
}
public B logCompilationWarningsAndErrors(boolean logCompilationWarningsAndErrors) {
this.logCompilationWarningsAndErrors = logCompilationWarningsAndErrors;
return (B) this;
}
public B typeCache(JavaTypeCache javaTypeCache) {
this.javaTypeCache = javaTypeCache;
return (B) this;
}
public B charset(Charset charset) {
this.charset = charset;
return (B) this;
}
@SuppressWarnings("unused")
public B dependsOn(Collection<Input> inputs) {
this.dependsOn = inputs;
return (B) this;
}
public B dependsOn(@Language("java") String... inputsAsStrings) {
this.dependsOn = Arrays.stream(inputsAsStrings)
.map(input -> Input.fromString(resolveSourcePathFromSourceText(Paths.get(""), input), input))
.collect(toList());
return (B) this;
}
public B classpath(Collection<Path> classpath) {
this.artifactNames = emptyList();
this.classpath = classpath;
return (B) this;
}
// internal method which doesn't overwrite the classpath but just amends it
@Incubating(since = "8.18.3")
public B addClasspathEntry(Path entry) {
if (classpath.isEmpty()) {
classpath = singletonList(entry);
} else if (!classpath.contains(entry)) {
classpath = new ArrayList<>(classpath);
classpath.add(entry);
}
return (B) this;
}
/**
* Sets the classpath from runtime classpath dependencies of the process constructing the parser. Predates
* {@link #classpathFromResources(ExecutionContext, String...)}, with the latter preferred to limit dependencies
* needed on the recipe runtime classpath, as the runtime classpath may differ in say the CLI or Platform.
* <p>
* This is suitable, for example, when writing tests that may require older versions of dependencies,
* without needing to add them to the recipe runtime classpath through resources.
*
* @return A list of paths to jars on the runtime classpath of the process constructing the parser.
*/
public B classpath(String... artifactNames) {
this.artifactNames = Arrays.asList(artifactNames);
this.classpath = emptyList();
return (B) this;
}
/**
* Load artifacts from packaged resources. This is useful for loading dependencies which are not on the recipe
* execution classpath, or where you need to load multiple different versions of the same artifact.
* Supports both {@link TypeTable} `classpath.tsv.gz` and packaged resource jars in `META-INF/rewrite/classpath/`.
*
* @param ctx The execution context to use for loading resources.
* @param classpath artifact prefix to match, e.g. "guava" or "guava-31" for a specific version.
* @return A list of paths to the located artifacts.
*/
@SuppressWarnings({"UnusedReturnValue", "unused"})
public B classpathFromResources(ExecutionContext ctx, String... classpath) {
this.artifactNames = emptyList();
this.classpath = dependenciesFromResources(ctx, classpath);
return (B) this;
}
/**
* @deprecated prefer {@link #classpath} and {@link #classpathFromResources(ExecutionContext, String...)}.
*/
@Deprecated
@ToBeRemoved(after = "2025-12-31", reason = "Use classpath or classpathFromResources instead.")
public B classpath(byte[]... classpath) {
this.classBytesClasspath = Arrays.asList(classpath);
return (B) this;
}
public B styles(Iterable<? extends NamedStyles> styles) {
for (NamedStyles style : styles) {
this.styles.add(style);
}
return (B) this;
}
protected Collection<Path> resolvedClasspath() {
if (!artifactNames.isEmpty()) {
classpath = new ArrayList<>(classpath);
classpath.addAll(JavaParser.dependenciesFromClasspath(artifactNames.toArray(new String[0])));
artifactNames = emptyList();
}
return classpath;
}
@Override
public abstract P build();
@Override
public String getDslName() {
return "java";
}
@Override
public Builder<P, B> clone() {
Builder<P, B> clone = (Builder<P, B>) super.clone();
clone.javaTypeCache = this.javaTypeCache.clone();
return 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+)");
Pattern publicClassPattern = Pattern.compile("public\\s+" + classPattern.pattern());
Function<String, @Nullable String> simpleName = sourceStr -> {
Matcher classMatcher = publicClassPattern.matcher(sourceStr);
if (classMatcher.find()) {
return classMatcher.group(3);
}
classMatcher = classPattern.matcher(sourceStr);
return classMatcher.find() ? classMatcher.group(3) : null;
};
Matcher packageMatcher = packagePattern.matcher(sourceCode);
String pkg = packageMatcher.find() ? packageMatcher.group(1).replace('.', '/') + "/" : "";
String className = Optional.ofNullable(simpleName.apply(sourceCode))
.orElse(Long.toString(System.nanoTime())) + ".java";
return prefix.resolve(Paths.get(pkg + className));
}
}
@UtilityClass
class RuntimeClasspathCache {
@Nullable
private static volatile List<Path> runtimeClasspath = null;
static List<Path> getRuntimeClasspath() {
List<Path> paths = runtimeClasspath;
if (paths == null) {
synchronized (RuntimeClasspathCache.class) {
paths = runtimeClasspath;
if (paths == null) {
runtimeClasspath = paths = new ClassGraph()
.disableNestedJarScanning()
.getClasspathURIs().stream()
.filter(uri -> "file".equals(uri.getScheme()))
.map(Paths::get)
.collect(toList());
}
}
}
return paths;
}
}
@UtilityClass
class JdkParserBuilderCache {
// Cached supplier for the parser builder - initialized on first access
private static volatile @Nullable Supplier<JavaParser.Builder<? extends JavaParser, ?>> cachedBuilderSupplier = null;
static JavaParser.Builder<? extends JavaParser, ?> getBuilder() {
Supplier<JavaParser.Builder<? extends JavaParser, ?>> supplier = cachedBuilderSupplier;
if (supplier != null) {
return supplier.get();
}
synchronized (JdkParserBuilderCache.class) {
// Double-check after acquiring lock
supplier = cachedBuilderSupplier;
if (supplier != null) {
return supplier.get();
}
// Determine Java version once
String[] versionParts = System.getProperty("java.version").split("[.-]");
int version = Integer.parseInt(versionParts[0]);
if (version == 1) {
version = 8;
}
// Try to find and cache appropriate parser
// Parsers are compiled only for their matching Java version, given their use of internal/unstable APIs.
// Language features introduced in non-LTS versions are only supported from the next LTS version onwards.
// We recommend running on LTS versions wherever possible; non-LTS versions may struggle given the above.
// e.g.: Java 23+ changed the return type of `DocCommentTable.getCommentTree` from `DCDocComment` to `DocCommentTree`
if (version >= 25) {
supplier = tryCreateBuilderSupplier("org.openrewrite.java.Java25Parser");
}
if (supplier == null && version >= 21) {
supplier = tryCreateBuilderSupplier("org.openrewrite.java.Java21Parser");
}
if (supplier == null && version >= 17) {
supplier = tryCreateBuilderSupplier("org.openrewrite.java.Java17Parser");
}
if (supplier == null && version >= 11) {
supplier = tryCreateBuilderSupplier("org.openrewrite.java.Java11Parser");
}
if (supplier == null) {
supplier = tryCreateBuilderSupplier("org.openrewrite.java.Java8Parser");
}
if (supplier != null) {
cachedBuilderSupplier = supplier;
return supplier.get();
}
throw new IllegalStateException("Unable to create a Java parser instance. " +
"`rewrite-java-8`, `rewrite-java-11`, `rewrite-java-17`, `rewrite-java-21`, or `rewrite-java-25` must be on the classpath.");
}
}
private static @Nullable Supplier<JavaParser.Builder<? extends JavaParser, ?>> tryCreateBuilderSupplier(String className) {
try {
Class<?> clazz = Class.forName(className);
Method builderMethod = clazz.getDeclaredMethod("builder");
return () -> {
try {
//noinspection rawtypes,unchecked
return (JavaParser.Builder) builderMethod.invoke(null);
} catch (Throwable e) {
throw new RuntimeException("Failed to invoke builder() on " + className, e);
}
};
} catch (ClassNotFoundException | NoSuchMethodException e) {
return null; // This parser version isn't available
}
}
}