heuristics update

This commit is contained in:
2026-06-24 06:57:46 +02:00
parent f30538e8c9
commit b35effd05c
6 changed files with 949 additions and 143 deletions

View File

@@ -0,0 +1,133 @@
package click.kamil.springstatemachineexporter.analysis.service;
import click.kamil.springstatemachineexporter.analysis.model.CallChain;
import click.kamil.springstatemachineexporter.analysis.model.EntryPoint;
import click.kamil.springstatemachineexporter.analysis.model.TriggerPoint;
import click.kamil.springstatemachineexporter.ast.common.CodebaseContext;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@Tag("heuristic_guess_paths_and_scrape_regex_replace_with_data_flow_analysis")
class HeuristicCallGraphEngineExtendedTest {
/**
* Diagnostic: Generics, inherited processing, multi-enum constructor args,
* and internal constructor mapping via a switch method.
* <p>
* Pattern:
* 1. Abstract base class AbstractEventClass<T> holds the payload and transitionType.
* 2. Abstract base class also contains the method that actually fires the state machine.
* 3. MyEventClass<T> constructor takes multiple enums (InputEnum1, InputEnum2).
* 4. MyEventClass assigns: this.transitionType = computeTransitionType(InputEnum2).
* 5. computeTransitionType uses a switch to map InputEnum2 to TransitionEnum.
* <p>
* Root cause targeted: The engine must not eagerly grab InputEnum1.IGNORE or
* InputEnum2.SOURCE_B. It must evaluate the internal computeTransitionType()
* method during instantiation to resolve the final TransitionEnum.STATE_Y.
*/
@Test
void shouldResolveTransitionEnumThroughGenericsAndInternalConstructorSwitchComputation() throws IOException {
String source = """
package com.example;
public class SystemController {
private StateMachine machine;
public void processIncomingPayload(String rawData) {
// Creates concrete class with multiple enums and a generic payload.
// Engine MUST NOT stop at IGNORE or SOURCE_B.
MyEventClass<String> event = new MyEventClass<>(
rawData,
InputEnum1.IGNORE,
InputEnum2.SOURCE_B
);
// Calls the inherited method that actually processes the event
event.fireEvent(machine);
}
}
abstract class AbstractEventClass<T> {
protected T payload;
protected TransitionEnum transitionType;
public AbstractEventClass(T payload) {
this.payload = payload;
}
// The method to actually process events
public void fireEvent(StateMachine machine) {
machine.fire(this.getTransitionType());
}
public TransitionEnum getTransitionType() {
return this.transitionType;
}
}
class MyEventClass<T> extends AbstractEventClass<T> {
private InputEnum1 routingFlag;
public MyEventClass(T payload, InputEnum1 flag, InputEnum2 source) {
super(payload);
this.routingFlag = flag;
// The trap: Assignment via an internal method evaluation
this.transitionType = computeTransitionType(source);
}
private TransitionEnum computeTransitionType(InputEnum2 source) {
return switch (source) {
case SOURCE_A -> TransitionEnum.STATE_X;
case SOURCE_B -> TransitionEnum.STATE_Y; // Expected resolution
case SOURCE_C -> TransitionEnum.STATE_Z;
};
}
}
class StateMachine {
public void fire(TransitionEnum event) {}
}
enum InputEnum1 { IGNORE, PROCESS_ASYNC }
enum InputEnum2 { SOURCE_A, SOURCE_B, SOURCE_C }
enum TransitionEnum { STATE_X, STATE_Y, STATE_Z }
""";
java.nio.file.Path tempDir = java.nio.file.Files.createTempDirectory("callgraph_test_generics_computation");
java.nio.file.Files.writeString(tempDir.resolve("OrderConfig.java"), source);
CodebaseContext context = new CodebaseContext();
context.scan(tempDir);
HeuristicCallGraphEngine engine = new HeuristicCallGraphEngine(context);
EntryPoint entryPoint = EntryPoint.builder()
.className("com.example.SystemController")
.methodName("processIncomingPayload")
.build();
TriggerPoint trigger = TriggerPoint.builder()
.className("com.example.StateMachine")
.methodName("fire")
.event("event")
.build();
List<CallChain> chains = engine.findChains(List.of(entryPoint), List.of(trigger));
assertThat(chains).hasSize(1);
System.out.println("GENERICS & COMPUTATION polymorphicEvents: " +
chains.get(0).getTriggerPoint().getPolymorphicEvents());
// The assertion to prove the bug is fixed.
// If the engine returns InputEnum2.SOURCE_B or InputEnum1.IGNORE, it fails.
// It MUST evaluate computeTransitionType() and return TransitionEnum.STATE_Y.
assertThat(chains.get(0).getTriggerPoint().getPolymorphicEvents())
.containsExactlyInAnyOrder("TransitionEnum.STATE_Y");
}
}

View File

@@ -2991,4 +2991,112 @@ class HeuristicCallGraphEngineTest {
assertThat(chains.get(0).getTriggerPoint().getPolymorphicEvents())
.containsExactlyInAnyOrder("SMEvent.PAY_RECEIVED");
}
/**
* Diagnostic: Complex multi-argument initialization with abstract base class mapping.
*
* Pattern:
* 1. A Factory creates a specific subclass (WebCommand), passing multiple arguments
* including an intermediate enum (SourceType.WEB_API) alongside strings and ints.
* 2. The subclass delegates to an abstract BaseCommand via super(...).
* 3. The base class stores the intermediate enum in a protected field.
* 4. The controller calls an inherited method: cmd.getTransitionEvent()
* 5. The inherited method uses a switch on the protected field to return a TransitionEnum.
*
* Root cause targeted: The engine traces the field back to the constructor and finds
* SourceType.WEB_API. If the engine is buggy, it will eagerly return "SourceType.WEB_API".
* If correct, it will evaluate the switch mapping and return "TransitionEnum.ON_WEB_START".
*/
@Test
void shouldResolveMappedTransitionEnumThroughAbstractBaseClassAndMultipleArgs() throws IOException {
String source = """
package com.example;
public class FlowController {
private CommandFactory factory;
private StateMachine machine;
public void handleRequest(String payload) {
// Creates subclass with multiple args, including intermediate enum
BaseCommand cmd = factory.buildCommand(payload);
// Fires using the mapped enum from the inherited base method
machine.fire(cmd.getTransitionEvent());
}
}
class CommandFactory {
public BaseCommand buildCommand(String payload) {
// Passing multiple args: String, Enum1, String, int
return new WebCommand(payload, SourceType.WEB_API, "meta_v1", 42);
}
}
abstract class BaseCommand {
protected String id;
protected SourceType sourceType; // The intermediate enum
protected String metadata;
public BaseCommand(String id, SourceType sourceType, String metadata) {
this.id = id;
this.sourceType = sourceType;
this.metadata = metadata;
}
// The tricky part: Inherited method doing the mapping
public TransitionEnum getTransitionEvent() {
return switch (this.sourceType) {
case WEB_API -> TransitionEnum.ON_WEB_START;
case MOBILE_APP -> TransitionEnum.ON_MOBILE_START;
case CRON_JOB -> TransitionEnum.ON_SYSTEM_START;
};
}
}
class WebCommand extends BaseCommand {
private int priorityLevel;
public WebCommand(String id, SourceType sourceType, String metadata, int priorityLevel) {
super(id, sourceType, metadata); // Engine must track through super()
this.priorityLevel = priorityLevel;
}
}
class StateMachine {
public void fire(TransitionEnum event) {}
}
enum SourceType { WEB_API, MOBILE_APP, CRON_JOB }
enum TransitionEnum { ON_WEB_START, ON_MOBILE_START, ON_SYSTEM_START }
""";
java.nio.file.Path tempDir = java.nio.file.Files.createTempDirectory("callgraph_test_complex_inheritance");
java.nio.file.Files.writeString(tempDir.resolve("OrderConfig.java"), source);
CodebaseContext context = new CodebaseContext();
context.scan(tempDir);
HeuristicCallGraphEngine engine = new HeuristicCallGraphEngine(context);
EntryPoint entryPoint = EntryPoint.builder()
.className("com.example.FlowController")
.methodName("handleRequest")
.build();
TriggerPoint trigger = TriggerPoint.builder()
.className("com.example.StateMachine")
.methodName("fire")
.event("event")
.build();
List<CallChain> chains = engine.findChains(List.of(entryPoint), List.of(trigger));
assertThat(chains).hasSize(1);
System.out.println("COMPLEX INHERITANCE polymorphicEvents: " +
chains.get(0).getTriggerPoint().getPolymorphicEvents());
// The assertion to prove the bug is fixed.
// It MUST NOT contain "SourceType.WEB_API"
assertThat(chains.get(0).getTriggerPoint().getPolymorphicEvents())
.containsExactlyInAnyOrder("TransitionEnum.ON_WEB_START");
}
}