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:
@@ -688,16 +688,84 @@ public final class EntryPointBindingExpander {
|
|||||||
if (fragmentObj instanceof VariableDeclarationFragment fragment
|
if (fragmentObj instanceof VariableDeclarationFragment fragment
|
||||||
&& fieldName.equals(fragment.getName().getIdentifier())) {
|
&& fieldName.equals(fragment.getName().getIdentifier())) {
|
||||||
Expression initializer = fragment.getInitializer();
|
Expression initializer = fragment.getInitializer();
|
||||||
if (initializer == null) {
|
if (initializer != null) {
|
||||||
return 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;
|
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(
|
private static Map<String, String> extractMapOfLiteralEntries(
|
||||||
Expression expression,
|
Expression expression,
|
||||||
TypeDeclaration declaringType,
|
TypeDeclaration declaringType,
|
||||||
|
|||||||
@@ -2994,7 +2994,7 @@ class EntryPointBindingExpanderTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldNotExpandCommandKeyFromStaticInitializerMap(@TempDir Path tempDir) throws Exception {
|
void shouldExpandCommandKeyFromStaticInitializerMapPutEntries(@TempDir Path tempDir) throws Exception {
|
||||||
String source = """
|
String source = """
|
||||||
package com.example;
|
package com.example;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@@ -3029,6 +3029,63 @@ class EntryPointBindingExpanderTest {
|
|||||||
HeuristicCallGraphEngine engine = new HeuristicCallGraphEngine(context);
|
HeuristicCallGraphEngine engine = new HeuristicCallGraphEngine(context);
|
||||||
Map<String, List<click.kamil.springstatemachineexporter.analysis.model.CallEdge>> graph = engine.buildCallGraph();
|
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()
|
EntryPoint entryPoint = EntryPoint.builder()
|
||||||
.className("com.example.GenericCommandController")
|
.className("com.example.GenericCommandController")
|
||||||
.methodName("execute")
|
.methodName("execute")
|
||||||
|
|||||||
Reference in New Issue
Block a user