3

Here the function signature I would like to implement.

public boolean isBaseTypeOrArray(Object obj){~}

I want to return true only if obj is of one of the following types.

boolean or boolean[]
byte or byte[]
short or short[]
int or int[]
long or long[]
float or float[]
double or double[]
char or char[]
java.lang.String or String[]

For a lone value checking if it is instance of one of the wrapper classes (Integer, Float, ...) or String should work because of auto boxing but I do not know how to check for the array case. Any ideas?

le-doude
  • 3,345
  • 2
  • 25
  • 55
  • How would this be used and why? – Paul Bellora Jan 20 '14 at 05:12
  • 4
    You will not be able to determine if argument was primitive because it will be autoboxed to Object type. You will have to create separate methods for primitive types. – Pshemo Jan 20 '14 at 05:13
  • 1
    http://stackoverflow.com/questions/709961/determining-if-an-object-is-of-primitive-type – Kamlesh Arya Jan 20 '14 at 05:13
  • The simplest way might be to make a big list of 18 `Class` objects and check if the object's class is in the list. – user2357112 Jan 20 '14 at 05:13
  • Are you actually going to be passing primitives to this function? You can't actually pass a primitive to this function without already knowing that it's a primitive. (Strictly speaking, you can't pass a primitive to this function at all, since it'll be boxed before it reaches the function.) While it's possible to determine whether a value is one of the 8 box types, boxes aren't primitives. Also, `String` isn't primitive. – user2357112 Jan 20 '14 at 05:15
  • @PaulBellora I am receiving collections of `Object` and I need to test them. – le-doude Jan 20 '14 at 05:21
  • @user2357112 I know about String and I know this will be a separate case. – le-doude Jan 20 '14 at 05:22
  • Why not just overload the function with these parameters? and write seperate functions to accept your inputs? Then you can write a common function for your business logic? That is one way of doing it right. – Hrishikesh Jan 20 '14 at 05:23
  • @Hrishikesh please see my comment to PaulBellora – le-doude Jan 20 '14 at 05:25
  • if the default value of the object is != null then return true? I guess this might work, any comments – praveen_mohan Jan 20 '14 at 05:38

4 Answers4

14
  • To test if object is an array just getClass() and see if it isArray().
  • To get type of elements array is declared to hold use getComponentType() on Class instance.
  • To test if some type is primitive you can use isPrimitive().
  • If you want to check if type represents String just use equals(String.class).

So to test if Object represents array of primitive type or array of strings you can use

public static boolean isBaseTypeOrArray(Object obj) {
    Class<?> c = obj.getClass();
    return  c.equals(String.class) 
         || c.equals(String[].class)
         || c.isArray() && c.getComponentType().isPrimitive();
}

Problem with above method is that it can't accept primitive type values because it expect Object obj which means that it enforces autoboxing of any primitive type value to its wrapper class. For instance if wa call it like isBaseTypeOrArray(1) primitive int 1 will be wrapped to Integer 1.

To accept arguments of pure primitive type (like int and not Integer) we need overloaded versions of that method which would accept primitive types like boolean isBaseTypeOrArray(int obj). Such methods can immediately return true as result because fact that they ware invoked means that primitive type value was passed as argument.

So to handle all primitive types we need to add below methods:

public static boolean isBaseTypeOrArray(boolean obj) {return true;}
public static boolean isBaseTypeOrArray(byte obj) {return true;}
public static boolean isBaseTypeOrArray(short obj) {return true;}
public static boolean isBaseTypeOrArray(char obj) {return true;}
public static boolean isBaseTypeOrArray(int obj) {return true;}
public static boolean isBaseTypeOrArray(long obj) {return true;}
public static boolean isBaseTypeOrArray(float obj) {return true;}
public static boolean isBaseTypeOrArray(double obj) {return true;}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
4

I think, you should dynamically get and test a class description of your value:

if (obj.getClass().equals(Boolean.class)) {
    //...
}
//...
if (obj.getClass().equals(boolean[].class)) {
    //...
}
//...
if (obj.getClass().equals(String.class)) {
    //...
}
Filipp Voronov
  • 4,077
  • 5
  • 25
  • 32
2

You can try something like this

public static void main(String[] args) {
    System.out.println(isBaseTypeOrArray(new int[5]));
}

public static boolean isBaseTypeOrArray(Object obj){
   return (obj.getClass().getComponentType().equals(int.class)||
           obj.getClass().getComponentType().equals(byte.class));
}
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
1

Simple solution

public static boolean isPrimitiveArray(Object obj) {
    return obj != null 
        && obj.getClass().isArray() 
        && obj.getClass().getComponentType() != null 
        && obj.getClass().getComponentType().isPrimitive();
}
Woo
  • 41
  • 3