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