SONARJAVA-6791 Handle @Qualifier annotations on autowired dependencies in BeanDefinitionGatherer - #5936
Conversation
cf0c655 to
ef6cdf8
Compare
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
| /** Names of other beans this bean depends on. */ | ||
| private List<String> dependingBeans; | ||
| /** Dependencies this bean requires, each capturing the required type and an optional {@code @Qualifier} name. */ | ||
| private List<BeanDependency> dependingBeans; |
There was a problem hiding this comment.
This list can be rather large, and we need to have fast look-up to be able to define needed dependency. I'd suggest to think about using Map / Set, the key should be the bean's name. In Spring framework this should be qualifier or type.
Code Review ✅ Approved 5 resolved / 5 findingsAdds ✅ 5 resolved✅ Quality: Redundant same-package import breaks import ordering
✅ Quality: Duplicated BeanData construction into two parallel lists
✅ Bug: Qualifier values written raw to cache break serialization
✅ Quality: Doc comment says decapitalized class name, code uses field name
✅ Edge Case: Map keyed by field/param name silently drops colliding dependencies
OptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Gitar |
|
|
|
||
| @ParameterizedTest(name = "{0}") | ||
| @MethodSource("qualifiedDependencyArguments") | ||
| void qualifier_is_captured_on_qualified_dependency(String filePath, String expectedBeanName) { |
There was a problem hiding this comment.
To illustrate properly how it will work we should create two beans of the same type (and different names) and specify one of them via Qualifier. An example:
public interface PaymentProcessor {
void process();
}
@Component("creditCard") // Bean Name: "creditCard"
public class CreditCardProcessor implements PaymentProcessor {
public void process() { System.out.println("Processing Credit Card..."); }
}
@Component("paypal") // Bean Name: "paypal"
public class PayPalProcessor implements PaymentProcessor {
public void process() { System.out.println("Processing PayPal..."); }
}
@Service
public class OrderService {
private final PaymentProcessor paymentProcessor;
// @Qualifier explicitly picks the "paypal" bean instead of "creditCard"
@Autowired
public OrderService(@Qualifier("paypal") PaymentProcessor paymentProcessor) {
this.paymentProcessor = paymentProcessor;
}
public void checkout() {
paymentProcessor.process(); // Outputs: Processing PayPal...
}
}



Summary by Gitar
BeanDependencyrecord to capture dependency type and optional@Qualifiervalue.BeanDefinitionGathererto extract@Qualifierannotations on fields, constructors, and@Beanmethods.This will update automatically on new commits.