mirror of
https://github.com/apache/commons-lang.git
synced 2025-02-06 10:08:32 +00:00
[LANG-1360] Add methods to ObjectUtils to get various forms of class
names in a null-safe manner
This commit is contained in:
parent
88654b79c9
commit
6ea2fc8d38
@ -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-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-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-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>
|
||||||
|
|
||||||
<release version="3.6" date="2017-06-08" description="New features and bug fixes. Requires Java 7.">
|
<release version="3.6" date="2017-06-08" description="New features and bug fixes. Requires Java 7.">
|
||||||
|
@ -1033,4 +1033,36 @@ public static <T> T CONST(final T v) {
|
|||||||
return v;
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,6 +38,7 @@
|
|||||||
import org.apache.commons.lang3.exception.CloneFailedException;
|
import org.apache.commons.lang3.exception.CloneFailedException;
|
||||||
import org.apache.commons.lang3.mutable.MutableObject;
|
import org.apache.commons.lang3.mutable.MutableObject;
|
||||||
import org.apache.commons.lang3.text.StrBuilder;
|
import org.apache.commons.lang3.text.StrBuilder;
|
||||||
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -666,4 +667,36 @@ public int compare(final CharSequence o1, final CharSequence o2) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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")));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user