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:org.codhaus.groovy.grails.validation.NullableConstraint.java

@SuppressWarnings("rawtypes")
public boolean supports(Class type) {
    return type != null && !type.isPrimitive();
}

From source file:com.ms.commons.summer.web.util.json.JsonBeanUtils.java

private static Object convertPropertyValueToArray(String key, Object value, Class targetType,
        JsonConfig jsonConfig, Map classMap) {
    Class innerType = JSONUtils.getInnerComponentType(targetType);
    Class targetInnerType = findTargetClass(key, classMap);
    if (innerType.equals(Object.class) && targetInnerType != null && !targetInnerType.equals(Object.class)) {
        innerType = targetInnerType;//from  w  w  w.ja  v  a2 s. co m
    }
    JsonConfig jsc = jsonConfig.copy();
    jsc.setRootClass(innerType);
    jsc.setClassMap(classMap);
    Object array = JSONArray.toArray((JSONArray) value, jsc);
    if (innerType.isPrimitive() || JSONUtils.isNumber(innerType) || Boolean.class.isAssignableFrom(innerType)
            || JSONUtils.isString(innerType)) {
        array = JSONUtils.getMorpherRegistry().morph(Array.newInstance(innerType, 0).getClass(), array);
    } else if (!array.getClass().equals(targetType)) {
        if (!targetType.equals(Object.class)) {
            Morpher morpher = JSONUtils.getMorpherRegistry()
                    .getMorpherFor(Array.newInstance(innerType, 0).getClass());
            if (IdentityObjectMorpher.getInstance().equals(morpher)) {
                ObjectArrayMorpher beanMorpher = new ObjectArrayMorpher(
                        new BeanMorpher(innerType, JSONUtils.getMorpherRegistry()));
                JSONUtils.getMorpherRegistry().registerMorpher(beanMorpher);
            }
            array = JSONUtils.getMorpherRegistry().morph(Array.newInstance(innerType, 0).getClass(), array);
        }
    }
    return array;
}

From source file:com.facebook.GraphObjectWrapper.java

static <U> U coerceValueToExpectedType(Object value, Class<U> expectedType,
        ParameterizedType expectedTypeAsParameterizedType) {
    if (value == null) {
        return null;
    }/*from   ww  w.  j ava2 s .c  om*/

    Class<?> valueType = value.getClass();
    if (expectedType.isAssignableFrom(valueType)) {
        @SuppressWarnings("unchecked")
        U result = (U) value;
        return result;
    }

    if (expectedType.isPrimitive()) {
        // If the result is a primitive, let the runtime succeed or fail at unboxing it.
        @SuppressWarnings("unchecked")
        U result = (U) value;
        return result;
    }

    if (GraphObject.class.isAssignableFrom(expectedType)) {
        @SuppressWarnings("unchecked")
        Class<? extends GraphObject> graphObjectClass = (Class<? extends GraphObject>) expectedType;

        // We need a GraphObject, but we don't have one.
        if (JSONObject.class.isAssignableFrom(valueType)) {
            // We can wrap a JSONObject as a GraphObject.
            @SuppressWarnings("unchecked")
            U result = (U) createGraphObjectProxy(graphObjectClass, (JSONObject) value);
            return result;
        } else if (GraphObject.class.isAssignableFrom(valueType)) {
            // We can cast a GraphObject-derived class to another GraphObject-derived class.
            @SuppressWarnings("unchecked")
            U result = (U) ((GraphObject) value).cast(graphObjectClass);
            return result;
        } else {
            throw new FacebookGraphObjectException("Can't create GraphObject from " + valueType.getName());
        }
    } else if (Iterable.class.equals(expectedType) || Collection.class.equals(expectedType)
            || List.class.equals(expectedType) || GraphObjectList.class.equals(expectedType)) {
        if (expectedTypeAsParameterizedType == null) {
            throw new FacebookGraphObjectException("can't infer generic type of: " + expectedType.toString());
        }

        Type[] actualTypeArguments = expectedTypeAsParameterizedType.getActualTypeArguments();

        if (actualTypeArguments == null || actualTypeArguments.length != 1
                || !(actualTypeArguments[0] instanceof Class<?>)) {
            throw new FacebookGraphObjectException(
                    "Expect collection properties to be of a type with exactly one generic parameter.");
        }
        Class<?> collectionGenericArgument = (Class<?>) actualTypeArguments[0];

        if (JSONArray.class.isAssignableFrom(valueType)) {
            JSONArray jsonArray = (JSONArray) value;
            @SuppressWarnings("unchecked")
            U result = (U) wrapArray(jsonArray, collectionGenericArgument);
            return result;
        } else {
            throw new FacebookGraphObjectException("Can't create Collection from " + valueType.getName());
        }
    } else if (String.class.equals(expectedType)) {
        if (Number.class.isAssignableFrom(valueType)) {
            @SuppressWarnings("unchecked")
            U result = (U) String.format("%d", value);
            return result;
        }
    } else if (Date.class.equals(expectedType)) {
        if (String.class.isAssignableFrom(valueType)) {
            for (SimpleDateFormat format : dateFormats) {
                try {
                    Date date = format.parse((String) value);
                    if (date != null) {
                        @SuppressWarnings("unchecked")
                        U result = (U) date;
                        return result;
                    }
                } catch (ParseException e) {
                    // Keep going.
                }
            }
        }
    }
    throw new FacebookGraphObjectException(
            "Can't convert type" + valueType.getName() + " to " + expectedType.getName());
}

From source file:ca.oson.json.util.ObjectUtil.java

public static boolean isBasicDataType(Class valueType) {
    if (valueType == null) { // no idea, just assume
        return true;
    }/*from   w  w  w.  j  a  v a 2 s . c o  m*/

    if (valueType.isPrimitive() || valueType.isEnum()) {
        return true;
    }

    if (Number.class.isAssignableFrom(valueType) || Date.class.isAssignableFrom(valueType)) {
        return true;
    }

    if (valueType == String.class || valueType == Character.class || valueType == Boolean.class) {
        return true;
    }

    return false;
}

From source file:com.sunchenbin.store.feilong.core.lang.ClassUtil.java

/**
 *  class info map for LOGGER./*from w  w  w. j a va 2  s. c  o m*/
 *
 * @param klass
 *            the clz
 * @return the map for log
 */
public static Map<String, Object> getClassInfoMapForLog(Class<?> klass) {
    if (Validator.isNullOrEmpty(klass)) {
        return Collections.emptyMap();
    }

    Map<String, Object> map = new LinkedHashMap<String, Object>();

    map.put("clz.getCanonicalName()", klass.getCanonicalName());//"com.sunchenbin.store.feilong.core.date.DatePattern"
    map.put("clz.getName()", klass.getName());//"com.sunchenbin.store.feilong.core.date.DatePattern"
    map.put("clz.getSimpleName()", klass.getSimpleName());//"DatePattern"

    map.put("clz.getComponentType()", klass.getComponentType());
    // ?? ,voidboolean?byte?char?short?int?long?float  double?
    map.put("clz.isPrimitive()", klass.isPrimitive());

    // ??,
    map.put("clz.isLocalClass()", klass.isLocalClass());
    // ????,?,?????
    map.put("clz.isMemberClass()", klass.isMemberClass());

    //isSynthetic()?Class????java??false,?true,JVM???,java??????
    map.put("clz.isSynthetic()", klass.isSynthetic());
    map.put("clz.isArray()", klass.isArray());
    map.put("clz.isAnnotation()", klass.isAnnotation());

    //??true
    map.put("clz.isAnonymousClass()", klass.isAnonymousClass());
    map.put("clz.isEnum()", klass.isEnum());

    return map;
}

From source file:ca.oson.json.util.ObjectUtil.java

public static boolean isObject(Class valueType) {
    if (valueType == null) { // no idea, just assume
        return false;
    }/*from  w ww  .  ja va2s .  c  o m*/

    if (valueType.isPrimitive() || valueType.isEnum()) {
        return false;
    }

    if (Number.class.isAssignableFrom(valueType) || Date.class.isAssignableFrom(valueType)) {
        return false;
    }

    if (Map.class.isAssignableFrom(valueType)) {
        return false;
    }

    if (isArrayOrCollection(valueType)) {
        return false;
    }

    if (valueType == String.class || valueType == Character.class || valueType == Boolean.class) {
        return false;
    }

    return true;
}

From source file:pl.bristleback.server.bristle.conf.resolver.action.ParameterResolver.java

private Class getParameterClass(Class parameterClass) {
    if (parameterClass.isPrimitive()) {
        return ReflectionUtils.getWrapperClassForPrimitive(parameterClass);
    } else {//from www.j  ava 2  s . c  o  m
        return parameterClass;
    }
}

From source file:ca.oson.json.util.ObjectUtil.java

public static boolean isSameType(Class aType, Class bType) {
    if (aType == null || bType == null) {
        return false;
    }/*  w w  w  .j a v  a  2  s  .c om*/

    if (aType.isPrimitive() || bType.isPrimitive()) {
        if (aType.isPrimitive()) {
            if (bType.isPrimitive()) {
                return (aType == bType);
            }

        } else {
            Class c = bType;
            bType = aType;
            aType = c;
        }

        if (aType == int.class && bType == Integer.class) {
            return true;
        } else if (aType == long.class && bType == Long.class) {
            return true;
        } else if (aType == byte.class && bType == Byte.class) {
            return true;
        } else if (aType == double.class && bType == Double.class) {
            return true;
        } else if (aType == short.class && bType == Short.class) {
            return true;
        } else if (aType == float.class && bType == Float.class) {
            return true;
        } else if (aType == char.class && bType == Character.class) {
            return true;
        } else if (aType == boolean.class && bType == Boolean.class) {
            return true;
        }

    } else if (aType.isAssignableFrom(bType) || bType.isAssignableFrom(aType)) {
        return true;
    }

    return false;
}

From source file:jef.tools.ArrayUtils.java

/**
 * ?/*from   www. j  a  va  2s .  com*/
 * 
 * @param obj
 * @return
 */
public static Object[] toObject(Object obj) {
    Class<?> c = obj.getClass();
    Assert.isTrue(c.isArray());
    Class<?> priType = c.getComponentType();
    if (!priType.isPrimitive())
        return (Object[]) obj;
    if (priType == Boolean.TYPE) {
        return toObject((boolean[]) obj);
    } else if (priType == Byte.TYPE) {
        return toObject((byte[]) obj);
    } else if (priType == Character.TYPE) {
        return toObject((char[]) obj);
    } else if (priType == Integer.TYPE) {
        return toObject((int[]) obj);
    } else if (priType == Long.TYPE) {
        return toObject((long[]) obj);
    } else if (priType == Float.TYPE) {
        return toObject((float[]) obj);
    } else if (priType == Double.TYPE) {
        return toObject((double[]) obj);
    } else if (priType == Short.TYPE) {
        return toObject((short[]) obj);
    }
    throw new IllegalArgumentException();
}

From source file:com.kixeye.chassis.transport.http.SerDeHttpMessageConverter.java

/**
 * Whether we support this class./*w w  w.  j  a v a 2  s  .  c o m*/
 * 
 * @param clazz
 * @return
 */
protected boolean supports(Class<?> clazz) {
    return !(clazz.isPrimitive() || byte[].class.equals(clazz) || ByteBuffer.class.equals(clazz)
            || String.class.equals(clazz) || InputStream.class.equals(clazz) || Resource.class.equals(clazz));
}