Follow nested static factories and fix cross-module factory resolution.

Delegate single-return static factories through nested calls and static field references with cycle detection, resolve factories without JDT bindings via receiver type lookup, and add regression coverage including sibling-module factories and fail-closed loop or recursive cases.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-07-14 18:54:38 +02:00
parent bb28e72bbe
commit 13de69862b
3 changed files with 394 additions and 11 deletions

View File

@@ -971,18 +971,31 @@ public final class EntryPointBindingExpander {
TypeDeclaration defaultDeclaringType, TypeDeclaration defaultDeclaringType,
CompilationUnit compilationUnit, CompilationUnit compilationUnit,
CodebaseContext context) { CodebaseContext context) {
return extractCompileTimeMapFromStaticMethodCall(
expression, defaultDeclaringType, compilationUnit, context, new LinkedHashSet<>());
}
private static Map<String, String> extractCompileTimeMapFromStaticMethodCall(
Expression expression,
TypeDeclaration defaultDeclaringType,
CompilationUnit compilationUnit,
CodebaseContext context,
Set<String> visitingMethods) {
if (!(expression instanceof MethodInvocation invocation)) { if (!(expression instanceof MethodInvocation invocation)) {
return null; return null;
} }
org.eclipse.jdt.core.dom.IMethodBinding binding = invocation.resolveMethodBinding(); org.eclipse.jdt.core.dom.IMethodBinding binding = invocation.resolveMethodBinding();
if (binding == null || !Modifier.isStatic(binding.getModifiers())) { String methodName = invocation.getName().getIdentifier();
Expression receiver = invocation.getExpression();
TypeDeclaration declaringType = null;
if (binding != null) {
if (!Modifier.isStatic(binding.getModifiers())) {
return null; return null;
} }
TypeDeclaration declaringType = null;
if (binding.getDeclaringClass() != null) { if (binding.getDeclaringClass() != null) {
declaringType = context.getTypeDeclaration(binding.getDeclaringClass().getQualifiedName()); declaringType = context.getTypeDeclaration(binding.getDeclaringClass().getQualifiedName());
} }
Expression receiver = invocation.getExpression(); }
if (declaringType == null && receiver != null) { if (declaringType == null && receiver != null) {
declaringType = resolveHelperDeclaringType(receiver, compilationUnit, context); declaringType = resolveHelperDeclaringType(receiver, compilationUnit, context);
} }
@@ -992,9 +1005,14 @@ public final class EntryPointBindingExpander {
if (declaringType == null) { if (declaringType == null) {
return null; return null;
} }
MethodDeclaration method = context.findMethodDeclaration( MethodDeclaration method = context.findMethodDeclaration(declaringType, methodName, true);
declaringType, invocation.getName().getIdentifier(), true); if (method == null || method.getBody() == null || !isStaticMethod(method)) {
if (method == null || method.getBody() == null) { return null;
}
String methodKey = binding != null && binding.getKey() != null
? binding.getKey()
: context.getFqn(declaringType) + "#" + methodName;
if (!visitingMethods.add(methodKey)) {
return null; return null;
} }
Expression returnExpression = extractSingleReturnExpression(method.getBody()); Expression returnExpression = extractSingleReturnExpression(method.getBody());
@@ -1005,7 +1023,24 @@ public final class EntryPointBindingExpander {
if (direct != null) { if (direct != null) {
return direct; return direct;
} }
return extractDoubleBraceMapPutEntries(returnExpression, declaringType, context); Map<String, String> doubleBrace = extractDoubleBraceMapPutEntries(returnExpression, declaringType, context);
if (doubleBrace != null) {
return doubleBrace;
}
if (returnExpression instanceof MethodInvocation) {
return extractCompileTimeMapFromStaticMethodCall(
returnExpression, declaringType, compilationUnit, context, visitingMethods);
}
return resolveCompileTimeMapEntries(returnExpression, declaringType, compilationUnit, context);
}
private static boolean isStaticMethod(MethodDeclaration method) {
for (Object modifierObj : method.modifiers()) {
if (modifierObj instanceof Modifier modifier && modifier.isStatic()) {
return true;
}
}
return false;
} }
private static Expression extractSingleReturnExpression(Block body) { private static Expression extractSingleReturnExpression(Block body) {

View File

@@ -4321,4 +4321,275 @@ class EntryPointBindingExpanderTest {
assertThat(variants).isEmpty(); assertThat(variants).isEmpty();
} }
@Test
void shouldExpandCommandKeyFromNestedStaticFactoryDelegation(@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 = routes();
static Map<String, OrderEvent> routes() {
return seed();
}
static Map<String, OrderEvent> seed() {
return 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 shouldExpandCommandKeyFromStaticFactoryReturningStaticFieldMap(@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 = routes();
private static final Map<String, OrderEvent> SEED = Map.of(
"order.pay", OrderEvent.PAY,
"order.ship", OrderEvent.SHIP
);
static Map<String, OrderEvent> routes() {
return SEED;
}
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 shouldExpandCommandKeyFromStaticFactoryReturningMapOfEntries(@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 = routes();
static Map<String, OrderEvent> routes() {
return Map.ofEntries(
Map.entry("order.pay", OrderEvent.PAY),
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");
}
@Test
void shouldNotExpandCommandKeyFromStaticBlockLoopPopulation(@TempDir Path tempDir) throws Exception {
String source = """
package com.example;
import java.util.HashMap;
import java.util.List;
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<>();
for (String key : List.of("order.pay", "order.ship")) {
ROUTES.put(key, OrderEvent.PAY);
}
}
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).isEmpty();
}
@Test
void shouldNotExpandCommandKeyFromRecursiveStaticFactory(@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 = routes();
static Map<String, OrderEvent> routes() {
return 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).isEmpty();
}
} }

View File

@@ -729,4 +729,81 @@ class MultiModulePathBindingExpanderTest {
assertThat(variants).extracting(map -> map.get("commandKey")) assertThat(variants).extracting(map -> map.get("commandKey"))
.containsExactlyInAnyOrder("order.pay", "order.ship"); .containsExactlyInAnyOrder("order.pay", "order.ship");
} }
@Test
void shouldExpandCommandKeyFromCrossModuleStaticFactoryReturningMapOf(@TempDir Path tempDir) throws Exception {
Path root = tempDir.resolve("order-system");
Files.createDirectories(root);
Files.writeString(root.resolve("settings.gradle"), "include 'api-module', 'core-module'");
Path apiModule = root.resolve("api-module");
Files.createDirectories(apiModule.resolve("src/main/java/com/example/routes"));
Files.writeString(apiModule.resolve("build.gradle"), "");
Files.writeString(apiModule.resolve("src/main/java/com/example/routes/CommandRoutes.java"), """
package com.example.routes;
import java.util.Map;
public class CommandRoutes {
public static Map<String, String> seed() {
return Map.of(
"order.pay", "OrderEvent.PAY",
"order.ship", "OrderEvent.SHIP");
}
}
""");
Path coreModule = root.resolve("core-module");
Files.createDirectories(coreModule.resolve("src/main/java/com/example"));
Files.writeString(coreModule.resolve("build.gradle"),
"dependencies { implementation project(':api-module') }");
Files.writeString(coreModule.resolve("src/main/java/com/example/GenericCommandController.java"), """
package com.example;
import com.example.routes.CommandRoutes;
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 = CommandRoutes.seed();
void trigger(String commandKey) {
OrderEvent event = OrderEvent.valueOf(ROUTES.get(commandKey));
StateMachine sm = new StateMachine();
sm.sendEvent(event);
}
}
enum OrderEvent { PAY, SHIP }
class StateMachine { void sendEvent(OrderEvent event) {} }
""");
SiblingDependencyResolver siblingResolver = new SiblingDependencyResolver();
ProjectModuleGraph graph = siblingResolver.analyzeProject(coreModule);
Set<Path> scanPaths = graph.resolveScanPaths(coreModule, false);
CodebaseContext context = new CodebaseContext();
context.setResolveBindings(true);
context.scan(scanPaths, Collections.emptySet());
HeuristicCallGraphEngine engine = new HeuristicCallGraphEngine(context);
Map<String, List<click.kamil.springstatemachineexporter.analysis.model.CallEdge>> callGraph =
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, callGraph);
assertThat(variants).extracting(map -> map.get("commandKey"))
.containsExactlyInAnyOrder("order.pay", "order.ship");
}
} }