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.discovery.darchrow.lang.ClassUtil.java

/**
 *  class info map for LOGGER.// w w w.j av a2 s.co m
 *
 * @param klass
 *            the clz
 * @return the map for log
 */
public static Map<String, Object> getClassInfoMapForLog(Class<?> klass) {
    if (Validator.isNullOrEmpty(klass)) {
        return null;
    }

    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?trueJVM???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:org.grails.datastore.mapping.reflect.ReflectionUtils.java

/**
 * <p>Tests whether or not the left hand type is compatible with the right hand type in Groovy
 * terms, i.e. can the left type be assigned a value of the right hand type in Groovy.</p>
 * <p>This handles Java primitive type equivalence and uses isAssignableFrom for all other types,
 * with a bit of magic for native types and polymorphism i.e. Number assigned an int.
 * If either parameter is null an exception is thrown</p>
 *
 * @param leftType The type of the left hand part of a notional assignment
 * @param rightType The type of the right hand part of a notional assignment
 * @return True if values of the right hand type can be assigned in Groovy to variables of the left hand type.
 *//*from   ww  w  .j a va  2s.  co  m*/
public static boolean isAssignableFrom(final Class<?> leftType, final Class<?> rightType) {
    if (leftType == null) {
        throw new NullPointerException("Left type is null!");
    }
    if (rightType == null) {
        throw new NullPointerException("Right type is null!");
    }
    if (leftType == Object.class) {
        return true;
    }
    if (leftType == rightType) {
        return true;
    }
    // check for primitive type equivalence
    Class<?> r = PRIMITIVE_TYPE_COMPATIBLE_CLASSES.get(leftType);
    boolean result = r == rightType;

    if (!result) {
        // If no primitive <-> wrapper match, it may still be assignable
        // from polymorphic primitives i.e. Number -> int (AKA Integer)
        if (rightType.isPrimitive()) {
            // see if incompatible
            r = PRIMITIVE_TYPE_COMPATIBLE_CLASSES.get(rightType);
            if (r != null) {
                result = leftType.isAssignableFrom(r);
            }
        } else {
            // Otherwise it may just be assignable using normal Java polymorphism
            result = leftType.isAssignableFrom(rightType);
        }
    }
    return result;
}

From source file:adalid.core.XS1.java

private static boolean isRestrictedFieldType(Class<?> fieldType) {
    int modifiers = fieldType.getModifiers();
    boolean b = fieldType.isPrimitive();
    b |= Modifier.isAbstract(modifiers) || !Modifier.isPublic(modifiers);
    b |= fieldType.isAnnotation();//from ww  w. j  a  v  a  2  s. co m
    b |= fieldType.isAnonymousClass();
    b |= fieldType.isArray();
    b |= fieldType.isEnum();
    b |= fieldType.isLocalClass();
    b |= fieldType.isInterface();
    return b;
}

From source file:com.alibaba.fastjson.parser.ParserConfig.java

/**
 * @deprecated  internal method, dont call
 *///from   w w w. jav a 2  s .  c  o m
public static boolean isPrimitive2(Class<?> clazz) {
    return clazz.isPrimitive() //
            || clazz == Boolean.class //
            || clazz == Character.class //
            || clazz == Byte.class //
            || clazz == Short.class //
            || clazz == Integer.class //
            || clazz == Long.class //
            || clazz == Float.class //
            || clazz == Double.class //
            || clazz == BigInteger.class //
            || clazz == BigDecimal.class //
            || clazz == String.class //
            || clazz == java.util.Date.class //
            || clazz == java.sql.Date.class //
            || clazz == java.sql.Time.class //
            || clazz == java.sql.Timestamp.class //
            || clazz.isEnum() //
    ;
}

From source file:com.softwarementors.extjs.djn.router.processor.standard.json.JsonRequestProcessor.java

private static boolean isValidJsonTypeForJavaType(JsonElement jsonElement, Class<?> parameterType) {
    assert jsonElement != null;
    assert parameterType != null;

    // Check json nulls
    if (jsonElement.isJsonNull()) {
        return !parameterType.isPrimitive();
    }/*from   w w  w  . j  a  va  2 s.  com*/

    if (parameterType.isArray()) {
        // This is *always* ok because if the value is not a json array
        // we will instantiate a single item array and attempt conversion
        return true;
    }

    if (parameterType.equals(Boolean.class) || parameterType.equals(boolean.class)) {
        return jsonElement.isJsonPrimitive() && ((JsonPrimitive) jsonElement).isBoolean();
    } else if (parameterType.equals(char.class) || parameterType.equals(Character.class)) {
        if (jsonElement.isJsonPrimitive() && ((JsonPrimitive) jsonElement).isString()) {
            return jsonElement.getAsString().length() == 1;
        }
        return false;
    } else if (parameterType.equals(String.class)) {
        return jsonElement.isJsonPrimitive() && ((JsonPrimitive) jsonElement).isString();
    } else if (ClassUtils.isNumericType(parameterType)) {
        return jsonElement.isJsonPrimitive() && ((JsonPrimitive) jsonElement).isNumber();
    }

    // If we arrived here, assume somebody will know how to handle the json element, maybe customizing Gson's serialization
    return true;
}

From source file:com.erudika.para.utils.Utils.java

/**
 * Checks if a class is primitive, String or a primitive wrapper.
 *
 * @param clazz a class/*from   w  w w .j a v  a  2s .  c  o m*/
 * @return true if primitive or wrapper
 */
public static boolean isBasicType(Class<?> clazz) {
    return (clazz == null) ? false
            : (clazz.isPrimitive() || clazz.equals(String.class) || clazz.equals(Long.class)
                    || clazz.equals(Integer.class) || clazz.equals(Boolean.class) || clazz.equals(Byte.class)
                    || clazz.equals(Short.class) || clazz.equals(Float.class) || clazz.equals(Double.class)
                    || clazz.equals(Character.class));
}

From source file:com.bstek.dorado.data.method.MethodAutoMatchingUtils.java

public static boolean isSimpleType(Type type) {
    Class<?> cl = toClass(type);
    return (String.class.equals(cl) || cl.isPrimitive() || Boolean.class.equals(cl)
            || Number.class.isAssignableFrom(cl) || cl.isEnum());
}

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

public static Class getObjectType(Class type) {
    if (type.isPrimitive()) {
        if (type == int.class) {
            return Integer.class;
        } else if (type == char.class) {
            return Character.class;
        } else if (type == byte.class) {
            return Byte.class;
        } else if (type == float.class) {
            return Float.class;
        } else if (type == double.class) {
            return Double.class;
        } else if (type == long.class) {
            return Long.class;
        } else if (type == short.class) {
            return Short.class;
        } else if (type == boolean.class) {
            return Boolean.class;
        }/*from   ww  w  .  j a  v a  2  s. co m*/
    }

    return type;
}

From source file:com.netspective.commons.lang.ValueBeanGeneratorClassLoader.java

/**
 * Convert runtime java.lang.Class to BCEL Type object.
 *
 * @param cl Java class/*from w  ww. j a v  a 2 s  .c  o m*/
 *
 * @return corresponding Type object
 */
public static Type getBCELType(java.lang.Class cl) {
    if (cl == null) {
        throw new IllegalArgumentException("Class must not be null");
    }

    /* That's an amzingly easy case, because getName() returns
     * the signature. That's what we would have liked anyway.
     */
    if (cl.isArray()) {
        return Type.getType(cl.getName());
    } else if (cl.isPrimitive()) {
        if (cl == Integer.TYPE) {
            return Type.INT;
        } else if (cl == Void.TYPE) {
            return Type.VOID;
        } else if (cl == Double.TYPE) {
            return Type.DOUBLE;
        } else if (cl == Float.TYPE) {
            return Type.FLOAT;
        } else if (cl == Boolean.TYPE) {
            return Type.BOOLEAN;
        } else if (cl == Byte.TYPE) {
            return Type.BYTE;
        } else if (cl == Short.TYPE) {
            return Type.SHORT;
        } else if (cl == Long.TYPE) {
            return Type.LONG;
        } else if (cl == Character.TYPE) {
            return Type.CHAR;
        } else {
            throw new IllegalStateException("Ooops, what primitive type is " + cl);
        }
    } else { // "Real" class
        return new ObjectType(cl.getName());
    }
}

From source file:com.jeeframework.util.classes.ClassUtils.java

/**
 * Check if the given class represents a primitive (i.e. boolean, byte,
 * char, short, int, long, float, or double) or a primitive wrapper
 * (i.e. Boolean, Byte, Character, Short, Integer, Long, Float, or Double).
 * @param clazz the class to check/*from  w w w. j  av a 2  s . co  m*/
 * @return whether the given class is a primitive or primitive wrapper class
 */
public static boolean isPrimitiveOrWrapper(Class clazz) {
    Assert.notNull(clazz, "Class must not be null");
    return (clazz.isPrimitive() || isPrimitiveWrapper(clazz));
}