commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject [lang] [LANG-1360] Add methods to ClassUtils to get various forms of class names in a null-safe manner
Date Mon, 23 Oct 2017 16:56:12 GMT
Repository: commons-lang
Updated Branches:
  refs/heads/master 125cfb4ca -> 8e8e78d84


[LANG-1360] Add methods to ClassUtils 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/8e8e78d8
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/8e8e78d8
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/8e8e78d8

Branch: refs/heads/master
Commit: 8e8e78d849825696237b9f540f3e082a44b1e838
Parents: 125cfb4
Author: Gary Gregory <ggregory@apache.org>
Authored: Mon Oct 23 10:56:09 2017 -0600
Committer: Gary Gregory <ggregory@apache.org>
Committed: Mon Oct 23 10:56:09 2017 -0600

----------------------------------------------------------------------
 .../org/apache/commons/lang3/ClassUtils.java    | 140 +++++++++++++++++--
 .../org/apache/commons/lang3/ObjectUtils.java   |  32 -----
 .../apache/commons/lang3/ClassUtilsTest.java    |  59 +++++++-
 .../apache/commons/lang3/ObjectUtilsTest.java   |  31 ----
 4 files changed, 180 insertions(+), 82 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/8e8e78d8/src/main/java/org/apache/commons/lang3/ClassUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/lang3/ClassUtils.java b/src/main/java/org/apache/commons/lang3/ClassUtils.java
index c294f67..c81ac3c 100644
--- a/src/main/java/org/apache/commons/lang3/ClassUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ClassUtils.java
@@ -44,6 +44,7 @@ import org.apache.commons.lang3.mutable.MutableObject;
  * @since 2.0
  */
 public class ClassUtils {
+    
     /**
      * Inclusivity literals for {@link #hierarchy(Class, Interfaces)}.
      * @since 3.2
@@ -242,32 +243,101 @@ public class ClassUtils {
     /**
      * <p>Null-safe version of <code>aClass.getSimpleName()</code></p>
      *
-     * @param cls the class for which to get the simple name.
+     * @param cls the class for which to get the simple name; may be null
      * @return the simple class name.
      * @since 3.0
      * @see Class#getSimpleName()
      */
     public static String getSimpleName(final Class<?> cls) {
-        if (cls == null) {
-            return StringUtils.EMPTY;
-        }
-        return cls.getSimpleName();
+        return getSimpleName(cls, StringUtils.EMPTY);
+    }
+
+    /**
+     * <p>Null-safe version of <code>aClass.getSimpleName()</code></p>
+     *
+     * @param cls the class for which to get the simple name; may be null
+     * @param valueIfNull  the value to return if null
+     * @return the simple class name or {@code valueIfNull}
+     * @since 3.0
+     * @see Class#getSimpleName()
+     */
+    public static String getSimpleName(final Class<?> cls, String valueIfNull) {
+        return cls == null ? valueIfNull : cls.getSimpleName();
     }
 
     /**
      * <p>Null-safe version of <code>aClass.getSimpleName()</code></p>
      *
-     * @param object the object for which to get the simple class name.
+     * @param object the object for which to get the simple class name; may be null
+     * @return the simple class name or the empty String
+     * @since 3.7
+     * @see Class#getSimpleName()
+     */
+    public static String getSimpleName(final Object object) {
+        return getSimpleName(object, StringUtils.EMPTY);
+    }
+
+    /**
+     * <p>Null-safe version of <code>aClass.getSimpleName()</code></p>
+     *
+     * @param object the object for which to get the simple class name; may be null
      * @param valueIfNull the value to return if <code>object</code> is <code>null</code>
-     * @return the simple class name.
+     * @return the simple class name or {@code valueIfNull}
      * @since 3.0
      * @see Class#getSimpleName()
      */
     public static String getSimpleName(final Object object, final String valueIfNull) {
-        if (object == null) {
-            return valueIfNull;
-        }
-        return getSimpleName(object.getClass());
+        return object == null ? valueIfNull : object.getClass().getSimpleName();
+    }
+
+    /**
+     * <p>Null-safe version of <code>Class.getName()</code></p>
+     *
+     * @param cls the class for which to get the class name; may be null
+     * @return the class name or the empty String.
+     * @since 3.7
+     * @see Class#getSimpleName()
+     */
+    public static String getName(final Class<?> cls) {
+        return getName(cls, StringUtils.EMPTY);
+    }
+
+    /**
+     * <p>Null-safe version of <code>aClass.getName()</code></p>
+     *
+     * @param cls the class for which to get the class name; may be null
+     * @param valueIfNull the return value if <code>cls</code> is <code>null</code>
+     * @return the class name or {@code valueIfNull}
+     * @since 3.7
+     * @see Class#getName()
+     */
+    public static String getName(final Class<?> cls, final String valueIfNull) {
+        return cls == null ? valueIfNull : cls.getName();
+    }
+
+    /**
+     * <p>Null-safe version of <code>Class.getName()</code></p>
+     *
+     * @param object the object for which to get the class name; may be null
+     * @return the class name or the empty String
+     * @since 3.7
+     * @see Class#getSimpleName()
+     */
+    public static String getName(final Object object) {
+        return getName(object, StringUtils.EMPTY);
+    }
+
+    /**
+     * <p>Null-safe version of <code>aClass.getSimpleName()</code></p>
+     *
+     * @param object the object for which to get the class name; may be null
+     * @param valueIfNull the value to return if <code>object</code> is <code>null</code>
+     * @return the class name or {@code valueIfNull}
+     * @since 3.0
+     * @see Class#getName()
+     */
+    public static String getName(final Object object, final String valueIfNull) {
+        return object == null ? valueIfNull : object.getClass().getName();
     }
 
     // Package name
@@ -1112,9 +1182,55 @@ public class ClassUtils {
     }
 
     /**
+     * <p>Gets the canonical class name for a {@code Class}.</p>
+     *
+     * @param cls the class for which to get the canonical class name; may be null
+     * @return the canonical name of the class or the empty String
+     * @since 3.7
+     */
+    public static String getCanonicalName(final Class<?> cls) {
+        return getCanonicalName(cls, StringUtils.EMPTY);
+    }
+
+    /**
+     * <p>Gets the canonical name for a {@code Class}.</p>
+     *
+     * @param cls the class for which to get the canonical class name; may be null
+     * @param valueIfNull  the return value if null
+     * @return the canonical name of the class or {@code valueIfNull}
+     * @since 3.7
+     */
+    public static String getCanonicalName(final Class<?> cls, final String valueIfNull)
{
+        return cls == null ? valueIfNull : cls.getClass().getCanonicalName();
+    }
+
+    /**
+     * <p>Gets the canonical name for an {@code Object}.</p>
+     *
+     * @param object the object for which to get the canonical class name; may be null
+     * @return the canonical name of the object, or the empty String
+     * @since 3.7
+     */
+    public static String getCanonicalName(final Object object) {
+        return getCanonicalName(object, StringUtils.EMPTY);
+    }
+
+    /**
+     * <p>Gets the canonical name for an {@code Object}.</p>
+     *
+     * @param object the object for which to get the canonical class name; may be null
+     * @param valueIfNull  the return value if null
+     * @return the canonical name of the object or {@code valueIfNull}
+     * @since 3.7
+     */
+    public static String getCanonicalName(final Object object, final String valueIfNull)
{
+        return object == null ? valueIfNull : object.getClass().getCanonicalName();
+    }
+
+    /**
      * <p>Gets the canonical name minus the package name from a {@code Class}.</p>
      *
-     * @param cls  the class to get the short name for.
+     * @param cls the class for which to get the short canonical class name; may be null
      * @return the canonical name without the package name or an empty string
      * @since 2.4
      */

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/8e8e78d8/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 8bebba9..1ec0956 100644
--- a/src/main/java/org/apache/commons/lang3/ObjectUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ObjectUtils.java
@@ -1033,36 +1033,4 @@ 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/8e8e78d8/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java b/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java
index 9290902..4ae2294 100644
--- a/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java
@@ -343,10 +343,10 @@ public class ClassUtilsTest  {
         class Named {
             // empty
         }
-        assertEquals("ClassUtilsTest.7", ClassUtils.getShortCanonicalName(new Object() {
+        assertEquals("ClassUtilsTest.4", ClassUtils.getShortCanonicalName(new Object() {
             // empty
         }.getClass()));
-        assertEquals("ClassUtilsTest.6Named", ClassUtils.getShortCanonicalName(Named.class));
+        assertEquals("ClassUtilsTest.4Named", ClassUtils.getShortCanonicalName(Named.class));
         assertEquals("ClassUtilsTest.Inner", ClassUtils.getShortCanonicalName(Inner.class));
     }
 
@@ -363,7 +363,7 @@ public class ClassUtilsTest  {
         class Named {
             // empty
         }
-        assertEquals("ClassUtilsTest.6", ClassUtils.getShortCanonicalName(new Object() {
+        assertEquals("ClassUtilsTest.5", ClassUtils.getShortCanonicalName(new Object() {
             // empty
         }, "<null>"));
         assertEquals("ClassUtilsTest.5Named", ClassUtils.getShortCanonicalName(new Named(),
"<null>"));
@@ -427,13 +427,58 @@ public class ClassUtilsTest  {
         class Named {
             // empty
         }
-        assertEquals("ClassUtilsTest.2", ClassUtils.getShortClassName(new Object() {
+        assertEquals("ClassUtilsTest.6", ClassUtils.getShortClassName(new Object() {
             // empty
         }.getClass()));
-        assertEquals("ClassUtilsTest.2Named", ClassUtils.getShortClassName(Named.class));
+        assertEquals("ClassUtilsTest.6Named", ClassUtils.getShortClassName(Named.class));
         assertEquals("ClassUtilsTest.Inner", ClassUtils.getShortClassName(Inner.class));
     }
 
+    @Test
+    public void test_getClassName_Class() {
+        assertEquals("org.apache.commons.lang3.ClassUtils", ClassUtils.getName(ClassUtils.class));
+        assertEquals("java.util.Map$Entry", ClassUtils.getName(Map.Entry.class));
+        assertEquals("", ClassUtils.getName((Class<?>) null));
+
+        assertEquals("[Ljava.lang.String;", ClassUtils.getName(String[].class));
+        assertEquals("[Ljava.util.Map$Entry;", ClassUtils.getName(Map.Entry[].class));
+
+        // Primitives
+        assertEquals("boolean", ClassUtils.getName(boolean.class));
+        assertEquals("byte", ClassUtils.getName(byte.class));
+        assertEquals("char", ClassUtils.getName(char.class));
+        assertEquals("short", ClassUtils.getName(short.class));
+        assertEquals("int", ClassUtils.getName(int.class));
+        assertEquals("long", ClassUtils.getName(long.class));
+        assertEquals("float", ClassUtils.getName(float.class));
+        assertEquals("double", ClassUtils.getName(double.class));
+
+        // Primitive Arrays
+        assertEquals("[Z", ClassUtils.getName(boolean[].class));
+        assertEquals("[B", ClassUtils.getName(byte[].class));
+        assertEquals("[C", ClassUtils.getName(char[].class));
+        assertEquals("[S", ClassUtils.getName(short[].class));
+        assertEquals("[I", ClassUtils.getName(int[].class));
+        assertEquals("[J", ClassUtils.getName(long[].class));
+        assertEquals("[F", ClassUtils.getName(float[].class));
+        assertEquals("[D", ClassUtils.getName(double[].class));
+
+        // Arrays of arrays of ...
+        assertEquals("[[Ljava.lang.String;", ClassUtils.getName(String[][].class));
+        assertEquals("[[[Ljava.lang.String;", ClassUtils.getName(String[][][].class));
+        assertEquals("[[[[Ljava.lang.String;", ClassUtils.getName(String[][][][].class));
+
+        // Inner types
+        class Named {
+            // empty
+        }
+        assertEquals("org.apache.commons.lang3.ClassUtilsTest$7", ClassUtils.getName(new
Object() {
+            // empty
+        }.getClass()));
+        assertEquals("org.apache.commons.lang3.ClassUtilsTest$7Named", ClassUtils.getName(Named.class));
+        assertEquals("org.apache.commons.lang3.ClassUtilsTest$Inner", ClassUtils.getName(Inner.class));
+    }
+
     // -------------------------------------------------------------------------
     @Test
     public void test_getShortClassName_Object() {
@@ -446,10 +491,10 @@ public class ClassUtilsTest  {
         class Named {
             // empty
         }
-        assertEquals("ClassUtilsTest.1", ClassUtils.getShortClassName(new Object() {
+        assertEquals("ClassUtilsTest.8", ClassUtils.getShortClassName(new Object() {
             // empty
         }, "<null>"));
-        assertEquals("ClassUtilsTest.1Named", ClassUtils.getShortClassName(new Named(), "<null>"));
+        assertEquals("ClassUtilsTest.8Named", ClassUtils.getShortClassName(new Named(), "<null>"));
         assertEquals("ClassUtilsTest.Inner", ClassUtils.getShortClassName(new Inner(), "<null>"));
     }
 

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/8e8e78d8/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 2bf036c..e48fd0d 100644
--- a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
@@ -668,35 +668,4 @@ 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")));
-    }
 }


Mime
View raw message