-
Notifications
You must be signed in to change notification settings - Fork 683
Add LocalVariableNameFactory. #3271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 4.0.x
Are you sure you want to change the base?
Changes from all commits
0612b25
dacd58a
cb93ce2
7b5f476
4a48edc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2025 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.repository.aot.generate; | ||
|
||
import java.util.Set; | ||
|
||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.util.MultiValueMap; | ||
|
||
/** | ||
* Non thread safe {@link VariableNameFactory} implementation keeping track of defined names resolving name clashes | ||
* using internal counters appending {@code _%d} to a suggested name in case of a clash. | ||
* | ||
* @author Christoph Strobl | ||
* @since 4.0 | ||
*/ | ||
class LocalVariableNameFactory implements VariableNameFactory { | ||
|
||
private final MultiValueMap<String, String> variables; | ||
|
||
/** | ||
* Create a new {@link LocalVariableNameFactory} considering available {@link MethodMetadata#getMethodArguments() | ||
* method arguments}. | ||
* | ||
* @param methodMetadata source metadata | ||
* @return new instance of {@link LocalVariableNameFactory}. | ||
*/ | ||
static LocalVariableNameFactory forMethod(MethodMetadata methodMetadata) { | ||
return of(methodMetadata.getMethodArguments().keySet()); | ||
} | ||
|
||
/** | ||
* Create a new {@link LocalVariableNameFactory} with a predefined set of initial variable names. | ||
* | ||
* @param predefinedVariables variables already known to be used in the given context. | ||
* @return new instance of {@link LocalVariableNameFactory}. | ||
*/ | ||
static LocalVariableNameFactory of(Set<String> predefinedVariables) { | ||
return new LocalVariableNameFactory(predefinedVariables); | ||
} | ||
|
||
LocalVariableNameFactory(Iterable<String> predefinedVariableNames) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A simplification of this could be: LocalVariableNameFactory(Iterable<String> predefinedVariableNames) {
predefinedVariableNames.forEach((paramName) -> variables.put(paramName, 0L));
} Wdyt? |
||
|
||
variables = new LinkedMultiValueMap<>(); | ||
predefinedVariableNames.forEach(varName -> variables.add(varName, varName)); | ||
} | ||
|
||
@Override | ||
onobc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public String generateName(String intendedVariableName) { | ||
|
||
if (!variables.containsKey(intendedVariableName)) { | ||
variables.add(intendedVariableName, intendedVariableName); | ||
return intendedVariableName; | ||
} | ||
|
||
String targetName = suggestTargetName(intendedVariableName); | ||
variables.add(intendedVariableName, targetName); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to update the original This code is fine but concurrency aside, I still think something like the following that would replace line62-86 would simplify things: @Override
public String generateName(String suggestedName) {
var counter = variables.compute(suggestedName,
(__, currentCount) -> currentCount == null ? 0 : currentCount.longValue() + 1);
return counter == 0 ? suggestedName : "%s_%s".formatted(suggestedName, counter));
} |
||
variables.add(targetName, targetName); | ||
return targetName; | ||
} | ||
|
||
String suggestTargetName(String suggested) { | ||
return suggestTargetName(suggested, 1); | ||
} | ||
|
||
String suggestTargetName(String suggested, int counter) { | ||
|
||
String targetName = "%s_%s".formatted(suggested, counter); | ||
if (!variables.containsKey(targetName)) { | ||
return targetName; | ||
} | ||
return suggestTargetName(suggested, counter + 1); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,12 +16,16 @@ | |
package org.springframework.data.repository.aot.generate; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.ArrayList; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
|
||
import org.jspecify.annotations.Nullable; | ||
|
||
import org.springframework.core.DefaultParameterNameDiscoverer; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.core.ParameterNameDiscoverer; | ||
import org.springframework.core.ResolvableType; | ||
import org.springframework.data.repository.core.RepositoryInformation; | ||
import org.springframework.javapoet.ParameterSpec; | ||
|
@@ -31,17 +35,19 @@ | |
* Metadata about an AOT Repository method. | ||
* | ||
* @author Christoph Strobl | ||
* @since 4.0 | ||
*/ | ||
class MethodMetadata { | ||
|
||
private final Map<String, ParameterSpec> methodArguments = new LinkedHashMap<>(); | ||
private final ResolvableType actualReturnType; | ||
private final ResolvableType returnType; | ||
|
||
public MethodMetadata(RepositoryInformation repositoryInformation, Method method) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I liked the visibility modifiers to indicate which methods are desired API and to differentiate between those ones, that are package-private for testing purposes. |
||
MethodMetadata(RepositoryInformation repositoryInformation, Method method) { | ||
|
||
this.returnType = repositoryInformation.getReturnType(method).toResolvableType(); | ||
this.actualReturnType = ResolvableType.forType(repositoryInformation.getReturnedDomainClass(method)); | ||
this.initParameters(repositoryInformation, method, new DefaultParameterNameDiscoverer()); | ||
} | ||
|
||
@Nullable | ||
|
@@ -54,20 +60,50 @@ public String getParameterNameOf(Class<?> type) { | |
return null; | ||
} | ||
|
||
public ResolvableType getReturnType() { | ||
ResolvableType getReturnType() { | ||
return returnType; | ||
} | ||
|
||
public ResolvableType getActualReturnType() { | ||
ResolvableType getActualReturnType() { | ||
return actualReturnType; | ||
} | ||
|
||
public void addParameter(ParameterSpec parameterSpec) { | ||
void addParameter(ParameterSpec parameterSpec) { | ||
this.methodArguments.put(parameterSpec.name, parameterSpec); | ||
} | ||
|
||
public Map<String, ParameterSpec> getMethodArguments() { | ||
Map<String, ParameterSpec> getMethodArguments() { | ||
return methodArguments; | ||
} | ||
|
||
@Nullable | ||
String getParameterName(int position) { | ||
|
||
if (0 > position) { | ||
return null; | ||
} | ||
|
||
List<Entry<String, ParameterSpec>> entries = new ArrayList<>(methodArguments.entrySet()); | ||
if (position < entries.size()) { | ||
return entries.get(position).getKey(); | ||
} | ||
return null; | ||
} | ||
|
||
private void initParameters(RepositoryInformation repositoryInformation, Method method, | ||
ParameterNameDiscoverer nameDiscoverer) { | ||
|
||
ResolvableType repositoryInterface = ResolvableType.forClass(repositoryInformation.getRepositoryInterface()); | ||
|
||
for (java.lang.reflect.Parameter parameter : method.getParameters()) { | ||
|
||
MethodParameter methodParameter = MethodParameter.forParameter(parameter); | ||
methodParameter.initParameterNameDiscovery(nameDiscoverer); | ||
ResolvableType resolvableParameterType = ResolvableType.forMethodParameter(methodParameter, repositoryInterface); | ||
|
||
TypeName parameterType = TypeName.get(resolvableParameterType.getType()); | ||
|
||
addParameter(ParameterSpec.builder(parameterType, methodParameter.getParameterName()).build()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2025 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.repository.aot.generate; | ||
|
||
import org.springframework.lang.CheckReturnValue; | ||
|
||
/** | ||
* Name factory for generating clash free variable names checking an intended name against predefined and already used | ||
* ones. | ||
* | ||
* @author Christoph Strobl | ||
onobc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @since 4.0 | ||
*/ | ||
interface VariableNameFactory { | ||
|
||
/** | ||
* Compare and potentially generate a new name for the given intended variable name. | ||
* | ||
* @param intendedVariableName must not be {@literal null}. | ||
* @return the {@literal intendedVariableName} if no naming clash detected or a clash free generated name. | ||
*/ | ||
@CheckReturnValue | ||
String generateName(String intendedVariableName); | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest refining the way we approach the problem. We generally hold details in the context and can look these up. I would suggest that we keep a map around with parameter name mappings and use this method as
getLocalVariableName(…)
orlocalVariable(String logicalName)
. One could then call the method with the same name twice. On the first call, we would ensure there is no clash, the second call would return the mapped name. Would also alleviate the need to track the variable name in the calling code.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry I don't get that comment. Why would I want to call the same method twice?
you'll have to keep track of variable name in the calling code since you might have to apply it in different code blocks or have to pass it around between query creation and execution so you refer to right thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling code would then not be required to track the actual variable name but could refer to the context so the context provides the information.