Skip to content

Commit 25102c5

Browse files
committed
Polishing.
Refine local variable handling to logical and physical naming where the logical name is used in AOT contributor code while the physical name is rendered.
1 parent b0ebc7f commit 25102c5

File tree

5 files changed

+36
-17
lines changed

5 files changed

+36
-17
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,14 @@ public TypeName getReturnTypeName() {
128128
}
129129

130130
/**
131-
* Suggest naming clash free variant for the given intended variable name within the local method context.
131+
* Obtain a naming-clash free variant for the given logical variable name within the local method context. Returns the
132+
* target variable name when called multiple times with the same {@code variableName}.
132133
*
133-
* @param variableName the intended variable name.
134-
* @return the suggested VariableName
134+
* @param variableName the logical variable name.
135+
* @return the variable name used in the generated code.
135136
*/
136-
public String suggestLocalVariableName(String variableName) {
137-
return variableNameFactory.generateName(variableName);
137+
public String localVariable(String variableName) {
138+
return targetMethodMetadata.getLocalVariables().computeIfAbsent(variableName, variableNameFactory::generateName);
138139
}
139140

140141
/**

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
*/
5353
class AotRepositoryBuilder {
5454

55+
private static final Log logger = LogFactory.getLog(AotRepositoryBuilder.class);
56+
5557
private final RepositoryInformation repositoryInformation;
5658
private final String moduleName;
5759
private final ProjectionFactory projectionFactory;
@@ -146,7 +148,14 @@ public AotBundle build() {
146148
return it.getDeclaringClass().getName();
147149
}).thenComparing(Method::getName).thenComparing(Method::getParameterCount).thenComparing(Method::toString))
148150
.forEach(method -> {
149-
contributeMethod(method, repositoryComposition, methodMetadata, builder);
151+
try {
152+
contributeMethod(method, repositoryComposition, methodMetadata, builder);
153+
} catch (RuntimeException e) {
154+
if (logger.isErrorEnabled()) {
155+
logger.error("Failed to contribute Repository method [%s.%s]"
156+
.formatted(repositoryInformation.getRepositoryInterface().getName(), method.getName()), e);
157+
}
158+
}
150159
});
151160

152161
// write fields at the end so we make sure to capture things added by methods

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,16 @@ class LocalVariableNameFactory implements VariableNameFactory {
3131

3232
private final MultiValueMap<String, String> variables;
3333

34+
LocalVariableNameFactory(Iterable<String> predefinedVariableNames) {
35+
36+
variables = new LinkedMultiValueMap<>();
37+
predefinedVariableNames.forEach(varName -> variables.add(varName, varName));
38+
}
39+
3440
/**
3541
* Create a new {@link LocalVariableNameFactory} considering available {@link MethodMetadata#getMethodArguments()
3642
* method arguments}.
37-
*
43+
*
3844
* @param methodMetadata source metadata
3945
* @return new instance of {@link LocalVariableNameFactory}.
4046
*/
@@ -52,12 +58,6 @@ static LocalVariableNameFactory of(Set<String> predefinedVariables) {
5258
return new LocalVariableNameFactory(predefinedVariables);
5359
}
5460

55-
LocalVariableNameFactory(Iterable<String> predefinedVariableNames) {
56-
57-
variables = new LinkedMultiValueMap<>();
58-
predefinedVariableNames.forEach(varName -> variables.add(varName, varName));
59-
}
60-
6161
@Override
6262
public String generateName(String intendedVariableName) {
6363

@@ -84,4 +84,5 @@ String suggestTargetName(String suggested, int counter) {
8484
}
8585
return suggestTargetName(suggested, counter + 1);
8686
}
87+
8788
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@
3535
* Metadata about an AOT Repository method.
3636
*
3737
* @author Christoph Strobl
38+
* @author Mark Paluch
3839
* @since 4.0
3940
*/
4041
class MethodMetadata {
4142

4243
private final Map<String, ParameterSpec> methodArguments = new LinkedHashMap<>();
44+
private final Map<String, String> localVariables = new LinkedHashMap<>();
4345
private final ResolvableType actualReturnType;
4446
private final ResolvableType returnType;
4547

@@ -90,6 +92,10 @@ String getParameterName(int position) {
9092
return null;
9193
}
9294

95+
Map<String, String> getLocalVariables() {
96+
return localVariables;
97+
}
98+
9399
private void initParameters(RepositoryInformation repositoryInformation, Method method,
94100
ParameterNameDiscoverer nameDiscoverer) {
95101

src/test/java/org/springframework/data/repository/aot/generate/AotQueryMethodGenerationContextUnitTests.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
*/
1616
package org.springframework.data.repository.aot.generate;
1717

18-
import static org.assertj.core.api.Assertions.assertThat;
19-
import static org.mockito.ArgumentMatchers.eq;
18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.mockito.ArgumentMatchers.*;
2020

2121
import java.lang.reflect.Method;
2222

2323
import org.junit.jupiter.api.Test;
2424
import org.mockito.Mockito;
25+
2526
import org.springframework.data.domain.Pageable;
2627
import org.springframework.data.repository.core.RepositoryInformation;
2728
import org.springframework.data.repository.query.QueryMethod;
@@ -31,6 +32,7 @@
3132
* Tests targeting {@link AotQueryMethodGenerationContext}.
3233
*
3334
* @author Christoph Strobl
35+
* @author Mark Paluch
3436
*/
3537
class AotQueryMethodGenerationContextUnitTests {
3638

@@ -39,8 +41,8 @@ void suggestLocalVariableNameConsidersMethodArguments() throws NoSuchMethodExcep
3941

4042
AotQueryMethodGenerationContext ctx = ctxFor("reservedParameterMethod");
4143

42-
assertThat(ctx.suggestLocalVariableName("foo")).isEqualTo("foo");
43-
assertThat(ctx.suggestLocalVariableName("arg0")).isNotIn("arg0", "arg1", "arg2");
44+
assertThat(ctx.localVariable("foo")).isEqualTo("foo");
45+
assertThat(ctx.localVariable("arg0")).isNotIn("arg0", "arg1", "arg2");
4446
}
4547

4648
AotQueryMethodGenerationContext ctxFor(String methodName) throws NoSuchMethodException {

0 commit comments

Comments
 (0)