[LANG-1360] Add methods to ObjectUtils to get various forms of class

names in a null-safe manner
This commit is contained in:
Gary Gregory 2017-10-20 13:19:56 -06:00
parent 88654b79c9
commit 6ea2fc8d38
3 changed files with 66 additions and 0 deletions

View File

@ -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.">

View File

@ -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();
}
}

View File

@ -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")));
}
}