Skip to content

Commit

Permalink
Move registerResourceIfNecessary() to ResourceHints
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Sep 6, 2022
1 parent 5c2859f commit 8fbd214
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@
import java.util.function.Consumer;
import java.util.stream.Stream;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.lang.Nullable;

/**
* Gather the need for resources available at runtime.
*
* @author Stephane Nicoll
* @author Sam Brannen
* @since 6.0
*/
public class ResourceHints {
Expand Down Expand Up @@ -75,7 +78,8 @@ public Stream<ResourceBundleHint> resourceBundles() {
* @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<ResourcePatternHints.Builder> resourceHint) {
public ResourceHints registerPatternIfPresent(@Nullable ClassLoader classLoader, String location,
Consumer<ResourcePatternHints.Builder> resourceHint) {
ClassLoader classLoaderToUse = (classLoader != null) ? classLoader : getClass().getClassLoader();
if (classLoaderToUse.getResource(location) != null) {
registerPattern(resourceHint);
Expand Down Expand Up @@ -108,6 +112,19 @@ public ResourceHints registerPattern(String include) {
return registerPattern(builder -> builder.includes(include));
}

/**
* Determine if the supplied resource is a {@link ClassPathResource} that
* {@linkplain Resource#exists() exists} and register the resource for run-time
* availability accordingly.
* @param resource the resource to register
* @see #registerPattern(String)
*/
public void registerResourceIfNecessary(Resource resource) {
if (resource instanceof ClassPathResource classPathResource && classPathResource.exists()) {
registerPattern(classPathResource.getPath());
}
}

/**
* Register that the bytecode of the type defined by the specified
* {@link TypeReference} should be made available at runtime.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@
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.
Expand Down Expand Up @@ -95,18 +92,4 @@ public static void registerAnnotationIfNecessary(RuntimeHints hints, MergedAnnot
}
}

/**
* 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());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
import java.util.List;
import java.util.function.Consumer;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import org.springframework.aot.hint.ResourceHintsTests.Nested.Inner;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.DescriptiveResource;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
Expand All @@ -33,6 +36,7 @@
* Tests for {@link ResourceHints}.
*
* @author Stephane Nicoll
* @author Sam Brannen
*/
class ResourceHintsTests {

Expand Down Expand Up @@ -110,6 +114,39 @@ void registerIfPresentIgnoreMissingLocation() {
verifyNoInteractions(hintBuilder);
}

@Test
void registerResourceIfNecessaryWithUnsupportedResourceType() {
DescriptiveResource resource = new DescriptiveResource("bogus");
this.resourceHints.registerResourceIfNecessary(resource);
assertThat(this.resourceHints.resourcePatterns()).isEmpty();
}

@Test
void registerResourceIfNecessaryWithNonexistentClassPathResource() {
ClassPathResource resource = new ClassPathResource("bogus", getClass());
this.resourceHints.registerResourceIfNecessary(resource);
assertThat(this.resourceHints.resourcePatterns()).isEmpty();
}

@Test
void registerResourceIfNecessaryWithExistingClassPathResource() {
String path = "org/springframework/aot/hint/support";
ClassPathResource resource = new ClassPathResource(path);
this.resourceHints.registerResourceIfNecessary(resource);
assertThat(this.resourceHints.resourcePatterns()).singleElement().satisfies(patternOf(path));
}

@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);
this.resourceHints.registerResourceIfNecessary(resource);
// This unfortunately fails since ClassPathResource.getPath() returns
// "support" instead of "org/springframework/aot/hint/support".
assertThat(this.resourceHints.resourcePatterns()).singleElement().satisfies(patternOf(path));
}

@Test
void registerResourceBundle() {
this.resourceHints.registerResourceBundle("com.example.message");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
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;
Expand All @@ -29,11 +28,8 @@
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}.
Expand All @@ -45,39 +41,6 @@ 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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
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;
Expand Down Expand Up @@ -101,7 +100,7 @@ private void registerClasspathResources(String[] paths, RuntimeHints runtimeHint
Arrays.stream(paths)
.filter(path -> path.startsWith(CLASSPATH_URL_PREFIX))
.map(resourceLoader::getResource)
.forEach(resource -> RuntimeHintsUtils.registerResourceIfNecessary(runtimeHints, resource));
.forEach(runtimeHints.resources()::registerResourceIfNecessary);
}

private void registerClasspathResourceDirectoryStructure(String directory, RuntimeHints runtimeHints) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
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;
Expand Down Expand Up @@ -397,7 +396,7 @@ private void registerClasspathResources(String[] paths, RuntimeHints runtimeHint
Arrays.stream(paths)
.filter(path -> path.startsWith(CLASSPATH_URL_PREFIX))
.map(resourceLoader::getResource)
.forEach(resource -> RuntimeHintsUtils.registerResourceIfNecessary(runtimeHints, resource));
.forEach(runtimeHints.resources()::registerResourceIfNecessary);
}

}

0 comments on commit 8fbd214

Please sign in to comment.