Expand path bindings from static initializer map put entries.

When a static map field is populated in a static block via put calls with compile-time string keys, expand endpoint path parameters through those entries while remaining fail-closed for computed keys.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-07-14 18:43:12 +02:00
parent 85af06c89d
commit 60cd6511d4
2 changed files with 129 additions and 4 deletions

View File

@@ -688,16 +688,84 @@ public final class EntryPointBindingExpander {
if (fragmentObj instanceof VariableDeclarationFragment fragment
&& fieldName.equals(fragment.getName().getIdentifier())) {
Expression initializer = fragment.getInitializer();
if (initializer == null) {
return null;
if (initializer != null) {
Map<String, String> direct = extractMapOfLiteralEntries(initializer, typeDecl, context);
if (direct != null) {
return direct;
}
}
return extractMapOfLiteralEntries(initializer, typeDecl, context);
return extractStaticBlockMapPutEntries(typeDecl, fieldName, context);
}
}
}
return null;
}
private static Map<String, String> extractStaticBlockMapPutEntries(
TypeDeclaration typeDecl,
String fieldName,
CodebaseContext context) {
LinkedHashMap<String, String> entries = new LinkedHashMap<>();
boolean[] foundPut = {false};
boolean[] invalid = {false};
for (Object bodyDeclObj : typeDecl.bodyDeclarations()) {
if (!(bodyDeclObj instanceof Initializer initializer) || !isStaticInitializer(initializer)) {
continue;
}
Block block = initializer.getBody();
if (block == null) {
continue;
}
block.accept(new ASTVisitor() {
@Override
public boolean visit(MethodInvocation node) {
if (invalid[0] || !"put".equals(node.getName().getIdentifier())
|| node.arguments().size() != 2) {
return super.visit(node);
}
if (!mapReceiverReferencesField(node.getExpression(), fieldName)) {
return super.visit(node);
}
String key = resolveCompileTimeStringKey(
(Expression) node.arguments().get(0), typeDecl, context);
if (key == null) {
invalid[0] = true;
return super.visit(node);
}
entries.put(key, "*");
foundPut[0] = true;
return super.visit(node);
}
});
}
if (invalid[0] || !foundPut[0] || entries.isEmpty()) {
return null;
}
return entries;
}
private static boolean isStaticInitializer(Initializer initializer) {
for (Object modifierObj : initializer.modifiers()) {
if (modifierObj instanceof Modifier modifier && modifier.isStatic()) {
return true;
}
}
return false;
}
private static boolean mapReceiverReferencesField(Expression receiver, String fieldName) {
if (receiver instanceof SimpleName simpleName) {
return fieldName.equals(simpleName.getIdentifier());
}
if (receiver instanceof FieldAccess fieldAccess) {
return fieldName.equals(fieldAccess.getName().getIdentifier());
}
if (receiver instanceof QualifiedName qualifiedName) {
return fieldName.equals(qualifiedName.getName().getIdentifier());
}
return false;
}
private static Map<String, String> extractMapOfLiteralEntries(
Expression expression,
TypeDeclaration declaringType,

View File

@@ -2994,7 +2994,7 @@ class EntryPointBindingExpanderTest {
}
@Test
void shouldNotExpandCommandKeyFromStaticInitializerMap(@TempDir Path tempDir) throws Exception {
void shouldExpandCommandKeyFromStaticInitializerMapPutEntries(@TempDir Path tempDir) throws Exception {
String source = """
package com.example;
import java.util.HashMap;
@@ -3029,6 +3029,63 @@ 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 shouldNotExpandCommandKeyFromStaticInitializerMapWithComputedPutKeys(@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 {
ROUTES = new HashMap<>();
ROUTES.put(prefix("order.pay"), OrderEvent.PAY);
ROUTES.put("order.ship", OrderEvent.SHIP);
}
static String prefix(String value) {
return value;
}
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")