-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add capacity for ReflectionToStringBuilder to include methods annotated with @ToStringInclude #1082
base: master
Are you sure you want to change the base?
Add capacity for ReflectionToStringBuilder to include methods annotated with @ToStringInclude #1082
Conversation
…ed with ToStringInclude
Codecov Report
@@ Coverage Diff @@
## master #1082 +/- ##
============================================
- Coverage 92.08% 92.08% -0.01%
- Complexity 7501 7508 +7
============================================
Files 195 195
Lines 15720 15738 +18
Branches 2897 2900 +3
============================================
+ Hits 14476 14492 +16
- Misses 670 671 +1
- Partials 574 575 +1
... and 1 file with indirect coverage changes 📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
All fileds are included by default, so why do we need to specify which ones to include? |
Yes. But the methods declared in a class are not included in the Surely we can rename the annotation from |
Adding a small example for more clarity. In the below example, lets say the usecase is to print "******" in place of the actual username (to mask the personally identifable information), this will help include the outputs of the methods by calling reflection invoke on the methods that are annotated with package org.apache.commons.lang3.builder;
import org.apache.commons.lang3.StringUtils;
class TestFixture {
@ToStringExclude
private String username;
@ToStringExclude
private String phoneNumber;
@ToStringInclude
private String toStringUsername() {
return StringUtils.repeat("*", StringUtils.length(username));
}
@ToStringInclude
private String toStringPhoneNumber() {
return StringUtils.repeat("*", StringUtils.length(phoneNumber));
}
@Override
public String toString() {
return new ReflectionToStringBuilder(this).build();
}
} |
Note: Don't use code like this example in production, you're revealing the length of secrets. This might not be harmful in itself, but, combined with other leaks, could prove fatal. Perhaps this a good enough reason to avoid providing a potential foot gun like this one. |
Agreed. Just a crude example to understand the use case. Couple of other uses I can think of here
|
Note: Please feel free to close this PR if this was previously discussed before and was decided not to be added to this implementation.
Currently,
ReflectionToStringBuilder
allows users to exclude fields from thetoString
output by adding an annotation@ToStringExclude
to the field but there is no support to include methods declared in the class to be included in thetoString
output.This PR introduces a new annotation
@ToStringInclude(String)
that can be added to the methods of a class with which, the methods in the toString output.Some of the points which can be considered
ReflectionToStringBuilder
) instead of modifying the current implementationallowReflectingMethods
before going into this added implementation.