Skip to content

Commit 09a8d9d

Browse files
committed
Polishing.
Remove test-only methods and move these into the tests. Improve encapsulation. Move AotRepositoryBeanDefinitionPropertiesDecorator from config to aot.generate package due to its strong coupling with aot.generate packages. See: #3344 Original pull request: #3351
1 parent 2092d4f commit 09a8d9d

13 files changed

+303
-270
lines changed

src/main/java/org/springframework/data/repository/aot/generate/AotQueryMethodGenerationContext.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,20 @@ public class AotQueryMethodGenerationContext {
5252
private final ExpressionMarker expressionMarker;
5353

5454
protected AotQueryMethodGenerationContext(RepositoryInformation repositoryInformation, Method method,
55-
QueryMethod queryMethod, AotRepositoryFragmentMetadata targetTypeMetadata) {
55+
QueryMethod queryMethod) {
56+
57+
this.method = method;
58+
this.annotations = MergedAnnotations.from(method);
59+
this.queryMethod = queryMethod;
60+
this.repositoryInformation = repositoryInformation;
61+
this.targetTypeMetadata = new AotRepositoryFragmentMetadata();
62+
this.targetMethodMetadata = new MethodMetadata(repositoryInformation, method);
63+
this.variableNameFactory = LocalVariableNameFactory.forMethod(targetMethodMetadata);
64+
this.expressionMarker = new ExpressionMarker();
65+
}
66+
67+
AotQueryMethodGenerationContext(RepositoryInformation repositoryInformation, Method method, QueryMethod queryMethod,
68+
AotRepositoryFragmentMetadata targetTypeMetadata) {
5669

5770
this.method = method;
5871
this.annotations = MergedAnnotations.from(method);
@@ -352,4 +365,5 @@ public String localVariable(String variableName) {
352365
public ExpressionMarker getExpressionMarker() {
353366
return expressionMarker;
354367
}
368+
355369
}
Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,23 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.data.repository.config;
16+
package org.springframework.data.repository.aot.generate;
1717

1818
import java.util.LinkedHashMap;
1919
import java.util.Map;
2020
import java.util.Map.Entry;
2121
import java.util.function.Supplier;
2222

23+
import javax.lang.model.element.Modifier;
24+
2325
import org.springframework.beans.factory.BeanFactory;
2426
import org.springframework.core.ResolvableType;
25-
import org.springframework.data.repository.aot.generate.RepositoryContributor;
2627
import org.springframework.data.repository.core.support.RepositoryComposition;
2728
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
2829
import org.springframework.javapoet.CodeBlock;
30+
import org.springframework.javapoet.MethodSpec;
2931
import org.springframework.javapoet.TypeName;
32+
import org.springframework.javapoet.TypeSpec;
3033
import org.springframework.util.Assert;
3134
import org.springframework.util.StringUtils;
3235

@@ -39,7 +42,7 @@
3942
* @author Christoph Strobl
4043
* @since 4.0
4144
*/
42-
class AotRepositoryBeanDefinitionPropertiesDecorator {
45+
public class AotRepositoryBeanDefinitionPropertiesDecorator {
4346

4447
private static final Map<ResolvableType, String> RESERVED_TYPES;
4548

@@ -57,10 +60,13 @@ class AotRepositoryBeanDefinitionPropertiesDecorator {
5760
* @param inheritedProperties bean definition code (containing properties and such) already added via another
5861
* component.
5962
* @param repositoryContributor the contributor providing the actual AOT repository implementation.
63+
* @throws IllegalArgumentException if {@link RepositoryContributor#getContributedTypeName()} is not set.
6064
*/
6165
public AotRepositoryBeanDefinitionPropertiesDecorator(Supplier<CodeBlock> inheritedProperties,
6266
RepositoryContributor repositoryContributor) {
6367

68+
Assert.notNull(repositoryContributor.getContributedTypeName(), "Contributed type name must not be null");
69+
6470
this.inheritedProperties = inheritedProperties;
6571
this.repositoryContributor = repositoryContributor;
6672
}
@@ -73,54 +79,58 @@ public AotRepositoryBeanDefinitionPropertiesDecorator(Supplier<CodeBlock> inheri
7379
* needs to have potential constructor arguments resolved.
7480
*
7581
* @return the decorated code block.
76-
* @throws IllegalArgumentException if {@link RepositoryContributor#getContributedTypeName()} is not set.
7782
*/
7883
public CodeBlock decorate() {
7984

80-
Assert.notNull(repositoryContributor.getContributedTypeName(), "Contributed type name must not be null");
81-
8285
CodeBlock.Builder builder = CodeBlock.builder();
86+
8387
// bring in properties as usual
8488
builder.add(inheritedProperties.get());
8589

86-
builder.add("beanDefinition.getPropertyValues().addPropertyValue(\"repositoryFragmentsFunction\", new $T() {\n",
87-
RepositoryFactoryBeanSupport.RepositoryFragmentsFunction.class);
88-
builder.indent();
90+
MethodSpec.Builder callbackMethod = MethodSpec.methodBuilder("getRepositoryFragments").addModifiers(Modifier.PUBLIC)
91+
.returns(RepositoryComposition.RepositoryFragments.class);
8992

90-
builder.add("public $T getRepositoryFragments(", RepositoryComposition.RepositoryFragments.class);
91-
int counter = 0;
9293
for (Entry<ResolvableType, String> entry : RESERVED_TYPES.entrySet()) {
93-
builder.add("$T $L", entry.getKey().toClass(), entry.getValue());
94-
if (++counter < RESERVED_TYPES.size()) {
95-
builder.add(", ");
96-
}
94+
callbackMethod.addParameter(entry.getKey().toClass(), entry.getValue());
9795
}
98-
builder.add(") {\n");
9996

100-
builder.indent();
97+
callbackMethod.addCode(buildCallbackBody());
98+
99+
TypeSpec repositoryFragmentsFunction = TypeSpec.anonymousClassBuilder("")
100+
.superclass(RepositoryFactoryBeanSupport.RepositoryFragmentsFunction.class).addMethod(callbackMethod.build())
101+
.build();
101102

102-
for (Map.Entry<String, ResolvableType> entry : repositoryContributor.requiredArgs().entrySet()) {
103+
builder.addStatement("beanDefinition.getPropertyValues().addPropertyValue($S, $L)", "repositoryFragmentsFunction",
104+
repositoryFragmentsFunction);
105+
106+
return builder.build();
107+
}
108+
109+
private CodeBlock buildCallbackBody() {
110+
111+
CodeBlock.Builder callback = CodeBlock.builder();
112+
113+
for (Entry<String, ResolvableType> entry : repositoryContributor.requiredArgs().entrySet()) {
103114

104115
TypeName argumentType = TypeName.get(entry.getValue().getType());
105116
String reservedArgumentName = RESERVED_TYPES.get(entry.getValue());
106117
if (reservedArgumentName == null) {
107-
builder.addStatement("$1T $2L = beanFactory.getBean($1T.class)", argumentType, entry.getKey());
118+
callback.addStatement("$1T $2L = beanFactory.getBean($1T.class)", argumentType, entry.getKey());
108119
} else {
109-
if (!reservedArgumentName.equals(entry.getKey())) {
110-
builder.addStatement("$T $L = $L", argumentType, entry.getKey(), reservedArgumentName);
120+
121+
if (reservedArgumentName.equals(entry.getKey())) {
122+
continue;
111123
}
124+
125+
callback.addStatement("$T $L = $L", argumentType, entry.getKey(), reservedArgumentName);
112126
}
113127
}
114128

115-
builder.addStatement("return RepositoryComposition.RepositoryFragments.just(new $L($L))",
129+
callback.addStatement("return $T.just(new $L($L))", RepositoryComposition.RepositoryFragments.class,
116130
repositoryContributor.getContributedTypeName().getCanonicalName(),
117131
StringUtils.collectionToDelimitedString(repositoryContributor.requiredArgs().keySet(), ", "));
118-
builder.unindent();
119-
builder.add("}\n");
120-
builder.unindent();
121-
builder.add("});\n");
122132

123-
return builder.build();
133+
return callback.build();
124134
}
125135

126136
}

src/main/java/org/springframework/data/repository/aot/generate/AotRepositoryConstructorBuilder.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import org.springframework.core.ResolvableType;
1919
import org.springframework.javapoet.CodeBlock;
20-
import org.springframework.javapoet.MethodSpec;
2120

2221
/**
2322
* Builder for AOT Repository Constructors.
@@ -34,7 +33,9 @@ public interface AotRepositoryConstructorBuilder {
3433
* @param parameterName name of the parameter.
3534
* @param type parameter type.
3635
*/
37-
void addParameter(String parameterName, Class<?> type);
36+
default void addParameter(String parameterName, Class<?> type) {
37+
addParameter(parameterName, ResolvableType.forClass(type));
38+
}
3839

3940
/**
4041
* Add constructor parameter and create a field storing its value.
@@ -51,26 +52,26 @@ default void addParameter(String parameterName, ResolvableType type) {
5152
*
5253
* @param parameterName name of the parameter.
5354
* @param type parameter type.
54-
* @param createField whether to create a field for the parameter and assign its value to the field.
55+
* @param bindToField whether to create a field for the parameter and assign its value to the field.
5556
*/
56-
default void addParameter(String parameterName, Class<?> type, boolean createField) {
57-
addParameter(parameterName, ResolvableType.forClass(type), createField);
57+
default void addParameter(String parameterName, Class<?> type, boolean bindToField) {
58+
addParameter(parameterName, ResolvableType.forClass(type), bindToField);
5859
}
5960

6061
/**
6162
* Add constructor parameter.
6263
*
6364
* @param parameterName name of the parameter.
6465
* @param type parameter type.
65-
* @param createField whether to create a field for the parameter and assign its value to the field.
66+
* @param bindToField whether to create a field for the parameter and assign its value to the field.
6667
*/
67-
void addParameter(String parameterName, ResolvableType type, boolean createField);
68+
void addParameter(String parameterName, ResolvableType type, boolean bindToField);
6869

6970
/**
70-
* Add constructor customizer. Customizer is invoked after adding constructor arguments and before assigning
71+
* Add constructor body customizer. The customizer is invoked after adding constructor arguments and before assigning
7172
* constructor arguments to fields.
7273
*
73-
* @param customizer the customizer with direct access to the {@link MethodSpec.Builder constructor builder}.
74+
* @param customizer the customizer with direct access to the {@link CodeBlock.Builder constructor builder}.
7475
*/
7576
void customize(ConstructorCustomizer customizer);
7677

0 commit comments

Comments
 (0)