LANG-1372: Add ToStringSummary annotation (closes #281)
This commit is contained in:
parent
63f11e9dc1
commit
f5ebb9a649
|
@ -84,6 +84,10 @@ import org.apache.commons.lang3.Validate;
|
||||||
* result.
|
* result.
|
||||||
* </p>
|
* </p>
|
||||||
* <p>
|
* <p>
|
||||||
|
* It is also possible to use the {@link ToStringSummary} annotation to output the summary information instead of the
|
||||||
|
* detailed information of a field.
|
||||||
|
* </p>
|
||||||
|
* <p>
|
||||||
* The exact format of the <code>toString</code> is determined by the {@link ToStringStyle} passed into the constructor.
|
* The exact format of the <code>toString</code> is determined by the {@link ToStringStyle} passed into the constructor.
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
|
@ -119,6 +123,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
|
||||||
* if the Object is <code>null</code>
|
* if the Object is <code>null</code>
|
||||||
*
|
*
|
||||||
* @see ToStringExclude
|
* @see ToStringExclude
|
||||||
|
* @see ToStringSummary
|
||||||
*/
|
*/
|
||||||
public static String toString(final Object object) {
|
public static String toString(final Object object) {
|
||||||
return toString(object, null, false, false, null);
|
return toString(object, null, false, false, null);
|
||||||
|
@ -153,6 +158,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
|
||||||
* if the Object or <code>ToStringStyle</code> is <code>null</code>
|
* if the Object or <code>ToStringStyle</code> is <code>null</code>
|
||||||
*
|
*
|
||||||
* @see ToStringExclude
|
* @see ToStringExclude
|
||||||
|
* @see ToStringSummary
|
||||||
*/
|
*/
|
||||||
public static String toString(final Object object, final ToStringStyle style) {
|
public static String toString(final Object object, final ToStringStyle style) {
|
||||||
return toString(object, style, false, false, null);
|
return toString(object, style, false, false, null);
|
||||||
|
@ -193,6 +199,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
|
||||||
* if the Object is <code>null</code>
|
* if the Object is <code>null</code>
|
||||||
*
|
*
|
||||||
* @see ToStringExclude
|
* @see ToStringExclude
|
||||||
|
* @see ToStringSummary
|
||||||
*/
|
*/
|
||||||
public static String toString(final Object object, final ToStringStyle style, final boolean outputTransients) {
|
public static String toString(final Object object, final ToStringStyle style, final boolean outputTransients) {
|
||||||
return toString(object, style, outputTransients, false, null);
|
return toString(object, style, outputTransients, false, null);
|
||||||
|
@ -240,6 +247,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
|
||||||
* if the Object is <code>null</code>
|
* if the Object is <code>null</code>
|
||||||
*
|
*
|
||||||
* @see ToStringExclude
|
* @see ToStringExclude
|
||||||
|
* @see ToStringSummary
|
||||||
* @since 2.1
|
* @since 2.1
|
||||||
*/
|
*/
|
||||||
public static String toString(final Object object, final ToStringStyle style, final boolean outputTransients, final boolean outputStatics) {
|
public static String toString(final Object object, final ToStringStyle style, final boolean outputTransients, final boolean outputStatics) {
|
||||||
|
@ -293,6 +301,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
|
||||||
* if the Object is <code>null</code>
|
* if the Object is <code>null</code>
|
||||||
*
|
*
|
||||||
* @see ToStringExclude
|
* @see ToStringExclude
|
||||||
|
* @see ToStringSummary
|
||||||
* @since 2.1
|
* @since 2.1
|
||||||
*/
|
*/
|
||||||
public static <T> String toString(
|
public static <T> String toString(
|
||||||
|
@ -351,6 +360,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
|
||||||
* if the Object is <code>null</code>
|
* if the Object is <code>null</code>
|
||||||
*
|
*
|
||||||
* @see ToStringExclude
|
* @see ToStringExclude
|
||||||
|
* @see ToStringSummary
|
||||||
* @since 3.6
|
* @since 3.6
|
||||||
*/
|
*/
|
||||||
public static <T> String toString(
|
public static <T> String toString(
|
||||||
|
@ -639,7 +649,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
|
||||||
// for primitive types.
|
// for primitive types.
|
||||||
final Object fieldValue = this.getValue(field);
|
final Object fieldValue = this.getValue(field);
|
||||||
if (!excludeNullValues || fieldValue != null) {
|
if (!excludeNullValues || fieldValue != null) {
|
||||||
this.append(fieldName, fieldValue);
|
this.append(fieldName, fieldValue, !field.isAnnotationPresent(ToStringSummary.class));
|
||||||
}
|
}
|
||||||
} catch (final IllegalAccessException ex) {
|
} catch (final IllegalAccessException ex) {
|
||||||
//this can't happen. Would get a Security exception
|
//this can't happen. Would get a Security exception
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
/*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership.
|
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
|
* (the "License"); you may not use this file except in compliance with
|
||||||
|
* the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.apache.commons.lang3.builder;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use this annotation on the fields to get the summary instead of the detailed
|
||||||
|
* information when using {@link ReflectionToStringBuilder}.
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* Notice that not all {@link ToStringStyle} implementations support the
|
||||||
|
* appendSummary method.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @since 3.8
|
||||||
|
*/
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target(ElementType.FIELD)
|
||||||
|
public @interface ToStringSummary {
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
/*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership.
|
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
|
* (the "License"); you may not use this file except in compliance with
|
||||||
|
* the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.apache.commons.lang3.builder;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class ReflectionToStringBuilderSummaryTest {
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
private String stringField = "string";
|
||||||
|
|
||||||
|
@ToStringSummary
|
||||||
|
private String summaryString = "summary";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSummary() {
|
||||||
|
Assert.assertEquals("[stringField=string,summaryString=<String>]",
|
||||||
|
new ReflectionToStringBuilder(this, ToStringStyle.NO_CLASS_NAME_STYLE).build());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue