Introduce RuntimeHintsUtils.registerResourceIfNecessary

This commit introduces a new registerResourceIfNecessary() method in
RuntimeHintsUtils that simplifies the registration of hints for
`classpath:` resources.

Closes gh-29083
This commit is contained in:
Sam Brannen
2022-09-05 16:45:47 +02:00
parent dd1e6b9412
commit 28c492cbdd
4 changed files with 82 additions and 35 deletions

View File

@@ -19,11 +19,14 @@ package org.springframework.aot.hint.support;
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;
/**
* Utility methods for runtime hints support code.
@@ -92,4 +95,18 @@ public abstract class RuntimeHintsUtils {
}
}
/**
* Determine if the supplied resource is a {@link ClassPathResource} that
* {@linkplain Resource#exists() exists} and register the resource for run-time
* availability accordingly.
* @param hints the {@link RuntimeHints} instance to use
* @param resource the resource to register
* @see ResourceHints#registerPattern(String)
*/
public static void registerResourceIfNecessary(RuntimeHints hints, Resource resource) {
if (resource instanceof ClassPathResource classPathResource && classPathResource.exists()) {
hints.resources().registerPattern(classPathResource.getPath());
}
}
}

View File

@@ -20,6 +20,7 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.function.Consumer;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.aot.hint.JdkProxyHint;
@@ -28,8 +29,11 @@ import org.springframework.aot.hint.TypeReference;
import org.springframework.core.annotation.AliasFor;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.DescriptiveResource;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.resource;
/**
* Tests for {@link RuntimeHintsUtils}.
@@ -41,6 +45,39 @@ class RuntimeHintsUtilsTests {
private final RuntimeHints hints = new RuntimeHints();
@Test
void registerResourceIfNecessaryWithUnsupportedResourceType() {
DescriptiveResource resource = new DescriptiveResource("bogus");
RuntimeHintsUtils.registerResourceIfNecessary(this.hints, resource);
assertThat(this.hints.resources().resourcePatterns()).isEmpty();
}
@Test
void registerResourceIfNecessaryWithNonexistentClassPathResource() {
ClassPathResource resource = new ClassPathResource("bogus", getClass());
RuntimeHintsUtils.registerResourceIfNecessary(this.hints, resource);
assertThat(this.hints.resources().resourcePatterns()).isEmpty();
}
@Test
void registerResourceIfNecessaryWithExistingClassPathResource() {
String path = "org/springframework/aot/hint/support";
ClassPathResource resource = new ClassPathResource(path);
RuntimeHintsUtils.registerResourceIfNecessary(this.hints, resource);
assertThat(resource().forResource(path)).accepts(this.hints);
}
@Disabled("Disabled since ClassPathResource.getPath() does not honor its contract for relative resources")
@Test
void registerResourceIfNecessaryWithExistingRelativeClassPathResource() {
String path = "org/springframework/aot/hint/support";
ClassPathResource resource = new ClassPathResource("support", RuntimeHints.class);
RuntimeHintsUtils.registerResourceIfNecessary(this.hints, resource);
// This unfortunately fails since ClassPathResource.getPath() returns
// "support" instead of "org/springframework/aot/hint/support".
assertThat(resource().forResource(path)).accepts(this.hints);
}
@Test
@SuppressWarnings("deprecation")
void registerSynthesizedAnnotation() {