commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "David Green (JIRA)" <j...@apache.org>
Subject [jira] [Updated] (LANG-985) ToStringBuilder reliably handle OpenPojo @BusinessKey annotated instances
Date Mon, 03 Mar 2014 14:19:24 GMT

     [ https://issues.apache.org/jira/browse/LANG-985?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]

David Green updated LANG-985:
-----------------------------

    Description: 
We use Google's OpenPojo library, annotating DTOs and persistence entities with @BusinessKey
in order to remove boilerplate hashcode() and equals().

I recently started noticing toString() was throwing exceptions when I didn't populate @BusinessKey
annotated field(s) as I was using the Null Object Pattern.  A workaround was to populate dummy
values but there's a risk this could be considered a real value further down the line, so
looked for an alternative solution and came up with the following extending StandardToStringStyle:-

{code}
import org.apache.commons.lang3.builder.StandardToStringStyle;
import org.apache.commons.lang3.builder.ToStringStyle;

public class CustomToStringStyle extends StandardToStringStyle {

    public static final ToStringStyle OPENPOJO_SAFE_STYLE = createOpenPojoSafeStyle();

    /**
     * Works better with {@link com.openpojo.business.annotation.BusinessKey} annotated OpenPojo
classes.
     * 
     * This instance does not call {@link ToStringStyle}.register(...)
     * which can throw a {@link com.openpojo.business.exception.BusinessException} 
	 * if the key hasn't been populated, for example if you used the Null Object Pattern.
     */    
    private static ToStringStyle createOpenPojoSafeStyle() {
        final StandardToStringStyle style = new StandardToStringStyle();
        style.setUseClassName(false);
        style.setUseIdentityHashCode(false);
        return style;
    }
}
{code}
Used as follows:
{code}
    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, CustomToStringStyle.OPENPOJO_SAFE_STYLE);
    }
{code}

I realise this doesn't handle recursion in fields because it skips over ToStringStyle.register(..)
but this suffices for our use case right now - perhaps an alternative implementation overriding
register() that uses a List (with documented performance penalty) could be added.

Is there anything already in Commons that could do this? If not we would like this to be considered
for inclusion in commons lang (happy to help out with development), potentially with implementation
improvements by those who know the code base better.


  was:
We use Google's OpenPojo library, annotating DTOs and persistence entities with @BusinessKey
in order to remove boilerplate hashcode() and equals().

I recently started noticing toString() was throwing exceptions when I didn't populate @BusinessKey
annotated field(s) as I was using the Null Object Pattern.  A workaround was to populate dummy
values but there's a risk this could be considered a real value further down the line, so
looked for an alternative solution and came up with the following extending StandardToStringStyle:-

{code}
import org.apache.commons.lang3.builder.StandardToStringStyle;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.openpojo.business.annotation.BusinessKey;
import com.openpojo.business.exception.BusinessException;

public class CustomToStringStyle extends StandardToStringStyle {

    public static final ToStringStyle OPENPOJO_SAFE_STYLE = createOpenPojoSafeStyle();

    /**
     * Works better with {@link BusinessKey} annotated OpenPojo classes.
     * 
     * This instance does not call {@link ToStringStyle}.register(...)
     * which can throw a {@link BusinessException} if the key hasn't been
     * populated, for example if you used the Null Object Pattern.
     */    
    private static ToStringStyle createOpenPojoSafeStyle() {
        final StandardToStringStyle style = new StandardToStringStyle();
        style.setUseClassName(false);
        style.setUseIdentityHashCode(false);
        return style;
    }
}
{code}
Used as follows:
{code}
    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, CustomToStringStyle.OPENPOJO_SAFE_STYLE);
    }
{code}

I realise this doesn't handle recursion in fields because it skips over ToStringStyle.register(..)
but this suffices for our use case right now - perhaps an alternative implementation overriding
register() that uses a List (with documented performance penalty) could be added.

Is there anything already in Commons that could do this? If not we would like this to be considered
for inclusion in commons lang (happy to help out with development), potentially with implementation
improvements by those who know the code base better.



> ToStringBuilder reliably handle OpenPojo @BusinessKey annotated instances
> -------------------------------------------------------------------------
>
>                 Key: LANG-985
>                 URL: https://issues.apache.org/jira/browse/LANG-985
>             Project: Commons Lang
>          Issue Type: Wish
>          Components: lang.builder.*
>            Reporter: David Green
>            Priority: Minor
>              Labels: features
>
> We use Google's OpenPojo library, annotating DTOs and persistence entities with @BusinessKey
in order to remove boilerplate hashcode() and equals().
> I recently started noticing toString() was throwing exceptions when I didn't populate
@BusinessKey annotated field(s) as I was using the Null Object Pattern.  A workaround was
to populate dummy values but there's a risk this could be considered a real value further
down the line, so looked for an alternative solution and came up with the following extending
StandardToStringStyle:-
> {code}
> import org.apache.commons.lang3.builder.StandardToStringStyle;
> import org.apache.commons.lang3.builder.ToStringStyle;
> public class CustomToStringStyle extends StandardToStringStyle {
>     public static final ToStringStyle OPENPOJO_SAFE_STYLE = createOpenPojoSafeStyle();
>     /**
>      * Works better with {@link com.openpojo.business.annotation.BusinessKey} annotated
OpenPojo classes.
>      * 
>      * This instance does not call {@link ToStringStyle}.register(...)
>      * which can throw a {@link com.openpojo.business.exception.BusinessException} 
> 	 * if the key hasn't been populated, for example if you used the Null Object Pattern.
>      */    
>     private static ToStringStyle createOpenPojoSafeStyle() {
>         final StandardToStringStyle style = new StandardToStringStyle();
>         style.setUseClassName(false);
>         style.setUseIdentityHashCode(false);
>         return style;
>     }
> }
> {code}
> Used as follows:
> {code}
>     @Override
>     public String toString() {
>         return ToStringBuilder.reflectionToString(this, CustomToStringStyle.OPENPOJO_SAFE_STYLE);
>     }
> {code}
> I realise this doesn't handle recursion in fields because it skips over ToStringStyle.register(..)
but this suffices for our use case right now - perhaps an alternative implementation overriding
register() that uses a List (with documented performance penalty) could be added.
> Is there anything already in Commons that could do this? If not we would like this to
be considered for inclusion in commons lang (happy to help out with development), potentially
with implementation improvements by those who know the code base better.



--
This message was sent by Atlassian JIRA
(v6.2#6252)

Mime
View raw message