You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Gary Gregory <gg...@seagullsoftware.com> on 2005/08/04 20:33:03 UTC
[lang] Using ReflectionToStringBuilder and excluding secure fields
Hello:
Right now, I use the code below to exclude password fields from output.
/**
* Builds a String for a toString method excluding the given field
name.
*
* @param obj
* The object to "toString".
* @param excludeFieldName
* The field name to exclude
* @return The toString String.
*/
public static String toStringExcluding(Object obj, final String
excludeFieldName) {
return (new ReflectionToStringBuilder(obj) {
protected boolean accept(Field f) {
return super.accept(f) &&
!f.getName().equals(excludeFieldName);
}
}).toString();
}
I could imagine writing my call sites like this instead:
new ReflectionToStringBuilder(obj).setExcludeFields(new
String[]{"password"}).toString();
Which means adding the set/getExcludeFields() feature.
Any thoughts?
Gary
Re: [lang] Using ReflectionToStringBuilder and excluding secure fields
Posted by Steven Caswell <st...@gmail.com>.
+1
I've thought about something similar, just never did anything about
it. I like the idea of being able to exclude some fields from the
string build.
On 8/4/05, Gary Gregory <gg...@seagullsoftware.com> wrote:
> Hello:
>
> Right now, I use the code below to exclude password fields from output.
>
> /**
> * Builds a String for a toString method excluding the given field
> name.
> *
> * @param obj
> * The object to "toString".
> * @param excludeFieldName
> * The field name to exclude
> * @return The toString String.
> */
> public static String toStringExcluding(Object obj, final String
> excludeFieldName) {
> return (new ReflectionToStringBuilder(obj) {
> protected boolean accept(Field f) {
> return super.accept(f) &&
> !f.getName().equals(excludeFieldName);
> }
> }).toString();
> }
>
> I could imagine writing my call sites like this instead:
>
> new ReflectionToStringBuilder(obj).setExcludeFields(new
> String[]{"password"}).toString();
>
> Which means adding the set/getExcludeFields() feature.
>
> Any thoughts?
>
> Gary
>
>
--
Steven Caswell
steven.caswell@gmail.com
(c) 404-693-4148
(o) 404-260-2382
Take back the web - http://www.mozilla.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org