Skip to content

Commit 7460be6

Browse files
committed
Consistent default ClassLoader fallback in hint classes
Closes gh-34470
1 parent ccf4b02 commit 7460be6

File tree

4 files changed

+36
-31
lines changed

4 files changed

+36
-31
lines changed

spring-core/src/main/java/org/springframework/aot/hint/ResourceHints.java

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@
2727
import org.springframework.core.io.ClassPathResource;
2828
import org.springframework.core.io.Resource;
2929
import org.springframework.lang.Nullable;
30+
import org.springframework.util.ClassUtils;
3031

3132
/**
3233
* Gather the need for resources available at runtime.
@@ -50,14 +51,14 @@ public ResourceHints() {
5051
this.resourceBundleHints = new LinkedHashSet<>();
5152
}
5253

54+
5355
/**
5456
* Return the resources that should be made available at runtime.
5557
* @return a stream of {@link ResourcePatternHints}
5658
*/
5759
public Stream<ResourcePatternHints> resourcePatternHints() {
5860
Stream<ResourcePatternHints> patterns = this.resourcePatternHints.stream();
59-
return (this.types.isEmpty() ? patterns
60-
: Stream.concat(Stream.of(typesPatternResourceHint()), patterns));
61+
return (this.types.isEmpty() ? patterns : Stream.concat(Stream.of(typesPatternResourceHint()), patterns));
6162
}
6263

6364
/**
@@ -70,18 +71,18 @@ public Stream<ResourceBundleHint> resourceBundleHints() {
7071

7172
/**
7273
* Register a pattern if the given {@code location} is available on the
73-
* classpath. This delegates to {@link ClassLoader#getResource(String)}
74-
* which validates directories as well. The location is not included in
75-
* the hint.
76-
* @param classLoader the classloader to use
74+
* classpath. This delegates to {@link ClassLoader#getResource(String)} which
75+
* validates directories as well. The location is not included in the hint.
76+
* @param classLoader the ClassLoader to use, or {@code null} for the default
7777
* @param location a '/'-separated path name that should exist
7878
* @param resourceHint a builder to customize the resource pattern
7979
* @return {@code this}, to facilitate method chaining
8080
*/
8181
public ResourceHints registerPatternIfPresent(@Nullable ClassLoader classLoader, String location,
8282
Consumer<ResourcePatternHints.Builder> resourceHint) {
83-
ClassLoader classLoaderToUse = (classLoader != null ? classLoader : getClass().getClassLoader());
84-
if (classLoaderToUse.getResource(location) != null) {
83+
84+
ClassLoader classLoaderToUse = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
85+
if (classLoaderToUse != null && classLoaderToUse.getResource(location) != null) {
8586
registerPattern(resourceHint);
8687
}
8788
return this;

spring-core/src/main/java/org/springframework/aot/hint/RuntimeHintsRegistrar.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,8 +25,9 @@
2525
*
2626
* <p>Implementations of this interface can be registered dynamically by using
2727
* {@link org.springframework.context.annotation.ImportRuntimeHints @ImportRuntimeHints}
28-
* or statically in {@code META-INF/spring/aot.factories} by using the FQN of this
29-
* interface as the key. A standard no-arg constructor is required for implementations.
28+
* or statically in {@code META-INF/spring/aot.factories} by using the fully-qualified
29+
* class name of this interface as the key. A standard no-arg constructor is required
30+
* for implementations.
3031
*
3132
* @author Brian Clozel
3233
* @author Stephane Nicoll
@@ -38,7 +39,7 @@ public interface RuntimeHintsRegistrar {
3839
/**
3940
* Contribute hints to the given {@link RuntimeHints} instance.
4041
* @param hints the hints contributed so far for the deployment unit
41-
* @param classLoader the classloader, or {@code null} if even the system ClassLoader isn't accessible
42+
* @param classLoader the ClassLoader to use, or {@code null} for the default
4243
*/
4344
void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader);
4445

spring-core/src/main/java/org/springframework/aot/hint/support/FilePatternResourceHintsRegistrar.java

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
import org.springframework.aot.hint.ResourceHints;
2424
import org.springframework.lang.Nullable;
2525
import org.springframework.util.Assert;
26+
import org.springframework.util.ClassUtils;
2627
import org.springframework.util.ResourceUtils;
2728

2829
/**
@@ -66,19 +67,21 @@ public FilePatternResourceHintsRegistrar(List<String> filePrefixes, List<String>
6667

6768
@Deprecated(since = "6.0.12", forRemoval = true)
6869
public void registerHints(ResourceHints hints, @Nullable ClassLoader classLoader) {
69-
ClassLoader classLoaderToUse = (classLoader != null ? classLoader : getClass().getClassLoader());
70-
List<String> includes = new ArrayList<>();
71-
for (String location : this.classpathLocations) {
72-
if (classLoaderToUse.getResource(location) != null) {
73-
for (String filePrefix : this.filePrefixes) {
74-
for (String fileExtension : this.fileExtensions) {
75-
includes.add(location + filePrefix + "*" + fileExtension);
70+
ClassLoader classLoaderToUse = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
71+
if (classLoaderToUse != null) {
72+
List<String> includes = new ArrayList<>();
73+
for (String location : this.classpathLocations) {
74+
if (classLoaderToUse.getResource(location) != null) {
75+
for (String filePrefix : this.filePrefixes) {
76+
for (String fileExtension : this.fileExtensions) {
77+
includes.add(location + filePrefix + "*" + fileExtension);
78+
}
7679
}
7780
}
7881
}
79-
}
80-
if (!includes.isEmpty()) {
81-
hints.registerPattern(hint -> hint.includes(includes.toArray(String[]::new)));
82+
if (!includes.isEmpty()) {
83+
hints.registerPattern(hint -> hint.includes(includes.toArray(String[]::new)));
84+
}
8285
}
8386
}
8487

@@ -246,8 +249,7 @@ private FilePatternResourceHintsRegistrar build() {
246249
* classpath location that resolves against the {@code ClassLoader}, files
247250
* with the configured file prefixes and extensions are registered.
248251
* @param hints the hints contributed so far for the deployment unit
249-
* @param classLoader the classloader, or {@code null} if even the system
250-
* ClassLoader isn't accessible
252+
* @param classLoader the ClassLoader to use, or {@code null} for the default
251253
*/
252254
public void registerHints(ResourceHints hints, @Nullable ClassLoader classLoader) {
253255
build().registerHints(hints, classLoader);

spring-core/src/main/java/org/springframework/aot/hint/support/SpringFactoriesLoaderRuntimeHints.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,10 +48,11 @@ class SpringFactoriesLoaderRuntimeHints implements RuntimeHintsRegistrar {
4848

4949
@Override
5050
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
51-
ClassLoader classLoaderToUse = (classLoader != null ? classLoader :
52-
SpringFactoriesLoaderRuntimeHints.class.getClassLoader());
53-
for (String resourceLocation : RESOURCE_LOCATIONS) {
54-
registerHints(hints, classLoaderToUse, resourceLocation);
51+
ClassLoader classLoaderToUse = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
52+
if (classLoaderToUse != null) {
53+
for (String resourceLocation : RESOURCE_LOCATIONS) {
54+
registerHints(hints, classLoaderToUse, resourceLocation);
55+
}
5556
}
5657
}
5758

0 commit comments

Comments
 (0)