Resolve inherited and interface static route map fields.
Walk superclass and interface hierarchies when locating compile-time map initializers for Map.get path binding expansion. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -705,6 +705,65 @@ public final class EntryPointBindingExpander {
|
|||||||
TypeDeclaration typeDecl,
|
TypeDeclaration typeDecl,
|
||||||
String fieldName,
|
String fieldName,
|
||||||
CodebaseContext context) {
|
CodebaseContext context) {
|
||||||
|
for (TypeDeclaration current = typeDecl; current != null; current = superTypeDeclaration(current, context)) {
|
||||||
|
Map<String, String> entries = extractStaticFieldMapInitializerOnType(current, fieldName, context);
|
||||||
|
if (entries != null) {
|
||||||
|
return entries;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return extractStaticFieldMapInitializerFromInterfaces(typeDecl, fieldName, context, new LinkedHashSet<>());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Map<String, String> extractStaticFieldMapInitializerFromInterfaces(
|
||||||
|
TypeDeclaration typeDecl,
|
||||||
|
String fieldName,
|
||||||
|
CodebaseContext context,
|
||||||
|
Set<String> visited) {
|
||||||
|
if (typeDecl == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
String typeFqn = context.getFqn(typeDecl);
|
||||||
|
if (typeFqn != null && !visited.add(typeFqn)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
CompilationUnit compilationUnit = typeDecl.getRoot() instanceof CompilationUnit cu ? cu : null;
|
||||||
|
for (Object interfaceTypeObj : typeDecl.superInterfaceTypes()) {
|
||||||
|
if (!(interfaceTypeObj instanceof Type interfaceType)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String interfaceName = interfaceType.toString();
|
||||||
|
TypeDeclaration interfaceDecl = context.getTypeDeclaration(interfaceName, compilationUnit);
|
||||||
|
if (interfaceDecl == null) {
|
||||||
|
interfaceDecl = context.getTypeDeclaration(interfaceName);
|
||||||
|
}
|
||||||
|
if (interfaceDecl == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Map<String, String> entries = extractStaticFieldMapInitializerOnType(interfaceDecl, fieldName, context);
|
||||||
|
if (entries != null) {
|
||||||
|
return entries;
|
||||||
|
}
|
||||||
|
Map<String, String> inherited = extractStaticFieldMapInitializerFromInterfaces(
|
||||||
|
interfaceDecl, fieldName, context, visited);
|
||||||
|
if (inherited != null) {
|
||||||
|
return inherited;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static TypeDeclaration superTypeDeclaration(TypeDeclaration typeDecl, CodebaseContext context) {
|
||||||
|
String superFqn = context.getSuperclassFqn(typeDecl);
|
||||||
|
if (superFqn == null || "java.lang.Object".equals(superFqn)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return context.getTypeDeclaration(superFqn);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Map<String, String> extractStaticFieldMapInitializerOnType(
|
||||||
|
TypeDeclaration typeDecl,
|
||||||
|
String fieldName,
|
||||||
|
CodebaseContext context) {
|
||||||
for (FieldDeclaration field : typeDecl.getFields()) {
|
for (FieldDeclaration field : typeDecl.getFields()) {
|
||||||
for (Object fragmentObj : field.fragments()) {
|
for (Object fragmentObj : field.fragments()) {
|
||||||
if (fragmentObj instanceof VariableDeclarationFragment fragment
|
if (fragmentObj instanceof VariableDeclarationFragment fragment
|
||||||
|
|||||||
@@ -4592,4 +4592,163 @@ class EntryPointBindingExpanderTest {
|
|||||||
|
|
||||||
assertThat(variants).isEmpty();
|
assertThat(variants).isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldExpandCommandKeyFromInheritedStaticMapField(@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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
abstract class BaseRoutes {
|
||||||
|
protected static final Map<String, OrderEvent> ROUTES = Map.of(
|
||||||
|
"order.pay", OrderEvent.PAY,
|
||||||
|
"order.ship", OrderEvent.SHIP
|
||||||
|
);
|
||||||
|
}
|
||||||
|
class OrderGateway extends BaseRoutes {
|
||||||
|
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 shouldExpandCommandKeyFromInterfaceStaticMapField(@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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
interface RouteSource {
|
||||||
|
Map<String, OrderEvent> ROUTES = Map.of(
|
||||||
|
"order.pay", OrderEvent.PAY,
|
||||||
|
"order.ship", OrderEvent.SHIP
|
||||||
|
);
|
||||||
|
}
|
||||||
|
class OrderGateway implements RouteSource {
|
||||||
|
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 shouldExpandCommandKeyFromQualifiedInheritedStaticMapField(@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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
abstract class BaseRoutes {
|
||||||
|
protected static final Map<String, OrderEvent> ROUTES = Map.of(
|
||||||
|
"order.pay", OrderEvent.PAY,
|
||||||
|
"order.ship", OrderEvent.SHIP
|
||||||
|
);
|
||||||
|
}
|
||||||
|
class OrderGateway extends BaseRoutes {
|
||||||
|
void trigger(String commandKey) {
|
||||||
|
StateMachine sm = new StateMachine();
|
||||||
|
sm.sendEvent(BaseRoutes.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