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:hu.bme.mit.sette.tools.spf.SpfParser.java

static ParameterType getParameterType(Class<?> javaClass) {
    if (javaClass.isPrimitive()) {
        javaClass = ClassUtils.primitiveToWrapper(javaClass);
    }//from w  w w .  j  ava 2 s.c om

    if (javaClass.equals(Byte.class)) {
        return ParameterType.BYTE;
    } else if (javaClass.equals(Short.class)) {
        return ParameterType.SHORT;
    } else if (javaClass.equals(Integer.class)) {
        return ParameterType.INT;
    } else if (javaClass.equals(Long.class)) {
        return ParameterType.LONG;
    } else if (javaClass.equals(Float.class)) {
        return ParameterType.FLOAT;
    } else if (javaClass.equals(Double.class)) {
        return ParameterType.DOUBLE;
    } else if (javaClass.equals(Boolean.class)) {
        return ParameterType.BOOLEAN;
    } else if (javaClass.equals(Character.class)) {
        return ParameterType.CHAR;
    } else {
        // string or null
        return ParameterType.EXPRESSION;
    }
}

From source file:hu.bme.mit.sette.tools.spf.SpfParser.java

static String getDefaultParameterString(Class<?> javaClass) {
    if (javaClass.isPrimitive()) {
        javaClass = ClassUtils.primitiveToWrapper(javaClass);
    }/* ww  w.  ja va 2s .com*/

    if (javaClass.equals(Byte.class)) {
        return "1";
    } else if (javaClass.equals(Short.class)) {
        return "1";
    } else if (javaClass.equals(Integer.class)) {
        return "1";
    } else if (javaClass.equals(Long.class)) {
        return "1";
    } else if (javaClass.equals(Float.class)) {
        return "1.0";
    } else if (javaClass.equals(Double.class)) {
        return "1.0";
    } else if (javaClass.equals(Boolean.class)) {
        return "false";
    } else if (javaClass.equals(Character.class)) {
        return " ";
    } else {
        // string or null
        return "null";
    }
}

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

public static void setObjectAttribute(Object parent, String attributeName, Object source) {
    try {/* w  w  w  .  j a v  a2 s  .  co  m*/
        if (source == null) {
            Class attributeType = getAttributeType(parent, attributeName);
            if (attributeType.isPrimitive() || isPrimitiveWrapper(attributeType)) {
                // If the source is null and attribute type is primitive
                // type,don't call BeanUtils to set the property and let the
                // attribute get Java default value. BeanUtils will set it
                // to
                // its default value. For example, a Boolean attribute will
                // be
                // set to false, instead of null.
                return;
            }
        }

        setProperty(parent, attributeName, source);
    } catch (Exception e) {
        throw new ServiceFixtureException(
                "Unable to set the attribute:" + attributeName + " on object:" + parent + " as:" + source, e);
    }
}

From source file:org.lambdamatic.internal.elasticsearch.codec.DocumentCodec.java

/**
 * Converts the given {@code value} into a value of type {@code targetType}.
 * <p>/*from w  ww . ja  v a  2s.  c  om*/
 * <strong>Note:</strong>The conversion relies on the existence of a static
 * {@code valueOf(String)} method in the given {@code targetType} to convert the value.
 * </p>
 * 
 * @param value the input value
 * @param targetType the target type of the value to return
 * @return the converted value (or the value itself if no conversion was necessary)
 */
protected static Object convertValue(final Object value, final Class<?> targetType) {
    if (targetType.isAssignableFrom(value.getClass())) {
        return value;
    } else if (targetType.isPrimitive()) {
        if (targetType == boolean.class) {
            return Boolean.parseBoolean(value.toString());
        } else if (targetType == byte.class) {
            return Byte.parseByte(value.toString());
        } else if (targetType == short.class) {
            return Short.parseShort(value.toString());
        } else if (targetType == int.class) {
            return Integer.parseInt(value.toString());
        } else if (targetType == long.class) {
            return Long.parseLong(value.toString());
        } else if (targetType == double.class) {
            return Double.parseDouble(value.toString());
        } else if (targetType == float.class) {
            return Float.parseFloat(value.toString());
        }
        throw new CodecException("Failed to convert value '" + value.toString() + "' ("
                + value.getClass().getName() + ")  into a " + targetType.getName()
                + ": no object to primitive conversion available.");
    }
    try {
        final Method convertMethod = getConvertMethod(targetType);
        if (convertMethod != null) {
            return convertMethod.invoke(null, value.toString());
        }
        throw new CodecException(
                "Failed to convert value '" + value.toString() + "' (" + value.getClass().getName()
                        + ")  into a " + targetType.getName() + ": no conversion method available.");
    } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
            | SecurityException e) {
        throw new CodecException("Failed to convert value '" + value.toString() + "' ("
                + value.getClass().getName() + ") into a " + targetType.getClass().getName(), e);
    }
}

From source file:hu.bme.mit.sette.common.model.runner.ParameterType.java

public static ParameterType primitiveFromJavaClass(Class<?> javaClass) {
    Validate.notNull(javaClass, "The Java class must not be null");
    Validate.isTrue(ClassUtils.isPrimitiveOrWrapper(javaClass),
            "The represented type is not primitive [javaClass: %s]", javaClass.getName());

    Class<?> primitiveClass;
    if (javaClass.isPrimitive()) {
        primitiveClass = javaClass;/*from   ww  w . j  ava  2 s.  c om*/
    } else {
        primitiveClass = ClassUtils.wrapperToPrimitive(javaClass);
    }

    Validate.isTrue(primitiveClass != void.class, "the parameter type must not be void [javaClass: %s]",
            javaClass.getName());

    return fromString(primitiveClass.getCanonicalName());
}

From source file:io.neba.core.resourcemodels.metadata.MappedFieldMetaData.java

/**
 * Whether a property cannot be represented by a resource but must stem
 * from a value map representing the properties of a resource.
 *///from  w  ww. j  a va  2 s  . c  o m
private static boolean isPropertyType(Class<?> type) {
    return type.isPrimitive() || type == String.class || type == Date.class || type == Calendar.class
            || ClassUtils.wrapperToPrimitive(type) != null;
}

From source file:io.coala.factory.ClassUtil.java

/**
 * @param primitive/*from  ww  w. j  a v a  2  s.c  om*/
 * @param wrapper
 * @return <tt>true</tt> if x is the primitive type wrapped by y,
 *         <tt>false</tt> otherwise
 */
public static boolean isPrimitiveOf(final Class<?> primitive, final Class<?> wrapper) {
    if (!primitive.isPrimitive() || wrapper.isPrimitive())
        return false;
    // System.err.println( primitive+" =?= "+wrapper );

    if (primitive.equals(byte.class) && wrapper.equals(Byte.class))
        return true;

    if (primitive.equals(short.class) && wrapper.equals(Short.class))
        return true;

    if (primitive.equals(int.class) && wrapper.equals(Integer.class))
        return true;

    if (primitive.equals(long.class) && wrapper.equals(Long.class))
        return true;

    if (primitive.equals(double.class) && wrapper.equals(Double.class))
        return true;

    if (primitive.equals(float.class) && wrapper.equals(Float.class))
        return true;

    if (primitive.equals(boolean.class) && wrapper.equals(Boolean.class))
        return true;

    if (primitive.equals(char.class) && wrapper.equals(Character.class))
        return true;

    return false;
}

From source file:objenome.util.TypeUtil.java

/**
 * Returns the primitive class of any numeric object class (any of <code>Byte,
 * Short, Integer, Long, Float, Double</code>). If a primitive type is
 * provided then the same type is returned.
 *
 * @param type a numeric type, which is either a primitive or an object
 * @return the equivalent primitive class type
 *//*from  w  ww . j a v  a 2  s . c  o m*/
public static Class<?> getPrimitiveType(Class<?> type) {

    //noinspection IfStatementWithTooManyBranches
    if (type.isPrimitive()) {
        return type;
    } else if (Integer.class.equals(type)) {
        return int.class;
    } else if (Long.class.equals(type)) {
        return long.class;
    } else if (Float.class.equals(type)) {
        return float.class;
    } else if (Double.class.equals(type)) {
        return double.class;
    } else if (Byte.class.equals(type)) {
        return byte.class;
    } else if (Short.class.equals(type)) {
        return short.class;
    } else {
        throw new IllegalArgumentException("Input class must be a numeric type");
    }
}

From source file:de.beyondjava.angularFaces.core.ELTools.java

/**
 * Is the parameter passed a primitive type (such as int, long, etc) or a type considered primitive by most
 * programmers (such as String)?/* w  ww . j  ava  2  s  . co m*/
 *
 * @param c
 * @return true if c is a de-facto-primitive
 */
private static boolean isPrimitive(Class<? extends Object> c) {
    return (null == c) || (Class.class == c) || (String.class == c) || c.isPrimitive() || (Integer.class == c)
            || (Long.class == c) || (Short.class == c) || (Byte.class == c) || (Character.class == c)
            || (Float.class == c) || (Double.class == c) || (Void.class == c) || (Boolean.class == c);
}

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

/**
 * recursively marshall [multidimensional? - of course!!! ] array
 *
 * @param array/* w w w .j av a2s.  co m*/
 * @return
 */
public static JSONArray marshallArray(Object array)
        throws InvocationTargetException, NoSuchMethodException, JSONException, IllegalAccessException {
    if (array != null && array.getClass().isArray()) {
        Class<?> componentType = array.getClass().getComponentType();
        JSONArray retval = new JSONArray();
        final int arrayLength = Array.getLength(array);
        // stirngs and primitives must be marshalled directly
        if (componentType.isPrimitive() || String.class.equals(componentType)) {

            for (int i = 0; i < arrayLength; i++) {
                retval.put(Array.get(array, i));
            }
        } else if (componentType.isArray()) {
            // that's cool, nested array recurse
            for (int i = 0; i < arrayLength; i++) {
                retval.put(marshallArray(Array.get(array, i)));
            }
        } else {
            // treat component as a bean   if it got default constructor
            try {
                if (componentType.getConstructor() != null) {
                    for (int i = 0; i < arrayLength; i++) {
                        retval.put(marshall(Array.get(array, i)));
                    }
                }
            } catch (NoSuchMethodException ex) {
                // just ignore it here, it means no such constructor was found
            }
        }

        return retval;
    }

    return null;
}