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.ejisto.modules.web.util.FieldSerializationUtil.java

private static MockedField translateField(Field field, Object container, String className, String contextPath) {
    Class<?> fieldType = field.getType();
    MockedField out = buildMockedField(className, field.getName(), contextPath, fieldType.getName());
    Object value = safeGet(field, container);
    if (value == null || isBlackListed(fieldType)) {
        out.setFieldValue(null);/*www .  j a va  2 s.  com*/
        return out;
    }
    int hashCode = System.identityHashCode(value);
    String existingClassMapping = CACHED_IDS.putIfAbsent(hashCode, out.getFieldType());
    if (StringUtils.isNotBlank(existingClassMapping)) {
        out.setLink(String.valueOf(hashCode));
        return out;
    } else if (field.isEnumConstant() || fieldType.isPrimitive()) {
        out.setFieldValue(String.valueOf(value));
    } else {
        out.setExpression(translateValue(value));
    }
    out.setRecordedObjectHashCode(hashCode);
    return out;
}

From source file:com.bstek.dorado.data.entity.EntityUtils.java

/**
 * ???/*w  w w . j  ava 2s.c  o m*/
 */
public static boolean isSimpleType(Class<?> cl) {
    boolean b = (String.class.equals(cl) || cl.isPrimitive() || Boolean.class.equals(cl)
            || Number.class.isAssignableFrom(cl) || cl.isEnum() || Date.class.isAssignableFrom(cl)
            || Character.class.isAssignableFrom(cl));
    if (!b && cl.isArray()) {
        b = isSimpleType(cl.getComponentType());
    }
    return b;
}

From source file:MultiMap.java

public static Object toArray(Object list, Class aClass) {
    if (list == null)
        return (Object[]) Array.newInstance(aClass, 0);

    if (list instanceof List) {
        List l = (List) list;
        if (aClass.isPrimitive()) {
            Object a = Array.newInstance(aClass, l.size());
            for (int i = 0; i < l.size(); i++)
                Array.set(a, i, l.get(i));
            return a;
        }/*from  ww  w .ja  va 2  s . c  om*/
        return l.toArray((Object[]) Array.newInstance(aClass, l.size()));

    }

    Object a = Array.newInstance(aClass, 1);
    Array.set(a, 0, list);
    return a;
}

From source file:com.sm.query.utils.QueryUtils.java

public static Object getParamArg(Class<?> cl) {
    if (!cl.isPrimitive())
        return null;
    else if (boolean.class.equals(cl))
        return Boolean.FALSE;
    else if (byte.class.equals(cl))
        return new Byte((byte) 0);
    else if (short.class.equals(cl))
        return new Short((short) 0);
    else if (char.class.equals(cl))
        return new Character((char) 0);
    else if (int.class.equals(cl))
        return Integer.valueOf(0);
    else if (long.class.equals(cl))
        return Long.valueOf(0);
    else if (float.class.equals(cl))
        return Float.valueOf(0);
    else if (double.class.equals(cl))
        return Double.valueOf(0);
    else/*from w w  w .jav  a2s.  co m*/
        throw new UnsupportedOperationException();
}

From source file:com.holonplatform.core.internal.beans.DefaultBeanPropertySet.java

/**
 * Get the value to write on bean property, converting <code>null</code> values to <code>false</code> or
 * <code>0</code> for primitive types.
 * @param valueType Value type/*  w w w .j av  a  2 s  . com*/
 * @param value Value to write
 * @return Actual value
 */
private static Object getValueToWrite(Class<?> valueType, Object value) {
    if (value == null) {
        // check primitive types
        if (valueType != null && valueType.isPrimitive()) {
            if (boolean.class == valueType) {
                return false;
            }
            return 0;
        }
    }
    return value;
}

From source file:at.ac.tuwien.infosys.jcloudscale.utility.ReflectionUtil.java

private static boolean isByRef(Class<?> param, Annotation[] annotations) {
    // if parameter has ByValue annotation, it is ByValueParameter
    if (containsAnnotation(annotations, ByValueParameter.class))
        return false;

    // if this is primitive or wrapper type, it is ByValueParameter
    if (param.isPrimitive() || param.equals(String.class) || Primitives.allWrapperTypes().contains(param))
        return false;

    // if this is enum, it is ByValueParameter
    if (param.isEnum())
        return false;

    // if parameter class has ByValueParameter annotation, it is ByValueParameter
    if (param.isAnnotationPresent(ByValueParameter.class))
        return false;

    // otherwise it is ByRefParameter
    return true;/*w  w w  .j a  v  a 2  s.co m*/
}

From source file:com.feilong.core.lang.ClassUtilTest.java

/**
 *  class info map for LOGGER./*from w  w  w .jav a 2s  .  c o  m*/
 *
 * @param klass
 *            the klass
 * @return  <code>klass</code> nullempty, {@link Collections#emptyMap()}<br>
 */
public static Map<String, Object> getClassInfoMapForLog(Class<?> klass) {
    if (isNullOrEmpty(klass)) {
        return Collections.emptyMap();
    }

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

    map.put("clz.getCanonicalName()", klass.getCanonicalName());//"com.feilong.core.date.DatePattern"
    map.put("clz.getName()", klass.getName());//"com.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:com.sunchenbin.store.feilong.core.bean.ConvertUtil.java

/**
 * ? Primitive./*  ww w . ja v  a  2 s. c o m*/
 *
 * @param o
 *            the o
 * @return true, if checks if is primitive array
 * 
 * @since 1.4.0
 */
private static boolean isPrimitiveArray(Object o) {
    // Allocate a new Array
    Class<? extends Object> klass = o.getClass();

    if (!klass.isArray()) {
        return false;
    }

    Class<?> componentType = klass.getComponentType();
    //
    return componentType.isPrimitive();
}

From source file:de.dfki.iui.mmds.dialogue.SiamContext.java

private static <T> Class<T> wrap(Class<T> c) {
    if (PRIMITIVES_TO_WRAPPERS.isEmpty()) {
        PRIMITIVES_TO_WRAPPERS.put(boolean.class, Boolean.class);
        PRIMITIVES_TO_WRAPPERS.put(byte.class, Byte.class);
        PRIMITIVES_TO_WRAPPERS.put(char.class, Character.class);
        PRIMITIVES_TO_WRAPPERS.put(double.class, Double.class);
        PRIMITIVES_TO_WRAPPERS.put(float.class, Float.class);
        PRIMITIVES_TO_WRAPPERS.put(int.class, Integer.class);
        PRIMITIVES_TO_WRAPPERS.put(long.class, Long.class);
        PRIMITIVES_TO_WRAPPERS.put(short.class, Short.class);
        PRIMITIVES_TO_WRAPPERS.put(void.class, Void.class);
    }//from ww w  .j ava  2 s.  c  o  m
    return c.isPrimitive() ? (Class<T>) PRIMITIVES_TO_WRAPPERS.get(c) : c;
}

From source file:com.tmind.framework.pub.utils.MethodUtils.java

/**
 * Find a non primitive representation for given primitive class.
 *
 * @param clazz the class to find a representation for, not null
 * @return the original class if it not a primitive. Otherwise the wrapper class. Not null
 *//*from  w  w  w. j  av  a 2s. c  o  m*/
public static Class toNonPrimitiveClass(Class clazz) {
    if (clazz.isPrimitive()) {
        Class primitiveClazz = MethodUtils.getPrimitiveWrapper(clazz);
        // the above method returns
        if (primitiveClazz != null) {
            return primitiveClazz;
        } else {
            return clazz;
        }
    } else {
        return clazz;
    }
}