Polishing

This commit is contained in:
Juergen Hoeller 2022-10-06 12:03:25 +02:00
parent aedef9321a
commit b45a48461f
12 changed files with 71 additions and 100 deletions

View File

@ -44,6 +44,7 @@ public interface BeanRegistrationAotContribution {
*/
default BeanRegistrationCodeFragments customizeBeanRegistrationCodeFragments(
GenerationContext generationContext, BeanRegistrationCodeFragments codeFragments) {
return codeFragments;
}
@ -52,8 +53,7 @@ public interface BeanRegistrationAotContribution {
* @param generationContext the generation context
* @param beanRegistrationCode the generated registration
*/
void applyTo(GenerationContext generationContext,
BeanRegistrationCode beanRegistrationCode);
void applyTo(GenerationContext generationContext, BeanRegistrationCode beanRegistrationCode);
/**
* Create a {@link BeanRegistrationAotContribution} that customizes
@ -66,20 +66,19 @@ public interface BeanRegistrationAotContribution {
*/
static BeanRegistrationAotContribution withCustomCodeFragments(
UnaryOperator<BeanRegistrationCodeFragments> defaultCodeFragments) {
Assert.notNull(defaultCodeFragments, "'defaultCodeFragments' must not be null");
return new BeanRegistrationAotContribution() {
Assert.notNull(defaultCodeFragments, "'defaultCodeFragments' must not be null");
return new BeanRegistrationAotContribution() {
@Override
public BeanRegistrationCodeFragments customizeBeanRegistrationCodeFragments(
GenerationContext generationContext, BeanRegistrationCodeFragments codeFragments) {
return defaultCodeFragments.apply(codeFragments);
}
@Override
public void applyTo(GenerationContext generationContext,
BeanRegistrationCode beanRegistrationCode) {
}
};
}

View File

@ -94,8 +94,7 @@ public class BeanRegistrationCodeFragmentsDecorator implements BeanRegistrationC
Executable constructorOrFactoryMethod, boolean allowDirectSupplierShortcut) {
return this.delegate.generateInstanceSupplierCode(generationContext,
beanRegistrationCode, constructorOrFactoryMethod,
allowDirectSupplierShortcut);
beanRegistrationCode, constructorOrFactoryMethod, allowDirectSupplierShortcut);
}
@Override

View File

@ -202,8 +202,7 @@ class DefaultBeanRegistrationCodeFragments implements BeanRegistrationCodeFragme
Executable constructorOrFactoryMethod, boolean allowDirectSupplierShortcut) {
return new InstanceSupplierCodeGenerator(generationContext,
beanRegistrationCode.getClassName(),
beanRegistrationCode.getMethods(), allowDirectSupplierShortcut)
beanRegistrationCode.getClassName(), beanRegistrationCode.getMethods(), allowDirectSupplierShortcut)
.generateCode(this.registeredBean, constructorOrFactoryMethod);
}

View File

@ -81,8 +81,7 @@ class InstanceSupplierCodeGenerator {
InstanceSupplierCodeGenerator(GenerationContext generationContext,
ClassName className, GeneratedMethods generatedMethods,
boolean allowDirectSupplierShortcut) {
ClassName className, GeneratedMethods generatedMethods, boolean allowDirectSupplierShortcut) {
this.generationContext = generationContext;
this.className = className;
@ -104,9 +103,7 @@ class InstanceSupplierCodeGenerator {
"No suitable executor found for " + registeredBean.getBeanName());
}
private CodeBlock generateCodeForConstructor(RegisteredBean registeredBean,
Constructor<?> constructor) {
private CodeBlock generateCodeForConstructor(RegisteredBean registeredBean, Constructor<?> constructor) {
String beanName = registeredBean.getBeanName();
Class<?> beanClass = registeredBean.getBeanClass();
Class<?> declaringClass = constructor.getDeclaringClass();
@ -122,18 +119,16 @@ class InstanceSupplierCodeGenerator {
private CodeBlock generateCodeForAccessibleConstructor(String beanName, Class<?> beanClass,
Constructor<?> constructor, boolean dependsOnBean, Class<?> declaringClass) {
this.generationContext.getRuntimeHints().reflection()
.registerConstructor(constructor, ExecutableMode.INTROSPECT);
this.generationContext.getRuntimeHints().reflection().registerConstructor(
constructor, ExecutableMode.INTROSPECT);
if (!dependsOnBean && constructor.getParameterCount() == 0) {
if (!this.allowDirectSupplierShortcut) {
return CodeBlock.of("$T.using($T::new)", InstanceSupplier.class,
declaringClass);
return CodeBlock.of("$T.using($T::new)", InstanceSupplier.class, declaringClass);
}
if (!isThrowingCheckedException(constructor)) {
return CodeBlock.of("$T::new", declaringClass);
}
return CodeBlock.of("$T.of($T::new)", ThrowingSupplier.class,
declaringClass);
return CodeBlock.of("$T.of($T::new)", ThrowingSupplier.class, declaringClass);
}
GeneratedMethod generatedMethod = generateGetInstanceSupplierMethod(method ->
buildGetInstanceMethodForConstructor(method, beanName, beanClass, constructor,
@ -167,12 +162,11 @@ class InstanceSupplierCodeGenerator {
CodeBlock.Builder code = CodeBlock.builder();
code.add(generateResolverForConstructor(beanClass, constructor, parameterOffset));
boolean hasArguments = constructor.getParameterCount() > 0;
CodeBlock arguments = hasArguments
? new AutowiredArgumentsCodeGenerator(declaringClass, constructor)
.generateCode(constructor.getParameterTypes(), parameterOffset)
CodeBlock arguments = hasArguments ?
new AutowiredArgumentsCodeGenerator(declaringClass, constructor)
.generateCode(constructor.getParameterTypes(), parameterOffset)
: NO_ARGS;
CodeBlock newInstance = generateNewInstanceCodeForConstructor(dependsOnBean,
declaringClass, arguments);
CodeBlock newInstance = generateNewInstanceCodeForConstructor(dependsOnBean, declaringClass, arguments);
code.add(generateWithGeneratorCode(hasArguments, newInstance));
method.addStatement(code.build());
}
@ -180,10 +174,8 @@ class InstanceSupplierCodeGenerator {
private CodeBlock generateResolverForConstructor(Class<?> beanClass,
Constructor<?> constructor, int parameterOffset) {
CodeBlock parameterTypes = generateParameterTypesCode(
constructor.getParameterTypes(), parameterOffset);
return CodeBlock.of("return $T.<$T>forConstructor($L)",
BeanInstanceSupplier.class, beanClass, parameterTypes);
CodeBlock parameterTypes = generateParameterTypesCode(constructor.getParameterTypes(), parameterOffset);
return CodeBlock.of("return $T.<$T>forConstructor($L)", BeanInstanceSupplier.class, beanClass, parameterTypes);
}
private CodeBlock generateNewInstanceCodeForConstructor(boolean dependsOnBean,
@ -207,18 +199,17 @@ class InstanceSupplierCodeGenerator {
boolean dependsOnBean = !Modifier.isStatic(factoryMethod.getModifiers());
Visibility accessVisibility = getAccessVisibility(registeredBean, factoryMethod);
if (accessVisibility != Visibility.PRIVATE) {
return generateCodeForAccessibleFactoryMethod(beanName, beanClass, factoryMethod,
declaringClass, dependsOnBean);
return generateCodeForAccessibleFactoryMethod(
beanName, beanClass, factoryMethod, declaringClass, dependsOnBean);
}
return generateCodeForInaccessibleFactoryMethod(beanName, beanClass, factoryMethod,
declaringClass);
return generateCodeForInaccessibleFactoryMethod(beanName, beanClass, factoryMethod, declaringClass);
}
private CodeBlock generateCodeForAccessibleFactoryMethod(String beanName,
Class<?> beanClass, Method factoryMethod, Class<?> declaringClass, boolean dependsOnBean) {
this.generationContext.getRuntimeHints().reflection()
.registerMethod(factoryMethod, ExecutableMode.INTROSPECT);
this.generationContext.getRuntimeHints().reflection().registerMethod(
factoryMethod, ExecutableMode.INTROSPECT);
if (!dependsOnBean && factoryMethod.getParameterCount() == 0) {
CodeBlock.Builder code = CodeBlock.builder();
code.add("$T.<$T>forFactoryMethod($T.class, $S)", BeanInstanceSupplier.class,
@ -235,14 +226,13 @@ class InstanceSupplierCodeGenerator {
private CodeBlock generateCodeForInaccessibleFactoryMethod(String beanName, Class<?> beanClass,
Method factoryMethod, Class<?> declaringClass) {
this.generationContext.getRuntimeHints().reflection()
.registerMethod(factoryMethod, ExecutableMode.INVOKE);
this.generationContext.getRuntimeHints().reflection().registerMethod(factoryMethod, ExecutableMode.INVOKE);
GeneratedMethod getInstanceMethod = generateGetInstanceSupplierMethod(method -> {
method.addJavadoc("Get the bean instance supplier for '$L'.", beanName);
method.addModifiers(PRIVATE_STATIC);
method.returns(ParameterizedTypeName.get(BeanInstanceSupplier.class, beanClass));
method.addStatement(generateInstanceSupplierForFactoryMethod(beanClass, factoryMethod,
declaringClass, factoryMethod.getName()));
method.addStatement(generateInstanceSupplierForFactoryMethod(
beanClass, factoryMethod, declaringClass, factoryMethod.getName()));
});
return generateReturnStatement(getInstanceMethod);
}
@ -256,14 +246,15 @@ class InstanceSupplierCodeGenerator {
method.addModifiers(modifiers);
method.returns(ParameterizedTypeName.get(BeanInstanceSupplier.class, beanClass));
CodeBlock.Builder code = CodeBlock.builder();
code.add(generateInstanceSupplierForFactoryMethod(beanClass, factoryMethod, declaringClass, factoryMethodName));
code.add(generateInstanceSupplierForFactoryMethod(
beanClass, factoryMethod, declaringClass, factoryMethodName));
boolean hasArguments = factoryMethod.getParameterCount() > 0;
CodeBlock arguments = hasArguments
? new AutowiredArgumentsCodeGenerator(declaringClass, factoryMethod)
.generateCode(factoryMethod.getParameterTypes())
CodeBlock arguments = hasArguments ?
new AutowiredArgumentsCodeGenerator(declaringClass, factoryMethod)
.generateCode(factoryMethod.getParameterTypes())
: NO_ARGS;
CodeBlock newInstance = generateNewInstanceCodeForMethod(dependsOnBean,
declaringClass, factoryMethodName, arguments);
CodeBlock newInstance = generateNewInstanceCodeForMethod(
dependsOnBean, declaringClass, factoryMethodName, arguments);
code.add(generateWithGeneratorCode(hasArguments, newInstance));
method.addStatement(code.build());
}
@ -276,11 +267,9 @@ class InstanceSupplierCodeGenerator {
BeanInstanceSupplier.class, beanClass, declaringClass,
factoryMethodName);
}
CodeBlock parameterTypes = generateParameterTypesCode(
factoryMethod.getParameterTypes(), 0);
CodeBlock parameterTypes = generateParameterTypesCode(factoryMethod.getParameterTypes(), 0);
return CodeBlock.of("return $T.<$T>forFactoryMethod($T.class, $S, $L)",
BeanInstanceSupplier.class, beanClass, declaringClass,
factoryMethodName, parameterTypes);
BeanInstanceSupplier.class, beanClass, declaringClass, factoryMethodName, parameterTypes);
}
private CodeBlock generateNewInstanceCodeForMethod(boolean dependsOnBean,
@ -310,11 +299,8 @@ class InstanceSupplierCodeGenerator {
return code.build();
}
private Visibility getAccessVisibility(RegisteredBean registeredBean,
Member member) {
AccessControl beanTypeAccessControl = AccessControl
.forResolvableType(registeredBean.getBeanType());
private Visibility getAccessVisibility(RegisteredBean registeredBean, Member member) {
AccessControl beanTypeAccessControl = AccessControl.forResolvableType(registeredBean.getBeanType());
AccessControl memberAccessControl = AccessControl.forMember(member);
return AccessControl.lowest(beanTypeAccessControl, memberAccessControl).getVisibility();
}

View File

@ -1351,7 +1351,6 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
* @param mbd the bean definition for the bean
* @param bw the BeanWrapper with bean instance
*/
@SuppressWarnings("deprecation") // for postProcessPropertyValues
protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) {
if (bw == null) {
if (mbd.hasPropertyValues()) {

View File

@ -256,10 +256,11 @@ public class BeanDefinitionValueResolver {
*/
public <T> T resolveInnerBean(@Nullable String innerBeanName, BeanDefinition innerBd,
BiFunction<String, RootBeanDefinition, T> resolver) {
String nameToUse = (innerBeanName != null ? innerBeanName : "(inner bean)"
+ BeanFactoryUtils.GENERATED_BEAN_NAME_SEPARATOR + ObjectUtils.getIdentityHexString(innerBd));
return resolver.apply(nameToUse, this.beanFactory.getMergedBeanDefinition(
nameToUse, innerBd, this.beanDefinition));
String nameToUse = (innerBeanName != null ? innerBeanName : "(inner bean)" +
BeanFactoryUtils.GENERATED_BEAN_NAME_SEPARATOR + ObjectUtils.getIdentityHexString(innerBd));
return resolver.apply(nameToUse,
this.beanFactory.getMergedBeanDefinition(nameToUse, innerBd, this.beanDefinition));
}
/**

View File

@ -208,8 +208,7 @@ class ConfigurationClassEnhancer {
* Also exposes the application ClassLoader as thread context ClassLoader for the time of
* class generation (in order for ASM to pick it up when doing common superclass resolution).
*/
private static class BeanFactoryAwareGeneratorStrategy extends
ClassLoaderAwareGeneratorStrategy {
private static class BeanFactoryAwareGeneratorStrategy extends ClassLoaderAwareGeneratorStrategy {
public BeanFactoryAwareGeneratorStrategy(@Nullable ClassLoader classLoader) {
super(classLoader);

View File

@ -61,8 +61,7 @@ public abstract class AotProcessor {
private final String artifactId;
/**
* Create a new instance.
*
* Create a new processor instance.
* @param application the application entry point
* @param sourceOutput the location of generated sources
* @param resourceOutput the location of generated resources
@ -74,6 +73,7 @@ public abstract class AotProcessor {
*/
protected AotProcessor(Class<?> application, Path sourceOutput, Path resourceOutput,
Path classOutput, String groupId, String artifactId) {
this.application = application;
this.sourceOutput = sourceOutput;
this.resourceOutput = resourceOutput;
@ -158,6 +158,7 @@ public abstract class AotProcessor {
private void registerEntryPointHint(DefaultGenerationContext generationContext,
ClassName generatedInitializerClassName) {
TypeReference generatedType = TypeReference.of(generatedInitializerClassName.canonicalName());
TypeReference applicationType = TypeReference.of(this.application);
ReflectionHints reflection = generationContext.getRuntimeHints().reflection();

View File

@ -430,6 +430,10 @@ public class ReflectUtils {
}
// SPRING PATCH BEGIN
public static void setGeneratedClassHandler(BiConsumer<String, byte[]> handler) {
generatedClassHandler = handler;
}
public static Class defineClass(String className, byte[] b, ClassLoader loader) throws Exception {
return defineClass(className, b, loader, null, null);
}
@ -440,10 +444,6 @@ public class ReflectUtils {
return defineClass(className, b, loader, protectionDomain, null);
}
public static void setGeneratedClassHandler(BiConsumer<String, byte[]> handler) {
generatedClassHandler = handler;
}
@SuppressWarnings({"deprecation", "serial"})
public static Class defineClass(String className, byte[] b, ClassLoader loader,
ProtectionDomain protectionDomain, Class<?> contextClass) throws Exception {

View File

@ -97,10 +97,6 @@ public class SpringFactoriesLoader {
*/
public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";
private static final ArgumentResolver NO_ARGUMENT_RESOLVER = null;
private static final FailureHandler NO_FAILURE_HANDLER = null;
private static final FailureHandler THROWING_FAILURE_HANDLER = FailureHandler.throwing();
private static final Log logger = LogFactory.getLog(SpringFactoriesLoader.class);
@ -143,7 +139,7 @@ public class SpringFactoriesLoader {
* @since 6.0
*/
public <T> List<T> load(Class<T> factoryType) {
return load(factoryType, NO_ARGUMENT_RESOLVER, NO_FAILURE_HANDLER);
return load(factoryType, null, null);
}
/**
@ -161,7 +157,7 @@ public class SpringFactoriesLoader {
* @since 6.0
*/
public <T> List<T> load(Class<T> factoryType, @Nullable ArgumentResolver argumentResolver) {
return load(factoryType, argumentResolver, NO_FAILURE_HANDLER);
return load(factoryType, argumentResolver, null);
}
/**
@ -179,7 +175,7 @@ public class SpringFactoriesLoader {
* @since 6.0
*/
public <T> List<T> load(Class<T> factoryType, @Nullable FailureHandler failureHandler) {
return load(factoryType, NO_ARGUMENT_RESOLVER, failureHandler);
return load(factoryType, null, failureHandler);
}
/**
@ -198,7 +194,9 @@ public class SpringFactoriesLoader {
* @param failureHandler strategy used to handle factory instantiation failures
* @since 6.0
*/
public <T> List<T> load(Class<T> factoryType, @Nullable ArgumentResolver argumentResolver, @Nullable FailureHandler failureHandler) {
public <T> List<T> load(Class<T> factoryType, @Nullable ArgumentResolver argumentResolver,
@Nullable FailureHandler failureHandler) {
Assert.notNull(factoryType, "'factoryType' must not be null");
List<String> implementationNames = loadFactoryNames(factoryType);
logger.trace(LogMessage.format("Loaded [%s] names: %s", factoryType.getName(), implementationNames));
@ -224,8 +222,8 @@ public class SpringFactoriesLoader {
try {
Class<?> factoryImplementationClass = ClassUtils.forName(implementationName, this.classLoader);
Assert.isTrue(type.isAssignableFrom(factoryImplementationClass),
() -> "Class [%s] is not assignable to factory type [%s]".formatted(implementationName, type.getName()));
Assert.isTrue(type.isAssignableFrom(factoryImplementationClass), () ->
"Class [%s] is not assignable to factory type [%s]".formatted(implementationName, type.getName()));
FactoryInstantiator<T> factoryInstantiator = FactoryInstantiator.forClass(factoryImplementationClass);
return factoryInstantiator.instantiate(argumentResolver);
}
@ -303,8 +301,8 @@ public class SpringFactoriesLoader {
/**
* Create a {@link SpringFactoriesLoader} instance that will load and
* instantiate the factory implementations from the given location, using
* the default class loader.
* instantiate the factory implementations from the given location,
* using the default class loader.
* @param resourceLocation the resource location to look for factories
* @return a {@link SpringFactoriesLoader} instance
* @since 6.0
@ -316,11 +314,11 @@ public class SpringFactoriesLoader {
/**
* Create a {@link SpringFactoriesLoader} instance that will load and
* instantiate the factory implementations from the given location, using
* the given class loader.
* instantiate the factory implementations from the given location,
* using the given class loader.
* @param resourceLocation the resource location to look for factories
* @param classLoader the ClassLoader to use for loading resources; can be
* {@code null} to use the default
* @param classLoader the ClassLoader to use for loading resources;
* can be {@code null} to use the default
* @return a {@link SpringFactoriesLoader} instance
* @since 6.0
* @see #forResourceLocation(String)
@ -329,7 +327,7 @@ public class SpringFactoriesLoader {
Assert.hasText(resourceLocation, "'resourceLocation' must not be empty");
ClassLoader resourceClassLoader = (classLoader != null ? classLoader :
SpringFactoriesLoader.class.getClassLoader());
Map<String, SpringFactoriesLoader> loaders = SpringFactoriesLoader.cache.computeIfAbsent(
Map<String, SpringFactoriesLoader> loaders = cache.computeIfAbsent(
resourceClassLoader, key -> new ConcurrentReferenceHashMap<>());
return loaders.computeIfAbsent(resourceLocation, key ->
new SpringFactoriesLoader(classLoader, loadFactoriesResource(resourceClassLoader, resourceLocation)));
@ -345,7 +343,7 @@ public class SpringFactoriesLoader {
properties.forEach((name, value) -> {
List<String> implementations = result.computeIfAbsent(((String) name).trim(), key -> new ArrayList<>());
Arrays.stream(StringUtils.commaDelimitedListToStringArray((String) value))
.map(String::trim).forEach(implementations::add);
.map(String::trim).forEach(implementations::add);
});
}
result.replaceAll(SpringFactoriesLoader::toDistinctUnmodifiableList);
@ -370,13 +368,11 @@ public class SpringFactoriesLoader {
private final Constructor<T> constructor;
private FactoryInstantiator(Constructor<T> constructor) {
ReflectionUtils.makeAccessible(constructor);
this.constructor = constructor;
}
T instantiate(@Nullable ArgumentResolver argumentResolver) throws Exception {
Object[] args = resolveArgs(argumentResolver);
if (isKotlinType(this.constructor.getDeclaringClass())) {
@ -437,7 +433,6 @@ public class SpringFactoriesLoader {
return null;
}
}
}
@ -497,7 +492,6 @@ public class SpringFactoriesLoader {
private static <T> T instantiate(KFunction<T> kotlinConstructor, Map<KParameter, Object> args) {
return kotlinConstructor.callBy(args);
}
}
@ -609,7 +603,6 @@ public class SpringFactoriesLoader {
};
}
}
@ -681,7 +674,6 @@ public class SpringFactoriesLoader {
messageHandler.accept(messageSupplier, failure);
};
}
}
}

View File

@ -47,9 +47,9 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
class PathMatchingResourcePatternResolverTests {
private static final String[] CLASSES_IN_CORE_IO_SUPPORT = { "EncodedResource.class",
"LocalizedResourceHelper.class", "PathMatchingResourcePatternResolver.class", "PropertiesLoaderSupport.class",
"PropertiesLoaderUtils.class", "ResourceArrayPropertyEditor.class", "ResourcePatternResolver.class",
"ResourcePatternUtils.class", "SpringFactoriesLoader.class" };
"LocalizedResourceHelper.class", "PathMatchingResourcePatternResolver.class", "PropertiesLoaderSupport.class",
"PropertiesLoaderUtils.class", "ResourceArrayPropertyEditor.class", "ResourcePatternResolver.class",
"ResourcePatternUtils.class", "SpringFactoriesLoader.class" };
private static final String[] TEST_CLASSES_IN_CORE_IO_SUPPORT = { "PathMatchingResourcePatternResolverTests.class" };
@ -99,7 +99,7 @@ class PathMatchingResourcePatternResolverTests {
}
@Test
void usingFilePrototol() {
void usingFileProtocol() {
Path testResourcesDir = Path.of("src/test/resources").toAbsolutePath();
String pattern = "file:%s/scanned-resources/**".formatted(testResourcesDir);
String pathPrefix = ".+scanned-resources/";
@ -107,9 +107,7 @@ class PathMatchingResourcePatternResolverTests {
assertExactFilenames(pattern, "resource#test1.txt", "resource#test2.txt");
assertExactSubPaths(pattern, pathPrefix, "resource#test1.txt", "resource#test2.txt");
}
}
}
@ -147,7 +145,6 @@ class PathMatchingResourcePatternResolverTests {
.as("Could not find aspectj_1_5_0.dtd in the root of the aspectjweaver jar")
.containsExactly("aspectj_1_5_0.dtd");
}
}

View File

@ -67,7 +67,6 @@ public class TestAotProcessor {
/**
* Create a new processor for the specified test classpath roots and
* general settings.
*
* @param classpathRoots the classpath roots to scan for test classes
* @param sourceOutput the location of generated sources
* @param resourceOutput the location of generated resources