From 14b99267ed89101c9c62af2db7dadb30bc628533 Mon Sep 17 00:00:00 2001 From: Jonathan Schneider Date: Tue, 21 Jan 2025 16:04:42 -0500 Subject: [PATCH] Don't require type table to exist --- .../java/org/openrewrite/java/JavaParser.java | 11 ++++++----- .../java/internal/parser/TypeTable.java | 8 ++++++-- .../java/internal/parser/package-info.java | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 rewrite-java/src/main/java/org/openrewrite/java/internal/parser/package-info.java 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 651f9376e66..7bfdede7cbd 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/JavaParser.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/JavaParser.java @@ -126,12 +126,13 @@ static List dependenciesFromResources(ExecutionContext ctx, String... arti Set missingArtifactNames = new LinkedHashSet<>(Arrays.asList(artifactNamesWithVersions)); try (RewriteClasspathJarClasspathLoader rewriteClasspathJarClasspathLoader = new RewriteClasspathJarClasspathLoader(ctx)) { - for (JavaParserClasspathLoader locator : new JavaParserClasspathLoader[]{ - new TypeTable(ctx, missingArtifactNames), - rewriteClasspathJarClasspathLoader, - }) { + List 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); diff --git a/rewrite-java/src/main/java/org/openrewrite/java/internal/parser/TypeTable.java b/rewrite-java/src/main/java/org/openrewrite/java/internal/parser/TypeTable.java index 3da7957d3a3..7dafd6e134c 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/internal/parser/TypeTable.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/internal/parser/TypeTable.java @@ -78,8 +78,12 @@ public class TypeTable implements JavaParserClasspathLoader { private static final Map classesDirByArtifact = new LinkedHashMap<>(); - public TypeTable(ExecutionContext ctx, Collection artifactNames) { - this(ctx, findCaller().getClassLoader().getResourceAsStream(DEFAULT_RESOURCE_PATH), artifactNames); + public static @Nullable TypeTable fromClasspath(ExecutionContext ctx, Collection 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 artifactNames) { diff --git a/rewrite-java/src/main/java/org/openrewrite/java/internal/parser/package-info.java b/rewrite-java/src/main/java/org/openrewrite/java/internal/parser/package-info.java new file mode 100644 index 00000000000..69e4e77650a --- /dev/null +++ b/rewrite-java/src/main/java/org/openrewrite/java/internal/parser/package-info.java @@ -0,0 +1,19 @@ +/* + * Copyright 2020 the original author or authors. + *

+ * 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 + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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;