You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by gg...@apache.org on 2004/06/30 20:21:49 UTC

cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang/builder ToStringStyle.java ToStringBuilder.java

ggregory    2004/06/30 11:21:49

  Modified:    lang/src/java/org/apache/commons/lang/builder
                        ToStringStyle.java ToStringBuilder.java
  Log:
  PR: http://issues.apache.org/bugzilla/show_bug.cgi?id=27876
  [lang] ReflectionToStringBuilder.toString(null) throws exception by design
  ReflectionToStringBuilder.toString is now null-safe and returns the style's nullText.
  ToStringBuilder constructors are now null-safe. A new ToStringBuilder on a null followed by a call to toString returns "".
  
  Revision  Changes    Path
  1.30      +14 -9     jakarta-commons/lang/src/java/org/apache/commons/lang/builder/ToStringStyle.java
  
  Index: ToStringStyle.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/builder/ToStringStyle.java,v
  retrieving revision 1.29
  retrieving revision 1.30
  diff -u -r1.29 -r1.30
  --- ToStringStyle.java	18 Feb 2004 22:53:24 -0000	1.29
  +++ ToStringStyle.java	30 Jun 2004 18:21:49 -0000	1.30
  @@ -234,11 +234,13 @@
        *  <code>toString</code> for, must not be <code>null</code>
        */
       public void appendStart(StringBuffer buffer, Object object) {
  -        appendClassName(buffer, object);
  -        appendIdentityHashCode(buffer, object);
  -        appendContentStart(buffer);
  -        if (fieldSeparatorAtStart) {
  -            appendFieldSeparator(buffer);
  +        if (object != null) {
  +            appendClassName(buffer, object);
  +            appendIdentityHashCode(buffer, object);
  +            appendContentStart(buffer);
  +            if (fieldSeparatorAtStart) {
  +                appendFieldSeparator(buffer);
  +            }
           }
       }
   
  @@ -247,9 +249,12 @@
        * 
        * @param buffer  the <code>StringBuffer</code> to populate
        * @param object  the <code>Object</code> to build a
  -     *  <code>toString</code> for, must not be <code>null</code>
  +     *  <code>toString</code> for.
        */
       public void appendEnd(StringBuffer buffer, Object object) {
  +        if (object == null){
  +            return;
  +        }
           if (fieldSeparatorAtEnd == false) {
               removeLastFieldSeparator(buffer);
           }
  @@ -1282,7 +1287,7 @@
        * @param object  the <code>Object</code> whose name to output
        */
       protected void appendClassName(StringBuffer buffer, Object object) {
  -        if (useClassName) {
  +        if (useClassName && object != null) {
               if (useShortClassName) {
                   buffer.append(getShortClassName(object.getClass()));
               } else {
  @@ -1298,7 +1303,7 @@
        * @param object  the <code>Object</code> whose id to output
        */
       protected void appendIdentityHashCode(StringBuffer buffer, Object object) {
  -        if (useIdentityHashCode) {
  +        if (this.isUseIdentityHashCode() && object!=null) {
               buffer.append('@');
               buffer.append(Integer.toHexString(System.identityHashCode(object)));
           }
  
  
  
  1.33      +4 -10     jakarta-commons/lang/src/java/org/apache/commons/lang/builder/ToStringBuilder.java
  
  Index: ToStringBuilder.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/builder/ToStringBuilder.java,v
  retrieving revision 1.32
  retrieving revision 1.33
  diff -u -r1.32 -r1.33
  --- ToStringBuilder.java	18 Feb 2004 22:53:24 -0000	1.32
  +++ ToStringBuilder.java	30 Jun 2004 18:21:49 -0000	1.33
  @@ -144,7 +144,7 @@
       /**
        * <p>Forwards to <code>ReflectionToStringBuilder</code>.</p>
        * 
  -     * @see ReflectionToStringBuilder#toString(Object,ToStringStyle,boolean,Class)
  +     * @see ReflectionToStringBuilder#toString(Object,ToStringStyle,boolean,boolean,Class)
        * @since 2.0
        */
       public static String reflectionToString(
  @@ -228,14 +228,8 @@
        *  may be <code>null</code>
        * @param buffer  the <code>StringBuffer</code> to populate, may be
        *  <code>null</code>
  -     * @throws IllegalArgumentException  if the Object passed in is
  -     *  <code>null</code>
        */
       public ToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer) {
  -        super();
  -        if (object == null) {
  -            throw new IllegalArgumentException("The object to create a toString for must not be null");
  -        }
           if (style == null) {
               style = getDefaultStyle();
           }
  @@ -1038,8 +1032,8 @@
        * @return the String <code>toString</code>
        */
       public String toString() {
  -        style.appendEnd(buffer, object);
  -        return buffer.toString();
  +        style.appendEnd(this.getStringBuffer(), this.getObject());
  +        return this.getStringBuffer().toString();
       }
   
   }
  
  
  

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


Re: cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang/builder ToStringStyle.java ToStringBuilder.java

Posted by "C. Scott Ananian" <cs...@cscott.net>.
On Wed, 30 Jun 2004 ggregory@apache.org wrote:

>   PR: http://issues.apache.org/bugzilla/show_bug.cgi?id=27876
>   [lang] ReflectionToStringBuilder.toString(null) throws exception by design
>   ReflectionToStringBuilder.toString is now null-safe and returns the style's nullText.
>   ToStringBuilder constructors are now null-safe. A new ToStringBuilder on a null followed by a call to toString returns "".

you didn't update all the javadoc.  for example:

>   Index: ToStringStyle.java
>   ===================================================================
>   RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/builder/ToStringStyle.java,v
>   retrieving revision 1.29
>   retrieving revision 1.30
>   diff -u -r1.29 -r1.30
>   --- ToStringStyle.java	18 Feb 2004 22:53:24 -0000	1.29
>   +++ ToStringStyle.java	30 Jun 2004 18:21:49 -0000	1.30
>   @@ -234,11 +234,13 @@
>         *  <code>toString</code> for, must not be <code>null</code>
                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                       here.

>         */
>        public void appendStart(StringBuffer buffer, Object object) {
>   -        appendClassName(buffer, object);
>   -        appendIdentityHashCode(buffer, object);
>   -        appendContentStart(buffer);
>   -        if (fieldSeparatorAtStart) {

and shouldn't a new ToStringBuilder on null create the nullText, not ""?
 --scott

mustard Milosevic Waihopai early warning Uzi BATF Secretary Justice
agent Indonesia Marxist non-violent protest MI6 munitions payment
                         ( http://cscott.net/ )

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