Skip to content

Introduce registerResource(Resource) in ResourceHints #29083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@

package org.springframework.aot.hint.support;

import java.util.Arrays;
import java.util.function.Consumer;

import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.ResourceHints;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.TypeHint;
import org.springframework.aot.hint.TypeHint.Builder;
import org.springframework.core.annotation.AliasFor;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;

/**
* Utility methods for runtime hints support code.
Expand Down Expand Up @@ -92,4 +97,25 @@ public static void registerAnnotationIfNecessary(RuntimeHints hints, MergedAnnot
}
}

/**
* Register that the supplied resource should be made available at runtime.
* <p>If the supplied resource is not a {@link ClassPathResource}, it will
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Or does not exist"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current Javadoc reads like this:

Determine if the supplied resource is a ClassPathResource that exists and register the resource for run-time availability accordingly.

* not be registered.
* @param hints the {@link RuntimeHints} instance to use
* @param resource the resource to register
* @throws IllegalArgumentException if the supplied resource does not
* {@linkplain Resource#exists() exist}
* @see ResourceHints#registerPattern(String)
*/
public static void registerResource(RuntimeHints hints, Resource resource) {
if (resource instanceof ClassPathResource classPathResource) {
Assert.isTrue(resource.exists(), () -> "Resource does not exist: " + resource);
hints.resources().registerPattern(classPathResource.getPath());
}
}

public static void registerResources(RuntimeHints hints, Resource... resources) {
Arrays.stream(resources).forEach(resource -> registerResource(hints, resource));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import java.util.List;

import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.support.RuntimeHintsUtils;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ActiveProfilesResolver;
import org.springframework.test.context.ContextLoader;
Expand Down Expand Up @@ -50,12 +52,12 @@ class StandardTestRuntimeHints implements TestRuntimeHintsRegistrar {
public void registerHints(MergedContextConfiguration mergedConfig, List<Class<?>> testClasses,
RuntimeHints runtimeHints, ClassLoader classLoader) {

registerHintsForMergedContextConfiguration(runtimeHints, mergedConfig);
registerHintsForMergedContextConfiguration(runtimeHints, classLoader, mergedConfig);
testClasses.forEach(testClass -> registerHintsForActiveProfilesResolvers(runtimeHints, testClass));
}

private void registerHintsForMergedContextConfiguration(
RuntimeHints runtimeHints, MergedContextConfiguration mergedConfig) {
RuntimeHints runtimeHints, ClassLoader classLoader, MergedContextConfiguration mergedConfig) {

// @ContextConfiguration(loader = ...)
ContextLoader contextLoader = mergedConfig.getContextLoader();
Expand All @@ -68,10 +70,10 @@ private void registerHintsForMergedContextConfiguration(
.forEach(clazz -> registerDeclaredConstructors(runtimeHints, clazz));

// @ContextConfiguration(locations = ...)
registerClasspathResources(runtimeHints, mergedConfig.getLocations());
registerClasspathResources(runtimeHints, classLoader, mergedConfig.getLocations());

// @TestPropertySource(locations = ... )
registerClasspathResources(runtimeHints, mergedConfig.getPropertySourceLocations());
registerClasspathResources(runtimeHints, classLoader, mergedConfig.getPropertySourceLocations());

// @WebAppConfiguration(value = ...)
if (mergedConfig instanceof WebMergedContextConfiguration webConfig) {
Expand All @@ -94,11 +96,14 @@ private void registerDeclaredConstructors(RuntimeHints runtimeHints, Class<?> ty
runtimeHints.reflection().registerType(type, INVOKE_DECLARED_CONSTRUCTORS);
}

private void registerClasspathResources(RuntimeHints runtimeHints, String... locations) {
private void registerClasspathResources(RuntimeHints runtimeHints, ClassLoader classLoader, String... locations) {
DefaultResourceLoader resourceLoader = new DefaultResourceLoader(classLoader);
Arrays.stream(locations)
// For the sake of efficiency, we still filter out locations that are not classpath: resources,
// even though the current implementation of RuntimeHintsUtils#registerResource handles that.
.filter(location -> location.startsWith(CLASSPATH_URL_PREFIX))
.map(this::cleanClasspathResource)
.forEach(runtimeHints.resources()::registerPattern);
.map(resourceLoader::getResource)
.forEach(resource -> RuntimeHintsUtils.registerResource(runtimeHints, resource));
}

private void registerClasspathResourceDirectoryStructure(RuntimeHints runtimeHints, String directory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
import org.apache.commons.logging.LogFactory;

import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.support.RuntimeHintsUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.lang.NonNull;
Expand Down Expand Up @@ -148,10 +150,10 @@ public void afterTestMethod(TestContext testContext) {
@Override
public void processAheadOfTime(Class<?> testClass, RuntimeHints runtimeHints, ClassLoader classLoader) {
getSqlAnnotationsFor(testClass).forEach(sql ->
registerClasspathResources(runtimeHints, getScripts(sql, testClass, null, true)));
registerClasspathResources(runtimeHints, classLoader, getScripts(sql, testClass, null, true)));
getSqlMethods(testClass).forEach(testMethod ->
getSqlAnnotationsFor(testMethod).forEach(sql ->
registerClasspathResources(runtimeHints, getScripts(sql, testClass, testMethod, false))));
registerClasspathResources(runtimeHints, classLoader, getScripts(sql, testClass, testMethod, false))));
}

/**
Expand Down Expand Up @@ -390,19 +392,10 @@ private Stream<Method> getSqlMethods(Class<?> testClass) {
return Arrays.stream(ReflectionUtils.getUniqueDeclaredMethods(testClass, sqlMethodFilter));
}

private void registerClasspathResources(RuntimeHints runtimeHints, String... locations) {
Arrays.stream(locations)
.filter(location -> location.startsWith(CLASSPATH_URL_PREFIX))
.map(this::cleanClasspathResource)
.forEach(runtimeHints.resources()::registerPattern);
}

private String cleanClasspathResource(String location) {
location = location.substring(CLASSPATH_URL_PREFIX.length());
if (!location.startsWith(SLASH)) {
location = SLASH + location;
}
return location;
private void registerClasspathResources(RuntimeHints runtimeHints, ClassLoader classLoader, String... locations) {
DefaultResourceLoader resourceLoader = new DefaultResourceLoader(classLoader);
RuntimeHintsUtils.registerResources(runtimeHints,
TestContextResourceUtils.convertToResources(resourceLoader, locations));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -181,21 +181,21 @@ private static void assertRuntimeHints(RuntimeHints runtimeHints) {
).forEach(type -> assertReflectionRegistered(runtimeHints, type, INVOKE_DECLARED_CONSTRUCTORS));

// @ContextConfiguration(locations = ...)
assertThat(resource().forResource("/org/springframework/test/context/aot/samples/xml/test-config.xml"))
assertThat(resource().forResource("org/springframework/test/context/aot/samples/xml/test-config.xml"))
.accepts(runtimeHints);

// @TestPropertySource(locations = ...)
assertThat(resource().forResource("/org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests.properties"))
assertThat(resource().forResource("org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests.properties"))
.accepts(runtimeHints);

// @WebAppConfiguration(value = ...)
assertThat(resource().forResource("/META-INF/web-resources/resources/Spring.js")).accepts(runtimeHints);
assertThat(resource().forResource("/META-INF/web-resources/WEB-INF/views/home.jsp")).accepts(runtimeHints);

// @Sql(scripts = ...)
assertThat(resource().forResource("/org/springframework/test/context/jdbc/schema.sql"))
assertThat(resource().forResource("org/springframework/test/context/jdbc/schema.sql"))
.accepts(runtimeHints);
assertThat(resource().forResource("/org/springframework/test/context/aot/samples/jdbc/SqlScriptsSpringJupiterTests.test.sql"))
assertThat(resource().forResource("org/springframework/test/context/aot/samples/jdbc/SqlScriptsSpringJupiterTests.test.sql"))
.accepts(runtimeHints);
}

Expand Down