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:
@@ -62,9 +62,9 @@ public class InjectionPointAnalyzer {
|
||||
try {
|
||||
IAnnotationBinding[] paramAnns = method.getParameterAnnotations(i);
|
||||
String paramQual = getQualifierValue(paramAnns);
|
||||
if (paramQual != null && paramType.getErasure().isEqualTo(binding.getType().getErasure())) {
|
||||
// For setters, verify it roughly matches the field name or just rely on type.
|
||||
// We will rely on type equality for now as a heuristic.
|
||||
if (paramQual != null
|
||||
&& paramType.getErasure().isEqualTo(binding.getType().getErasure())
|
||||
&& parameterMatchesField(method, i, binding.getName())) {
|
||||
return paramQual;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@@ -90,4 +90,29 @@ public class InjectionPointAnalyzer {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static boolean parameterMatchesField(
|
||||
org.eclipse.jdt.core.dom.IMethodBinding method,
|
||||
int parameterIndex,
|
||||
String fieldName) {
|
||||
if (method == null || fieldName == null || fieldName.isBlank() || parameterIndex < 0) {
|
||||
return false;
|
||||
}
|
||||
String[] names;
|
||||
try {
|
||||
names = method.getParameterNames();
|
||||
} catch (Exception ignored) {
|
||||
names = null;
|
||||
}
|
||||
if (names != null && parameterIndex < names.length) {
|
||||
return fieldName.equals(names[parameterIndex]);
|
||||
}
|
||||
// If parameter names aren't available, only accept clear setter-like methods.
|
||||
String methodName = method.getName();
|
||||
if (methodName != null && methodName.startsWith("set") && method.getParameterTypes().length == 1) {
|
||||
String expected = "set" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
|
||||
return expected.equals(methodName);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user