Is there a way to know if a given class is a class that boxes a primitive type or do I have to make an ugly method like this :
public boolean isBoxingClass(Class clazz){
String simpleName=clazz.getSimpleName();
switch(simpleName){
case "Integer":
case "Long":
case "Boolean":
case "Double":
case "Float":
return true;
default :
return false;
}
}
EDIT:
If finally opted for this solution :
public static final List<Class> BOXING_CLASSES= Arrays.asList(new Class[]{
Integer.class,
Long.class,
Short.class,
Boolean.class,
Double.class,
Float.class,
Character.class,
Void.class,
Byte.class});
public static boolean isBoxing(Class clazz){
return BOXING_CLASSES.contains(clazz);
}
This is the simplest way I could think of. The wrapper classes are present only in java.lang
package. And apart from the wrapper classes, no other class in java.lang
has a variable named TYPE
. You could use that to check whether a class is Wrapper class or not.
public static boolean isBoxingClass(Class<?> clazz)
{
String pack = clazz.getPackage().getName();
if(!"java.lang".equals(pack))
return false;
try
{
clazz.getField("TYPE");
}
catch (NoSuchFieldException e)
{
return false;
}
return true;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With