Expand double-brace and linked-local route map putAll bindings.

Support putAll from compile-time maps inside anonymous HashMap initializers and add regression coverage for linked locals, mixed put/putAll, and empty inline HashMap fields populated in static blocks.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-07-14 18:48:48 +02:00
parent dd25db0109
commit 01c8c58277
2 changed files with 246 additions and 8 deletions

View File

@@ -893,14 +893,15 @@ public final class EntryPointBindingExpander {
block.accept(new ASTVisitor() { block.accept(new ASTVisitor() {
@Override @Override
public boolean visit(MethodInvocation node) { public boolean visit(MethodInvocation node) {
if (invalid[0] || !"put".equals(node.getName().getIdentifier()) if (invalid[0]) {
|| node.arguments().size() != 2) {
return super.visit(node); return super.visit(node);
} }
Expression receiver = node.getExpression(); Expression receiver = node.getExpression();
if (receiver != null && !(receiver instanceof ThisExpression)) { if (receiver != null && !(receiver instanceof ThisExpression)) {
return super.visit(node); return super.visit(node);
} }
String methodName = node.getName().getIdentifier();
if ("put".equals(methodName) && node.arguments().size() == 2) {
String key = resolveCompileTimeStringKey( String key = resolveCompileTimeStringKey(
(Expression) node.arguments().get(0), declaringType, context); (Expression) node.arguments().get(0), declaringType, context);
if (key == null) { if (key == null) {
@@ -911,6 +912,22 @@ public final class EntryPointBindingExpander {
foundPut[0] = true; foundPut[0] = true;
return super.visit(node); return super.visit(node);
} }
if ("putAll".equals(methodName) && node.arguments().size() == 1) {
CompilationUnit compilationUnit = block.getRoot() instanceof CompilationUnit cu
? cu
: null;
Map<String, String> merged = resolveCompileTimeMapEntries(
(Expression) node.arguments().get(0), declaringType, compilationUnit, context);
if (merged == null || merged.isEmpty()) {
invalid[0] = true;
return super.visit(node);
}
entries.putAll(merged);
foundPut[0] = true;
return super.visit(node);
}
return super.visit(node);
}
}); });
} }
if (invalid[0] || !foundPut[0] || entries.isEmpty()) { if (invalid[0] || !foundPut[0] || entries.isEmpty()) {

View File

@@ -3657,4 +3657,225 @@ class EntryPointBindingExpanderTest {
assertThat(variants).isEmpty(); assertThat(variants).isEmpty();
} }
@Test
void shouldExpandCommandKeyFromStaticBlockLocalPutAllWithMapOf(@TempDir Path tempDir) throws Exception {
String source = """
package com.example;
import java.util.HashMap;
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;
static {
Map<String, OrderEvent> routes = new HashMap<>();
routes.putAll(Map.of(
"order.pay", OrderEvent.PAY,
"order.ship", OrderEvent.SHIP
));
ROUTES = routes;
}
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");
}
@Test
void shouldExpandCommandKeyFromStaticBlockMixedPutAllAndPutOnLinkedLocal(@TempDir Path tempDir) throws Exception {
String source = """
package com.example;
import java.util.HashMap;
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;
static {
Map<String, OrderEvent> routes = new HashMap<>();
routes.putAll(Map.of("order.pay", OrderEvent.PAY));
routes.put("order.ship", OrderEvent.SHIP);
ROUTES = routes;
}
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");
}
@Test
void shouldExpandCommandKeyFromDoubleBraceInitializerPutAllWithMapOf(@TempDir Path tempDir) throws Exception {
String source = """
package com.example;
import java.util.HashMap;
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 = new HashMap<>() {{
putAll(Map.of(
"order.pay", OrderEvent.PAY,
"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");
}
@Test
void shouldExpandCommandKeyFromInlineEmptyHashMapWithStaticBlockPutAll(@TempDir Path tempDir) throws Exception {
String source = """
package com.example;
import java.util.HashMap;
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 = new HashMap<>();
static {
ROUTES.putAll(Map.of(
"order.pay", OrderEvent.PAY,
"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");
}
} }