mirror of
https://github.com/apache/commons-lang.git
synced 2025-02-09 03:24:58 +00:00
[LANG-756] Add APIs ClassUtils.isPrimitiveWrapper(Class<?>) and isPrimitiveOrWrapper(Class<?>)
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1174469 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
c919ac7250
commit
4e2882b6c3
@ -523,6 +523,37 @@ public static boolean isAssignable(Class<?>[] classArray, Class<?>[] toClassArra
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the given {@code type} is a primitive or primitive wrapper ({@link Boolean}, {@link Byte}, {@link Character},
|
||||
* {@link Short}, {@link Integer}, {@link Long}, {@link Double}, {@link Float}).
|
||||
*
|
||||
* @param type
|
||||
* The class to query or null.
|
||||
* @return true if the given {@code type} is a primitive or primitive wrapper ({@link Boolean}, {@link Byte}, {@link Character},
|
||||
* {@link Short}, {@link Integer}, {@link Long}, {@link Double}, {@link Float}).
|
||||
* @since 3.0.2
|
||||
*/
|
||||
public static boolean isPrimitiveOrWrapper(Class<?> type) {
|
||||
if (type == null) {
|
||||
return false;
|
||||
}
|
||||
return type.isPrimitive() || isPrimitiveWrapper(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the given {@code type} is a primitive wrapper ({@link Boolean}, {@link Byte}, {@link Character}, {@link Short},
|
||||
* {@link Integer}, {@link Long}, {@link Double}, {@link Float}).
|
||||
*
|
||||
* @param type
|
||||
* The class to query or null.
|
||||
* @return true if the given {@code type} is a primitive wrapper ({@link Boolean}, {@link Byte}, {@link Character}, {@link Short},
|
||||
* {@link Integer}, {@link Long}, {@link Double}, {@link Float}).
|
||||
* @since 3.0.2
|
||||
*/
|
||||
public static boolean isPrimitiveWrapper(Class<?> type) {
|
||||
return wrapperPrimitiveMap.containsKey(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Checks if one {@code Class} can be assigned to a variable of
|
||||
* another {@code Class}.</p>
|
||||
@ -1100,4 +1131,5 @@ private static String getCanonicalName(String className) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -21,7 +21,8 @@
|
||||
</properties>
|
||||
<body>
|
||||
|
||||
<release version="3.0.2" date="unreleased" description="September release">
|
||||
<release version="3.0.2" date="unreleased" description="September release">
|
||||
<action type="add" issue="LANG-756">Add APIs ClassUtils.isPrimitiveWrapper(Class<?>) and isPrimitiveOrWrapper(Class<?>)</action>
|
||||
<action type="update" issue="LANG-752">Fix createLong() so it behaves like createInteger()</action>
|
||||
<action type="update" issue="LANG-751">Include the actual type in the Validate.isInstance and isAssignableFrom exception messages</action>
|
||||
<action type="fix" issue="LANG-746">NumberUtils does not handle upper-case hex: 0X and -0X</action>
|
||||
|
@ -716,6 +716,66 @@ public void test_isAssignable_Unboxing_Widening() throws Exception {
|
||||
assertTrue("boolean -> boolean", ClassUtils.isAssignable(Boolean.class, Boolean.TYPE, true));
|
||||
}
|
||||
|
||||
public void testIsPrimitiveOrWrapper() {
|
||||
|
||||
// test primitive wrapper classes
|
||||
assertTrue("Boolean.class", ClassUtils.isPrimitiveOrWrapper(Boolean.class));
|
||||
assertTrue("Byte.class", ClassUtils.isPrimitiveOrWrapper(Byte.class));
|
||||
assertTrue("Character.class", ClassUtils.isPrimitiveOrWrapper(Character.class));
|
||||
assertTrue("Short.class", ClassUtils.isPrimitiveOrWrapper(Short.class));
|
||||
assertTrue("Integer.class", ClassUtils.isPrimitiveOrWrapper(Integer.class));
|
||||
assertTrue("Long.class", ClassUtils.isPrimitiveOrWrapper(Long.class));
|
||||
assertTrue("Double.class", ClassUtils.isPrimitiveOrWrapper(Double.class));
|
||||
assertTrue("Float.class", ClassUtils.isPrimitiveOrWrapper(Float.class));
|
||||
|
||||
// test primitive classes
|
||||
assertTrue("boolean", ClassUtils.isPrimitiveOrWrapper(Boolean.TYPE));
|
||||
assertTrue("byte", ClassUtils.isPrimitiveOrWrapper(Byte.TYPE));
|
||||
assertTrue("char", ClassUtils.isPrimitiveOrWrapper(Character.TYPE));
|
||||
assertTrue("short", ClassUtils.isPrimitiveOrWrapper(Short.TYPE));
|
||||
assertTrue("int", ClassUtils.isPrimitiveOrWrapper(Integer.TYPE));
|
||||
assertTrue("long", ClassUtils.isPrimitiveOrWrapper(Long.TYPE));
|
||||
assertTrue("double", ClassUtils.isPrimitiveOrWrapper(Double.TYPE));
|
||||
assertTrue("float", ClassUtils.isPrimitiveOrWrapper(Float.TYPE));
|
||||
assertTrue("Void.TYPE", ClassUtils.isPrimitiveOrWrapper(Void.TYPE));
|
||||
|
||||
// others
|
||||
assertFalse("null", ClassUtils.isPrimitiveOrWrapper(null));
|
||||
assertFalse("Void.class", ClassUtils.isPrimitiveOrWrapper(Void.class));
|
||||
assertFalse("String.class", ClassUtils.isPrimitiveOrWrapper(String.class));
|
||||
assertFalse("this.getClass()", ClassUtils.isPrimitiveOrWrapper(this.getClass()));
|
||||
}
|
||||
|
||||
public void testIsPrimitiveWrapper() {
|
||||
|
||||
// test primitive wrapper classes
|
||||
assertTrue("Boolean.class", ClassUtils.isPrimitiveWrapper(Boolean.class));
|
||||
assertTrue("Byte.class", ClassUtils.isPrimitiveWrapper(Byte.class));
|
||||
assertTrue("Character.class", ClassUtils.isPrimitiveWrapper(Character.class));
|
||||
assertTrue("Short.class", ClassUtils.isPrimitiveWrapper(Short.class));
|
||||
assertTrue("Integer.class", ClassUtils.isPrimitiveWrapper(Integer.class));
|
||||
assertTrue("Long.class", ClassUtils.isPrimitiveWrapper(Long.class));
|
||||
assertTrue("Double.class", ClassUtils.isPrimitiveWrapper(Double.class));
|
||||
assertTrue("Float.class", ClassUtils.isPrimitiveWrapper(Float.class));
|
||||
|
||||
// test primitive classes
|
||||
assertFalse("boolean", ClassUtils.isPrimitiveWrapper(Boolean.TYPE));
|
||||
assertFalse("byte", ClassUtils.isPrimitiveWrapper(Byte.TYPE));
|
||||
assertFalse("char", ClassUtils.isPrimitiveWrapper(Character.TYPE));
|
||||
assertFalse("short", ClassUtils.isPrimitiveWrapper(Short.TYPE));
|
||||
assertFalse("int", ClassUtils.isPrimitiveWrapper(Integer.TYPE));
|
||||
assertFalse("long", ClassUtils.isPrimitiveWrapper(Long.TYPE));
|
||||
assertFalse("double", ClassUtils.isPrimitiveWrapper(Double.TYPE));
|
||||
assertFalse("float", ClassUtils.isPrimitiveWrapper(Float.TYPE));
|
||||
|
||||
// others
|
||||
assertFalse("null", ClassUtils.isPrimitiveWrapper(null));
|
||||
assertFalse("Void.class", ClassUtils.isPrimitiveWrapper(Void.class));
|
||||
assertFalse("Void.TYPE", ClassUtils.isPrimitiveWrapper(Void.TYPE));
|
||||
assertFalse("String.class", ClassUtils.isPrimitiveWrapper(String.class));
|
||||
assertFalse("this.getClass()", ClassUtils.isPrimitiveWrapper(this.getClass()));
|
||||
}
|
||||
|
||||
public void testPrimitiveToWrapper() {
|
||||
|
||||
// test primitive classes
|
||||
|
Loading…
x
Reference in New Issue
Block a user