Skip to content

Commit

Permalink
Don't require type table to exist
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Jan 21, 2025
1 parent c7d46ae commit 14b9926
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
11 changes: 6 additions & 5 deletions rewrite-java/src/main/java/org/openrewrite/java/JavaParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,13 @@ static List<Path> dependenciesFromResources(ExecutionContext ctx, String... arti
Set<String> missingArtifactNames = new LinkedHashSet<>(Arrays.asList(artifactNamesWithVersions));

try (RewriteClasspathJarClasspathLoader rewriteClasspathJarClasspathLoader = new RewriteClasspathJarClasspathLoader(ctx)) {
for (JavaParserClasspathLoader locator : new JavaParserClasspathLoader[]{
new TypeTable(ctx, missingArtifactNames),
rewriteClasspathJarClasspathLoader,
}) {
List<JavaParserClasspathLoader> loaders = new ArrayList<>(2);
Optional.ofNullable(TypeTable.fromClasspath(ctx, missingArtifactNames)).ifPresent(loaders::add);
loaders.add(rewriteClasspathJarClasspathLoader);

for (JavaParserClasspathLoader loader : loaders) {
for (String missingArtifactName : new ArrayList<>(missingArtifactNames)) {
Path located = locator.load(missingArtifactName);
Path located = loader.load(missingArtifactName);
if (located != null) {
artifacts.add(located);
missingArtifactNames.remove(missingArtifactName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,12 @@ public class TypeTable implements JavaParserClasspathLoader {

private static final Map<GroupArtifactVersion, Path> classesDirByArtifact = new LinkedHashMap<>();

public TypeTable(ExecutionContext ctx, Collection<String> artifactNames) {
this(ctx, findCaller().getClassLoader().getResourceAsStream(DEFAULT_RESOURCE_PATH), artifactNames);
public static @Nullable TypeTable fromClasspath(ExecutionContext ctx, Collection<String> artifactNames) {
try (InputStream is = findCaller().getClassLoader().getResourceAsStream(DEFAULT_RESOURCE_PATH)) {
return is == null ? null : new TypeTable(ctx, is, artifactNames);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

public TypeTable(ExecutionContext ctx, InputStream is, Collection<String> artifactNames) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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.
*/
@NullMarked
package org.openrewrite.java.internal.parser;

import org.jspecify.annotations.NullMarked;

0 comments on commit 14b9926

Please sign in to comment.