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.browseengine.bobo.serialize.JSONSerializer.java

private static void dumpObject(JSONArray jsonArray, Class type, Object array, int index)
        throws JSONSerializationException, JSONException {
    if (type.isPrimitive()) {
        jsonArray.put(String.valueOf(Array.get(array, index)));
    } else if (type == String.class) {
        String val = (String) Array.get(array, index);
        if (val != null) {
            jsonArray.put(val);
        }/*from   w  w  w  . j a v a  2s . c  om*/
    } else if (JSONSerializable.class.isAssignableFrom(type)) {
        JSONSerializable o = (JSONSerializable) Array.get(array, index);
        JSONObject jobj = serializeJSONObject(o);
        jsonArray.put(jobj);
    } else if (type.isArray() && array != null) {
        Class compType = type.getComponentType();
        Object subArray = Array.get(array, index);
        int len = Array.getLength(subArray);
        JSONArray arr = new JSONArray();
        for (int k = 0; k < len; ++k) {
            dumpObject(arr, compType, subArray, k);
        }
        jsonArray.put(arr);
    }
}

From source file:com.expressui.core.util.ReflectionUtil.java

/**
 * Creates a default value for a primitive type, false for boolean and 0 for everything else.
 *
 * @param primitiveType primitive type to create default value for
 * @return primitive wrapper with default value
 *///w w w .jav a  2s .  c om
public static Object createDefaultPrimitiveValue(Class primitiveType) {
    Assert.PROGRAMMING.isTrue(primitiveType.isPrimitive(), "argument must be a primitive type");
    if (isBooleanType(primitiveType)) {
        return false;
    } else {
        return 0;
    }
}

From source file:com.wcs.base.util.ArrayUtils.java

/**
 *
 * @param c1//from w  w  w  .  j av a  2s .  co m
 * @param c2
 * @return
 *
 */
public static Class commonClass(Class c1, Class c2) {
    if (c1 == c2) {
        return c1;
    }

    if ((c1 == Object.class) || c1.isAssignableFrom(c2)) {
        return c1;
    }

    if (c2.isAssignableFrom(c1)) {
        return c2;
    }

    if (c1.isPrimitive() || c2.isPrimitive()) {
        throw new IllegalArgumentException("incompatible types " + c1 + " and " + c2);
    }

    return Object.class;
}

From source file:com.alibaba.doris.admin.service.impl.ValueParseUtil.java

/**
 * ?String ?:/*  www .  j av  a 2 s .  com*/
 * 
 * <pre>
 * short, int, long, float : 0
 * char, byte: 0
 * String: null
 * Map, List: null
 * Integer, Long, Float : null
 * Date: null
 * array: null
 * </pre>
 * 
 * @param strValue
 * @param clazz
 * @return
 */
@SuppressWarnings("unchecked")
public static <T> T parseStringValue(String strValue, Class<T> clazz, boolean autoDefault) {

    if (DEF_NULL.equals(strValue)) {
        if (!clazz.isPrimitive()) {
            return null;
        }
        if (autoDefault) {
            return (T) getInternalDefaultValue(clazz);
        } else {
            return null;
        }
    }

    if (DEF_EMPTY.equals(strValue)) {
        if (clazz.isArray()) {
            return (T) Array.newInstance(clazz.getComponentType(), 0);
        }

        if (Map.class.isAssignableFrom(clazz)) {
            return (T) Collections.EMPTY_MAP;
        }

        if (List.class.isAssignableFrom(clazz)) {
            return (T) new ArrayList<Object>();
        }

        if (Set.class.isAssignableFrom(clazz)) {
            return (T) new HashSet<Object>();
        }

        if (String.class.equals(clazz)) {
            return (T) StringUtils.EMPTY;
        }

        if (Character.TYPE.equals(clazz) || Character.class.equals(clazz)) {
            return (T) Character.valueOf(' ');
        }

        if (autoDefault) {
            return (T) getInternalDefaultValue(clazz);
        } else {
            return null;
        }
    }

    if (StringUtils.isBlank(strValue)) {// ?
        if (autoDefault) {
            return (T) getInternalDefaultValue(clazz);
        } else {
            return null;
        }
    } else {

        if (String.class.equals(clazz)) {
            return (T) strValue;
        }

        if (Short.TYPE.equals(clazz) || Short.class.equals(clazz)) {
            return (T) Short.valueOf(strValue);
        }

        if (Integer.TYPE.equals(clazz) || Integer.class.equals(clazz)) {
            return (T) Integer.valueOf(strValue);
        }
        if (Long.TYPE.equals(clazz) || Long.class.equals(clazz)) {
            return (T) Long.valueOf(strValue);
        }
        if (Boolean.TYPE.equals(clazz) || Boolean.class.equals(clazz)) {
            return (T) Boolean.valueOf(strValue);
        }
        if (Float.TYPE.equals(clazz) || Float.class.equals(clazz)) {
            return (T) Float.valueOf(strValue);
        }
        if (Double.TYPE.equals(clazz) || Double.class.equals(clazz)) {
            return (T) Double.valueOf(strValue);
        }
        if (Byte.TYPE.equals(clazz) || Byte.class.equals(clazz)) {
            return (T) Byte.valueOf(strValue);
        }
        if (Character.TYPE.equals(clazz) || Character.class.equals(clazz)) {
            return (T) Character.valueOf(strValue.charAt(0));
        }

        if (clazz.isArray()) {
            final Class<?> componentType = clazz.getComponentType();
            // String[]
            if (String.class.equals(componentType)) {
                return (T) StringUtils.split(strValue, ',');
            }
            // ?char[]
            if (Character.TYPE.equals(componentType)) {
                return (T) strValue.toCharArray();
            }

            if (Character.class.equals(componentType)) {
                final char[] tmp = strValue.toCharArray();
                final Character[] result = new Character[tmp.length];
                for (int i = 0; i < result.length; i++) {
                    result[i] = tmp[i];
                }
                return (T) result;
            }

            if (Byte.TYPE.equals(componentType) || Byte.class.equals(componentType)) {
                return (T) (strValue == null ? null : strValue.getBytes());
            }
        }
    }

    return null;

}

From source file:net.servicefixture.util.ReflectionUtils.java

public static boolean isLowestLevelType(Class type) {
    type = CollectionBuilderFactory.getElementType(type);
    if (type.isPrimitive() || type.getName().startsWith("java.") || type.getName().startsWith("javax.")
            || isEnumarationPatternClass(type) || type.isEnum()) {
        return true;
    }/*from w w w  . j  a  va  2s. c o m*/
    return false;
}

From source file:com.amazonaws.services.dynamodbv2.datamodeling.DynamoDbPropertyMarshaller.java

public static ScalarAttributeType getAttributeType(final Class<?> propertyClass) {
    if (propertyClass == ByteBuffer.class || propertyClass == byte[].class) {
        return ScalarAttributeType.B;
    } else if (char.class.isAssignableFrom(propertyClass)) {
        return ScalarAttributeType.S;
    } else if (propertyClass.isPrimitive() || Number.class.isAssignableFrom(propertyClass)
            || Boolean.class.isAssignableFrom(propertyClass)) {
        return ScalarAttributeType.N;
    } else {/*from ww w. j  a v a 2 s .  co m*/
        return ScalarAttributeType.S;
    }
}

From source file:com.initialxy.cordova.themeablebrowser.ThemeableBrowserUnmarshaller.java

/**
 * Checks if given class is one of the primitive types or more importantly,
 * one of the classes associated with a primitive type. eg. Integer, Double
 * etc./*from  ww  w  .  j  a  v  a  2  s  .c o  m*/
 *
 * @param cls
 * @return
 */
private static boolean isPrimitive(Class<?> cls) {
    return cls.isPrimitive() || cls.isAssignableFrom(Byte.class) || cls.isAssignableFrom(Short.class)
            || cls.isAssignableFrom(Integer.class) || cls.isAssignableFrom(Long.class)
            || cls.isAssignableFrom(Float.class) || cls.isAssignableFrom(Double.class)
            || cls.isAssignableFrom(Boolean.class) || cls.isAssignableFrom(Character.class);
}

From source file:net.sf.morph.util.TestUtils.java

/**
 * Return a "not-same" instance./*from   ww w. jav a 2 s .c o  m*/
 * @param type
 * @param o
 * @return
 */
public static Object getDifferentInstance(Class type, Object o) {
    if (type == null) {
        throw new IllegalArgumentException("Non-null type must be specified");
    }
    if (type.isPrimitive()) {
        type = ClassUtils.getPrimitiveWrapper(type);
    }
    if (o != null && !type.isInstance(o)) {
        throw new IllegalArgumentException("Negative example object should be of type " + type);
    }
    if (type == Number.class) {
        type = Integer.class;
    }
    if (Number.class.isAssignableFrom(type)) {
        byte b = (byte) (o == null ? 0 : ((Number) o).byteValue() + 1);
        try {
            return type.getConstructor(ONE_STRING).newInstance(new Object[] { Byte.toString(b) });
        } catch (Exception e) {
            throw e instanceof RuntimeException ? (RuntimeException) e : new NestableRuntimeException(e);
        }
    }
    if (type == Character.class) {
        char c = (char) (o == null ? 0 : ((Character) o).charValue() + 1);
        return new Character(c);
    }
    if (type == Boolean.class) {
        return o != null && ((Boolean) o).booleanValue() ? Boolean.FALSE : Boolean.TRUE;
    }
    if (type.isArray()) {
        return Array.newInstance(type.getComponentType(), 0);
    }
    if (type == Class.class) {
        return o == Object.class ? Class.class : Object.class;
    }
    return ClassUtils.newInstance(convertCommonInterfaces(type));
}

From source file:com.browseengine.bobo.serialize.JSONSerializer.java

private static void loadObject(Object array, Class type, int index, JSONArray jsonArray)
        throws JSONSerializationException, JSONException {
    if (type.isPrimitive()) {
        if (type == Integer.TYPE) {
            Array.setInt(array, index, jsonArray.getInt(index));
        } else if (type == Long.TYPE) {
            Array.setLong(array, index, jsonArray.getInt(index));
        } else if (type == Short.TYPE) {
            Array.setShort(array, index, (short) jsonArray.getInt(index));
        } else if (type == Boolean.TYPE) {
            Array.setBoolean(array, index, jsonArray.getBoolean(index));
        } else if (type == Double.TYPE) {
            Array.setDouble(array, index, jsonArray.getDouble(index));
        } else if (type == Float.TYPE) {
            Array.setFloat(array, index, (float) jsonArray.getDouble(index));
        } else if (type == Character.TYPE) {
            char ch = jsonArray.getString(index).charAt(0);
            Array.setChar(array, index, ch);
        } else if (type == Byte.TYPE) {
            Array.setByte(array, index, (byte) jsonArray.getInt(index));
        } else {//from ww w .ja  v a  2  s  .com
            throw new JSONSerializationException("Unknown primitive: " + type);
        }
    } else if (type == String.class) {
        Array.set(array, index, jsonArray.getString(index));
    } else if (JSONSerializable.class.isAssignableFrom(type)) {
        JSONObject jObj = jsonArray.getJSONObject(index);
        JSONSerializable serObj = deSerialize(type, jObj);
        Array.set(array, index, serObj);
    } else if (type.isArray()) {
        Class componentClass = type.getComponentType();
        JSONArray subArray = jsonArray.getJSONArray(index);
        int len = subArray.length();

        Object newArray = Array.newInstance(componentClass, len);
        for (int k = 0; k < len; ++k) {
            loadObject(newArray, componentClass, k, subArray);
        }
    }
}

From source file:net.servicefixture.util.ReflectionUtils.java

public static boolean isAbstractType(Class type) {
    return (type.isInterface() || Modifier.isAbstract(type.getModifiers())) && !type.isPrimitive();
}