milan
milan

Reputation: 2515

Default value from a class

How do I write a function which returns a default value for the given class? If a class is one of the primitives it should return a wrapper class with default value, else return null.

public <B> Object defaultValue(Class<B> clazz) {
    return clazz.isPrimitive() ? ... : null;
}

So for int.class it should return Integer(0).

Upvotes: 2

Views: 1992

Answers (4)

Pshemo
Pshemo

Reputation: 124225

How do I write a function which returns a default value for the given class? If a class is one of the primitives it should return a wrapper class with default value, else return null.

So I assume that for primitives like int.class you want to return new Integer(0), but for lets say Objects you want to return null.

If that is true you can use fact that arrays are filled with default values at start. Try maybe this way

import java.lang.reflect.Array;
...

public static <B> Object defaultValue(Class<B> clazz) {
    return Array.get(Array.newInstance(clazz, 1),0);
}
  • Array.newInstance(clazz, 1) will create array of type clazz with one default element,
  • Array.get(<someArray>, 0) will return its first element.
  • also since your method defaultValue returns Object it will be autoboxed to corresponding type int->Integer.

Example

System.out.println(defaultValue(int.class));
System.out.println(defaultValue(int.class).getClass());

prints

0
class java.lang.Integer

Upvotes: 3

phoenix7360
phoenix7360

Reputation: 2907

You can use the ClassUtils class of the Apache lang3 library:

import org.apache.commons.lang3.ClassUtils;

public Object defaultValue(Class<?> clazz) throws InstantiationException, IllegalAccessException,
        IllegalArgumentException, InvocationTargetException {
    final Class<?> c = ClassUtils.primitiveToWrapper(clazz);
    if (c != null && !c.equals(clazz)) {
        final Constructor[] ctors = c.getConstructors();
        Constructor ctor = null;
        for (int i = 0; i < ctors.length; i++) {
            ctor = ctors[i];
            if (ctor.getGenericParameterTypes().length == 0)
            break;
        }
        return ctor.newInstance(0);
    } else {
        return null;
    }
}

It returns a null if the given class is not a primitive. You might want to handle the exception in a better way.

Upvotes: 1

Daniel Pereira
Daniel Pereira

Reputation: 2785

One way of achieve it is using a map to store the values:

private static Map<Class, Object> defaultValues = new HashMap<Class, Object>();

static {
    defaultValues.put(byte.class, 0);
    defaultValues.put(short.class, 0);
    defaultValues.put(int.class, 0);
    defaultValues.put(long.class, 0L);
    defaultValues.put(float.class, 0.0f);
    defaultValues.put(double.class, 0.0d);
    defaultValues.put(char.class, '\u0000');
    defaultValues.put(boolean.class, false);
}

public <B> Object defaultValue(Class<B> clazz) {
    return defaultValues.containsKey(clazz) ? defaultValues.get(clazz) : null;
}

Upvotes: 1

durron597
durron597

Reputation: 32323

Why do you need this? Defaulting is done by the compiler, not by calling existing methods.

Here are the default values for primitives, you could certainly write a function that turns this table into values...

Data Type   Default Value (for fields)
byte        0
short       0
int         0
long        0L
float       0.0f
double      0.0d
char        '\u0000'
Object      null
boolean     false

Read this tutorial link

Upvotes: -1

Related Questions