Support Objects.equals for path binding and branch constraints.
Expand machineType routing when dispatchers use java.util.Objects.equals with helper-wrapped parameters, and evaluate those compound constraints without breaking existing receiver-equals forms. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -62,4 +62,17 @@ class BooleanConstraintEvaluatorBindingsTest {
|
||||
"\"DOCUMENT\".equalsIgnoreCase(normalizeType(machineType))",
|
||||
Map.of("machineType", "DOCUMENT"))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldEvaluateObjectsEqualsWithHelperWrappedParameter() {
|
||||
assertThat(BooleanConstraintEvaluator.isCompatibleWithBindings(
|
||||
"java.util.Objects.equals(\"ORDER\", normalizeType(machineType))",
|
||||
Map.of("machineType", "ORDER"))).isTrue();
|
||||
assertThat(BooleanConstraintEvaluator.isCompatibleWithBindings(
|
||||
"java.util.Objects.equals(\"ORDER\", normalizeType(machineType))",
|
||||
Map.of("machineType", "DOCUMENT"))).isFalse();
|
||||
assertThat(BooleanConstraintEvaluator.isCompatibleWithBindings(
|
||||
"java.util.Objects.equals(normalizeType(machineType), \"DOCUMENT\")",
|
||||
Map.of("machineType", "DOCUMENT"))).isTrue();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -963,4 +963,132 @@ class EntryPointBindingExpanderTest {
|
||||
.toList())
|
||||
.containsExactlyInAnyOrder("SUBMIT", "APPROVE");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldExpandMachineTypeFromArgumentEqualsOnSingleArgHelper(@TempDir Path tempDir) throws Exception {
|
||||
String source = """
|
||||
package com.example;
|
||||
public class MachineController {
|
||||
StateMachineDispatcher dispatcher;
|
||||
public void transition(String machineType, String event) {
|
||||
dispatcher.dispatch(machineType, event);
|
||||
}
|
||||
}
|
||||
class StateMachineDispatcher {
|
||||
void dispatch(String machineType, String eventString) {
|
||||
if (normalizeType(machineType).equalsIgnoreCase("ORDER")) {
|
||||
fireOrder(eventString);
|
||||
} else if (normalizeType(machineType).equalsIgnoreCase("DOCUMENT")) {
|
||||
fireDocument(eventString);
|
||||
}
|
||||
}
|
||||
String normalizeType(String raw) {
|
||||
return raw.trim();
|
||||
}
|
||||
void fireOrder(String eventString) {
|
||||
OrderEvent.valueOf(eventString.toUpperCase());
|
||||
}
|
||||
void fireDocument(String eventString) {
|
||||
DocumentEvent.valueOf(eventString.toUpperCase());
|
||||
}
|
||||
}
|
||||
enum OrderEvent { PAY, SHIP }
|
||||
enum DocumentEvent { SUBMIT, APPROVE }
|
||||
""";
|
||||
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.MachineController")
|
||||
.methodName("transition")
|
||||
.name("POST /api/machine/{machineType}/transition/{event}")
|
||||
.parameters(List.of(
|
||||
EntryPoint.Parameter.builder()
|
||||
.name("machineType")
|
||||
.type("String")
|
||||
.annotations(List.of("PathVariable"))
|
||||
.build(),
|
||||
EntryPoint.Parameter.builder()
|
||||
.name("event")
|
||||
.type("String")
|
||||
.annotations(List.of("PathVariable"))
|
||||
.build()))
|
||||
.build();
|
||||
|
||||
List<Map<String, String>> variants =
|
||||
EntryPointBindingExpander.expandPathVariableBindings(entryPoint, context, graph);
|
||||
|
||||
assertThat(variants.stream().map(v -> v.get("machineType")).distinct())
|
||||
.containsExactlyInAnyOrder("ORDER", "DOCUMENT");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldExpandMachineTypeFromObjectsEqualsOnSingleArgHelper(@TempDir Path tempDir) throws Exception {
|
||||
String source = """
|
||||
package com.example;
|
||||
public class MachineController {
|
||||
StateMachineDispatcher dispatcher;
|
||||
public void transition(String machineType, String event) {
|
||||
dispatcher.dispatch(machineType, event);
|
||||
}
|
||||
}
|
||||
class StateMachineDispatcher {
|
||||
void dispatch(String machineType, String eventString) {
|
||||
if (java.util.Objects.equals("ORDER", normalizeType(machineType))) {
|
||||
fireOrder(eventString);
|
||||
} else if (java.util.Objects.equals("DOCUMENT", normalizeType(machineType))) {
|
||||
fireDocument(eventString);
|
||||
}
|
||||
}
|
||||
String normalizeType(String raw) {
|
||||
return raw.trim();
|
||||
}
|
||||
void fireOrder(String eventString) {
|
||||
OrderEvent.valueOf(eventString.toUpperCase());
|
||||
}
|
||||
void fireDocument(String eventString) {
|
||||
DocumentEvent.valueOf(eventString.toUpperCase());
|
||||
}
|
||||
}
|
||||
enum OrderEvent { PAY, SHIP }
|
||||
enum DocumentEvent { SUBMIT, APPROVE }
|
||||
""";
|
||||
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.MachineController")
|
||||
.methodName("transition")
|
||||
.name("POST /api/machine/{machineType}/transition/{event}")
|
||||
.parameters(List.of(
|
||||
EntryPoint.Parameter.builder()
|
||||
.name("machineType")
|
||||
.type("String")
|
||||
.annotations(List.of("PathVariable"))
|
||||
.build(),
|
||||
EntryPoint.Parameter.builder()
|
||||
.name("event")
|
||||
.type("String")
|
||||
.annotations(List.of("PathVariable"))
|
||||
.build()))
|
||||
.build();
|
||||
|
||||
List<Map<String, String>> variants =
|
||||
EntryPointBindingExpander.expandPathVariableBindings(entryPoint, context, graph);
|
||||
|
||||
assertThat(variants.stream().map(v -> v.get("machineType")).distinct())
|
||||
.containsExactlyInAnyOrder("ORDER", "DOCUMENT");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,4 +180,89 @@ class MultiModulePathBindingExpanderTest {
|
||||
assertThat(variants).extracting(map -> map.get("event"))
|
||||
.containsExactlyInAnyOrder("PAY", "SHIP");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldExpandMachineTypeFromIfEqualsOnSiblingModuleHelper(@TempDir Path tempDir) throws Exception {
|
||||
Path root = tempDir.resolve("enterprise-style");
|
||||
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/util"));
|
||||
Files.writeString(apiModule.resolve("build.gradle"), "");
|
||||
Files.writeString(apiModule.resolve("src/main/java/com/example/util/MachineTypeNormalizer.java"), """
|
||||
package com.example.util;
|
||||
public class MachineTypeNormalizer {
|
||||
public static String normalize(String raw) {
|
||||
return raw.trim();
|
||||
}
|
||||
}
|
||||
""");
|
||||
|
||||
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/MachineController.java"), """
|
||||
package com.example;
|
||||
import com.example.util.MachineTypeNormalizer;
|
||||
public class MachineController {
|
||||
StateMachineDispatcher dispatcher;
|
||||
public void transition(String machineType, String event) {
|
||||
dispatcher.dispatch(machineType, event);
|
||||
}
|
||||
}
|
||||
class StateMachineDispatcher {
|
||||
void dispatch(String machineType, String eventString) {
|
||||
if ("ORDER".equalsIgnoreCase(MachineTypeNormalizer.normalize(machineType))) {
|
||||
OrderEvent.valueOf(eventString.toUpperCase());
|
||||
} else if ("DOCUMENT".equalsIgnoreCase(MachineTypeNormalizer.normalize(machineType))) {
|
||||
DocumentEvent.valueOf(eventString.toUpperCase());
|
||||
}
|
||||
}
|
||||
}
|
||||
enum OrderEvent { PAY, SHIP }
|
||||
enum DocumentEvent { SUBMIT, APPROVE }
|
||||
""");
|
||||
|
||||
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.MachineController")
|
||||
.methodName("transition")
|
||||
.name("POST /api/machine/{machineType}/transition/{event}")
|
||||
.parameters(List.of(
|
||||
EntryPoint.Parameter.builder()
|
||||
.name("machineType")
|
||||
.type("String")
|
||||
.annotations(List.of("PathVariable"))
|
||||
.build(),
|
||||
EntryPoint.Parameter.builder()
|
||||
.name("event")
|
||||
.type("String")
|
||||
.annotations(List.of("PathVariable"))
|
||||
.build()))
|
||||
.build();
|
||||
|
||||
List<Map<String, String>> variants =
|
||||
EntryPointBindingExpander.expandPathVariableBindings(entryPoint, context, callGraph);
|
||||
|
||||
assertThat(variants.stream().map(v -> v.get("machineType")).distinct())
|
||||
.containsExactlyInAnyOrder("ORDER", "DOCUMENT");
|
||||
assertThat(variants.stream()
|
||||
.filter(v -> "ORDER".equals(v.get("machineType")))
|
||||
.map(v -> v.get("event"))
|
||||
.toList())
|
||||
.containsExactlyInAnyOrder("PAY", "SHIP");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user