Thanks for the reply Rob,

To examplify what I mean:

We have a class with 7 attributes: a, b, c, d, e, f.

Example example...
example.setA("test");
example.setB(1);
...
example.setE(false);
...

We need to print only "a", "b" and "e"; According with your suggestion, it would be:

ToStringBuilder toStringBuilder = new ToStringBuilder(example, ToStringStyle.JSON_STYLE);
toStringBuilder.append("a", example.getA());
toStringBuilder.append("b", example.getB());
toStringBuilder.append("e", example.isE());
System.out.println(toStringBuilder.build());

It works, but I see some negative points:
- We instance the ToStringBuilder with the object, but it's meaningless to this situation; - We can put whatever we want as attribute name and value, therefore it's not related to the object we want to print; - It's verbose; The more attributes we want to print, more calls to append we need;


The way I want to propose (I already have some code in my fork - https://github.com/GutoVeronezi/commons-lang/tree/create-methods-on-reflectiontostringbuilder-to-select-fields) would work like a intersection between excludeFields and selectedFields. It would have some methods to set the selected fields (like exclude fields has) and a validation in the method "accept":

protected boolean accept(final Field field) {
    ...
    if (this.excludeFieldNames != null
       && Arrays.binarySearch(this.excludeFieldNames, field.getName()) >= 0) {
        // Reject fields from the getExcludeFieldNames list.
        return false;
    }
    if (this.selectedFieldNames != null) {
        // Only accept fields that were selected on getSelectedFieldNames list.         return Arrays.binarySearch(this.selectedFieldNames, field.getName()) >= 0;
    }
    return !field.isAnnotationPresent(ToStringExclude.class);
}


Then, to use the proposed solution, we would have the following code snippet:


ReflectionToStringBuilder reflectionToStringBuilder = new ReflectionToStringBuilder(example, ToStringStyle.JSON_STYLE);
reflectionToStringBuilder.setSelectedFieldNames("a", "b", "e");
System.out.println(reflectionToStringBuilder.build());



Both ways would generate the result:
{"a":"test","b":1,"e":false}

However, selecting the fields, as I am proposing, is less verbose and more intuitive as it relates directly the fields we want with the object that we are targeting to generate the `toString` to.

Best Regards,
Daniel Salvador

> If you know the fields, why use ReflectionToStringBuilder? Why not use a

> regular ToStringBuilder? That allows you to append each field you want.
> Yes, you have to type each field name twice (for the name and the
> value), but is that so much more work that it would require complicating
> the ReflectiontoStringBuilder API?
>
>
> On 02/07/2021 20:06, Daniel Augusto Veronezi Salvador wrote:
> > Hello,
> >
> > This is my first interaction with this community, so I may commit some
> > mistakes 😁.
> >
> > *ReflectionToStringBuilder* has methods to exclude fields from
> > *toString*; If we have an object with several fields and want to reflect
> > only a some, we have to list all the fields that we don't want to
> > reflect and pass to *excludeFieldNames*. I did this trick locally, but I > > was wondering if it would be valid to implement a way to pass the fields
> > that we want and reflect only the selected fields in
> > *ReflectionToStringBuilder*.
> >
> > As the contributing documentation guide, I submitted an issue on Jira
> > too (https://issues.apache.org/jira/browse/LANG-1662).
> >
> >
> > Best regards,
> >
> > Daniel Salvador

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
For additional commands, e-mail: dev-h...@commons.apache.org

Reply via email to