Example usage for java.lang Class isPrimitive

List of usage examples for java.lang Class isPrimitive

Introduction

In this page you can find the example usage for java.lang Class isPrimitive.

Prototype

@HotSpotIntrinsicCandidate
public native boolean isPrimitive();

Source Link

Document

Determines if the specified Class object represents a primitive type.

Usage

From source file:com.adobe.acs.commons.mcp.util.AnnotatedFieldDeserializer.java

private static Object convertValue(String value, Class<?> type) throws ParseException {
    Class clazz = type.isArray() ? type.getComponentType() : type;
    if (clazz.isPrimitive() || Number.class.isAssignableFrom(clazz) || clazz == Boolean.class) {
        return convertPrimitiveValue(value, clazz);
    } else if (clazz == String.class) {
        return value;
    } else if (clazz.isEnum()) {
        return Enum.valueOf((Class<Enum>) clazz, value);
    }// w  w  w.j  a  va2  s  .c  o m

    return null;
}

From source file:Main.java

/**
 * Force the given class to be loaded fully.
 * /*from  w w w.  ja v  a  2s  .c  o m*/
 * <p>
 * This method attempts to locate a static method on the given class the
 * attempts to invoke it with dummy arguments in the hope that the virtual
 * machine will prepare the class for the method call and call all of the
 * static class initializers.
 * 
 * @param type
 *          Class to force load.
 * 
 * @throws NullArgumentException
 *           Type is <i>null</i>.
 */
public static void forceLoad(final Class type) {
    if (type == null)
        System.out.println("Null Argument Exception");

    // don't attempt to force primitives to load
    if (type.isPrimitive())
        return;

    // don't attempt to force java.* classes to load
    String packageName = Classes.getPackageName(type);
    // System.out.println("package name: " + packageName);

    if (packageName.startsWith("java.") || packageName.startsWith("javax.")) {
        return;
    }

    // System.out.println("forcing class to load: " + type);

    try {
        Method methods[] = type.getDeclaredMethods();
        Method method = null;
        for (int i = 0; i < methods.length; i++) {
            int modifiers = methods[i].getModifiers();
            if (Modifier.isStatic(modifiers)) {
                method = methods[i];
                break;
            }
        }

        if (method != null) {
            method.invoke(null, (Object[]) null);
        } else {
            type.newInstance();
        }
    } catch (Exception ignore) {
        ignore.printStackTrace();
    }
}

From source file:com.robertsmieja.test.utils.junit.GenericObjectFactory.java

protected static Class<?> convertPrimitiveToWrapperOrReturn(Class<?> primitiveClass) {
    if (primitiveClass.isPrimitive()) {
        return ClassUtils.primitiveToWrapper(primitiveClass);
    }// w  ww.j  a va  2  s .  c o m
    return primitiveClass;
}

From source file:net.femtoparsec.jnlmin.utils.ReflectUtils.java

private static Class<?> boxClass(Class<?> clazz) {
    if (clazz.isPrimitive()) {
        return Validate.notNull(PRIMITIVE_TO_BOXED_MAP.get(clazz));
    }/* ww w .  j  a  va  2 s .  c  o  m*/
    return clazz;
}

From source file:Main.java

private static boolean isSimpleObjectClass(Class<?> objectClass) {
    if (objectClass.isArray()) {
        return isSimpleObjectClass(objectClass.getComponentType());
    } else {//w  w  w. java  2s.c om
        return objectClass.isPrimitive() || CharSequence.class.isAssignableFrom(objectClass)
                || Character.class.isAssignableFrom(objectClass) || Boolean.class.isAssignableFrom(objectClass)
                || Number.class.isAssignableFrom(objectClass);
    }
}

From source file:com.amazonaws.hal.client.ConversionUtil.java

private static Object convertFromNull(Type type) {
    if (!(type instanceof Class)) {
        return null;
    }/*from   w w w.jav  a 2s  .  com*/

    Class<?> clazz = (Class) type;

    if (!clazz.isPrimitive()) {
        return null;
    }

    if (int.class.isAssignableFrom(clazz)) {
        return 0;
    } else if (long.class.isAssignableFrom(clazz)) {
        return 0L;
    } else if (short.class.isAssignableFrom(clazz)) {
        return 0;
    } else if (double.class.isAssignableFrom(clazz)) {
        return 0.0;
    } else if (float.class.isAssignableFrom(clazz)) {
        return 0.0F;
    } else if (boolean.class.isAssignableFrom(clazz)) {
        return Boolean.FALSE;
    } else if (char.class.isAssignableFrom(clazz)) {
        return 0;
    } else if (byte.class.isAssignableFrom(clazz)) {
        return 0;
    } else {
        throw new RuntimeException("Unexpected primitive type: " + clazz.getSimpleName());
    }
}

From source file:de.pribluda.android.jsonmarshaller.JSONMarshaller.java

/**
 * recursively marshall to JSON/* w  w w  .  ja  v  a  2s. com*/
 *
 * @param sink
 * @param object
 */
static void marshallRecursive(JSONObject sink, Object object)
        throws JSONException, InvocationTargetException, IllegalAccessException, NoSuchMethodException {
    // nothing to marshall
    if (object == null)
        return;
    // primitive object is a field and does not interes us here
    if (object.getClass().isPrimitive())
        return;
    // object not null,  and is not primitive - iterate through getters
    for (Method method : object.getClass().getMethods()) {
        // our getters are parameterless and start with "get"
        if ((method.getName().startsWith(GETTER_PREFIX) && method.getName().length() > BEGIN_INDEX
                || method.getName().startsWith(IS_PREFIX) && method.getName().length() > IS_LENGTH)
                && (method.getModifiers() & Modifier.PUBLIC) != 0 && method.getParameterTypes().length == 0
                && method.getReturnType() != void.class) {
            // is return value primitive?
            Class<?> type = method.getReturnType();
            if (type.isPrimitive() || String.class.equals(type)) {
                // it is, marshall it
                Object val = method.invoke(object);
                if (val != null) {
                    sink.put(propertize(method.getName()), val);
                }
                continue;
            } else if (type.isArray()) {
                Object val = marshallArray(method.invoke(object));
                if (val != null) {
                    sink.put(propertize(method.getName()), val);
                }
                continue;
            } else if (type.isAssignableFrom(Date.class)) {
                Date date = (Date) method.invoke(object);
                if (date != null) {
                    sink.put(propertize(method.getName()), date.getTime());
                }
                continue;
            } else if (type.isAssignableFrom(Boolean.class)) {
                Boolean b = (Boolean) method.invoke(object);
                if (b != null) {
                    sink.put(propertize(method.getName()), b.booleanValue());
                }
                continue;
            } else if (type.isAssignableFrom(Integer.class)) {
                Integer i = (Integer) method.invoke(object);
                if (i != null) {
                    sink.put(propertize(method.getName()), i.intValue());
                }
                continue;
            } else if (type.isAssignableFrom(Long.class)) {
                Long l = (Long) method.invoke(object);
                if (l != null) {
                    sink.put(propertize(method.getName()), l.longValue());
                }
                continue;
            } else {
                // does it have default constructor?
                try {
                    if (method.getReturnType().getConstructor() != null) {
                        Object val = marshall(method.invoke(object));
                        if (val != null) {
                            sink.put(propertize(method.getName()), val);
                        }
                        continue;
                    }
                } catch (NoSuchMethodException ex) {
                    // just ignore it here, it means no such constructor was found
                }
            }
        }
    }
}

From source file:nl.strohalm.cyclos.utils.PropertyHelper.java

/**
 * Converts a String into an Object with the given converter
 *//*from  w  w  w  . j  a  v a2 s.c om*/
public static <T> T getAsObject(final Class<T> toType, final String value, final Converter<T> converter) {
    if (StringUtils.isEmpty(value) && !toType.isPrimitive()) {
        return null;
    }
    Object ret = null;
    if (converter != null) {
        ret = converter.valueOf(value);
    } else {
        ret = value;
    }
    return CoercionHelper.coerce(toType, ret);
}

From source file:de.odysseus.calyxo.base.util.ParseUtils.java

private static Object nullValue(Class type) {
        if (type.isPrimitive()) {
            if (type == boolean.class)
                return Boolean.FALSE;
            if (type == byte.class)
                return new Byte((byte) 0);
            if (type == char.class)
                return new Character((char) 0);
            if (type == short.class)
                return new Short((short) 0);
            if (type == int.class)
                return new Integer(0);
            if (type == long.class)
                return new Long(0);
            if (type == float.class)
                return new Float(0);
            if (type == double.class)
                return new Double(0);
        }//from w w w  .ja  v a 2 s  .  c  o m
        return null;
    }

From source file:de.odysseus.calyxo.base.util.ParseUtils.java

private static Class objectType(Class type) {
        if (type.isPrimitive()) {
            if (type == boolean.class)
                return Boolean.class;
            if (type == byte.class)
                return Byte.class;
            if (type == char.class)
                return Character.class;
            if (type == short.class)
                return Short.class;
            if (type == int.class)
                return Integer.class;
            if (type == long.class)
                return Long.class;
            if (type == float.class)
                return Float.class;
            if (type == double.class)
                return Double.class;
        }//from   ww  w. j a  va2 s  . c  o  m
        return type;
    }