-
Notifications
You must be signed in to change notification settings - Fork 38.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add native hints for SpringFactoriesLoader file content
Add `SpringFactoriesLoaderRuntimeHintsRegistrar` which provides native hints for `spring.factories` content. Closes gh-27955 Co-authored-by: Phillip Webb <[email protected]>
- Loading branch information
Showing
4 changed files
with
178 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
.../java/org/springframework/core/io/support/SpringFactoriesLoaderRuntimeHintsRegistrar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
74 changes: 74 additions & 0 deletions
74
.../org/springframework/core/io/support/SpringFactoriesLoaderRuntimeHintsRegistrarTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |