Harden static map path binding for cross-class and ofEntries routes.

Resolve Routes.ROUTES-style QualifiedName receivers and Map.ofEntries initializers so commandKey expansion works for typical route-table dispatch patterns.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-07-14 18:30:34 +02:00
parent 79bb4e2aba
commit 12db9dbef4
2 changed files with 169 additions and 11 deletions

View File

@@ -619,11 +619,16 @@ public final class EntryPointBindingExpander {
}
String fieldName = null;
TypeDeclaration declaringType = null;
if (receiver instanceof SimpleName simpleName) {
fieldName = simpleName.getIdentifier();
declaringType = AstUtils.findEnclosingType(receiver);
} else if (receiver instanceof FieldAccess fieldAccess) {
if (receiver instanceof FieldAccess fieldAccess) {
fieldName = fieldAccess.getName().getIdentifier();
org.eclipse.jdt.core.dom.IVariableBinding fieldBinding = fieldAccess.resolveFieldBinding();
if (fieldBinding != null && fieldBinding.isField()) {
org.eclipse.jdt.core.dom.ITypeBinding declaringClass = fieldBinding.getDeclaringClass();
if (declaringClass != null) {
declaringType = context.getTypeDeclaration(declaringClass.getQualifiedName());
}
}
if (declaringType == null) {
Expression qualifier = fieldAccess.getExpression();
if (qualifier instanceof SimpleName typeName) {
CompilationUnit compilationUnit = (CompilationUnit) ownerMethod.getRoot();
@@ -635,6 +640,32 @@ public final class EntryPointBindingExpander {
declaringType = context.getTypeDeclaration(qualifiedName.getFullyQualifiedName());
}
}
} else if (receiver instanceof SimpleName simpleName) {
fieldName = simpleName.getIdentifier();
org.eclipse.jdt.core.dom.IBinding binding = simpleName.resolveBinding();
if (binding instanceof org.eclipse.jdt.core.dom.IVariableBinding variableBinding
&& variableBinding.isField()) {
org.eclipse.jdt.core.dom.ITypeBinding declaringClass = variableBinding.getDeclaringClass();
if (declaringClass != null) {
declaringType = context.getTypeDeclaration(declaringClass.getQualifiedName());
}
}
if (declaringType == null) {
declaringType = AstUtils.findEnclosingType(receiver);
}
} else if (receiver instanceof QualifiedName qualifiedName) {
fieldName = qualifiedName.getName().getIdentifier();
Name qualifier = qualifiedName.getQualifier();
if (qualifier instanceof SimpleName typeName) {
CompilationUnit compilationUnit = (CompilationUnit) ownerMethod.getRoot();
declaringType = context.getTypeDeclaration(typeName.getIdentifier(), compilationUnit);
if (declaringType == null) {
declaringType = context.getTypeDeclaration(typeName.getIdentifier());
}
} else if (qualifier instanceof QualifiedName typeQualifiedName) {
declaringType = context.getTypeDeclaration(typeQualifiedName.getFullyQualifiedName());
}
}
if (fieldName == null || declaringType == null) {
return null;
}
@@ -662,14 +693,41 @@ public final class EntryPointBindingExpander {
return null;
}
String factoryMethod = mapInvocation.getName().getIdentifier();
if (!"of".equals(factoryMethod)) {
return null;
}
if (!isMapFactoryReceiver(mapInvocation.getExpression())) {
return null;
}
if ("of".equals(factoryMethod)) {
return extractAlternatingLiteralPairs(mapInvocation.arguments());
}
if ("ofEntries".equals(factoryMethod)) {
return extractMapEntryCallArguments(mapInvocation.arguments());
}
return null;
}
private static Map<String, String> extractMapEntryCallArguments(List<?> arguments) {
Map<String, String> entries = new LinkedHashMap<>();
for (Object argObj : arguments) {
if (!(argObj instanceof MethodInvocation entryCall)) {
return null;
}
if (!"entry".equals(entryCall.getName().getIdentifier())) {
return null;
}
if (!isMapFactoryReceiver(entryCall.getExpression())) {
return null;
}
if (entryCall.arguments().size() != 2) {
return null;
}
Object keyObj = entryCall.arguments().get(0);
if (!(keyObj instanceof StringLiteral stringLiteral)) {
return null;
}
entries.put(stringLiteral.getLiteralValue(), "*");
}
return entries.isEmpty() ? null : entries;
}
private static boolean isMapFactoryReceiver(Expression receiver) {
if (receiver instanceof SimpleName simpleName) {

View File

@@ -1886,4 +1886,104 @@ class EntryPointBindingExpanderTest {
assertThat(variants.stream().map(v -> v.get("machineType")).distinct())
.containsExactlyInAnyOrder("ORDER", "DOCUMENT");
}
@Test
void shouldExpandCommandKeyFromCrossClassStaticMapLookup(@TempDir Path tempDir) throws Exception {
String source = """
package com.example;
public class GenericCommandController {
OrderGateway gateway;
public void execute(String commandKey) {
gateway.trigger(commandKey);
}
}
class OrderGateway {
void trigger(String commandKey) {
StateMachine sm = new StateMachine();
sm.sendEvent(Routes.ROUTES.get(commandKey));
}
}
class Routes {
static final java.util.Map<String, OrderEvent> ROUTES = java.util.Map.of(
"order.pay", OrderEvent.PAY,
"order.ship", OrderEvent.SHIP);
}
class StateMachine { void sendEvent(OrderEvent event) {} }
enum OrderEvent { PAY, SHIP }
""";
Files.writeString(tempDir.resolve("App.java"), source);
CodebaseContext context = new CodebaseContext();
context.setResolveBindings(true);
context.scan(tempDir);
HeuristicCallGraphEngine engine = new HeuristicCallGraphEngine(context);
Map<String, List<click.kamil.springstatemachineexporter.analysis.model.CallEdge>> graph = engine.buildCallGraph();
EntryPoint entryPoint = EntryPoint.builder()
.className("com.example.GenericCommandController")
.methodName("execute")
.name("POST /api/commands/{commandKey}")
.parameters(List.of(EntryPoint.Parameter.builder()
.name("commandKey")
.type("String")
.annotations(List.of("PathVariable"))
.build()))
.build();
List<Map<String, String>> variants =
EntryPointBindingExpander.expandPathVariableBindings(entryPoint, context, graph);
assertThat(variants).extracting(map -> map.get("commandKey"))
.containsExactlyInAnyOrder("order.pay", "order.ship");
}
@Test
void shouldExpandCommandKeyFromStaticMapOfEntriesLookup(@TempDir Path tempDir) throws Exception {
String source = """
package com.example;
public class GenericCommandController {
OrderGateway gateway;
public void execute(String commandKey) {
gateway.trigger(commandKey);
}
}
class OrderGateway {
private static final java.util.Map<String, OrderEvent> ROUTES = java.util.Map.ofEntries(
java.util.Map.entry("order.pay", OrderEvent.PAY),
java.util.Map.entry("order.ship", OrderEvent.SHIP));
void trigger(String commandKey) {
StateMachine sm = new StateMachine();
sm.sendEvent(ROUTES.get(commandKey));
}
}
class StateMachine { void sendEvent(OrderEvent event) {} }
enum OrderEvent { PAY, SHIP }
""";
Files.writeString(tempDir.resolve("App.java"), source);
CodebaseContext context = new CodebaseContext();
context.setResolveBindings(true);
context.scan(tempDir);
HeuristicCallGraphEngine engine = new HeuristicCallGraphEngine(context);
Map<String, List<click.kamil.springstatemachineexporter.analysis.model.CallEdge>> graph = engine.buildCallGraph();
EntryPoint entryPoint = EntryPoint.builder()
.className("com.example.GenericCommandController")
.methodName("execute")
.name("POST /api/commands/{commandKey}")
.parameters(List.of(EntryPoint.Parameter.builder()
.name("commandKey")
.type("String")
.annotations(List.of("PathVariable"))
.build()))
.build();
List<Map<String, String>> variants =
EntryPointBindingExpander.expandPathVariableBindings(entryPoint, context, graph);
assertThat(variants).extracting(map -> map.get("commandKey"))
.containsExactlyInAnyOrder("order.pay", "order.ship");
}
}