5

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);
}
Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
  • 1
    What about `Character`? – zw324 May 27 '13 at 15:51
  • 6
    That would return `true` for `my.package.Integer`! It's better to do it like this `class == Integer.class || class == Long.class || ...` (or equivalent, a `Set>` would come in handy). – Joachim Sauer May 27 '13 at 15:52
  • 1
    @Ziyao Wei : I want a method precisely because I do not want to miss some cases like this ;) – Arnaud Denoyelle May 27 '13 at 15:52
  • @Joachim Sauer : Thank you for raising this issue, I also missed this one :) – Arnaud Denoyelle May 27 '13 at 15:54
  • 7
    Jon Skeet said no you cannot have a simpler way: http://stackoverflow.com/questions/709961/determining-if-an-object-is-of-primitive-type – zw324 May 27 '13 at 15:55
  • 1
    Small note, you should use a Set instead of a List, at this scale it won't make any performance difference but still a Set is the proper structure to use here. – Julien Grenier May 27 '13 at 18:39
  • that seems to be good. Do you have a nice way to declare and init this set in a simple line ? (an equivalent to something like Sets.asSet()) – Arnaud Denoyelle May 27 '13 at 19:10
  • @adenoyelle: `new HashSet(Arrays.asList(...))` is the shortest you can get within JDK-classes only (also you can skip the `new Class[]`, as `asList()` is a var-args method). Guava adds `ImmutableSet.of()` (but if you use Guava, you can just use the `Primitives` class). – Joachim Sauer May 28 '13 at 05:23

2 Answers2

2

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;        
}
Rahul Bobhate
  • 4,892
  • 3
  • 25
  • 48
-1

How about looking if the class implements Number, or if it's Character, or Boolean (I think those are the only options)?

public boolean isBoxingClass(Class clazz)
{   
    return Number.class.isAssignableFrom(clazz) ||
           Character.class.isAssignableFrom(clazz) || 
           Boolean.class.isAssignableFrom(clazz);
}

In this case you get true for eg. BigDecimal and AtomicInteger as well, not sure if that's what you want. But in any case you don't need to go through getSimpleName().

zetches
  • 148
  • 8