Polishing

This commit is contained in:
Juergen Hoeller
2024-09-24 18:59:17 +02:00
parent d6e4bd7c90
commit 5f6b8d5582
6 changed files with 32 additions and 28 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2024 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.
@@ -60,21 +60,18 @@ public class AutowiredArgumentsCodeGenerator {
return generateCode(parameterTypes, startIndex, "args");
}
public CodeBlock generateCode(Class<?>[] parameterTypes, int startIndex,
String variableName) {
public CodeBlock generateCode(Class<?>[] parameterTypes, int startIndex, String variableName) {
Assert.notNull(parameterTypes, "'parameterTypes' must not be null");
Assert.notNull(variableName, "'variableName' must not be null");
boolean ambiguous = isAmbiguous();
CodeBlock.Builder code = CodeBlock.builder();
for (int i = startIndex; i < parameterTypes.length; i++) {
code.add((i != startIndex) ? ", " : "");
code.add(i > startIndex ? ", " : "");
if (!ambiguous) {
code.add("$L.get($L)", variableName, i - startIndex);
}
else {
code.add("$L.get($L, $T.class)", variableName, i - startIndex,
parameterTypes[i]);
code.add("$L.get($L, $T.class)", variableName, i - startIndex, parameterTypes[i]);
}
}
return code.build();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
@@ -58,16 +58,14 @@ public final class AutowiredFieldValueResolver extends AutowiredElementResolver
private final boolean required;
@Nullable
private final String shortcut;
private final String shortcutBeanName;
private AutowiredFieldValueResolver(String fieldName, boolean required,
@Nullable String shortcut) {
private AutowiredFieldValueResolver(String fieldName, boolean required, @Nullable String shortcut) {
Assert.hasText(fieldName, "'fieldName' must not be empty");
this.fieldName = fieldName;
this.required = required;
this.shortcut = shortcut;
this.shortcutBeanName = shortcut;
}
@@ -97,7 +95,7 @@ public final class AutowiredFieldValueResolver extends AutowiredElementResolver
* direct bean name injection shortcut.
* @param beanName the bean name to use as a shortcut
* @return a new {@link AutowiredFieldValueResolver} instance that uses the
* shortcuts
* given shortcut bean name
*/
public AutowiredFieldValueResolver withShortcut(String beanName) {
return new AutowiredFieldValueResolver(this.fieldName, this.required, beanName);
@@ -178,8 +176,8 @@ public final class AutowiredFieldValueResolver extends AutowiredElementResolver
ConfigurableBeanFactory beanFactory = registeredBean.getBeanFactory();
DependencyDescriptor descriptor = new DependencyDescriptor(field, this.required);
descriptor.setContainingClass(beanClass);
if (this.shortcut != null) {
descriptor = new ShortcutDependencyDescriptor(descriptor, this.shortcut);
if (this.shortcutBeanName != null) {
descriptor = new ShortcutDependencyDescriptor(descriptor, this.shortcutBeanName);
}
Set<String> autowiredBeanNames = new LinkedHashSet<>(1);
TypeConverter typeConverter = beanFactory.getTypeConverter();

View File

@@ -63,17 +63,17 @@ public final class AutowiredMethodArgumentsResolver extends AutowiredElementReso
private final boolean required;
@Nullable
private final String[] shortcuts;
private final String[] shortcutBeanNames;
private AutowiredMethodArgumentsResolver(String methodName, Class<?>[] parameterTypes,
boolean required, @Nullable String[] shortcuts) {
boolean required, @Nullable String[] shortcutBeanNames) {
Assert.hasText(methodName, "'methodName' must not be empty");
this.methodName = methodName;
this.parameterTypes = parameterTypes;
this.required = required;
this.shortcuts = shortcuts;
this.shortcutBeanNames = shortcutBeanNames;
}
@@ -105,7 +105,7 @@ public final class AutowiredMethodArgumentsResolver extends AutowiredElementReso
* @param beanNames the bean names to use as shortcuts (aligned with the
* method parameters)
* @return a new {@link AutowiredMethodArgumentsResolver} instance that uses
* the shortcuts
* the given shortcut bean names
*/
public AutowiredMethodArgumentsResolver withShortcut(String... beanNames) {
return new AutowiredMethodArgumentsResolver(this.methodName, this.parameterTypes, this.required, beanNames);
@@ -171,7 +171,7 @@ public final class AutowiredMethodArgumentsResolver extends AutowiredElementReso
MethodParameter parameter = new MethodParameter(method, i);
DependencyDescriptor descriptor = new DependencyDescriptor(parameter, this.required);
descriptor.setContainingClass(beanClass);
String shortcut = (this.shortcuts != null ? this.shortcuts[i] : null);
String shortcut = (this.shortcutBeanNames != null ? this.shortcutBeanNames[i] : null);
if (shortcut != null) {
descriptor = new ShortcutDependencyDescriptor(descriptor, shortcut);
}