Repository: commons-lang
Updated Branches:
  refs/heads/master 88654b79c -> 6ea2fc8d3


[LANG-1360] Add methods to ObjectUtils to get various forms of class
names in a null-safe manner

Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/6ea2fc8d
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/6ea2fc8d
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/6ea2fc8d

Branch: refs/heads/master
Commit: 6ea2fc8d38e035bafaa92c7d3b007be38c2e9000
Parents: 88654b7
Author: Gary Gregory <ggreg...@apache.org>
Authored: Fri Oct 20 13:19:56 2017 -0600
Committer: Gary Gregory <ggreg...@apache.org>
Committed: Fri Oct 20 13:19:56 2017 -0600

----------------------------------------------------------------------
 src/changes/changes.xml                         |  1 +
 .../org/apache/commons/lang3/ObjectUtils.java   | 32 +++++++++++++++++++
 .../apache/commons/lang3/ObjectUtilsTest.java   | 33 ++++++++++++++++++++
 3 files changed, 66 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/6ea2fc8d/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index cf4296e..66dd510 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -53,6 +53,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action issue="LANG-1350" type="fix" dev="ggregory" due-to="Brett 
Kail">ConstructorUtils.invokeConstructor(Class, Object...) regression</action>
     <action issue="LANG-1349" type="fix" dev="pschumacher" due-to="Naman 
Nigam">EqualsBuilder#isRegistered: swappedPair construction bug</action>
     <action issue="LANG-1357" type="fix" dev="ggregory" 
due-to="BruceKuiLiu">org.apache.commons.lang3.time.FastDateParser should use 
toUpperCase(Locale)</action>
+    <action issue="LANG-1360" type="add" dev="ggregory" due-to="Gary 
Gregory">Add methods to ObjectUtils to get various forms of class names in a 
null-safe manner</action>
   </release>
 
   <release version="3.6" date="2017-06-08" description="New features and bug 
fixes. Requires Java 7.">

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/6ea2fc8d/src/main/java/org/apache/commons/lang3/ObjectUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/lang3/ObjectUtils.java 
b/src/main/java/org/apache/commons/lang3/ObjectUtils.java
index 1ec0956..16a6b93 100644
--- a/src/main/java/org/apache/commons/lang3/ObjectUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ObjectUtils.java
@@ -1033,4 +1033,36 @@ public class ObjectUtils {
         return v;
     }
 
+    /**
+     * Gets the class name of the given object.
+     * 
+     * @param object the object to query, may be null
+     * @return the given object's class name or null if the object is null
+     * @since 3.7
+     */
+    public static String getClassName(final Object object) {
+        return object == null ? null : object.getClass().getName();
+    }
+
+    /**
+     * Gets the class simple name of the given object.
+     * 
+     * @param object the object to query, may be null
+     * @return the given object's class simple name or null if the object is 
null
+     * @since 3.7
+     */
+    public static String getClassSimpleName(final Object object) {
+        return object == null ? null : object.getClass().getSimpleName();
+    }
+
+    /**
+     * Gets the class canonical name of the given object.
+     * 
+     * @param object the object to query, may be null
+     * @return the given object's class canonical name or null if the object 
is null
+     * @since 3.7
+     */
+    public static String getClassCanonicalName(final Object object) {
+        return object == null ? null : object.getClass().getCanonicalName();
+    }
 }

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/6ea2fc8d/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java 
b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
index 3da8443..2bf036c 100644
--- a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
@@ -38,6 +38,7 @@ import java.util.List;
 import org.apache.commons.lang3.exception.CloneFailedException;
 import org.apache.commons.lang3.mutable.MutableObject;
 import org.apache.commons.lang3.text.StrBuilder;
+import org.junit.Assert;
 import org.junit.Test;
 
 /**
@@ -666,4 +667,36 @@ public class ObjectUtilsTest {
         }
 
     }
+
+    /**
+     * @since 3.7
+     */
+    @Test
+    public void testGetClassName() {
+        Assert.assertNull(ObjectUtils.getClassName(null));
+        Assert.assertEquals("java.lang.String", ObjectUtils.getClassName(new 
String()));
+        
Assert.assertEquals("org.apache.commons.lang3.ObjectUtilsTest$CloneableString",
+                ObjectUtils.getClassName(new CloneableString("test")));
+    }
+
+    /**
+     * @since 3.7
+     */
+    @Test
+    public void testGetSimpleName() {
+        Assert.assertNull(ObjectUtils.getClassSimpleName(null));
+        Assert.assertEquals("String", ObjectUtils.getClassSimpleName(new 
String()));
+        Assert.assertEquals("CloneableString", 
ObjectUtils.getClassSimpleName(new CloneableString("test")));
+    }
+
+    /**
+     * @since 3.7
+     */
+    @Test
+    public void testGetCanonicalName() {
+        Assert.assertNull(ObjectUtils.getClassCanonicalName(null));
+        Assert.assertEquals("java.lang.String", 
ObjectUtils.getClassCanonicalName(new String()));
+        
Assert.assertEquals("org.apache.commons.lang3.ObjectUtilsTest.CloneableString",
+                ObjectUtils.getClassCanonicalName(new 
CloneableString("test")));
+    }
 }

Reply via email to