Skip to content

Commit d96a28a

Browse files
committed
Use accessors in ToStringStyle so subclasses can effectively override
them
1 parent 30c4652 commit d96a28a

File tree

2 files changed

+51
-50
lines changed

2 files changed

+51
-50
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ The <action> type attribute can be add,update,fix,remove.
4949
<action type="fix" dev="ggregory" due-to="mayuming, Gary Gregory">Optimize ObjectToStringComparator.compare() method #1449.</action>
5050
<action type="fix" dev="ggregory" due-to="Marcono1234, Gary Gregory">[javadoc] Improve StringUtils Javadoc #1450.</action>
5151
<action type="fix" dev="ggregory" due-to="mayuming, Gary Gregory">Fix internal inverted logic in private isEnum() method and correct its usage in getFirstEnum() #1454.</action>
52+
<action type="fix" dev="ggregory" due-to="Damien Carbonne, Gary Gregory, Rob Spoor">Use accessors in ToStringStyle so subclasses can effectively override them.</action>
5253
<!-- ADD -->
5354
<!-- UPDATE -->
5455
</release>

src/main/java/org/apache/commons/lang3/builder/ToStringStyle.java

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,9 +1152,9 @@ public void append(final StringBuffer buffer, final String fieldName, final shor
11521152
* @param object the {@link Object} whose name to output
11531153
*/
11541154
protected void appendClassName(final StringBuffer buffer, final Object object) {
1155-
if (useClassName && object != null) {
1155+
if (isUseClassName() && object != null) {
11561156
register(object);
1157-
if (useShortClassName) {
1157+
if (isUseShortClassName()) {
11581158
buffer.append(getShortClassName(object.getClass()));
11591159
} else {
11601160
buffer.append(object.getClass().getName());
@@ -1168,7 +1168,7 @@ protected void appendClassName(final StringBuffer buffer, final Object object) {
11681168
* @param buffer the {@link StringBuffer} to populate
11691169
*/
11701170
protected void appendContentEnd(final StringBuffer buffer) {
1171-
buffer.append(contentEnd);
1171+
buffer.append(getContentEnd());
11721172
}
11731173

11741174
/**
@@ -1177,7 +1177,7 @@ protected void appendContentEnd(final StringBuffer buffer) {
11771177
* @param buffer the {@link StringBuffer} to populate
11781178
*/
11791179
protected void appendContentStart(final StringBuffer buffer) {
1180-
buffer.append(contentStart);
1180+
buffer.append(getContentStart());
11811181
}
11821182

11831183
/**
@@ -1218,14 +1218,14 @@ protected void appendDetail(final StringBuffer buffer, final String fieldName, f
12181218
* not {@code null}
12191219
*/
12201220
protected void appendDetail(final StringBuffer buffer, final String fieldName, final boolean[] array) {
1221-
buffer.append(arrayStart);
1221+
buffer.append(getArrayStart());
12221222
for (int i = 0; i < array.length; i++) {
12231223
if (i > 0) {
1224-
buffer.append(arraySeparator);
1224+
buffer.append(getArraySeparator());
12251225
}
12261226
appendDetail(buffer, fieldName, array[i]);
12271227
}
1228-
buffer.append(arrayEnd);
1228+
buffer.append(getArrayEnd());
12291229
}
12301230

12311231
/**
@@ -1250,14 +1250,14 @@ protected void appendDetail(final StringBuffer buffer, final String fieldName, f
12501250
* not {@code null}
12511251
*/
12521252
protected void appendDetail(final StringBuffer buffer, final String fieldName, final byte[] array) {
1253-
buffer.append(arrayStart);
1253+
buffer.append(getArrayStart());
12541254
for (int i = 0; i < array.length; i++) {
12551255
if (i > 0) {
1256-
buffer.append(arraySeparator);
1256+
buffer.append(getArraySeparator());
12571257
}
12581258
appendDetail(buffer, fieldName, array[i]);
12591259
}
1260-
buffer.append(arrayEnd);
1260+
buffer.append(getArrayEnd());
12611261
}
12621262

12631263
/**
@@ -1282,14 +1282,14 @@ protected void appendDetail(final StringBuffer buffer, final String fieldName, f
12821282
* not {@code null}
12831283
*/
12841284
protected void appendDetail(final StringBuffer buffer, final String fieldName, final char[] array) {
1285-
buffer.append(arrayStart);
1285+
buffer.append(getArrayStart());
12861286
for (int i = 0; i < array.length; i++) {
12871287
if (i > 0) {
1288-
buffer.append(arraySeparator);
1288+
buffer.append(getArraySeparator());
12891289
}
12901290
appendDetail(buffer, fieldName, array[i]);
12911291
}
1292-
buffer.append(arrayEnd);
1292+
buffer.append(getArrayEnd());
12931293
}
12941294

12951295
/**
@@ -1326,14 +1326,14 @@ protected void appendDetail(final StringBuffer buffer, final String fieldName, f
13261326
* not {@code null}
13271327
*/
13281328
protected void appendDetail(final StringBuffer buffer, final String fieldName, final double[] array) {
1329-
buffer.append(arrayStart);
1329+
buffer.append(getArrayStart());
13301330
for (int i = 0; i < array.length; i++) {
13311331
if (i > 0) {
1332-
buffer.append(arraySeparator);
1332+
buffer.append(getArraySeparator());
13331333
}
13341334
appendDetail(buffer, fieldName, array[i]);
13351335
}
1336-
buffer.append(arrayEnd);
1336+
buffer.append(getArrayEnd());
13371337
}
13381338

13391339
/**
@@ -1358,14 +1358,14 @@ protected void appendDetail(final StringBuffer buffer, final String fieldName, f
13581358
* not {@code null}
13591359
*/
13601360
protected void appendDetail(final StringBuffer buffer, final String fieldName, final float[] array) {
1361-
buffer.append(arrayStart);
1361+
buffer.append(getArrayStart());
13621362
for (int i = 0; i < array.length; i++) {
13631363
if (i > 0) {
1364-
buffer.append(arraySeparator);
1364+
buffer.append(getArraySeparator());
13651365
}
13661366
appendDetail(buffer, fieldName, array[i]);
13671367
}
1368-
buffer.append(arrayEnd);
1368+
buffer.append(getArrayEnd());
13691369
}
13701370

13711371
/**
@@ -1392,12 +1392,12 @@ protected void appendDetail(final StringBuffer buffer, final String fieldName, f
13921392
*/
13931393
protected void appendDetail(final StringBuffer buffer, final String fieldName, final int i, final Object item) {
13941394
if (i > 0) {
1395-
buffer.append(arraySeparator);
1395+
buffer.append(getArraySeparator());
13961396
}
13971397
if (item == null) {
13981398
appendNullText(buffer, fieldName);
13991399
} else {
1400-
appendInternal(buffer, fieldName, item, arrayContentDetail);
1400+
appendInternal(buffer, fieldName, item, isArrayContentDetail());
14011401
}
14021402
}
14031403

@@ -1411,14 +1411,14 @@ protected void appendDetail(final StringBuffer buffer, final String fieldName, f
14111411
* not {@code null}
14121412
*/
14131413
protected void appendDetail(final StringBuffer buffer, final String fieldName, final int[] array) {
1414-
buffer.append(arrayStart);
1414+
buffer.append(getArrayStart());
14151415
for (int i = 0; i < array.length; i++) {
14161416
if (i > 0) {
1417-
buffer.append(arraySeparator);
1417+
buffer.append(getArraySeparator());
14181418
}
14191419
appendDetail(buffer, fieldName, array[i]);
14201420
}
1421-
buffer.append(arrayEnd);
1421+
buffer.append(getArrayEnd());
14221422
}
14231423

14241424
/**
@@ -1443,14 +1443,14 @@ protected void appendDetail(final StringBuffer buffer, final String fieldName, f
14431443
* not {@code null}
14441444
*/
14451445
protected void appendDetail(final StringBuffer buffer, final String fieldName, final long[] array) {
1446-
buffer.append(arrayStart);
1446+
buffer.append(getArrayStart());
14471447
for (int i = 0; i < array.length; i++) {
14481448
if (i > 0) {
1449-
buffer.append(arraySeparator);
1449+
buffer.append(getArraySeparator());
14501450
}
14511451
appendDetail(buffer, fieldName, array[i]);
14521452
}
1453-
buffer.append(arrayEnd);
1453+
buffer.append(getArrayEnd());
14541454
}
14551455

14561456
/**
@@ -1488,11 +1488,11 @@ protected void appendDetail(final StringBuffer buffer, final String fieldName, f
14881488
* not {@code null}
14891489
*/
14901490
protected void appendDetail(final StringBuffer buffer, final String fieldName, final Object[] array) {
1491-
buffer.append(arrayStart);
1491+
buffer.append(getArrayStart());
14921492
for (int i = 0; i < array.length; i++) {
14931493
appendDetail(buffer, fieldName, i, array[i]);
14941494
}
1495-
buffer.append(arrayEnd);
1495+
buffer.append(getArrayEnd());
14961496
}
14971497

14981498
/**
@@ -1517,14 +1517,14 @@ protected void appendDetail(final StringBuffer buffer, final String fieldName, f
15171517
* not {@code null}
15181518
*/
15191519
protected void appendDetail(final StringBuffer buffer, final String fieldName, final short[] array) {
1520-
buffer.append(arrayStart);
1520+
buffer.append(getArrayStart());
15211521
for (int i = 0; i < array.length; i++) {
15221522
if (i > 0) {
1523-
buffer.append(arraySeparator);
1523+
buffer.append(getArraySeparator());
15241524
}
15251525
appendDetail(buffer, fieldName, array[i]);
15261526
}
1527-
buffer.append(arrayEnd);
1527+
buffer.append(getArrayEnd());
15281528
}
15291529

15301530
/**
@@ -1535,7 +1535,7 @@ protected void appendDetail(final StringBuffer buffer, final String fieldName, f
15351535
* {@code toString} for.
15361536
*/
15371537
public void appendEnd(final StringBuffer buffer, final Object object) {
1538-
if (!this.fieldSeparatorAtEnd) {
1538+
if (!isFieldSeparatorAtEnd()) {
15391539
removeLastFieldSeparator(buffer);
15401540
}
15411541
appendContentEnd(buffer);
@@ -1558,7 +1558,7 @@ protected void appendFieldEnd(final StringBuffer buffer, final String fieldName)
15581558
* @param buffer the {@link StringBuffer} to populate
15591559
*/
15601560
protected void appendFieldSeparator(final StringBuffer buffer) {
1561-
buffer.append(fieldSeparator);
1561+
buffer.append(getFieldSeparator());
15621562
}
15631563

15641564
/**
@@ -1568,9 +1568,9 @@ protected void appendFieldSeparator(final StringBuffer buffer) {
15681568
* @param fieldName the field name
15691569
*/
15701570
protected void appendFieldStart(final StringBuffer buffer, final String fieldName) {
1571-
if (useFieldNames && fieldName != null) {
1571+
if (isUseFieldNames() && fieldName != null) {
15721572
buffer.append(fieldName);
1573-
buffer.append(fieldNameValueSeparator);
1573+
buffer.append(getFieldNameValueSeparator());
15741574
}
15751575
}
15761576

@@ -1713,7 +1713,7 @@ protected void appendInternal(final StringBuffer buffer, final String fieldName,
17131713
* @param fieldName the field name, typically not used as already appended
17141714
*/
17151715
protected void appendNullText(final StringBuffer buffer, final String fieldName) {
1716-
buffer.append(nullText);
1716+
buffer.append(getNullText());
17171717
}
17181718

17191719
/**
@@ -1727,7 +1727,7 @@ public void appendStart(final StringBuffer buffer, final Object object) {
17271727
appendClassName(buffer, object);
17281728
appendIdentityHashCode(buffer, object);
17291729
appendContentStart(buffer);
1730-
if (fieldSeparatorAtStart) {
1730+
if (isFieldSeparatorAtStart()) {
17311731
appendFieldSeparator(buffer);
17321732
}
17331733
}
@@ -1834,9 +1834,9 @@ protected void appendSummary(final StringBuffer buffer, final String fieldName,
18341834
* not {@code null}
18351835
*/
18361836
protected void appendSummary(final StringBuffer buffer, final String fieldName, final Object value) {
1837-
buffer.append(summaryObjectStartText);
1837+
buffer.append(getSummaryObjectStartText());
18381838
buffer.append(getShortClassName(value.getClass()));
1839-
buffer.append(summaryObjectEndText);
1839+
buffer.append(getSummaryObjectEndText());
18401840
}
18411841

18421842
/**
@@ -1881,9 +1881,9 @@ protected void appendSummary(final StringBuffer buffer, final String fieldName,
18811881
* @param size the size to append
18821882
*/
18831883
protected void appendSummarySize(final StringBuffer buffer, final String fieldName, final int size) {
1884-
buffer.append(sizeStartText);
1884+
buffer.append(getSizeStartText());
18851885
buffer.append(size);
1886-
buffer.append(sizeEndText);
1886+
buffer.append(getSizeEndText());
18871887
}
18881888

18891889
/**
@@ -1912,10 +1912,10 @@ public void appendSuper(final StringBuffer buffer, final String superToString) {
19121912
*/
19131913
public void appendToString(final StringBuffer buffer, final String toString) {
19141914
if (toString != null) {
1915-
final int pos1 = toString.indexOf(contentStart) + contentStart.length();
1916-
final int pos2 = toString.lastIndexOf(contentEnd);
1915+
final int pos1 = toString.indexOf(getContentStart()) + getContentStart().length();
1916+
final int pos2 = toString.lastIndexOf(getContentEnd());
19171917
if (pos1 != pos2 && pos1 >= 0 && pos2 >= 0) {
1918-
if (fieldSeparatorAtStart) {
1918+
if (isFieldSeparatorAtStart()) {
19191919
removeLastFieldSeparator(buffer);
19201920
}
19211921
buffer.append(toString, pos1, pos2);
@@ -2114,7 +2114,7 @@ protected boolean isFieldSeparatorAtStart() {
21142114
*/
21152115
protected boolean isFullDetail(final Boolean fullDetailRequest) {
21162116
if (fullDetailRequest == null) {
2117-
return defaultFullDetail;
2117+
return isDefaultFullDetail();
21182118
}
21192119
return fullDetailRequest.booleanValue();
21202120
}
@@ -2169,12 +2169,12 @@ protected boolean isUseShortClassName() {
21692169
* @since 2.0
21702170
*/
21712171
protected void reflectionAppendArrayDetail(final StringBuffer buffer, final String fieldName, final Object array) {
2172-
buffer.append(arrayStart);
2172+
buffer.append(getArrayStart());
21732173
final int length = Array.getLength(array);
21742174
for (int i = 0; i < length; i++) {
21752175
appendDetail(buffer, fieldName, i, Array.get(array, i));
21762176
}
2177-
buffer.append(arrayEnd);
2177+
buffer.append(getArrayEnd());
21782178
}
21792179

21802180
/**
@@ -2184,8 +2184,8 @@ protected void reflectionAppendArrayDetail(final StringBuffer buffer, final Stri
21842184
* @since 2.0
21852185
*/
21862186
protected void removeLastFieldSeparator(final StringBuffer buffer) {
2187-
if (Strings.CS.endsWith(buffer, fieldSeparator)) {
2188-
buffer.setLength(buffer.length() - fieldSeparator.length());
2187+
if (Strings.CS.endsWith(buffer, getFieldSeparator())) {
2188+
buffer.setLength(buffer.length() - getFieldSeparator().length());
21892189
}
21902190
}
21912191

0 commit comments

Comments
 (0)