Move registerResourceIfNecessary() to ResourceHints

See gh-29083
This commit is contained in:
Sam Brannen
2022-09-06 15:22:18 +02:00
parent 5c2859ffa7
commit 8fbd2141b7
6 changed files with 57 additions and 59 deletions

View File

@@ -24,12 +24,15 @@ import java.util.Set;
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 {
@@ -75,7 +78,8 @@ public class ResourceHints {
* @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);
@@ -108,6 +112,19 @@ public class ResourceHints {
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.

View File

@@ -19,14 +19,11 @@ 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.
@@ -95,18 +92,4 @@ 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());
}
}
}