[prev in list] [next in list] [prev in thread] [next in thread]
List: jakarta-commons-dev
Subject: cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang/builder ToStringStyle.java ToStrin
From: ggregory () apache ! org
Date: 2004-06-30 18:21:49
Message-ID: 20040630182149.69110.qmail () minotaur ! apache ! org
[Download RAW message or body]
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
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic