From 634d1dd20b16a8a6b1bd3d909da1e418d61853a7 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sun, 23 Feb 2025 15:15:25 +0100 Subject: [PATCH] Consistent default ClassLoader fallback in hint classes Closes gh-34470 --- .../aot/hint/ResourceHints.java | 19 +++++++------ .../aot/hint/RuntimeHintsRegistrar.java | 5 ++-- .../FilePatternResourceHintsRegistrar.java | 28 ++++++++++--------- .../SpringFactoriesLoaderRuntimeHints.java | 11 ++++---- 4 files changed, 33 insertions(+), 30 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/aot/hint/ResourceHints.java b/spring-core/src/main/java/org/springframework/aot/hint/ResourceHints.java index 490dd257f49..ebe6ab91695 100644 --- a/spring-core/src/main/java/org/springframework/aot/hint/ResourceHints.java +++ b/spring-core/src/main/java/org/springframework/aot/hint/ResourceHints.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2025 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. @@ -27,6 +27,7 @@ import java.util.stream.Stream; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.lang.Nullable; +import org.springframework.util.ClassUtils; /** * Gather the need for resources available at runtime. @@ -50,14 +51,14 @@ public class ResourceHints { this.resourceBundleHints = new LinkedHashSet<>(); } + /** * Return the resources that should be made available at runtime. * @return a stream of {@link ResourcePatternHints} */ public Stream resourcePatternHints() { Stream patterns = this.resourcePatternHints.stream(); - return (this.types.isEmpty() ? patterns - : Stream.concat(Stream.of(typesPatternResourceHint()), patterns)); + return (this.types.isEmpty() ? patterns : Stream.concat(Stream.of(typesPatternResourceHint()), patterns)); } /** @@ -70,18 +71,18 @@ public class ResourceHints { /** * Register a pattern if the given {@code location} is available on the - * classpath. This delegates to {@link ClassLoader#getResource(String)} - * which validates directories as well. The location is not included in - * the hint. - * @param classLoader the classloader to use + * classpath. This delegates to {@link ClassLoader#getResource(String)} which + * validates directories as well. The location is not included in the hint. + * @param classLoader the ClassLoader to use, or {@code null} for the default * @param location a '/'-separated path name that should exist * @param resourceHint a builder to customize the resource pattern * @return {@code this}, to facilitate method chaining */ public ResourceHints registerPatternIfPresent(@Nullable ClassLoader classLoader, String location, Consumer resourceHint) { - ClassLoader classLoaderToUse = (classLoader != null ? classLoader : getClass().getClassLoader()); - if (classLoaderToUse.getResource(location) != null) { + + ClassLoader classLoaderToUse = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader()); + if (classLoaderToUse != null && classLoaderToUse.getResource(location) != null) { registerPattern(resourceHint); } return this; diff --git a/spring-core/src/main/java/org/springframework/aot/hint/RuntimeHintsRegistrar.java b/spring-core/src/main/java/org/springframework/aot/hint/RuntimeHintsRegistrar.java index 742be48a411..83db8b9fbff 100644 --- a/spring-core/src/main/java/org/springframework/aot/hint/RuntimeHintsRegistrar.java +++ b/spring-core/src/main/java/org/springframework/aot/hint/RuntimeHintsRegistrar.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 the original author or authors. + * Copyright 2002-2025 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. @@ -39,8 +39,7 @@ public interface RuntimeHintsRegistrar { /** * Contribute hints to the given {@link RuntimeHints} instance. * @param hints the hints contributed so far for the deployment unit - * @param classLoader the classloader, or {@code null} if even the system - * ClassLoader is not accessible + * @param classLoader the ClassLoader to use, or {@code null} for the default */ void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader); diff --git a/spring-core/src/main/java/org/springframework/aot/hint/support/FilePatternResourceHintsRegistrar.java b/spring-core/src/main/java/org/springframework/aot/hint/support/FilePatternResourceHintsRegistrar.java index f5e3525a1e8..a41d5d31f28 100644 --- a/spring-core/src/main/java/org/springframework/aot/hint/support/FilePatternResourceHintsRegistrar.java +++ b/spring-core/src/main/java/org/springframework/aot/hint/support/FilePatternResourceHintsRegistrar.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2025 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. @@ -23,6 +23,7 @@ import java.util.List; import org.springframework.aot.hint.ResourceHints; import org.springframework.lang.Nullable; import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; import org.springframework.util.ResourceUtils; /** @@ -66,19 +67,21 @@ public class FilePatternResourceHintsRegistrar { @Deprecated(since = "6.0.12", forRemoval = true) public void registerHints(ResourceHints hints, @Nullable ClassLoader classLoader) { - ClassLoader classLoaderToUse = (classLoader != null ? classLoader : getClass().getClassLoader()); - List includes = new ArrayList<>(); - for (String location : this.classpathLocations) { - if (classLoaderToUse.getResource(location) != null) { - for (String filePrefix : this.filePrefixes) { - for (String fileExtension : this.fileExtensions) { - includes.add(location + filePrefix + "*" + fileExtension); + ClassLoader classLoaderToUse = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader()); + if (classLoaderToUse != null) { + List includes = new ArrayList<>(); + for (String location : this.classpathLocations) { + if (classLoaderToUse.getResource(location) != null) { + for (String filePrefix : this.filePrefixes) { + for (String fileExtension : this.fileExtensions) { + includes.add(location + filePrefix + "*" + fileExtension); + } } } } - } - if (!includes.isEmpty()) { - hints.registerPattern(hint -> hint.includes(includes.toArray(String[]::new))); + if (!includes.isEmpty()) { + hints.registerPattern(hint -> hint.includes(includes.toArray(String[]::new))); + } } } @@ -246,8 +249,7 @@ public class FilePatternResourceHintsRegistrar { * classpath location that resolves against the {@code ClassLoader}, files * with the configured file prefixes and extensions are registered. * @param hints the hints contributed so far for the deployment unit - * @param classLoader the classloader, or {@code null} if even the system - * ClassLoader isn't accessible + * @param classLoader the ClassLoader to use, or {@code null} for the default */ public void registerHints(ResourceHints hints, @Nullable ClassLoader classLoader) { build().registerHints(hints, classLoader); diff --git a/spring-core/src/main/java/org/springframework/aot/hint/support/SpringFactoriesLoaderRuntimeHints.java b/spring-core/src/main/java/org/springframework/aot/hint/support/SpringFactoriesLoaderRuntimeHints.java index 8081223c521..819361ffff1 100644 --- a/spring-core/src/main/java/org/springframework/aot/hint/support/SpringFactoriesLoaderRuntimeHints.java +++ b/spring-core/src/main/java/org/springframework/aot/hint/support/SpringFactoriesLoaderRuntimeHints.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 the original author or authors. + * Copyright 2002-2025 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. @@ -48,10 +48,11 @@ class SpringFactoriesLoaderRuntimeHints implements RuntimeHintsRegistrar { @Override public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) { - ClassLoader classLoaderToUse = (classLoader != null ? classLoader : - SpringFactoriesLoaderRuntimeHints.class.getClassLoader()); - for (String resourceLocation : RESOURCE_LOCATIONS) { - registerHints(hints, classLoaderToUse, resourceLocation); + ClassLoader classLoaderToUse = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader()); + if (classLoaderToUse != null) { + for (String resourceLocation : RESOURCE_LOCATIONS) { + registerHints(hints, classLoaderToUse, resourceLocation); + } } }