Skip to content

Commit

Permalink
Add native hints for SpringFactoriesLoader file content
Browse files Browse the repository at this point in the history
Add `SpringFactoriesLoaderRuntimeHintsRegistrar` which provides
native hints for `spring.factories` content.

Closes gh-27955

Co-authored-by: Phillip Webb <[email protected]>
  • Loading branch information
bclozel and philwebb committed May 11, 2022
1 parent e7e60f7 commit 267b914
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ private List<String> loadFactoryNames(Class<?> factoryType) {
}

@Nullable
protected <T> T instantiateFactory(String implementationName, Class<T> type, @Nullable ArgumentResolver argumentResolver, FailureHandler failureHandler) {
protected <T> T instantiateFactory(String implementationName, Class<T> type,
@Nullable ArgumentResolver argumentResolver, FailureHandler failureHandler) {

try {
Class<?> factoryImplementationClass = ClassUtils.forName(implementationName, this.classLoader);
Assert.isTrue(type.isAssignableFrom(factoryImplementationClass),
Expand Down Expand Up @@ -333,7 +335,7 @@ public static SpringFactoriesLoader forResourceLocation(@Nullable ClassLoader cl
return loader;
}

private static Map<String, List<String>> loadFactoriesResource(ClassLoader classLoader, String resourceLocation) {
static Map<String, List<String>> loadFactoriesResource(ClassLoader classLoader, String resourceLocation) {
Map<String, List<String>> result = new LinkedHashMap<>();
try {
Enumeration<URL> urls = classLoader.getResources(resourceLocation);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.core.io.support;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeHint;
import org.springframework.core.log.LogMessage;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;

/**
* {@link RuntimeHintsRegistrar} to register hints for {@code spring.factories}.
*
* @author Brian Clozel
* @author Phillip Webb
* @since 6.0
* @see SpringFactoriesLoader
*/
class SpringFactoriesLoaderRuntimeHintsRegistrar implements RuntimeHintsRegistrar {

private static List<String> RESOURCE_LOCATIONS = List
.of(SpringFactoriesLoader.FACTORIES_RESOURCE_LOCATION);

private static final Consumer<TypeHint.Builder> HINT = builder -> builder
.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);

private final Log logger = LogFactory.getLog(SpringFactoriesLoaderRuntimeHintsRegistrar.class);


@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
for (String resourceLocation : RESOURCE_LOCATIONS) {
registerHints(hints, classLoader, resourceLocation);
}
}

private void registerHints(RuntimeHints hints, ClassLoader classLoader,
String resourceLocation) {
hints.resources().registerPattern(resourceLocation);
Map<String, List<String>> factories = SpringFactoriesLoader
.loadFactoriesResource(classLoader, resourceLocation);
factories.forEach((factoryClassName, implementationClassNames) ->
registerHints(hints, classLoader, factoryClassName, implementationClassNames));
}

private void registerHints(RuntimeHints hints, ClassLoader classLoader,
String factoryClassName, List<String> implementationClassNames) {
Class<?> factoryClass = resolveClassName(classLoader, factoryClassName);
if(factoryClass == null) {
logger.trace(LogMessage.format("Skipping factories for [%s]", factoryClassName));
return;
}
logger.trace(LogMessage.format("Processing factories for [%s]", factoryClassName));
hints.reflection().registerType(factoryClass, HINT);
for (String implementationClassName : implementationClassNames) {
Class<?> implementationType = resolveClassName(classLoader, implementationClassName);
logger.trace(LogMessage.format("%s factory type [%s] and implementation [%s]",
(implementationType != null) ? "Processing" : "Skipping", factoryClassName, implementationClassName));
if (implementationType != null) {
hints.reflection().registerType(implementationType, HINT);
}
}
}

@Nullable
private Class<?> resolveClassName(ClassLoader classLoader, String factoryClassName) {
try {
return ClassUtils.resolveClassName(factoryClassName, classLoader);
}
catch (Exception ex) {
return null;
}
}

}
3 changes: 2 additions & 1 deletion spring-core/src/main/resources/META-INF/spring/aot.factories
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
org.springframework.aot.hint.RuntimeHintsRegistrar=\
org.springframework.core.annotation.CoreAnnotationsRuntimeHintsRegistrar
org.springframework.core.annotation.CoreAnnotationsRuntimeHintsRegistrar,\
org.springframework.core.io.support.SpringFactoriesLoaderRuntimeHintsRegistrar
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.core.io.support;

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

import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeHint;
import org.springframework.aot.hint.TypeReference;
import org.springframework.util.ClassUtils;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link SpringFactoriesLoaderRuntimeHintsRegistrar}.
*
* @author Phillip Webb
*/
class SpringFactoriesLoaderRuntimeHintsRegistrarTests {

private RuntimeHints hints;

@BeforeEach
void setup() {
this.hints = new RuntimeHints();
SpringFactoriesLoader.forResourceLocation("META-INF/spring/aot.factories")
.load(RuntimeHintsRegistrar.class).forEach(registrar -> registrar
.registerHints(this.hints, ClassUtils.getDefaultClassLoader()));
}

@Test
void resourceLocationHasHints() {
assertThat(this.hints.resources().resourcePatterns())
.anySatisfy(hint -> assertThat(hint.getIncludes())
.contains(SpringFactoriesLoader.FACTORIES_RESOURCE_LOCATION));
}

@Test
void factoryTypeHasHint() {
TypeReference type = TypeReference.of(DummyFactory.class);
assertThat(this.hints.reflection().getTypeHint(type))
.satisfies(this::expectedHints);
}

@Test
void factoryImplementationHasHint() {
TypeReference type = TypeReference.of(MyDummyFactory1.class);
assertThat(this.hints.reflection().getTypeHint(type))
.satisfies(this::expectedHints);
}

private void expectedHints(TypeHint hint) {
assertThat(hint.getMemberCategories())
.containsExactly(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
}

}

0 comments on commit 267b914

Please sign in to comment.