Resolve chained static string constants for path binding cases.
Follow one-hop static final indirection for Map.of keys and switch case labels so route constants can alias literals without losing fail-closed behavior on cycles or computed values. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -2478,4 +2478,111 @@ class EntryPointBindingExpanderTest {
|
||||
|
||||
assertThat(variants).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldExpandCommandKeyFromSwitchExpressionWithStaticFinalCaseConstants(@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 String PAY_KEY = "order.pay";
|
||||
private static final String SHIP_KEY = "order.ship";
|
||||
void trigger(String commandKey) {
|
||||
DomainCommand command = switch (commandKey) {
|
||||
case PAY_KEY -> DomainCommand.ORDER_PAY;
|
||||
case SHIP_KEY -> DomainCommand.ORDER_SHIP;
|
||||
default -> throw new IllegalArgumentException("Unknown command: " + commandKey);
|
||||
};
|
||||
StateMachine sm = new StateMachine();
|
||||
sm.sendEvent(command);
|
||||
}
|
||||
}
|
||||
enum DomainCommand { ORDER_PAY, ORDER_SHIP }
|
||||
class StateMachine { void sendEvent(DomainCommand command) {} }
|
||||
""";
|
||||
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 shouldExpandCommandKeyFromChainedStaticFinalStringConstantMapKeys(@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 String PAY_BASE = "order.pay";
|
||||
private static final String PAY_KEY = PAY_BASE;
|
||||
private static final String SHIP_BASE = "order.ship";
|
||||
private static final String SHIP_KEY = SHIP_BASE;
|
||||
private static final Map<String, OrderEvent> ROUTES = Map.of(
|
||||
PAY_KEY, OrderEvent.PAY,
|
||||
SHIP_KEY, 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");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user