You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by pa...@apache.org on 2018/01/12 17:31:39 UTC
[1/2] [lang] LANG-1372: Add ToStringSummary annotation (closes #281)
Repository: commons-lang
Updated Branches:
refs/heads/master 63f11e9dc -> e843239cf
LANG-1372: Add ToStringSummary annotation (closes #281)
Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/f5ebb9a6
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/f5ebb9a6
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/f5ebb9a6
Branch: refs/heads/master
Commit: f5ebb9a649e9dd0773f9bd3457d6ce1895266d59
Parents: 63f11e9
Author: Sergio Ozaki <se...@gmail.com>
Authored: Sun Jul 30 15:37:37 2017 -0300
Committer: pascalschumacher <pa...@gmx.net>
Committed: Fri Jan 12 18:28:27 2018 +0100
----------------------------------------------------------------------
.../builder/ReflectionToStringBuilder.java | 12 +++++-
.../commons/lang3/builder/ToStringSummary.java | 40 ++++++++++++++++++++
.../ReflectionToStringBuilderSummaryTest.java | 36 ++++++++++++++++++
3 files changed, 87 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/f5ebb9a6/src/main/java/org/apache/commons/lang3/builder/ReflectionToStringBuilder.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 e720deb..9390d83 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 @@ import org.apache.commons.lang3.Validate;
* result.
* </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.
* </p>
*
@@ -119,6 +123,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
* if the Object is <code>null</code>
*
* @see ToStringExclude
+ * @see ToStringSummary
*/
public static String toString(final Object object) {
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>
*
* @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 class ReflectionToStringBuilder extends ToStringBuilder {
* if the Object is <code>null</code>
*
* @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 class ReflectionToStringBuilder extends ToStringBuilder {
* if the Object is <code>null</code>
*
* @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 class ReflectionToStringBuilder extends ToStringBuilder {
* if the Object is <code>null</code>
*
* @see ToStringExclude
+ * @see ToStringSummary
* @since 2.1
*/
public static <T> String toString(
@@ -351,6 +360,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
* if the Object is <code>null</code>
*
* @see ToStringExclude
+ * @see ToStringSummary
* @since 3.6
*/
public static <T> String toString(
@@ -639,7 +649,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
// 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
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/f5ebb9a6/src/main/java/org/apache/commons/lang3/builder/ToStringSummary.java
----------------------------------------------------------------------
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 0000000..ba255d4
--- /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.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 {
+
+}
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/f5ebb9a6/src/test/java/org/apache/commons/lang3/builder/ReflectionToStringBuilderSummaryTest.java
----------------------------------------------------------------------
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 0000000..d13fd0f
--- /dev/null
+++ b/src/test/java/org/apache/commons/lang3/builder/ReflectionToStringBuilderSummaryTest.java
@@ -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());
+ }
+
+}
[2/2] [lang] LANG-1372: Add ToStringSummary annotation
Posted by pa...@apache.org.
LANG-1372: Add ToStringSummary annotation
add changes.xml entry
Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/e843239c
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/e843239c
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/e843239c
Branch: refs/heads/master
Commit: e843239cf9764bb9b8a3b1914c07e5215b9df3c5
Parents: f5ebb9a
Author: pascalschumacher <pa...@gmx.net>
Authored: Fri Jan 12 18:31:10 2018 +0100
Committer: pascalschumacher <pa...@gmx.net>
Committed: Fri Jan 12 18:31:10 2018 +0100
----------------------------------------------------------------------
src/changes/changes.xml | 1 +
1 file changed, 1 insertion(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/e843239c/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index bf9f499..b1138d3 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -51,6 +51,7 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="LANG-1370" type="fix" dev="kinow" due-to="Andre Dieb">Fix EventCountCircuitBreaker increment batch</action>
<action issue="LANG-1367" type="update" dev="ggregory" due-to="Gary Gregory">ObjectUtils.identityToString(Object) and friends should allocate builders and buffers with a size</action>
<action issue="LANG-1352" type="add" dev="pschumacher" due-to="Ruslan Sibgatullin">EnumUtils.getEnumIgnoreCase and isValidEnumIgnoreCase methods added</action>
+ <action issue="LANG-1372" type="add" dev="pschumacher" due-to="Sérgio Ozaki">Add ToStringSummary annotation</action>
</release>
<release version="3.7" date="2017-11-04" description="New features and bug fixes. Requires Java 7, supports Java 8, 9, 10.">