Skip to content

Commit 5f6b8d5

Browse files
committed
Polishing
1 parent d6e4bd7 commit 5f6b8d5

File tree

6 files changed

+32
-28
lines changed

6 files changed

+32
-28
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/aot/AutowiredArgumentsCodeGenerator.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -60,21 +60,18 @@ public CodeBlock generateCode(Class<?>[] parameterTypes, int startIndex) {
6060
return generateCode(parameterTypes, startIndex, "args");
6161
}
6262

63-
public CodeBlock generateCode(Class<?>[] parameterTypes, int startIndex,
64-
String variableName) {
65-
63+
public CodeBlock generateCode(Class<?>[] parameterTypes, int startIndex, String variableName) {
6664
Assert.notNull(parameterTypes, "'parameterTypes' must not be null");
6765
Assert.notNull(variableName, "'variableName' must not be null");
6866
boolean ambiguous = isAmbiguous();
6967
CodeBlock.Builder code = CodeBlock.builder();
7068
for (int i = startIndex; i < parameterTypes.length; i++) {
71-
code.add((i != startIndex) ? ", " : "");
69+
code.add(i > startIndex ? ", " : "");
7270
if (!ambiguous) {
7371
code.add("$L.get($L)", variableName, i - startIndex);
7472
}
7573
else {
76-
code.add("$L.get($L, $T.class)", variableName, i - startIndex,
77-
parameterTypes[i]);
74+
code.add("$L.get($L, $T.class)", variableName, i - startIndex, parameterTypes[i]);
7875
}
7976
}
8077
return code.build();

spring-beans/src/main/java/org/springframework/beans/factory/aot/AutowiredFieldValueResolver.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -58,16 +58,14 @@ public final class AutowiredFieldValueResolver extends AutowiredElementResolver
5858
private final boolean required;
5959

6060
@Nullable
61-
private final String shortcut;
61+
private final String shortcutBeanName;
6262

6363

64-
private AutowiredFieldValueResolver(String fieldName, boolean required,
65-
@Nullable String shortcut) {
66-
64+
private AutowiredFieldValueResolver(String fieldName, boolean required, @Nullable String shortcut) {
6765
Assert.hasText(fieldName, "'fieldName' must not be empty");
6866
this.fieldName = fieldName;
6967
this.required = required;
70-
this.shortcut = shortcut;
68+
this.shortcutBeanName = shortcut;
7169
}
7270

7371

@@ -97,7 +95,7 @@ public static AutowiredFieldValueResolver forRequiredField(String fieldName) {
9795
* direct bean name injection shortcut.
9896
* @param beanName the bean name to use as a shortcut
9997
* @return a new {@link AutowiredFieldValueResolver} instance that uses the
100-
* shortcuts
98+
* given shortcut bean name
10199
*/
102100
public AutowiredFieldValueResolver withShortcut(String beanName) {
103101
return new AutowiredFieldValueResolver(this.fieldName, this.required, beanName);
@@ -178,8 +176,8 @@ private Object resolveValue(RegisteredBean registeredBean, Field field) {
178176
ConfigurableBeanFactory beanFactory = registeredBean.getBeanFactory();
179177
DependencyDescriptor descriptor = new DependencyDescriptor(field, this.required);
180178
descriptor.setContainingClass(beanClass);
181-
if (this.shortcut != null) {
182-
descriptor = new ShortcutDependencyDescriptor(descriptor, this.shortcut);
179+
if (this.shortcutBeanName != null) {
180+
descriptor = new ShortcutDependencyDescriptor(descriptor, this.shortcutBeanName);
183181
}
184182
Set<String> autowiredBeanNames = new LinkedHashSet<>(1);
185183
TypeConverter typeConverter = beanFactory.getTypeConverter();

spring-beans/src/main/java/org/springframework/beans/factory/aot/AutowiredMethodArgumentsResolver.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,17 @@ public final class AutowiredMethodArgumentsResolver extends AutowiredElementReso
6363
private final boolean required;
6464

6565
@Nullable
66-
private final String[] shortcuts;
66+
private final String[] shortcutBeanNames;
6767

6868

6969
private AutowiredMethodArgumentsResolver(String methodName, Class<?>[] parameterTypes,
70-
boolean required, @Nullable String[] shortcuts) {
70+
boolean required, @Nullable String[] shortcutBeanNames) {
7171

7272
Assert.hasText(methodName, "'methodName' must not be empty");
7373
this.methodName = methodName;
7474
this.parameterTypes = parameterTypes;
7575
this.required = required;
76-
this.shortcuts = shortcuts;
76+
this.shortcutBeanNames = shortcutBeanNames;
7777
}
7878

7979

@@ -105,7 +105,7 @@ public static AutowiredMethodArgumentsResolver forRequiredMethod(String methodNa
105105
* @param beanNames the bean names to use as shortcuts (aligned with the
106106
* method parameters)
107107
* @return a new {@link AutowiredMethodArgumentsResolver} instance that uses
108-
* the shortcuts
108+
* the given shortcut bean names
109109
*/
110110
public AutowiredMethodArgumentsResolver withShortcut(String... beanNames) {
111111
return new AutowiredMethodArgumentsResolver(this.methodName, this.parameterTypes, this.required, beanNames);
@@ -171,7 +171,7 @@ private AutowiredArguments resolveArguments(RegisteredBean registeredBean,
171171
MethodParameter parameter = new MethodParameter(method, i);
172172
DependencyDescriptor descriptor = new DependencyDescriptor(parameter, this.required);
173173
descriptor.setContainingClass(beanClass);
174-
String shortcut = (this.shortcuts != null ? this.shortcuts[i] : null);
174+
String shortcut = (this.shortcutBeanNames != null ? this.shortcutBeanNames[i] : null);
175175
if (shortcut != null) {
176176
descriptor = new ShortcutDependencyDescriptor(descriptor, shortcut);
177177
}

spring-context/src/test/java/org/springframework/context/aot/ApplicationContextAotGeneratorTests.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -644,25 +644,29 @@ private static TestGenerationContext processAheadOfTime(GenericApplicationContex
644644

645645
private static void testCompiledResult(GenericApplicationContext applicationContext,
646646
BiConsumer<ApplicationContextInitializer<GenericApplicationContext>, Compiled> result) {
647+
647648
testCompiledResult(processAheadOfTime(applicationContext), result);
648649
}
649650

650-
@SuppressWarnings({ "rawtypes", "unchecked" })
651+
@SuppressWarnings("unchecked")
651652
private static void testCompiledResult(TestGenerationContext generationContext,
652653
BiConsumer<ApplicationContextInitializer<GenericApplicationContext>, Compiled> result) {
654+
653655
TestCompiler.forSystem().with(generationContext).compile(compiled ->
654656
result.accept(compiled.getInstance(ApplicationContextInitializer.class), compiled));
655657
}
656658

657659
private static GenericApplicationContext toFreshApplicationContext(
658660
ApplicationContextInitializer<GenericApplicationContext> initializer) {
661+
659662
GenericApplicationContext freshApplicationContext = createFreshApplicationContext(initializer);
660663
freshApplicationContext.refresh();
661664
return freshApplicationContext;
662665
}
663666

664667
private static GenericApplicationContext createFreshApplicationContext(
665668
ApplicationContextInitializer<GenericApplicationContext> initializer) {
669+
666670
GenericApplicationContext freshApplicationContext = new GenericApplicationContext();
667671
initializer.initialize(freshApplicationContext);
668672
return freshApplicationContext;

spring-core/src/main/java/org/springframework/aot/generate/ValueCodeGenerationException.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@ public class ValueCodeGenerationException extends RuntimeException {
3030
@Nullable
3131
private final Object value;
3232

33+
3334
protected ValueCodeGenerationException(String message, @Nullable Object value, @Nullable Throwable cause) {
3435
super(message, cause);
3536
this.value = value;
@@ -49,9 +50,9 @@ private static String buildErrorMessage(@Nullable Object value) {
4950
return message.toString();
5051
}
5152

53+
5254
/**
5355
* Return the value that failed to be generated.
54-
* @return the value
5556
*/
5657
@Nullable
5758
public Object getValue() {

spring-core/src/main/java/org/springframework/aot/generate/ValueCodeGenerator.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,20 +33,24 @@
3333
*/
3434
public final class ValueCodeGenerator {
3535

36-
private static final ValueCodeGenerator INSTANCE = new ValueCodeGenerator(ValueCodeGeneratorDelegates.INSTANCES, null);
36+
private static final ValueCodeGenerator INSTANCE =
37+
new ValueCodeGenerator(ValueCodeGeneratorDelegates.INSTANCES, null);
3738

3839
private static final CodeBlock NULL_VALUE_CODE_BLOCK = CodeBlock.of("null");
3940

41+
4042
private final List<Delegate> delegates;
4143

4244
@Nullable
4345
private final GeneratedMethods generatedMethods;
4446

47+
4548
private ValueCodeGenerator(List<Delegate> delegates, @Nullable GeneratedMethods generatedMethods) {
4649
this.delegates = delegates;
4750
this.generatedMethods = generatedMethods;
4851
}
4952

53+
5054
/**
5155
* Return an instance that provides support for {@linkplain
5256
* ValueCodeGeneratorDelegates#INSTANCES common value types}.
@@ -75,6 +79,7 @@ public static ValueCodeGenerator with(List<Delegate> delegates) {
7579
return new ValueCodeGenerator(new ArrayList<>(delegates), null);
7680
}
7781

82+
7883
public ValueCodeGenerator add(List<Delegate> additionalDelegates) {
7984
Assert.notEmpty(additionalDelegates, "AdditionalDelegates must not be empty");
8085
List<Delegate> allDelegates = new ArrayList<>(this.delegates);
@@ -117,7 +122,6 @@ public CodeBlock generateCode(@Nullable Object value) {
117122
}
118123
}
119124

120-
121125
/**
122126
* Return the {@link GeneratedMethods} that represents the scope
123127
* in which code generated by this instance will be added, or
@@ -129,6 +133,7 @@ public GeneratedMethods getGeneratedMethods() {
129133
return this.generatedMethods;
130134
}
131135

136+
132137
/**
133138
* Strategy interface that can be used to implement code generation for a
134139
* particular value type.
@@ -146,7 +151,6 @@ public interface Delegate {
146151
*/
147152
@Nullable
148153
CodeBlock generateCode(ValueCodeGenerator valueCodeGenerator, Object value);
149-
150154
}
151155

152156
}

0 commit comments

Comments
 (0)