Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5cacf95

Browse files
committed
Use field accessors for updated JavaPoet version.
Closes #3342
1 parent b633f64 commit 5cacf95

File tree

6 files changed

+65
-17
lines changed

6 files changed

+65
-17
lines changed

‎src/main/java/org/springframework/data/repository/aot/generate/AotRepositoryFragmentMetadata.java

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,19 @@ public AotRepositoryFragmentMetadata(ClassName className) {
4545
this.className = className;
4646
}
4747

48+
/**
49+
* Lookup a field name by exact type. Returns the first field that matches the type or {@literal null} if no field
50+
* with that type was found.
51+
*
52+
* @param type
53+
* @return
54+
*/
4855
@Nullable
4956
public String fieldNameOf(Class<?> type) {
5057

5158
TypeName lookup = TypeName.get(type).withoutAnnotations();
5259
for (Entry<String, FieldSpec> field : fields.entrySet()) {
53-
if (field.getValue().type.withoutAnnotations().equals(lookup)) {
60+
if (field.getValue().type().withoutAnnotations().equals(lookup)) {
5461
return field.getKey();
5562
}
5663
}
@@ -62,29 +69,76 @@ public ClassName getTargetTypeName() {
6269
return className;
6370
}
6471

72+
/**
73+
* Add a field to the repository fragment.
74+
*
75+
* @param fieldName name of the field to add. Must be unique.
76+
* @param type field type.
77+
* @param modifiers modifiers for the field, e.g. {@link Modifier#PRIVATE}, {@link Modifier#FINAL}, etc.
78+
*/
6579
public void addField(String fieldName, TypeName type, Modifier... modifiers) {
6680
fields.put(fieldName, FieldSpec.builder(type, fieldName, modifiers).build());
6781
}
6882

83+
/**
84+
* Add a field to the repository fragment.
85+
*
86+
* @param fieldSpec the field specification to add.
87+
*/
6988
public void addField(FieldSpec fieldSpec) {
70-
fields.put(fieldSpec.name, fieldSpec);
89+
fields.put(fieldSpec.name(), fieldSpec);
7190
}
7291

92+
/**
93+
* Returns the fields of the repository fragment.
94+
*
95+
* @return the fields of the repository fragment.
96+
*/
7397
public Map<String, FieldSpec> getFields() {
7498
return fields;
7599
}
76100

77-
public Map<String, ConstructorArgument> getConstructorArguments() {
78-
return constructorArguments;
79-
}
80-
101+
/**
102+
* Add a constructor argument to the repository fragment.
103+
*
104+
* @param parameterName name of the constructor parameter to add. Must be unique.
105+
* @param type type of the constructor parameter.
106+
* @param fieldName name of the field to bind the constructor parameter to, or {@literal null} if no field should be
107+
* created.
108+
*/
81109
public void addConstructorArgument(String parameterName, TypeName type, @Nullable String fieldName) {
110+
82111
this.constructorArguments.put(parameterName, new ConstructorArgument(parameterName, type, fieldName));
112+
113+
if (fieldName != null) {
114+
addField(parameterName, type, Modifier.PRIVATE, Modifier.FINAL);
115+
}
83116
}
84117

118+
/**
119+
* Returns the constructor arguments of the repository fragment.
120+
*
121+
* @return the constructor arguments of the repository fragment.
122+
*/
123+
public Map<String, ConstructorArgument> getConstructorArguments() {
124+
return constructorArguments;
125+
}
126+
127+
/**
128+
* Constructor argument metadata.
129+
*
130+
* @param parameterName
131+
* @param typeName
132+
* @param fieldName
133+
*/
85134
public record ConstructorArgument(String parameterName, TypeName typeName, @Nullable String fieldName) {
86135

136+
@Deprecated(forRemoval = true)
87137
boolean isForLocalField() {
138+
return isBoundToField();
139+
}
140+
141+
boolean isBoundToField() {
88142
return fieldName != null;
89143
}
90144

‎src/main/java/org/springframework/data/repository/aot/generate/AotRepositoryMethodBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public MethodSpec buildMethod() {
9494

9595
builder.addJavadoc("AOT generated implementation of {@link $T#$L($L)}.", context.getMethod().getDeclaringClass(),
9696
context.getMethod().getName(), StringUtils.collectionToCommaDelimitedString(context.getTargetMethodMetadata()
97-
.getMethodArguments().values().stream().map(it -> it.type.toString()).collect(Collectors.toList())));
97+
.getMethodArguments().values().stream().map(it -> it.type().toString()).collect(Collectors.toList())));
9898
context.getTargetMethodMetadata().getMethodArguments().forEach((name, spec) -> builder.addParameter(spec));
9999
builder.addCode(methodBody);
100100
customizer.accept(context, builder);

‎src/main/java/org/springframework/data/repository/aot/generate/MethodMetadata.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ ResolvableType getActualReturnType() {
8080
}
8181

8282
void addParameter(ParameterSpec parameterSpec) {
83-
this.methodArguments.put(parameterSpec.name, parameterSpec);
83+
this.methodArguments.put(parameterSpec.name(), parameterSpec);
8484
}
8585

8686
Map<String, ParameterSpec> getMethodArguments() {

‎src/main/java/org/springframework/data/repository/aot/generate/RepositoryConstructorBuilder.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ public void addParameter(String parameterName, TypeName type) {
8383
public void addParameter(String parameterName, TypeName type, boolean createField) {
8484

8585
this.metadata.addConstructorArgument(parameterName, type, createField ? parameterName : null);
86-
87-
if (createField) {
88-
this.metadata.addField(parameterName, type, Modifier.PRIVATE, Modifier.FINAL);
89-
}
9086
}
9187

9288
/**
@@ -113,7 +109,7 @@ public MethodSpec buildConstructor() {
113109
customizer.customize(builder);
114110

115111
for (Entry<String, ConstructorArgument> parameter : this.metadata.getConstructorArguments().entrySet()) {
116-
if (parameter.getValue().isForLocalField()) {
112+
if (parameter.getValue().isBoundToField()) {
117113
builder.addStatement("this.$N = $N", parameter.getKey(), parameter.getKey());
118114
}
119115
}

‎src/main/java/org/springframework/data/repository/aot/generate/RepositoryContributor.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
import org.springframework.data.repository.query.QueryMethod;
3232
import org.springframework.javapoet.JavaFile;
3333
import org.springframework.javapoet.TypeName;
34-
import org.springframework.util.StringUtils;
35-
import org.springframework.javapoet.TypeSpec;
3634

3735
/**
3836
* Contributor for AOT repository fragments.
@@ -98,7 +96,7 @@ public void contribute(GenerationContext generationContext) {
9896
String repositoryJsonFileName = getRepositoryJsonFileName(repositoryInterface);
9997

10098
JavaFile javaFile = aotBundle.javaFile();
101-
String typeName = "%s.%s".formatted(javaFile.packageName, javaFile.typeSpec.name);
99+
String typeName = "%s.%s".formatted(javaFile.packageName(), javaFile.typeSpec().name());
102100
String repositoryJson;
103101

104102
try {

‎src/test/java/org/springframework/data/repository/aot/generate/AotRepositoryBuilderUnitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void appliesCtorArguments() {
7878
repoBuilder.withConstructorCustomizer(ctor -> {
7979
ctor.addParameter("param1", Metric.class);
8080
ctor.addParameter("param2", String.class);
81-
ctor.addParameter("ctorScoped", TypeName.OBJECT, false);
81+
ctor.addParameter("ctorScoped", TypeName.get(Object.class), false);
8282
});
8383
assertThat(repoBuilder.build().javaFile().toString()) //
8484
.contains("private final Metric param1;") //

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /