20
20
import java .util .List ;
21
21
22
22
import org .springframework .aot .hint .RuntimeHints ;
23
+ import org .springframework .aot .hint .support .RuntimeHintsUtils ;
23
24
import org .springframework .core .annotation .MergedAnnotations ;
25
+ import org .springframework .core .io .DefaultResourceLoader ;
24
26
import org .springframework .test .context .ActiveProfiles ;
25
27
import org .springframework .test .context .ActiveProfilesResolver ;
26
28
import org .springframework .test .context .ContextLoader ;
@@ -50,12 +52,12 @@ class StandardTestRuntimeHints implements TestRuntimeHintsRegistrar {
50
52
public void registerHints (MergedContextConfiguration mergedConfig , List <Class <?>> testClasses ,
51
53
RuntimeHints runtimeHints , ClassLoader classLoader ) {
52
54
53
- registerHintsForMergedContextConfiguration (runtimeHints , mergedConfig );
55
+ registerHintsForMergedContextConfiguration (runtimeHints , classLoader , mergedConfig );
54
56
testClasses .forEach (testClass -> registerHintsForActiveProfilesResolvers (runtimeHints , testClass ));
55
57
}
56
58
57
59
private void registerHintsForMergedContextConfiguration (
58
- RuntimeHints runtimeHints , MergedContextConfiguration mergedConfig ) {
60
+ RuntimeHints runtimeHints , ClassLoader classLoader , MergedContextConfiguration mergedConfig ) {
59
61
60
62
// @ContextConfiguration(loader = ...)
61
63
ContextLoader contextLoader = mergedConfig .getContextLoader ();
@@ -68,14 +70,14 @@ private void registerHintsForMergedContextConfiguration(
68
70
.forEach (clazz -> registerDeclaredConstructors (runtimeHints , clazz ));
69
71
70
72
// @ContextConfiguration(locations = ...)
71
- registerClasspathResources (runtimeHints , mergedConfig .getLocations ());
73
+ registerClasspathResources (mergedConfig .getLocations (), runtimeHints , classLoader );
72
74
73
75
// @TestPropertySource(locations = ... )
74
- registerClasspathResources (runtimeHints , mergedConfig .getPropertySourceLocations ());
76
+ registerClasspathResources (mergedConfig .getPropertySourceLocations (), runtimeHints , classLoader );
75
77
76
78
// @WebAppConfiguration(value = ...)
77
79
if (mergedConfig instanceof WebMergedContextConfiguration webConfig ) {
78
- registerClasspathResourceDirectoryStructure (runtimeHints , webConfig .getResourceBasePath ());
80
+ registerClasspathResourceDirectoryStructure (webConfig .getResourceBasePath (), runtimeHints );
79
81
}
80
82
}
81
83
@@ -94,16 +96,20 @@ private void registerDeclaredConstructors(RuntimeHints runtimeHints, Class<?> ty
94
96
runtimeHints .reflection ().registerType (type , INVOKE_DECLARED_CONSTRUCTORS );
95
97
}
96
98
97
- private void registerClasspathResources (RuntimeHints runtimeHints , String ... locations ) {
98
- Arrays .stream (locations )
99
- .filter (location -> location .startsWith (CLASSPATH_URL_PREFIX ))
100
- .map (this ::cleanClasspathResource )
101
- .forEach (runtimeHints .resources ()::registerPattern );
99
+ private void registerClasspathResources (String [] paths , RuntimeHints runtimeHints , ClassLoader classLoader ) {
100
+ DefaultResourceLoader resourceLoader = new DefaultResourceLoader (classLoader );
101
+ Arrays .stream (paths )
102
+ .filter (path -> path .startsWith (CLASSPATH_URL_PREFIX ))
103
+ .map (resourceLoader ::getResource )
104
+ .forEach (resource -> RuntimeHintsUtils .registerResourceIfNecessary (runtimeHints , resource ));
102
105
}
103
106
104
- private void registerClasspathResourceDirectoryStructure (RuntimeHints runtimeHints , String directory ) {
107
+ private void registerClasspathResourceDirectoryStructure (String directory , RuntimeHints runtimeHints ) {
105
108
if (directory .startsWith (CLASSPATH_URL_PREFIX )) {
106
- String pattern = cleanClasspathResource (directory );
109
+ String pattern = directory .substring (CLASSPATH_URL_PREFIX .length ());
110
+ if (pattern .startsWith (SLASH )) {
111
+ pattern = pattern .substring (1 );
112
+ }
107
113
if (!pattern .endsWith (SLASH )) {
108
114
pattern += SLASH ;
109
115
}
@@ -112,12 +118,4 @@ private void registerClasspathResourceDirectoryStructure(RuntimeHints runtimeHin
112
118
}
113
119
}
114
120
115
- private String cleanClasspathResource (String location ) {
116
- location = location .substring (CLASSPATH_URL_PREFIX .length ());
117
- if (location .startsWith (SLASH )) {
118
- location = location .substring (1 );
119
- }
120
- return location ;
121
- }
122
-
123
121
}
0 commit comments