Expand map route keys from static final string constants.

Resolve Map.of keys through compile-time static final String fields so route tables can use named constants while still failing closed on computed or non-static keys.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-07-14 18:34:59 +02:00
parent fba7a0c90c
commit b9db614cb1
2 changed files with 255 additions and 16 deletions

View File

@@ -2322,7 +2322,7 @@ class EntryPointBindingExpanderTest {
}
@Test
void shouldNotExpandCommandKeyWhenMapKeysAreNonLiteralConstants(@TempDir Path tempDir) throws Exception {
void shouldExpandCommandKeyFromStaticFinalStringConstantMapKeys(@TempDir Path tempDir) throws Exception {
String source = """
package com.example;
import java.util.Map;
@@ -2355,6 +2355,113 @@ class EntryPointBindingExpanderTest {
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 shouldExpandCommandKeyFromCrossClassStaticFinalStringConstantMapKeys(@TempDir Path tempDir) throws Exception {
String source = """
package com.example;
import java.util.Map;
public class GenericCommandController {
OrderGateway gateway;
public void execute(String commandKey) {
gateway.trigger(commandKey);
}
}
class OrderGateway {
private static final Map<String, OrderEvent> ROUTES = Map.of(
CommandKeys.PAY_KEY, OrderEvent.PAY,
CommandKeys.SHIP_KEY, OrderEvent.SHIP);
void trigger(String commandKey) {
StateMachine sm = new StateMachine();
sm.sendEvent(ROUTES.get(commandKey));
}
}
class CommandKeys {
static final String PAY_KEY = "order.pay";
static final String SHIP_KEY = "order.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 shouldNotExpandCommandKeyWhenMapKeysAreComputed(@TempDir Path tempDir) throws Exception {
String source = """
package com.example;
import java.util.Map;
public class GenericCommandController {
OrderGateway gateway;
public void execute(String commandKey) {
gateway.trigger(commandKey);
}
}
class OrderGateway {
private static final Map<String, OrderEvent> ROUTES = Map.of(
prefix("order.pay"), OrderEvent.PAY,
prefix("order.ship"), OrderEvent.SHIP);
void trigger(String commandKey) {
StateMachine sm = new StateMachine();
sm.sendEvent(ROUTES.get(commandKey));
}
static String prefix(String value) {
return value;
}
}
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")