9 Commits

Author SHA1 Message Date
d6b1571f18 test1 2026-06-19 17:45:57 +02:00
e617fb1754 update 1 test 2026-06-19 17:37:08 +02:00
06dc0ba641 update 1 2026-06-19 17:33:32 +02:00
ba412b905e moar 2026-06-19 17:25:01 +02:00
198c5d830e test 2026-06-19 17:23:27 +02:00
a383de5a5f fix html attempt 2 2026-06-19 17:19:26 +02:00
ef9ebe3512 fix html 2026-06-19 17:11:34 +02:00
b480dc83ef transition enricher rabbit 2026-06-19 09:22:27 +02:00
0a23daae05 transition enricher 2026-06-19 09:06:37 +02:00
38 changed files with 1505 additions and 62 deletions

View File

@@ -54,6 +54,30 @@ public class ConstantResolver {
return resolveManual(sn, context, visited); return resolveManual(sn, context, visited);
} }
if (expr instanceof MethodInvocation mi) {
IMethodBinding mb = mi.resolveMethodBinding();
if (mb != null) {
ITypeBinding returnType = mb.getReturnType();
if (returnType != null) {
java.util.List<String> values = context.getEnumValues(returnType.getQualifiedName());
if (values != null && !values.isEmpty()) {
return "ENUM_SET:" + String.join(",", values);
}
}
} else {
TypeDeclaration td = findEnclosingType(mi);
if (td != null) {
MethodDeclaration md = context.findMethodDeclaration(td, mi.getName().getIdentifier(), true);
if (md != null && md.getReturnType2() != null) {
java.util.List<String> values = context.getEnumValues(md.getReturnType2().toString());
if (values != null && !values.isEmpty()) {
return "ENUM_SET:" + String.join(",", values);
}
}
}
}
}
return null; return null;
} }

View File

@@ -58,47 +58,73 @@ public class CallGraphBuilder {
String event = tp.getEvent(); String event = tp.getEvent();
if (event == null || event.isEmpty() || event.contains("\"")) return tp; if (event == null || event.isEmpty() || event.contains("\"")) return tp;
TypeDeclaration td = context.getTypeDeclaration(tp.getClassName()); String currentParamName = event;
if (td != null) { String resolvedValue = event;
MethodDeclaration md = context.findMethodDeclaration(td, tp.getMethodName(), true);
if (md != null) { // Walk backwards up the call chain
int paramIndex = -1; for (int i = path.size() - 1; i > 0; i--) {
for (int i = 0; i < md.parameters().size(); i++) { String target = path.get(i);
SingleVariableDeclaration svd = (SingleVariableDeclaration) md.parameters().get(i); String caller = path.get(i - 1);
if (svd.getName().getIdentifier().equals(event)) {
paramIndex = i; // Find parameter index in target method
break; int paramIndex = getParameterIndex(target, currentParamName);
} if (paramIndex < 0) {
} break; // Parameter name changed or not found, stop tracing
}
if (paramIndex >= 0) {
String caller = path.get(path.size() - 2); // Find the edge from caller to target to get the argument passed
String target = path.get(path.size() - 1); List<CallEdge> edges = callGraph.get(caller);
List<CallEdge> edges = callGraph.get(caller); boolean found = false;
if (edges != null) { if (edges != null) {
for (CallEdge edge : edges) { for (CallEdge edge : edges) {
if (edge.getTargetMethod().equals(target) || isHeuristicMatch(edge.getTargetMethod(), target)) { if (edge.getTargetMethod().equals(target) || isHeuristicMatch(edge.getTargetMethod(), target)) {
if (paramIndex < edge.getArguments().size()) { if (paramIndex < edge.getArguments().size()) {
String resolvedValue = edge.getArguments().get(paramIndex); String arg = edge.getArguments().get(paramIndex);
if (resolvedValue != null) { if (arg != null) {
return TriggerPoint.builder() currentParamName = arg;
.event(resolvedValue) resolvedValue = arg;
.className(tp.getClassName()) found = true;
.methodName(tp.getMethodName()) break;
.sourceFile(tp.getSourceFile())
.lineNumber(tp.getLineNumber())
.build();
}
}
} }
} }
} }
} }
} }
if (!found) break; // Could not map argument
} }
if (!resolvedValue.equals(event)) {
return TriggerPoint.builder()
.event(resolvedValue)
.className(tp.getClassName())
.methodName(tp.getMethodName())
.sourceFile(tp.getSourceFile())
.lineNumber(tp.getLineNumber())
.build();
}
return tp; return tp;
} }
private int getParameterIndex(String methodFqn, String paramName) {
if (methodFqn == null || !methodFqn.contains(".")) return -1;
String className = methodFqn.substring(0, methodFqn.lastIndexOf('.'));
String methodName = methodFqn.substring(methodFqn.lastIndexOf('.') + 1);
TypeDeclaration td = context.getTypeDeclaration(className);
if (td != null) {
MethodDeclaration md = context.findMethodDeclaration(td, methodName, true);
if (md != null) {
for (int i = 0; i < md.parameters().size(); i++) {
SingleVariableDeclaration svd = (SingleVariableDeclaration) md.parameters().get(i);
if (svd.getName().getIdentifier().equals(paramName)) {
return i;
}
}
}
}
return -1;
}
private String extractContextMachineId(List<String> path, Map<String, List<CallEdge>> callGraph) { private String extractContextMachineId(List<String> path, Map<String, List<CallEdge>> callGraph) {
for (String node : path) { for (String node : path) {
List<CallEdge> edges = callGraph.get(node); List<CallEdge> edges = callGraph.get(node);
@@ -138,6 +164,20 @@ public class CallGraphBuilder {
for (String calledMethod : calledMethods) { for (String calledMethod : calledMethods) {
graph.computeIfAbsent(currentMethodFqn, k -> new ArrayList<>()).add(new CallEdge(calledMethod, args)); graph.computeIfAbsent(currentMethodFqn, k -> new ArrayList<>()).add(new CallEdge(calledMethod, args));
} }
for (Object argObj : node.arguments()) {
if (argObj instanceof ExpressionMethodReference emr) {
String typeName = emr.getExpression().toString();
if ("this".equals(typeName) || "super".equals(typeName)) {
TypeDeclaration td = findEnclosingType(node);
if (td != null) {
String refMethod = resolveMethodInType(td, emr.getName().getIdentifier());
if (refMethod != null) {
graph.computeIfAbsent(currentMethodFqn, k -> new ArrayList<>()).add(new CallEdge(refMethod, args));
}
}
}
}
}
} }
return super.visit(node); return super.visit(node);
} }
@@ -174,6 +214,45 @@ public class CallGraphBuilder {
List<String> args = new ArrayList<>(); List<String> args = new ArrayList<>();
for (Object argObj : astArguments) { for (Object argObj : astArguments) {
Expression expr = (Expression) argObj; Expression expr = (Expression) argObj;
// Extract from lambda
if (expr instanceof LambdaExpression le) {
ASTNode body = le.getBody();
if (body instanceof Expression bodyExpr) {
expr = bodyExpr;
} else if (body instanceof Block block) {
for (Object stmtObj : block.statements()) {
if (stmtObj instanceof ReturnStatement rs && rs.getExpression() != null) {
expr = rs.getExpression();
break;
}
}
}
}
if (expr instanceof ExpressionMethodReference emr) {
TypeDeclaration td = findEnclosingType(expr);
if (td != null) {
MethodDeclaration md = context.findMethodDeclaration(td, emr.getName().getIdentifier(), true);
if (md != null && md.getBody() != null) {
for (Object stmtObj : md.getBody().statements()) {
if (stmtObj instanceof ReturnStatement rs && rs.getExpression() != null) {
expr = rs.getExpression();
break;
}
}
}
}
}
// Extract from constructor args (e.g., new CustomMessage(OrderEvent.PROCESS, ...))
expr = traceVariable(expr);
if (expr instanceof ClassInstanceCreation cic) {
if (!cic.arguments().isEmpty()) {
expr = (Expression) cic.arguments().get(0);
}
}
String val = constantResolver.resolve(expr, context); String val = constantResolver.resolve(expr, context);
if (val == null) { if (val == null) {
val = expr.toString(); // Fallback to raw string (handles Enums, 'new Class()', etc) val = expr.toString(); // Fallback to raw string (handles Enums, 'new Class()', etc)
@@ -275,4 +354,41 @@ public class CallGraphBuilder {
} }
return (TypeDeclaration) parent; return (TypeDeclaration) parent;
} }
private Expression traceVariable(Expression expr) {
if (expr instanceof SimpleName sn) {
String varName = sn.getIdentifier();
MethodDeclaration enclosingMethod = findEnclosingMethod(expr);
if (enclosingMethod != null) {
final Expression[] initializer = new Expression[1];
enclosingMethod.accept(new ASTVisitor() {
@Override
public boolean visit(VariableDeclarationFragment node) {
if (node.getName().getIdentifier().equals(varName) && node.getInitializer() != null) {
initializer[0] = node.getInitializer();
}
return super.visit(node);
}
@Override
public boolean visit(Assignment node) {
if (node.getLeftHandSide() instanceof SimpleName asn && asn.getIdentifier().equals(varName)) {
initializer[0] = node.getRightHandSide();
}
return super.visit(node);
}
});
if (initializer[0] != null) {
return traceVariable(initializer[0]);
}
}
}
return expr;
}
private MethodDeclaration findEnclosingMethod(ASTNode node) {
ASTNode parent = node.getParent();
while (parent != null && !(parent instanceof MethodDeclaration)) {
parent = parent.getParent();
}
return (MethodDeclaration) parent;
}
} }

View File

@@ -47,10 +47,12 @@ public class GenericEventDetector {
} }
private void processSendEvent(MethodInvocation node, CompilationUnit cu, List<TriggerPoint> triggers) { private void processSendEvent(MethodInvocation node, CompilationUnit cu, List<TriggerPoint> triggers) {
TriggerPoint trigger = buildTriggerPoint(node, cu, null); List<TriggerPoint> builtTriggers = buildTriggerPoints(node, cu, null);
if (trigger != null) { if (builtTriggers != null) {
log.debug("Successfully built trigger point: {}", trigger.getEvent()); for (TriggerPoint trigger : builtTriggers) {
triggers.add(trigger); log.debug("Successfully built trigger point: {}", trigger.getEvent());
triggers.add(trigger);
}
} }
} }
@@ -62,10 +64,12 @@ public class GenericEventDetector {
for (LibraryHint hint : hints) { for (LibraryHint hint : hints) {
if (calledMethod.equals(hint.getMethodFqn()) || isHintMatch(calledMethod, hint.getMethodFqn())) { if (calledMethod.equals(hint.getMethodFqn()) || isHintMatch(calledMethod, hint.getMethodFqn())) {
TriggerPoint trigger = buildTriggerPoint(node, cu, hint.getEvent()); List<TriggerPoint> builtTriggers = buildTriggerPoints(node, cu, hint.getEvent());
if (trigger != null) { if (builtTriggers != null) {
log.debug("Successfully built synthetic trigger point from hint: {}", trigger.getEvent()); for (TriggerPoint trigger : builtTriggers) {
triggers.add(trigger); log.debug("Successfully built synthetic trigger point from hint: {}", trigger.getEvent());
triggers.add(trigger);
}
} }
} }
} }
@@ -102,12 +106,12 @@ public class GenericEventDetector {
return receiver.toString() + "." + methodName; return receiver.toString() + "." + methodName;
} }
private TriggerPoint buildTriggerPoint(MethodInvocation node, CompilationUnit cu, String forcedEvent) { private List<TriggerPoint> buildTriggerPoints(MethodInvocation node, CompilationUnit cu, String forcedEvent) {
String eventValue = null; String eventValue = null;
if (forcedEvent != null) { if (forcedEvent != null) {
eventValue = context.resolveString(forcedEvent); eventValue = context.resolveString(forcedEvent);
} else { } else {
if (node.arguments().isEmpty()) return null; if (node.arguments().isEmpty()) return Collections.emptyList();
Expression eventExpr = (Expression) node.arguments().get(0); Expression eventExpr = (Expression) node.arguments().get(0);
// Try MessageBuilder extraction first // Try MessageBuilder extraction first
@@ -123,18 +127,37 @@ public class GenericEventDetector {
MethodDeclaration method = findEnclosingMethod(node); MethodDeclaration method = findEnclosingMethod(node);
TypeDeclaration type = findEnclosingType(node); TypeDeclaration type = findEnclosingType(node);
if (type == null) return null; if (type == null) return Collections.emptyList();
String sourceState = extractSourceState(node); String sourceState = extractSourceState(node);
List<TriggerPoint> results = new ArrayList<>();
if (eventValue != null && eventValue.startsWith("ENUM_SET:")) {
String[] parts = eventValue.substring(9).split(",");
for (String part : parts) {
if (!part.trim().isEmpty()) {
results.add(TriggerPoint.builder()
.event(part.trim())
.sourceState(sourceState)
.className(context.getFqn(type))
.methodName(method != null ? method.getName().getIdentifier() : "initializer")
.sourceFile(context.getRelativePath(context.getFqn(type)))
.lineNumber(cu.getLineNumber(node.getStartPosition()))
.build());
}
}
} else {
results.add(TriggerPoint.builder()
.event(eventValue)
.sourceState(sourceState)
.className(context.getFqn(type))
.methodName(method != null ? method.getName().getIdentifier() : "initializer")
.sourceFile(context.getRelativePath(context.getFqn(type)))
.lineNumber(cu.getLineNumber(node.getStartPosition()))
.build());
}
return TriggerPoint.builder() return results;
.event(eventValue)
.sourceState(sourceState)
.className(context.getFqn(type))
.methodName(method != null ? method.getName().getIdentifier() : "initializer")
.sourceFile(context.getRelativePath(context.getFqn(type)))
.lineNumber(cu.getLineNumber(node.getStartPosition()))
.build();
} }
private String extractSourceState(ASTNode node) { private String extractSourceState(ASTNode node) {
@@ -193,6 +216,10 @@ public class GenericEventDetector {
} }
private String extractEventFromMessageBuilder(Expression expr) { private String extractEventFromMessageBuilder(Expression expr) {
if (expr instanceof CastExpression ce) {
return extractEventFromMessageBuilder(ce.getExpression());
}
if (expr instanceof SimpleName sn) { if (expr instanceof SimpleName sn) {
String varName = sn.getIdentifier(); String varName = sn.getIdentifier();
MethodDeclaration enclosingMethod = findEnclosingMethod(expr); MethodDeclaration enclosingMethod = findEnclosingMethod(expr);
@@ -218,6 +245,15 @@ public class GenericEventDetector {
if (initializer[0] != null) { if (initializer[0] != null) {
return extractEventFromMessageBuilder(initializer[0]); // recursive return extractEventFromMessageBuilder(initializer[0]); // recursive
} }
// If it has NO initializer, it might be a method parameter!
for (Object paramObj : enclosingMethod.parameters()) {
if (paramObj instanceof SingleVariableDeclaration svd) {
if (svd.getName().getIdentifier().equals(varName)) {
return varName;
}
}
}
} }
} }
@@ -240,10 +276,22 @@ public class GenericEventDetector {
if (resolved != null) return resolved; if (resolved != null) return resolved;
// If not a constant, it might be a parameter like 'event' // If not a constant, it might be a parameter like 'event'
if (payloadExpr instanceof CastExpression ce) {
payloadExpr = ce.getExpression();
}
if (payloadExpr instanceof SimpleName sn) { if (payloadExpr instanceof SimpleName sn) {
return sn.getIdentifier(); // Return the parameter name so the call graph can fill it String extracted = extractEventFromMessageBuilder(payloadExpr);
if (extracted != null) {
return extracted;
}
return sn.getIdentifier(); // Fall back to returning the parameter name
} }
return payloadExpr.toString(); return payloadExpr.toString();
} else if (current.arguments().isEmpty() && current.getExpression() instanceof SimpleName sn) {
// If the event is obtained by calling a method on a provider/supplier parameter,
// return the provider's name so CallGraphBuilder can trace the lambda argument
return sn.getIdentifier();
} }
Expression receiver = current.getExpression(); Expression receiver = current.getExpression();

View File

@@ -71,6 +71,7 @@ public class MessagingDetector {
.name(protocolName + ": " + destination) .name(protocolName + ": " + destination)
.className(context.getFqn((TypeDeclaration) method.getParent())) .className(context.getFqn((TypeDeclaration) method.getParent()))
.methodName(method.getName().getIdentifier()) .methodName(method.getName().getIdentifier())
.sourceFile(context.getRelativePath(context.getFqn((TypeDeclaration) method.getParent())))
.metadata(metadata) .metadata(metadata)
.parameters(extractParameters(method)) .parameters(extractParameters(method))
.build()); .build());

View File

@@ -170,6 +170,7 @@ public class CodebaseContext {
private final List<CompilationUnit> allCus = new ArrayList<>(); private final List<CompilationUnit> allCus = new ArrayList<>();
private final Map<String, List<String>> interfaceToImpls = new HashMap<>(); private final Map<String, List<String>> interfaceToImpls = new HashMap<>();
private final Map<String, List<String>> enumValues = new HashMap<>();
public void scan(Set<Path> rootDirs, Set<String> customIgnorePatterns) throws IOException { public void scan(Set<Path> rootDirs, Set<String> customIgnorePatterns) throws IOException {
this.allProperties = propertyResolver.resolveAllProperties(rootDirs); this.allProperties = propertyResolver.resolveAllProperties(rootDirs);
@@ -188,6 +189,8 @@ public class CodebaseContext {
for (Object type : cu.types()) { for (Object type : cu.types()) {
if (type instanceof TypeDeclaration td) { if (type instanceof TypeDeclaration td) {
indexType(td, packageName, javaFile); indexType(td, packageName, javaFile);
} else if (type instanceof EnumDeclaration ed) {
indexEnum(ed, packageName, javaFile);
} }
} }
} }
@@ -230,11 +233,53 @@ public class CodebaseContext {
// Try FQN match if input was simple name // Try FQN match if input was simple name
String fqn = simpleNameToFqn.get(interfaceName); String fqn = simpleNameToFqn.get(interfaceName);
if (fqn != null) return interfaceToImpls.getOrDefault(fqn, Collections.emptyList()); if (fqn != null && interfaceToImpls.containsKey(fqn)) {
return interfaceToImpls.get(fqn);
}
// Try simple name match if input was FQN
if (interfaceName.contains(".")) {
String simpleName = interfaceName.substring(interfaceName.lastIndexOf('.') + 1);
List<String> simpleImpls = interfaceToImpls.get(simpleName);
if (simpleImpls != null) {
// To be perfectly safe, we could check if simpleNameToFqn matches,
// but for call graphs, returning potential implementations is fine.
return simpleImpls;
}
}
return Collections.emptyList(); return Collections.emptyList();
} }
private void indexEnum(EnumDeclaration ed, String parentFqn, Path javaFile) {
String simpleName = ed.getName().getIdentifier();
String fqn = parentFqn.isEmpty() ? simpleName : parentFqn + "." + simpleName;
classes.put(fqn, (CompilationUnit) ed.getRoot());
classPaths.put(fqn, javaFile);
List<String> values = new ArrayList<>();
for (Object o : ed.enumConstants()) {
if (o instanceof EnumConstantDeclaration ecd) {
values.add(fqn + "." + ecd.getName().getIdentifier());
}
}
enumValues.put(fqn, values);
if (!simpleNameToFqn.containsKey(simpleName)) {
simpleNameToFqn.put(simpleName, fqn);
}
}
public List<String> getEnumValues(String fqnOrSimpleName) {
if (fqnOrSimpleName == null) return null;
List<String> values = enumValues.get(fqnOrSimpleName);
if (values != null) return values;
String fqn = simpleNameToFqn.get(fqnOrSimpleName);
if (fqn != null) return enumValues.get(fqn);
// Also try stripping array or generic types if any (e.g. MyEnum[])
return null;
}
public State resolveState(Expression expr, CompilationUnit cu) { public State resolveState(Expression expr, CompilationUnit cu) {
if (expr == null) if (expr == null)
return null; return null;

View File

@@ -32,6 +32,9 @@ public class FileUtils {
return paths return paths
.filter(p -> Files.isRegularFile(p) && p.toString().endsWith(extension)) .filter(p -> Files.isRegularFile(p) && p.toString().endsWith(extension))
.filter(p -> matchers.stream().noneMatch(m -> m.matches(p))) .filter(p -> matchers.stream().noneMatch(m -> m.matches(p)))
.map(Path::toAbsolutePath)
.map(Path::normalize)
.distinct()
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
} }

View File

@@ -124,9 +124,13 @@ public class PlantUml implements StateMachineExporter {
sb.append(" : [[#").append(linkId).append(" "); sb.append(" : [[#").append(linkId).append(" ");
if (!label.isEmpty()) { if (!label.isEmpty()) {
sb.append(label); String sanitized = label.replace("[", "&#91;")
.replace("]", "&#93;")
.replace("<", "&lt;")
.replace(">", "&gt;");
sb.append(sanitized);
} else { } else {
sb.append("<U+00A0>"); sb.append(".");
} }
sb.append("]]"); sb.append("]]");
} else if (!label.isEmpty()) { } else if (!label.isEmpty()) {

View File

@@ -137,6 +137,12 @@ public class RegressionTest {
root.resolve("state_machines/maven_multi_module/core-module"), root.resolve("state_machines/maven_multi_module/core-module"),
Path.of("src/test/resources/golden/MavenOrderStateMachine"), Path.of("src/test/resources/golden/MavenOrderStateMachine"),
"MavenOrderStateMachine" "MavenOrderStateMachine"
),
new TestScenario(
"Complex Multi-Module Sample",
root.resolve("state_machines/complex_multi_module_sm"),
Path.of("src/test/resources/golden/StateMachineConfig"),
"StateMachineConfig"
) )
); );
} }

View File

@@ -44,4 +44,42 @@ public class GenericEventDetectorTest {
assertThat(triggers.get(0).getEvent()).isEqualTo("myEvent"); assertThat(triggers.get(0).getEvent()).isEqualTo("myEvent");
assertThat(triggers.get(0).getMethodName()).isEqualTo("a"); assertThat(triggers.get(0).getMethodName()).isEqualTo("a");
} }
@Test
void testEnumGetterUnionExtraction(@TempDir Path tempDir) throws IOException {
Path dir = tempDir.resolve("com/example");
Files.createDirectories(dir);
Files.writeString(dir.resolve("MyEnum.java"),
"package com.example;\n" +
"public enum MyEnum {\n" +
" STATE_A, STATE_B;\n" +
"}\n");
Files.writeString(dir.resolve("MyService.java"),
"package com.example;\n" +
"import org.springframework.statemachine.StateMachine;\n" +
"public class MyService {\n" +
" private StateMachine<String, String> stateMachine;\n" +
" public MyEnum getEvent() { return MyEnum.STATE_A; }\n" + // Pretend it returns the enum
" public void trigger() {\n" +
" stateMachine.sendEvent(this.getEvent());\n" +
" }\n" +
"}\n");
CodebaseContext context = new CodebaseContext();
context.scan(tempDir);
GenericEventDetector detector = new GenericEventDetector(context, new ConstantResolver(), List.of());
CompilationUnit serviceCu = context.getCompilationUnits().stream()
.filter(cu -> cu.getJavaElement() == null || cu.getJavaElement().getElementName().contains("MyService"))
.findFirst().orElseThrow();
List<TriggerPoint> triggers = detector.detect(serviceCu);
System.out.println("TRIGGERS: " + triggers);
assertThat(triggers).hasSize(2);
assertThat(triggers).anyMatch(t -> t.getEvent().equals("com.example.MyEnum.STATE_A"));
assertThat(triggers).anyMatch(t -> t.getEvent().equals("com.example.MyEnum.STATE_B"));
}
} }

View File

@@ -0,0 +1,55 @@
package click.kamil.springstatemachineexporter.exporter;
import click.kamil.springstatemachineexporter.model.State;
import click.kamil.springstatemachineexporter.model.Transition;
import click.kamil.springstatemachineexporter.model.TransitionType;
import click.kamil.springstatemachineexporter.model.Guard;
import click.kamil.springstatemachineexporter.model.Action;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat;
class PlantUmlTest {
@Test
void testExportSanitizesLabels() {
PlantUml plantUml = new PlantUml();
ExportOptions options = ExportOptions.builder()
.embedIdentifiers(true)
.useLambdaGuards(false)
.build();
State state1 = State.of("State1");
State state2 = State.of("State2");
Transition t = new Transition();
t.setType(TransitionType.EXTERNAL);
t.setSourceStates(List.of(state1));
t.setTargetStates(List.of(state2));
// Add a guard and action containing problematic characters
t.setGuard(Guard.of("list.size() < 5", false, null, null, null, null));
t.setActions(List.of(Action.of("doSomething(new int[]{1, 2})", false, null, null, null, null)));
String result = plantUml.export(
"TestMachine",
List.of(t),
Set.of("State1"),
Set.of("State2"),
options
);
// Verify that < and > are replaced with HTML entities
assertThat(result).doesNotContain("< 5");
assertThat(result).contains("&lt; 5");
// Verify that [ and ] are replaced with HTML entities inside the link
assertThat(result).doesNotContain("[list.size() &lt; 5]");
assertThat(result).doesNotContain("new int[]{1, 2}");
assertThat(result).contains("&#91;list.size() &lt; 5&#93;");
assertThat(result).contains("new int&#91;&#93;{1, 2}");
}
}

View File

@@ -137,7 +137,7 @@
"name" : "JMS: shipping.queue", "name" : "JMS: shipping.queue",
"className" : "click.kamil.examples.enterprise.messaging.ShippingJmsListener", "className" : "click.kamil.examples.enterprise.messaging.ShippingJmsListener",
"methodName" : "onShippingReady", "methodName" : "onShippingReady",
"sourceFile" : null, "sourceFile" : "src/main/java/click/kamil/examples/enterprise/messaging/ShippingJmsListener.java",
"metadata" : { "metadata" : {
"protocol" : "JMS", "protocol" : "JMS",
"destination" : "shipping.queue" "destination" : "shipping.queue"
@@ -152,7 +152,7 @@
"name" : "RABBIT: returns.queue", "name" : "RABBIT: returns.queue",
"className" : "click.kamil.examples.enterprise.messaging.ReturnsRabbitListener", "className" : "click.kamil.examples.enterprise.messaging.ReturnsRabbitListener",
"methodName" : "onReturn", "methodName" : "onReturn",
"sourceFile" : null, "sourceFile" : "src/main/java/click/kamil/examples/enterprise/messaging/ReturnsRabbitListener.java",
"metadata" : { "metadata" : {
"protocol" : "RABBIT", "protocol" : "RABBIT",
"destination" : "returns.queue" "destination" : "returns.queue"
@@ -292,7 +292,7 @@
"name" : "JMS: shipping.queue", "name" : "JMS: shipping.queue",
"className" : "click.kamil.examples.enterprise.messaging.ShippingJmsListener", "className" : "click.kamil.examples.enterprise.messaging.ShippingJmsListener",
"methodName" : "onShippingReady", "methodName" : "onShippingReady",
"sourceFile" : null, "sourceFile" : "src/main/java/click/kamil/examples/enterprise/messaging/ShippingJmsListener.java",
"metadata" : { "metadata" : {
"protocol" : "JMS", "protocol" : "JMS",
"destination" : "shipping.queue" "destination" : "shipping.queue"
@@ -326,7 +326,7 @@
"name" : "RABBIT: returns.queue", "name" : "RABBIT: returns.queue",
"className" : "click.kamil.examples.enterprise.messaging.ReturnsRabbitListener", "className" : "click.kamil.examples.enterprise.messaging.ReturnsRabbitListener",
"methodName" : "onReturn", "methodName" : "onReturn",
"sourceFile" : null, "sourceFile" : "src/main/java/click/kamil/examples/enterprise/messaging/ReturnsRabbitListener.java",
"metadata" : { "metadata" : {
"protocol" : "RABBIT", "protocol" : "RABBIT",
"destination" : "returns.queue" "destination" : "returns.queue"

View File

@@ -66,7 +66,7 @@
"name" : "JMS: order.queue", "name" : "JMS: order.queue",
"className" : "click.kamil.maven.api.JmsOrderListener", "className" : "click.kamil.maven.api.JmsOrderListener",
"methodName" : "onMessage", "methodName" : "onMessage",
"sourceFile" : null, "sourceFile" : "api-module/src/main/java/click/kamil/maven/api/JmsOrderListener.java",
"metadata" : { "metadata" : {
"protocol" : "JMS", "protocol" : "JMS",
"destination" : "order.queue" "destination" : "order.queue"

View File

@@ -0,0 +1,15 @@
digraph statemachine {
rankdir=LR;
node [shape=rounded, style=filled, fillcolor=white, fontname="Arial"];
edge [fontname="Arial", fontsize=10];
_start [shape=circle, label="", fillcolor=black, width=0.1];
_start -> NEW;
COMPLETED [fillcolor=lightgray];
CANCELLED [fillcolor=lightgray];
NEW -> PROCESSING [label="OrderEvent.PROCESS", style="solid", color="black"];
PROCESSING -> COMPLETED [label="OrderEvent.COMPLETE", style="solid", color="black"];
NEW -> CANCELLED [label="OrderEvent.CANCEL", style="solid", color="black"];
PROCESSING -> CANCELLED [label="OrderEvent.CANCEL", style="solid", color="black"];
}

View File

@@ -0,0 +1,429 @@
{
"metadata" : {
"triggers" : [ {
"event" : "event",
"className" : "click.kamil.service.StateMachineServiceImpl",
"methodName" : "sendMessage",
"sourceFile" : "service/src/main/java/click/kamil/service/StateMachineServiceImpl.java",
"sourceModule" : null,
"stateMachineId" : null,
"sourceState" : null,
"lineNumber" : 25
}, {
"event" : "eventProvider",
"className" : "click.kamil.service.StateMachineServiceImpl",
"methodName" : "sendMessageWithProvider",
"sourceFile" : "service/src/main/java/click/kamil/service/StateMachineServiceImpl.java",
"sourceModule" : null,
"stateMachineId" : null,
"sourceState" : null,
"lineNumber" : 50
}, {
"event" : "customMessage",
"className" : "click.kamil.service.StateMachineServiceImpl",
"methodName" : "sendCustomMessage",
"sourceFile" : "service/src/main/java/click/kamil/service/StateMachineServiceImpl.java",
"sourceModule" : null,
"stateMachineId" : null,
"sourceState" : null,
"lineNumber" : 78
} ],
"entryPoints" : [ {
"type" : "REST",
"name" : "POST /api/orders/process",
"className" : "click.kamil.web.OrderController",
"methodName" : "processOrder",
"sourceFile" : "web/src/main/java/click/kamil/web/OrderController.java",
"metadata" : {
"path" : "/api/orders/process",
"verb" : "POST"
},
"parameters" : [ ]
}, {
"type" : "REST",
"name" : "POST /api/orders/complete",
"className" : "click.kamil.web.OrderController",
"methodName" : "completeOrder",
"sourceFile" : "web/src/main/java/click/kamil/web/OrderController.java",
"metadata" : {
"path" : "/api/orders/complete",
"verb" : "POST"
},
"parameters" : [ ]
}, {
"type" : "REST",
"name" : "POST /api/orders/cancel",
"className" : "click.kamil.web.OrderController",
"methodName" : "cancelOrder",
"sourceFile" : "web/src/main/java/click/kamil/web/OrderController.java",
"metadata" : {
"path" : "/api/orders/cancel",
"verb" : "POST"
},
"parameters" : [ ]
}, {
"type" : "REST",
"name" : "POST /api/orders/functional-complete",
"className" : "click.kamil.web.OrderController",
"methodName" : "functionalCompleteOrder",
"sourceFile" : "web/src/main/java/click/kamil/web/OrderController.java",
"metadata" : {
"path" : "/api/orders/functional-complete",
"verb" : "POST"
},
"parameters" : [ ]
}, {
"type" : "RABBIT",
"name" : "RABBIT: order.custom.transition.queue",
"className" : "click.kamil.web.RabbitOrderListener",
"methodName" : "handleCustomTransition",
"sourceFile" : "web/src/main/java/click/kamil/web/RabbitOrderListener.java",
"metadata" : {
"protocol" : "RABBIT",
"destination" : "order.custom.transition.queue"
},
"parameters" : [ {
"name" : "payload",
"type" : "String",
"annotations" : [ ]
} ]
}, {
"type" : "JMS",
"name" : "JMS: order.process.queue",
"className" : "click.kamil.web.JmsOrderListener",
"methodName" : "receiveProcessCommand",
"sourceFile" : "web/src/main/java/click/kamil/web/JmsOrderListener.java",
"metadata" : {
"protocol" : "JMS",
"destination" : "order.process.queue"
},
"parameters" : [ {
"name" : "message",
"type" : "String",
"annotations" : [ ]
} ]
}, {
"type" : "JMS",
"name" : "JMS: order.cancel.queue",
"className" : "click.kamil.web.JmsOrderListener",
"methodName" : "receiveCancelCommand",
"sourceFile" : "web/src/main/java/click/kamil/web/JmsOrderListener.java",
"metadata" : {
"protocol" : "JMS",
"destination" : "order.cancel.queue"
},
"parameters" : [ {
"name" : "message",
"type" : "String",
"annotations" : [ ]
} ]
} ],
"callChains" : [ {
"entryPoint" : {
"type" : "REST",
"name" : "POST /api/orders/process",
"className" : "click.kamil.web.OrderController",
"methodName" : "processOrder",
"sourceFile" : "web/src/main/java/click/kamil/web/OrderController.java",
"metadata" : {
"path" : "/api/orders/process",
"verb" : "POST"
},
"parameters" : [ ]
},
"methodChain" : [ "click.kamil.web.OrderController.processOrder", "click.kamil.service.StateMachineServiceImpl.sendMessageWithOutbox", "click.kamil.service.StateMachineServiceImpl.sendMessage" ],
"triggerPoint" : {
"event" : "OrderEvent.PROCESS",
"className" : "click.kamil.service.StateMachineServiceImpl",
"methodName" : "sendMessage",
"sourceFile" : "service/src/main/java/click/kamil/service/StateMachineServiceImpl.java",
"sourceModule" : null,
"stateMachineId" : null,
"sourceState" : null,
"lineNumber" : 25
},
"contextMachineId" : null,
"matchedTransitions" : [ {
"sourceState" : "OrderState.NEW",
"targetState" : "OrderState.PROCESSING",
"event" : "OrderEvent.PROCESS"
} ]
}, {
"entryPoint" : {
"type" : "REST",
"name" : "POST /api/orders/complete",
"className" : "click.kamil.web.OrderController",
"methodName" : "completeOrder",
"sourceFile" : "web/src/main/java/click/kamil/web/OrderController.java",
"metadata" : {
"path" : "/api/orders/complete",
"verb" : "POST"
},
"parameters" : [ ]
},
"methodChain" : [ "click.kamil.web.OrderController.completeOrder", "click.kamil.service.StateMachineServiceImpl.sendMessage" ],
"triggerPoint" : {
"event" : "OrderEvent.COMPLETE",
"className" : "click.kamil.service.StateMachineServiceImpl",
"methodName" : "sendMessage",
"sourceFile" : "service/src/main/java/click/kamil/service/StateMachineServiceImpl.java",
"sourceModule" : null,
"stateMachineId" : null,
"sourceState" : null,
"lineNumber" : 25
},
"contextMachineId" : null,
"matchedTransitions" : [ {
"sourceState" : "OrderState.PROCESSING",
"targetState" : "OrderState.COMPLETED",
"event" : "OrderEvent.COMPLETE"
} ]
}, {
"entryPoint" : {
"type" : "REST",
"name" : "POST /api/orders/cancel",
"className" : "click.kamil.web.OrderController",
"methodName" : "cancelOrder",
"sourceFile" : "web/src/main/java/click/kamil/web/OrderController.java",
"metadata" : {
"path" : "/api/orders/cancel",
"verb" : "POST"
},
"parameters" : [ ]
},
"methodChain" : [ "click.kamil.web.OrderController.cancelOrder", "click.kamil.service.StateMachineServiceImpl.sendMessage" ],
"triggerPoint" : {
"event" : "OrderEvent.CANCEL",
"className" : "click.kamil.service.StateMachineServiceImpl",
"methodName" : "sendMessage",
"sourceFile" : "service/src/main/java/click/kamil/service/StateMachineServiceImpl.java",
"sourceModule" : null,
"stateMachineId" : null,
"sourceState" : null,
"lineNumber" : 25
},
"contextMachineId" : null,
"matchedTransitions" : [ {
"sourceState" : "OrderState.NEW",
"targetState" : "OrderState.CANCELLED",
"event" : "OrderEvent.CANCEL"
}, {
"sourceState" : "OrderState.PROCESSING",
"targetState" : "OrderState.CANCELLED",
"event" : "OrderEvent.CANCEL"
} ]
}, {
"entryPoint" : {
"type" : "REST",
"name" : "POST /api/orders/functional-complete",
"className" : "click.kamil.web.OrderController",
"methodName" : "functionalCompleteOrder",
"sourceFile" : "web/src/main/java/click/kamil/web/OrderController.java",
"metadata" : {
"path" : "/api/orders/functional-complete",
"verb" : "POST"
},
"parameters" : [ ]
},
"methodChain" : [ "click.kamil.web.OrderController.functionalCompleteOrder", "click.kamil.web.OrderController.triggerTransitionFunction", "click.kamil.service.StateMachineServiceImpl.sendMessage" ],
"triggerPoint" : {
"event" : "OrderEvent.COMPLETE",
"className" : "click.kamil.service.StateMachineServiceImpl",
"methodName" : "sendMessage",
"sourceFile" : "service/src/main/java/click/kamil/service/StateMachineServiceImpl.java",
"sourceModule" : null,
"stateMachineId" : null,
"sourceState" : null,
"lineNumber" : 25
},
"contextMachineId" : null,
"matchedTransitions" : [ {
"sourceState" : "OrderState.PROCESSING",
"targetState" : "OrderState.COMPLETED",
"event" : "OrderEvent.COMPLETE"
} ]
}, {
"entryPoint" : {
"type" : "RABBIT",
"name" : "RABBIT: order.custom.transition.queue",
"className" : "click.kamil.web.RabbitOrderListener",
"methodName" : "handleCustomTransition",
"sourceFile" : "web/src/main/java/click/kamil/web/RabbitOrderListener.java",
"metadata" : {
"protocol" : "RABBIT",
"destination" : "order.custom.transition.queue"
},
"parameters" : [ {
"name" : "payload",
"type" : "String",
"annotations" : [ ]
} ]
},
"methodChain" : [ "click.kamil.web.RabbitOrderListener.handleCustomTransition", "click.kamil.service.StateMachineServiceImpl.sendCustomMessage" ],
"triggerPoint" : {
"event" : "OrderEvent.PROCESS",
"className" : "click.kamil.service.StateMachineServiceImpl",
"methodName" : "sendCustomMessage",
"sourceFile" : "service/src/main/java/click/kamil/service/StateMachineServiceImpl.java",
"sourceModule" : null,
"stateMachineId" : null,
"sourceState" : null,
"lineNumber" : 78
},
"contextMachineId" : null,
"matchedTransitions" : [ {
"sourceState" : "OrderState.NEW",
"targetState" : "OrderState.PROCESSING",
"event" : "OrderEvent.PROCESS"
} ]
}, {
"entryPoint" : {
"type" : "JMS",
"name" : "JMS: order.process.queue",
"className" : "click.kamil.web.JmsOrderListener",
"methodName" : "receiveProcessCommand",
"sourceFile" : "web/src/main/java/click/kamil/web/JmsOrderListener.java",
"metadata" : {
"protocol" : "JMS",
"destination" : "order.process.queue"
},
"parameters" : [ {
"name" : "message",
"type" : "String",
"annotations" : [ ]
} ]
},
"methodChain" : [ "click.kamil.web.JmsOrderListener.receiveProcessCommand", "click.kamil.service.StateMachineServiceImpl.sendMessageWithOutbox", "click.kamil.service.StateMachineServiceImpl.sendMessage" ],
"triggerPoint" : {
"event" : "OrderEvent.PROCESS",
"className" : "click.kamil.service.StateMachineServiceImpl",
"methodName" : "sendMessage",
"sourceFile" : "service/src/main/java/click/kamil/service/StateMachineServiceImpl.java",
"sourceModule" : null,
"stateMachineId" : null,
"sourceState" : null,
"lineNumber" : 25
},
"contextMachineId" : null,
"matchedTransitions" : [ {
"sourceState" : "OrderState.NEW",
"targetState" : "OrderState.PROCESSING",
"event" : "OrderEvent.PROCESS"
} ]
}, {
"entryPoint" : {
"type" : "JMS",
"name" : "JMS: order.cancel.queue",
"className" : "click.kamil.web.JmsOrderListener",
"methodName" : "receiveCancelCommand",
"sourceFile" : "web/src/main/java/click/kamil/web/JmsOrderListener.java",
"metadata" : {
"protocol" : "JMS",
"destination" : "order.cancel.queue"
},
"parameters" : [ {
"name" : "message",
"type" : "String",
"annotations" : [ ]
} ]
},
"methodChain" : [ "click.kamil.web.JmsOrderListener.receiveCancelCommand", "click.kamil.service.StateMachineServiceImpl.sendMessageWithProvider" ],
"triggerPoint" : {
"event" : "OrderEvent.CANCEL",
"className" : "click.kamil.service.StateMachineServiceImpl",
"methodName" : "sendMessageWithProvider",
"sourceFile" : "service/src/main/java/click/kamil/service/StateMachineServiceImpl.java",
"sourceModule" : null,
"stateMachineId" : null,
"sourceState" : null,
"lineNumber" : 50
},
"contextMachineId" : null,
"matchedTransitions" : [ {
"sourceState" : "OrderState.NEW",
"targetState" : "OrderState.CANCELLED",
"event" : "OrderEvent.CANCEL"
}, {
"sourceState" : "OrderState.PROCESSING",
"targetState" : "OrderState.CANCELLED",
"event" : "OrderEvent.CANCEL"
} ]
} ],
"properties" : {
"default" : { }
}
},
"name" : "click.kamil.domain.StateMachineConfig",
"renderChoicesAsDiamonds" : true,
"startStates" : [ "OrderState.NEW" ],
"transitions" : [ {
"type" : "EXTERNAL",
"sourceStates" : [ {
"rawName" : "OrderState.NEW",
"fullIdentifier" : "OrderState.NEW"
} ],
"targetStates" : [ {
"rawName" : "OrderState.PROCESSING",
"fullIdentifier" : "OrderState.PROCESSING"
} ],
"event" : {
"rawName" : "OrderEvent.PROCESS",
"fullIdentifier" : "OrderEvent.PROCESS"
},
"guard" : null,
"actions" : [ ],
"order" : null
}, {
"type" : "EXTERNAL",
"sourceStates" : [ {
"rawName" : "OrderState.PROCESSING",
"fullIdentifier" : "OrderState.PROCESSING"
} ],
"targetStates" : [ {
"rawName" : "OrderState.COMPLETED",
"fullIdentifier" : "OrderState.COMPLETED"
} ],
"event" : {
"rawName" : "OrderEvent.COMPLETE",
"fullIdentifier" : "OrderEvent.COMPLETE"
},
"guard" : null,
"actions" : [ ],
"order" : null
}, {
"type" : "EXTERNAL",
"sourceStates" : [ {
"rawName" : "OrderState.NEW",
"fullIdentifier" : "OrderState.NEW"
} ],
"targetStates" : [ {
"rawName" : "OrderState.CANCELLED",
"fullIdentifier" : "OrderState.CANCELLED"
} ],
"event" : {
"rawName" : "OrderEvent.CANCEL",
"fullIdentifier" : "OrderEvent.CANCEL"
},
"guard" : null,
"actions" : [ ],
"order" : null
}, {
"type" : "EXTERNAL",
"sourceStates" : [ {
"rawName" : "OrderState.PROCESSING",
"fullIdentifier" : "OrderState.PROCESSING"
} ],
"targetStates" : [ {
"rawName" : "OrderState.CANCELLED",
"fullIdentifier" : "OrderState.CANCELLED"
} ],
"event" : {
"rawName" : "OrderEvent.CANCEL",
"fullIdentifier" : "OrderEvent.CANCEL"
},
"guard" : null,
"actions" : [ ],
"order" : null
} ],
"endStates" : [ "OrderState.COMPLETED", "OrderState.CANCELLED" ]
}

View File

@@ -0,0 +1,35 @@
@startuml
!pragma layout smetana
set separator none
hide empty description
hide stereotype
skinparam state {
BackgroundColor white
BorderColor #94a3b8
BorderThickness 1
FontName Inter
FontSize 9
FontStyle bold
RoundCorner 20
Padding 1
}
skinparam shadowing false
skinparam ArrowFontName JetBrains Mono
skinparam ArrowFontSize 8
skinparam ArrowColor #cbd5e1
skinparam ArrowThickness 1
skinparam dpi 110
skinparam svgLinkTarget _self
[*] --> OrderState.NEW
OrderState.NEW -[#1E90FF,bold]-> OrderState.PROCESSING <<external>> : OrderEvent.PROCESS
OrderState.PROCESSING -[#1E90FF,bold]-> OrderState.COMPLETED <<external>> : OrderEvent.COMPLETE
OrderState.NEW -[#1E90FF,bold]-> OrderState.CANCELLED <<external>> : OrderEvent.CANCEL
OrderState.PROCESSING -[#1E90FF,bold]-> OrderState.CANCELLED <<external>> : OrderEvent.CANCEL
OrderState.COMPLETED --> [*]
OrderState.CANCELLED --> [*]
@enduml

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0" initial="NEW">
<state id="NEW">
<transition target="PROCESSING" event="OrderEvent.PROCESS"/>
<transition target="CANCELLED" event="OrderEvent.CANCEL"/>
</state>
<state id="PROCESSING">
<transition target="COMPLETED" event="OrderEvent.COMPLETE"/>
<transition target="CANCELLED" event="OrderEvent.CANCEL"/>
</state>
<state id="COMPLETED">
</state>
<state id="CANCELLED">
</state>
</scxml>

View File

@@ -576,7 +576,7 @@
if (!c.matchedTransitions || c.matchedTransitions.length === 0) return false; if (!c.matchedTransitions || c.matchedTransitions.length === 0) return false;
return c.matchedTransitions.some(t => { return c.matchedTransitions.some(t => {
const cEventStr = typeof t.event === 'object' ? (t.event.fullIdentifier || t.event.rawName || t.event) : t.event; const cEventStr = typeof t.event === 'object' ? (t.event.fullIdentifier || t.event.rawName || t.event) : t.event;
return normalize(cEventStr) === name; return normalize(cEventStr) === normalize(name);
}); });
}); });
@@ -631,7 +631,7 @@
let html = `<div style="font-size:0.65rem; font-weight:700; color:#94a3b8; margin-top:8px; margin-bottom:4px; text-transform:uppercase;">Input Data</div>`; let html = `<div style="font-size:0.65rem; font-weight:700; color:#94a3b8; margin-top:8px; margin-bottom:4px; text-transform:uppercase;">Input Data</div>`;
html += `<div class="payload-box">`; html += `<div class="payload-box">`;
params.forEach(p => { params.forEach(p => {
const annotation = (p.annotations || []).find(a => a.endsWith("RequestBody") || a.endsWith("PathVariable") || a.endsWith("RequestParam")); const annotation = (p.annotations || []).find(a => a.endsWith("RequestBody") || a.endsWith("PathVariable") || a.endsWith("RequestParam") || a.endsWith("Payload") || a.endsWith("Header") || a.endsWith("Headers"));
const cleanAnn = annotation ? "@" + annotation.substring(annotation.lastIndexOf('.') + 1) : ""; const cleanAnn = annotation ? "@" + annotation.substring(annotation.lastIndexOf('.') + 1) : "";
html += `<div class="payload-param"> html += `<div class="payload-param">
<span style="color:#f43f5e; font-size:0.65rem; font-weight:bold;">${cleanAnn}</span> <span style="color:#f43f5e; font-size:0.65rem; font-weight:bold;">${cleanAnn}</span>

View File

@@ -0,0 +1,33 @@
HELP.md
target/
.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/

View File

@@ -0,0 +1,33 @@
HELP.md
target/
.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>click.kamil.complex</groupId>
<artifactId>complex-multi-module-sm</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>domain</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.statemachine</groupId>
<artifactId>spring-statemachine-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,24 @@
package click.kamil.domain;
import org.springframework.messaging.support.GenericMessage;
import java.util.Map;
public class CustomStateMachineMessage<T, P> extends GenericMessage<T> {
private final P customPayload;
public CustomStateMachineMessage(T payload, P customPayload) {
super(payload);
this.customPayload = customPayload;
}
public CustomStateMachineMessage(T payload, Map<String, Object> headers, P customPayload) {
super(payload, headers);
this.customPayload = customPayload;
}
public P getCustomPayload() {
return customPayload;
}
}

View File

@@ -0,0 +1,4 @@
package click.kamil.domain;
public interface IEvent {
}

View File

@@ -0,0 +1,4 @@
package click.kamil.domain;
public interface IState {
}

View File

@@ -0,0 +1,5 @@
package click.kamil.domain;
public enum OrderEvent implements IEvent {
PROCESS, COMPLETE, CANCEL
}

View File

@@ -0,0 +1,5 @@
package click.kamil.domain;
public enum OrderState implements IState {
NEW, PROCESSING, COMPLETED, CANCELLED
}

View File

@@ -0,0 +1,38 @@
package click.kamil.domain;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import java.util.EnumSet;
@Configuration
@EnableStateMachine
public class StateMachineConfig extends EnumStateMachineConfigurerAdapter<OrderState, OrderEvent> {
@Override
public void configure(StateMachineStateConfigurer<OrderState, OrderEvent> states) throws Exception {
states
.withStates()
.initial(OrderState.NEW)
.states(EnumSet.allOf(OrderState.class));
}
@Override
public void configure(StateMachineTransitionConfigurer<OrderState, OrderEvent> transitions) throws Exception {
transitions
.withExternal()
.source(OrderState.NEW).target(OrderState.PROCESSING).event(OrderEvent.PROCESS)
.and()
.withExternal()
.source(OrderState.PROCESSING).target(OrderState.COMPLETED).event(OrderEvent.COMPLETE)
.and()
.withExternal()
.source(OrderState.NEW).target(OrderState.CANCELLED).event(OrderEvent.CANCEL)
.and()
.withExternal()
.source(OrderState.PROCESSING).target(OrderState.CANCELLED).event(OrderEvent.CANCEL);
}
}

View File

@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>click.kamil.complex</groupId>
<artifactId>complex-multi-module-sm</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<modules>
<module>domain</module>
<module>service</module>
<module>web</module>
</modules>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<spring-boot.version>3.2.0</spring-boot.version>
<spring-statemachine.version>3.2.0</spring-statemachine.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.statemachine</groupId>
<artifactId>spring-statemachine-core</artifactId>
<version>${spring-statemachine.version}</version>
</dependency>
<dependency>
<groupId>click.kamil.complex</groupId>
<artifactId>domain</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>click.kamil.complex</groupId>
<artifactId>service</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

View File

@@ -0,0 +1,33 @@
HELP.md
target/
.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/

View File

@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>click.kamil.complex</groupId>
<artifactId>complex-multi-module-sm</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>service</artifactId>
<dependencies>
<dependency>
<groupId>click.kamil.complex</groupId>
<artifactId>domain</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.statemachine</groupId>
<artifactId>spring-statemachine-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,7 @@
package click.kamil.service;
import click.kamil.domain.IEvent;
public interface OutboxAction<T extends IEvent> {
void onCommit(T event);
}

View File

@@ -0,0 +1,26 @@
package click.kamil.service;
import click.kamil.domain.IEvent;
import click.kamil.domain.IState;
public interface StateMachineService {
<S extends IState, T extends IEvent> void sendMessage(T event);
<S extends IState, T extends IEvent, A extends OutboxAction<T>> void sendMessageWithOutbox(T event, A action);
// Quirky pattern: Bounded generic supplier instead of reflection
<T extends click.kamil.domain.OrderEvent> void sendMessageWithProvider(java.util.function.Supplier<T> eventProvider);
// Quirky pattern 2: Generic Varargs
<T extends IEvent> void sendEventsVarargs(T... events);
// Functional quirks: passing functions and method references
<T extends IEvent> void executeFunctionalTransition(java.util.function.Supplier<T> eventSupplier,
java.util.function.Function<T, Void> transitionAction);
// Child class of Spring's GenericMessage with a generic payload
<T extends click.kamil.domain.OrderEvent, P> void sendCustomMessage(click.kamil.domain.CustomStateMachineMessage<T, P> customMessage);
// Quirky pattern 3: Multiple generic bounds (Intersection Types)
<T extends IEvent & java.io.Serializable & Cloneable> T processQuirkyEvent(T event);
}

View File

@@ -0,0 +1,87 @@
package click.kamil.service;
import click.kamil.domain.IEvent;
import click.kamil.domain.IState;
import click.kamil.domain.OrderEvent;
import click.kamil.domain.OrderState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.statemachine.StateMachine;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import reactor.core.publisher.Mono;
@Service
public class StateMachineServiceImpl implements StateMachineService {
@Autowired
private StateMachine<OrderState, OrderEvent> stateMachine;
@Override
public <S extends IState, T extends IEvent> void sendMessage(T event) {
if (event instanceof OrderEvent) {
stateMachine.sendEvent(Mono.just(MessageBuilder.withPayload((OrderEvent) event).build())).subscribe();
}
}
@Override
@Transactional
public <S extends IState, T extends IEvent, A extends OutboxAction<T>> void sendMessageWithOutbox(T event, A action) {
sendMessage(event);
if (TransactionSynchronizationManager.isSynchronizationActive()) {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
@Override
public void afterCommit() {
action.onCommit(event);
}
});
} else {
action.onCommit(event);
}
}
@Override
public <T extends OrderEvent> void sendMessageWithProvider(java.util.function.Supplier<T> eventProvider) {
T event = eventProvider.get();
if (event != null) {
stateMachine.sendEvent(Mono.just(MessageBuilder.withPayload((OrderEvent) event).build())).subscribe();
}
}
@Override
@SafeVarargs
public final <T extends IEvent> void sendEventsVarargs(T... events) {
for (T event : events) {
sendMessage(event);
}
}
@Override
public <T extends IEvent> void executeFunctionalTransition(java.util.function.Supplier<T> eventSupplier,
java.util.function.Function<T, Void> transitionAction) {
// Resolve the event from the getter / supplier function
T event = eventSupplier.get();
// Apply the transition via the provided function / method reference
transitionAction.apply(event);
}
@Override
public <T extends OrderEvent, P> void sendCustomMessage(click.kamil.domain.CustomStateMachineMessage<T, P> customMessage) {
// Because CustomStateMachineMessage extends GenericMessage<T> and implements Message<T>,
// we can pass it directly to the state machine as long as T resolves to OrderEvent!
// We cast it to Message<OrderEvent> to satisfy Mono's strict type requirements
org.springframework.messaging.Message<OrderEvent> msg = (org.springframework.messaging.Message<OrderEvent>) customMessage;
stateMachine.sendEvent(Mono.just(msg)).subscribe();
}
@Override
public <T extends IEvent & java.io.Serializable & Cloneable> T processQuirkyEvent(T event) {
// Quirky pattern 3: Multiple bounds, returning the same generic type
sendMessage(event);
return event; // returns something that is guaranteed to be IEvent, Serializable, AND Cloneable
}
}

View File

@@ -0,0 +1,33 @@
HELP.md
target/
.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/

View File

@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>click.kamil.complex</groupId>
<artifactId>complex-multi-module-sm</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>web</artifactId>
<dependencies>
<dependency>
<groupId>click.kamil.complex</groupId>
<artifactId>service</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,14 @@
package click.kamil.web;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jms.annotation.EnableJms;
@SpringBootApplication(scanBasePackages = "click.kamil")
@EnableJms
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@@ -0,0 +1,31 @@
package click.kamil.web;
import click.kamil.domain.OrderEvent;
import click.kamil.service.StateMachineService;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class JmsOrderListener {
private final StateMachineService stateMachineService;
// Constructor injection
public JmsOrderListener(StateMachineService stateMachineService) {
this.stateMachineService = stateMachineService;
}
@JmsListener(destination = "order.process.queue")
public void receiveProcessCommand(String message) {
System.out.println("Received JMS message: " + message);
stateMachineService.sendMessageWithOutbox(OrderEvent.PROCESS, event -> {
System.out.println("JMS Triggered Faulty Outbox Action executed post-commit for event: " + event);
});
}
@JmsListener(destination = "order.cancel.queue")
public void receiveCancelCommand(String message) {
System.out.println("Received JMS message: " + message);
stateMachineService.sendMessageWithProvider(() -> OrderEvent.CANCEL);
}
}

View File

@@ -0,0 +1,60 @@
package click.kamil.web;
import click.kamil.domain.OrderEvent;
import click.kamil.service.StateMachineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/orders")
public class OrderController {
@Autowired
private StateMachineService stateMachineService;
@PostMapping("/process")
public String processOrder() {
stateMachineService.sendMessageWithOutbox(OrderEvent.PROCESS, event -> {
System.out.println("Faulty Outbox Action executed post-commit for event: " + event);
});
return "Order process event sent within transaction";
}
@PostMapping("/complete")
public String completeOrder() {
stateMachineService.sendMessage(OrderEvent.COMPLETE);
return "Order complete event sent";
}
@PostMapping("/cancel")
public String cancelOrder() {
stateMachineService.sendMessage(OrderEvent.CANCEL);
return "Order cancel event sent";
}
// --- Functional Quirks for Static Analysis ---
// A getter returning a specific ENUM constant
public OrderEvent getCompleteEvent() {
return OrderEvent.COMPLETE;
}
// A specific function that performs the transition
public Void triggerTransitionFunction(click.kamil.domain.IEvent event) {
stateMachineService.sendMessage(event);
return null;
}
@PostMapping("/functional-complete")
public String functionalCompleteOrder() {
// Pass the getter and the transition applier as method references
stateMachineService.executeFunctionalTransition(
this::getCompleteEvent,
this::triggerTransitionFunction
);
return "Functional complete event triggered via method references";
}
}

View File

@@ -0,0 +1,32 @@
package click.kamil.web;
import click.kamil.domain.CustomStateMachineMessage;
import click.kamil.domain.OrderEvent;
import click.kamil.service.StateMachineService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class RabbitOrderListener {
private final StateMachineService stateMachineService;
// Constructor Injection
public RabbitOrderListener(StateMachineService stateMachineService) {
this.stateMachineService = stateMachineService;
}
// Listens to RabbitMQ, constructs the custom class, triggers a state transition
@RabbitListener(queues = "order.custom.transition.queue")
public void handleCustomTransition(String payload) {
System.out.println("Received RabbitMQ message: " + payload);
// Construct our custom event containing the generic payload and the enum state
CustomStateMachineMessage<OrderEvent, String> customMessage =
new CustomStateMachineMessage<>(OrderEvent.PROCESS, "My Rabbit Custom Payload: " + payload);
// Push the custom event into the state machine to perform the transition
stateMachineService.sendCustomMessage(customMessage);
System.out.println("Triggered PROCESS transition via custom message over RabbitMQ");
}
}