jaykataria1111 commented on issue #2769:
URL:
https://github.com/apache/logging-log4j2/issues/2769#issuecomment-2454079028
Hi @ppkarwasz ,
re: to modify the annotation processor in 2.x to fail if a plugin builder
attribute does not have a public setter (or at least a wither).
I was considering the changes needed here, and have already started working
on some, I did have a question though, when it comes to identifying if a Field
has a public setter, we cannot look at the implementation of the class' methods
(not sure if I could find any other way of finding setters looking at the
reflection API), and hence to check if a field has a public setter I have the
following proposal:
1. We go to through methods of the class and then check which one takes 1
parameter
2. That parameter has to be same as the type of the Field that we want to
check has a public setter.
3. The method starts with `set`. We could technically not have this check, I
understand a concern here, because people can have unconventional naming.
4. The method contains the fieldName somewhere. We could technically not
have this check, I understand a concern here, because people can have
unconventional naming.
For point 3 and 4, if someone is not using the naming standard for setters,
then there is really a problem there overall the logic looks something of this
sorts:
```
public boolean hasPublicSetter(Field field, Builder<?> builder) {
Class<?> declaringClass = field.getDeclaringClass();
Class<?> fieldType = field.getType();
Method[] methods = declaringClass.getMethods();
for (Method method : methods) {
if
(method.getName().toLowerCase(Locale.ROOT).contains(field.getName().toLowerCase(Locale.ROOT))
&&
method.getName().toLowerCase(Locale.ROOT).startsWith("set")
&& method.getParameterCount() == 1
&& method.getParameterTypes()[0].equals(fieldType)
&&
java.lang.reflect.Modifier.isPublic(method.getModifiers())) {
return true; // Found a valid public setter according to
convention.
}
}
return false; // Could not find a method with a valid public setter.
}
```
This would be sort of our best effort to check if a field has a public
setter.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]