Merge branch 'LANG-1132'

LANG-1132: ReflectionToStringBuilder doesn't throw IllegalArgumentException when
the constructor's object param is null. Thanks to Jack Tan.
This commit is contained in:
Benedikt Ritter 2015-05-07 20:44:47 +02:00
commit dee94449e6
4 changed files with 25 additions and 5 deletions

View File

@ -22,6 +22,7 @@
<body> <body>
<release version="3.5" date="tba" description="tba"> <release version="3.5" date="tba" description="tba">
<action issue="LANG-1132" type="add" dev="britter" due-to="Jack Tan">ReflectionToStringBuilder doesn't throw IllegalArgumentException when the constructor's object param is null</action>
<action issue="LANG-1122" type="fix" dev="britter" due-to="Adrian Ber">Inconsistent behavior of swap for malformed inputs</action> <action issue="LANG-1122" type="fix" dev="britter" due-to="Adrian Ber">Inconsistent behavior of swap for malformed inputs</action>
<action issue="LANG-701" type="add" dev="britter" due-to="James Sawle">StringUtils join with var args</action> <action issue="LANG-701" type="add" dev="britter" due-to="James Sawle">StringUtils join with var args</action>
<action issue="LANG-1130" type="fix" dev="britter">Fix critical issues reported by SonarQube</action> <action issue="LANG-1130" type="fix" dev="britter">Fix critical issues reported by SonarQube</action>

View File

@ -363,6 +363,13 @@ public static String toStringExclude(final Object object, final String... exclud
return new ReflectionToStringBuilder(object).setExcludeFieldNames(excludeFieldNames).toString(); return new ReflectionToStringBuilder(object).setExcludeFieldNames(excludeFieldNames).toString();
} }
private static Object checkNotNull(final Object obj) {
if (obj == null) {
throw new IllegalArgumentException("The Object passed in should not be null.");
}
return obj;
}
/** /**
* Whether or not to append static fields. * Whether or not to append static fields.
*/ */
@ -400,7 +407,7 @@ public static String toStringExclude(final Object object, final String... exclud
* if the Object passed in is <code>null</code> * if the Object passed in is <code>null</code>
*/ */
public ReflectionToStringBuilder(final Object object) { public ReflectionToStringBuilder(final Object object) {
super(object); super(checkNotNull(object));
} }
/** /**
@ -420,7 +427,7 @@ public ReflectionToStringBuilder(final Object object) {
* if the Object passed in is <code>null</code> * if the Object passed in is <code>null</code>
*/ */
public ReflectionToStringBuilder(final Object object, final ToStringStyle style) { public ReflectionToStringBuilder(final Object object, final ToStringStyle style) {
super(object, style); super(checkNotNull(object), style);
} }
/** /**
@ -446,7 +453,7 @@ public ReflectionToStringBuilder(final Object object, final ToStringStyle style)
* if the Object passed in is <code>null</code> * if the Object passed in is <code>null</code>
*/ */
public ReflectionToStringBuilder(final Object object, final ToStringStyle style, final StringBuffer buffer) { public ReflectionToStringBuilder(final Object object, final ToStringStyle style, final StringBuffer buffer) {
super(object, style, buffer); super(checkNotNull(object), style, buffer);
} }
/** /**
@ -471,7 +478,7 @@ public ReflectionToStringBuilder(final Object object, final ToStringStyle style,
public <T> ReflectionToStringBuilder( public <T> ReflectionToStringBuilder(
final T object, final ToStringStyle style, final StringBuffer buffer, final T object, final ToStringStyle style, final StringBuffer buffer,
final Class<? super T> reflectUpToClass, final boolean outputTransients, final boolean outputStatics) { final Class<? super T> reflectUpToClass, final boolean outputTransients, final boolean outputStatics) {
super(object, style, buffer); super(checkNotNull(object), style, buffer);
this.setUpToClass(reflectUpToClass); this.setUpToClass(reflectUpToClass);
this.setAppendTransients(outputTransients); this.setAppendTransients(outputTransients);
this.setAppendStatics(outputStatics); this.setAppendStatics(outputStatics);

View File

@ -0,0 +1,12 @@
package org.apache.commons.lang3.builder;
import org.junit.Test;
public class ReflectionToStringBuilderTest {
@Test(expected=IllegalArgumentException.class)
public void testConstructorWithNullObject() {
new ReflectionToStringBuilder(null, ToStringStyle.DEFAULT_STYLE, new StringBuffer());
}
}

View File

@ -1041,7 +1041,7 @@ class InheritedReflectionStaticFieldsFixture extends SimpleReflectionStaticField
static final int staticInt2 = 67890; static final int staticInt2 = 67890;
} }
@Test @Test(expected=IllegalArgumentException.class)
public void testReflectionNull() { public void testReflectionNull() {
assertEquals("<null>", ReflectionToStringBuilder.toString(null)); assertEquals("<null>", ReflectionToStringBuilder.toString(null));
} }