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:jp.furplag.util.JSONifier.java

/**
 * create the instance of specified class represented by the JSON String. Throw exceptions if convert has failed.
 *
 * @param str JSON String./*w  w  w.  j av  a 2  s  . co m*/
 * @param type destination Class.
 * @return the instance of specified Class.
 * @throws JsonParseException
 * @throws JsonMappingException
 * @throws IOException
 */
public static <T> T parse(final String str, final Class<T> type)
        throws JsonParseException, JsonMappingException, IOException {
    if (str == null)
        return type == null ? null : type.isPrimitive() ? MAPPER.readValue("", type) : null;

    return MAPPER.readValue(str, type);
}

From source file:com.paladin.sys.db.QueryHelper.java

private final static boolean IS_PRIMITIVE(Class<?> cls) {
    return cls.isPrimitive() || PRIMITIVE_CLASSES.contains(cls);
}

From source file:cop.raml.mocks.MockUtils.java

public static TypeElementMock createElement(@NotNull Class<?> cls) throws ClassNotFoundException {
    if (cls.isPrimitive())
        return createPrimitiveElement(cls);
    if (cls.isEnum())
        return createEnumElement(cls);
    if (cls.isArray())
        return setAnnotations(createArrayElement(cls.getComponentType()), cls);
    if (Collection.class.isAssignableFrom(cls))
        return setAnnotations(createCollectionElement(), cls);
    return createClassElement(cls);
}

From source file:net.buffalo.protocal.util.ClassUtil.java

public static Object convertValue(Object value, Class targetType) {

    if (value.getClass().equals(targetType))
        return value;

    if (targetType.isPrimitive()) {
        targetType = getWrapperClass(targetType);
    }/*from w  w  w  .ja  va2s  .c  o  m*/

    if (targetType.isAssignableFrom(value.getClass()))
        return value;

    if ((value instanceof String || value instanceof Number) && Number.class.isAssignableFrom(targetType)) {
        try {
            Constructor ctor = targetType.getConstructor(new Class[] { String.class });
            return ctor.newInstance(new Object[] { value.toString() });
        } catch (Exception e) {
            LOGGER.error("convert type error", e);
            throw new RuntimeException(
                    "Cannot convert from " + value.getClass().getName() + " to " + targetType, e);
        }
    }

    if (targetType.isArray() && Collection.class.isAssignableFrom(value.getClass())) {
        Collection collection = (Collection) value;
        Object array = Array.newInstance(targetType.getComponentType(), collection.size());
        int i = 0;
        for (Iterator iter = collection.iterator(); iter.hasNext();) {
            Object val = iter.next();
            Array.set(array, i++, val);
        }

        return array;
    }

    if (Collection.class.isAssignableFrom(targetType) && value.getClass().isArray()) {
        return Arrays.asList((Object[]) value);
    }

    throw new IllegalArgumentException(
            "Cannot convert from " + value.getClass().getName() + " to " + targetType);
}

From source file:hu.bme.mit.sette.tools.catg.CatgGenerator.java

private static String getTypeString(Class<?> javaClass) {
    if (javaClass.isPrimitive()) {
        javaClass = ClassUtils.primitiveToWrapper(javaClass);
    }/*from   w w  w  .  j av a2s .c o  m*/

    if (javaClass.equals(Byte.class)) {
        return "byte";
    } else if (javaClass.equals(Short.class)) {
        return "short";
    } else if (javaClass.equals(Integer.class)) {
        return "int";
    } else if (javaClass.equals(Long.class)) {
        return "long";
    } else if (javaClass.equals(Boolean.class)) {
        return "boolean";
    } else if (javaClass.equals(Character.class)) {
        return "char";
    } else if (javaClass.equals(String.class)) {
        return "String";
    } else {
        return null;
    }
}

From source file:hu.bme.mit.sette.tools.catg.CatgGenerator.java

private static String createCatgRead(Class<?> javaClass) {
    if (javaClass.isPrimitive()) {
        javaClass = ClassUtils.primitiveToWrapper(javaClass);
    }/*  ww  w. j  a  va 2s. c  o  m*/

    if (javaClass.equals(Byte.class)) {
        return "CATG.readByte((byte) 1)";
    } else if (javaClass.equals(Short.class)) {
        return "CATG.readShort((short) 1)";
    } else if (javaClass.equals(Integer.class)) {
        return "CATG.readInt(1)";
    } else if (javaClass.equals(Long.class)) {
        return "CATG.readLong(1L)";
    } else if (javaClass.equals(Boolean.class)) {
        return "CATG.readBool(false)";
    } else if (javaClass.equals(Character.class)) {
        return "CATG.readChar(' ')";
    } else if (javaClass.equals(String.class)) {
        return "CATG.readString(\"\")";
    } else {
        return null;
    }
}

From source file:edu.umich.flowfence.common.ParceledPayload.java

public static boolean canParcelType(Class<?> clazz, boolean allowVoid) {
    // All primitives and wrapper types are parcelable, except Character and Void.
    if (ClassUtils.isPrimitiveOrWrapper(clazz)) {
        return (clazz != char.class && clazz != Character.class
                && (allowVoid || (clazz != void.class && clazz != Void.class)));
    }//from   w w  w.  jav  a2  s.  c  om
    // String and CharSequence are parcelable.
    if (clazz == String.class || ClassUtils.isAssignable(clazz, CharSequence.class)) {
        return true;
    }
    // Object arrays are parcelable if their component type is parcelable.
    // Primitive boolean[], byte[], int[], and long[] arrays are parcelable.
    if (clazz.isArray()) {
        Class<?> componentType = clazz.getComponentType();
        if (componentType.isPrimitive()) {
            return (componentType == int.class || componentType == long.class || componentType == byte.class
                    || componentType == boolean.class);
        } else {
            return canParcelType(componentType, false);
        }
    }
    // Parcelable, obviously, is parcelable.
    // This covers Bundle as well.
    if (ClassUtils.isAssignable(clazz, Parcelable.class)) {
        return true;
    }
    // Map, List, and SparseArray are all parcelable, with restrictions on their component type
    // that we can't check here.
    if (ClassUtils.isAssignable(clazz, Map.class) || ClassUtils.isAssignable(clazz, List.class)
            || ClassUtils.isAssignable(clazz, SparseArray.class)) {
        return true;
    }
    // IBinder is parcelable.
    if (ClassUtils.isAssignable(clazz, IBinder.class)) {
        return true;
    }
    // Serializable is parcelable.
    if (ClassUtils.isAssignable(clazz, Serializable.class)) {
        return true;
    }
    return false;
}

From source file:kelly.util.BeanUtils.java

private static boolean isPrimitiveOrWrapper(Class<?> clazz) {
    Validate.notNull(clazz, "Class must not be null");
    return (clazz.isPrimitive() || isPrimitiveWrapper(clazz));
}

From source file:io.cloudslang.lang.entities.bindings.values.PyObjectValueProxyFactory.java

@SuppressWarnings("unchecked")
private static Object getParamDefaultValue(PyObject pyObject, Class<?> parameterType) throws Exception {
    if (parameterType.equals(PyType.class)) {
        return pyObject.getType();
    } else if (parameterType.isPrimitive()) {
        return ClassUtils.primitiveToWrapper(parameterType).getConstructor(String.class).newInstance("0");
    } else if (Number.class.isAssignableFrom(parameterType) || String.class.isAssignableFrom(parameterType)) {
        return parameterType.getConstructor(String.class).newInstance("0");
    } else {/*  w w w.ja  v a2s. co m*/
        return null;
    }
}

From source file:com.xpfriend.fixture.cast.temp.TypeConverter.java

public static Object changeType(String textValue, Class<?> type) {
    if (Strings.isEmpty(textValue) && !type.isPrimitive()) {
        return null;
    }//from   w ww .  j av a  2  s  .c  o m
    if (Object.class.equals(type)) {
        return textValue;
    }
    if (Number.class.isAssignableFrom(type)) {
        textValue = removeGroupingSeparator(textValue);
    }
    if (Date.class.isAssignableFrom(type)) {
        return convertByCustomFormat(textValue, type);
    }
    return beanUtils.getConvertUtils().convert(textValue, type);
}