Avoid borrowing @Qualifier from unrelated injection parameters

Require parameter-name or setter-name agreement when inferring a field qualifier from constructor/autowired parameter annotations, preventing false-positive routing for multi-bean StateMachine<String,String> injections.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-07-13 21:41:16 +02:00
parent 69361e5a60
commit 44497afd32
2 changed files with 80 additions and 3 deletions

View File

@@ -66,6 +66,58 @@ class SpringInjectionRoutingTest {
chain, "com.example.OrderStateMachineConfig", context)).isFalse();
}
@Test
void shouldNotBorrowConstructorQualifierFromUnrelatedParameter(@TempDir Path tempDir) throws Exception {
Files.writeString(tempDir.resolve("PaymentService.java"), """
package com.example;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.statemachine.StateMachine;
import org.springframework.stereotype.Service;
@Service
public class PaymentService {
private final StateMachine<String, String> orderMachine;
private final StateMachine<String, String> paymentMachine;
public PaymentService(StateMachine<String, String> orderMachine,
@Qualifier("paymentStateMachine") StateMachine<String, String> paymentMachine) {
this.orderMachine = orderMachine;
this.paymentMachine = paymentMachine;
}
public void captureOnOrder() {
orderMachine.sendEvent("CAPTURE");
}
}
@org.springframework.context.annotation.Configuration
@org.springframework.statemachine.config.EnableStateMachine(name = "paymentStateMachine")
class PaymentStateMachineConfig
extends org.springframework.statemachine.config.StateMachineConfigurerAdapter<String, String> {}
@org.springframework.context.annotation.Configuration
@org.springframework.statemachine.config.EnableStateMachine(name = "orderStateMachine")
class OrderStateMachineConfig
extends org.springframework.statemachine.config.StateMachineConfigurerAdapter<String, String> {}
""");
CodebaseContext context = new CodebaseContext();
context.setResolveBindings(true);
context.scan(tempDir);
HeuristicBeanResolutionEngine engine = new HeuristicBeanResolutionEngine();
CallChain chain = CallChain.builder()
.triggerPoint(TriggerPoint.builder()
.className("com.example.PaymentService")
.methodName("captureOnOrder")
.lineNumber(18)
.event("CAPTURE")
.eventTypeFqn("java.lang.String")
.stateTypeFqn("java.lang.String")
.build())
.methodChain(List.of("com.example.PaymentService.captureOnOrder()"))
.build();
// Both are String,String machines and receiver has no explicit qualifier -> should fail closed.
assertThat(engine.isRoutedToCorrectMachine(chain, "com.example.PaymentStateMachineConfig", context)).isFalse();
assertThat(engine.isRoutedToCorrectMachine(chain, "com.example.OrderStateMachineConfig", context)).isFalse();
}
@Test
void shouldFailClosedForAmbiguousStringStateMachineWithoutQualifier(@TempDir Path tempDir) throws Exception {
Files.writeString(tempDir.resolve("OrderService.java"), """