From 279fb9bcf125745760af716ed12b9420060878a5 Mon Sep 17 00:00:00 2001 From: Sergio Ozaki Date: Sun, 30 Jul 2017 15:37:37 -0300 Subject: [PATCH 1/3] Add ToStringSummary annotation --- .../builder/ReflectionToStringBuilder.java | 12 +++++- .../lang3/builder/ToStringSummary.java | 40 +++++++++++++++++++ .../ReflectionToStringBuilderSummaryTest.java | 35 ++++++++++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/apache/commons/lang3/builder/ToStringSummary.java create mode 100644 src/test/java/org/apache/commons/lang3/builder/ReflectionToStringBuilderSummaryTest.java diff --git a/src/main/java/org/apache/commons/lang3/builder/ReflectionToStringBuilder.java b/src/main/java/org/apache/commons/lang3/builder/ReflectionToStringBuilder.java index c59fcecff72..5cb47913348 100644 --- a/src/main/java/org/apache/commons/lang3/builder/ReflectionToStringBuilder.java +++ b/src/main/java/org/apache/commons/lang3/builder/ReflectionToStringBuilder.java @@ -84,6 +84,10 @@ * result. *

*

+ * It is also possible to use the {@link ToStringSummary} annotation to output the summary information instead of the + * detailed information of a field. + *

+ *

* The exact format of the toString is determined by the {@link ToStringStyle} passed into the constructor. *

* @@ -119,6 +123,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder { * if the Object is null * * @see ToStringExclude + * @see ToStringSummary */ public static String toString(final Object object) { return toString(object, null, false, false, null); @@ -153,6 +158,7 @@ public static String toString(final Object object) { * if the Object or ToStringStyle is null * * @see ToStringExclude + * @see ToStringSummary */ public static String toString(final Object object, final ToStringStyle style) { return toString(object, style, false, false, null); @@ -193,6 +199,7 @@ public static String toString(final Object object, final ToStringStyle style) { * if the Object is null * * @see ToStringExclude + * @see ToStringSummary */ public static String toString(final Object object, final ToStringStyle style, final boolean outputTransients) { return toString(object, style, outputTransients, false, null); @@ -240,6 +247,7 @@ public static String toString(final Object object, final ToStringStyle style, fi * if the Object is null * * @see ToStringExclude + * @see ToStringSummary * @since 2.1 */ public static String toString(final Object object, final ToStringStyle style, final boolean outputTransients, final boolean outputStatics) { @@ -293,6 +301,7 @@ public static String toString(final Object object, final ToStringStyle style, fi * if the Object is null * * @see ToStringExclude + * @see ToStringSummary * @since 2.1 */ public static String toString( @@ -351,6 +360,7 @@ public static String toString( * if the Object is null * * @see ToStringExclude + * @see ToStringSummary * @since 3.6 */ public static String toString( @@ -639,7 +649,7 @@ protected void appendFieldsIn(final Class clazz) { // for primitive types. final Object fieldValue = this.getValue(field); if (!excludeNullValues || fieldValue != null) { - this.append(fieldName, fieldValue); + this.append(fieldName, fieldValue, !field.isAnnotationPresent(ToStringSummary.class)); } } catch (final IllegalAccessException ex) { //this can't happen. Would get a Security exception diff --git a/src/main/java/org/apache/commons/lang3/builder/ToStringSummary.java b/src/main/java/org/apache/commons/lang3/builder/ToStringSummary.java new file mode 100644 index 00000000000..6a8b201421c --- /dev/null +++ b/src/main/java/org/apache/commons/lang3/builder/ToStringSummary.java @@ -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.Target; +import java.lang.annotation.RetentionPolicy; + +/** + * Use this annotation on the fields to get the summary instead + * of the detailed information when using {@link ReflectionToStringBuilder}. + * + *

+ * Notice that not all {@link ToStringStyle} implementations + * support the appendSummary method. + *

+ * + * @since 3.7 + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface ToStringSummary { + +} diff --git a/src/test/java/org/apache/commons/lang3/builder/ReflectionToStringBuilderSummaryTest.java b/src/test/java/org/apache/commons/lang3/builder/ReflectionToStringBuilderSummaryTest.java new file mode 100644 index 00000000000..a8b16423ffd --- /dev/null +++ b/src/test/java/org/apache/commons/lang3/builder/ReflectionToStringBuilderSummaryTest.java @@ -0,0 +1,35 @@ +/* + * 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=]", new ReflectionToStringBuilder(this, ToStringStyle.NO_CLASS_NAME_STYLE).build()); + } + +} From 64518f38884df5e47f545e21d7ca85a497038c8b Mon Sep 17 00:00:00 2001 From: Sergio Ozaki Date: Sun, 30 Jul 2017 16:51:58 -0300 Subject: [PATCH 2/3] fix style errors --- .../commons/lang3/builder/ToStringSummary.java | 12 ++++++------ .../ReflectionToStringBuilderSummaryTest.java | 17 +++++++++-------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/builder/ToStringSummary.java b/src/main/java/org/apache/commons/lang3/builder/ToStringSummary.java index 6a8b201421c..031208d57c3 100644 --- a/src/main/java/org/apache/commons/lang3/builder/ToStringSummary.java +++ b/src/main/java/org/apache/commons/lang3/builder/ToStringSummary.java @@ -19,16 +19,16 @@ import java.lang.annotation.ElementType; import java.lang.annotation.Retention; -import java.lang.annotation.Target; 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}. - * + * Use this annotation on the fields to get the summary instead of the detailed + * information when using {@link ReflectionToStringBuilder}. + * *

- * Notice that not all {@link ToStringStyle} implementations - * support the appendSummary method. + * Notice that not all {@link ToStringStyle} implementations support the + * appendSummary method. *

* * @since 3.7 diff --git a/src/test/java/org/apache/commons/lang3/builder/ReflectionToStringBuilderSummaryTest.java b/src/test/java/org/apache/commons/lang3/builder/ReflectionToStringBuilderSummaryTest.java index a8b16423ffd..d13fd0f9a7a 100644 --- a/src/test/java/org/apache/commons/lang3/builder/ReflectionToStringBuilderSummaryTest.java +++ b/src/test/java/org/apache/commons/lang3/builder/ReflectionToStringBuilderSummaryTest.java @@ -20,16 +20,17 @@ import org.junit.Test; public class ReflectionToStringBuilderSummaryTest { - - @SuppressWarnings("unused") - private String stringField = "string"; - - @ToStringSummary - private String summaryString = "summary"; - + + @SuppressWarnings("unused") + private String stringField = "string"; + + @ToStringSummary + private String summaryString = "summary"; + @Test public void testSummary() { - Assert.assertEquals("[stringField=string,summaryString=]", new ReflectionToStringBuilder(this, ToStringStyle.NO_CLASS_NAME_STYLE).build()); + Assert.assertEquals("[stringField=string,summaryString=]", + new ReflectionToStringBuilder(this, ToStringStyle.NO_CLASS_NAME_STYLE).build()); } } From 4bda75e29df55e663ab261721c5e5a7b673faa93 Mon Sep 17 00:00:00 2001 From: Sergio Ozaki Date: Sat, 30 Dec 2017 22:43:05 -0200 Subject: [PATCH 3/3] update version --- .../java/org/apache/commons/lang3/builder/ToStringSummary.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/apache/commons/lang3/builder/ToStringSummary.java b/src/main/java/org/apache/commons/lang3/builder/ToStringSummary.java index 031208d57c3..ba255d4e951 100644 --- a/src/main/java/org/apache/commons/lang3/builder/ToStringSummary.java +++ b/src/main/java/org/apache/commons/lang3/builder/ToStringSummary.java @@ -31,7 +31,7 @@ * appendSummary method. *

* - * @since 3.7 + * @since 3.8 */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD)