diff --git a/spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoader.java b/spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoader.java index 7cf9836e62e..dc2a952b065 100644 --- a/spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoader.java +++ b/spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoader.java @@ -217,7 +217,9 @@ public class SpringFactoriesLoader { } @Nullable - protected T instantiateFactory(String implementationName, Class type, @Nullable ArgumentResolver argumentResolver, FailureHandler failureHandler) { + protected T instantiateFactory(String implementationName, Class type, + @Nullable ArgumentResolver argumentResolver, FailureHandler failureHandler) { + try { Class factoryImplementationClass = ClassUtils.forName(implementationName, this.classLoader); Assert.isTrue(type.isAssignableFrom(factoryImplementationClass), @@ -333,7 +335,7 @@ public class SpringFactoriesLoader { return loader; } - private static Map> loadFactoriesResource(ClassLoader classLoader, String resourceLocation) { + static Map> loadFactoriesResource(ClassLoader classLoader, String resourceLocation) { Map> result = new LinkedHashMap<>(); try { Enumeration urls = classLoader.getResources(resourceLocation); diff --git a/spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoaderRuntimeHintsRegistrar.java b/spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoaderRuntimeHintsRegistrar.java new file mode 100644 index 00000000000..17edcf087d7 --- /dev/null +++ b/spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoaderRuntimeHintsRegistrar.java @@ -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 RESOURCE_LOCATIONS = List + .of(SpringFactoriesLoader.FACTORIES_RESOURCE_LOCATION); + + private static final Consumer 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> factories = SpringFactoriesLoader + .loadFactoriesResource(classLoader, resourceLocation); + factories.forEach((factoryClassName, implementationClassNames) -> + registerHints(hints, classLoader, factoryClassName, implementationClassNames)); + } + + private void registerHints(RuntimeHints hints, ClassLoader classLoader, + String factoryClassName, List 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; + } + } + +} \ No newline at end of file diff --git a/spring-core/src/main/resources/META-INF/spring/aot.factories b/spring-core/src/main/resources/META-INF/spring/aot.factories index dad3ddfbee9..ba5ee0bc830 100644 --- a/spring-core/src/main/resources/META-INF/spring/aot.factories +++ b/spring-core/src/main/resources/META-INF/spring/aot.factories @@ -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 diff --git a/spring-core/src/test/java/org/springframework/core/io/support/SpringFactoriesLoaderRuntimeHintsRegistrarTests.java b/spring-core/src/test/java/org/springframework/core/io/support/SpringFactoriesLoaderRuntimeHintsRegistrarTests.java new file mode 100644 index 00000000000..47868ae0bf4 --- /dev/null +++ b/spring-core/src/test/java/org/springframework/core/io/support/SpringFactoriesLoaderRuntimeHintsRegistrarTests.java @@ -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); + } + +}