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:Main.java

/**
 * @return Null if class might be a bean; type String (that identifies
 *   why it's not a bean) if not// w w w .  j  a  va2s  .com
 */
public static String canBeABeanType(Class<?> type) {
    // First: language constructs that ain't beans:
    if (type.isAnnotation()) {
        return "annotation";
    }
    if (type.isArray()) {
        return "array";
    }
    if (type.isEnum()) {
        return "enum";
    }
    if (type.isPrimitive()) {
        return "primitive";
    }

    // Anything else? Seems valid, then
    return null;
}

From source file:foundation.stack.datamill.configuration.impl.Classes.java

public static boolean isAssignable(Class<?> clazz, final Class<?> toClass) {
    if (toClass == null) {
        return false;
    }//from  ww w . j av  a2s  .c o  m

    if (clazz == null) {
        return !toClass.isPrimitive();
    }

    if (clazz.isPrimitive() && !toClass.isPrimitive()) {
        clazz = primitiveToWrapper(clazz);
        if (clazz == null) {
            return false;
        }
    }
    if (toClass.isPrimitive() && !clazz.isPrimitive()) {
        clazz = wrapperToPrimitive(clazz);
        if (clazz == null) {
            return false;
        }
    }

    if (clazz.equals(toClass)) {
        return true;
    }
    if (clazz.isPrimitive()) {
        if (!toClass.isPrimitive()) {
            return false;
        }
        if (Integer.TYPE.equals(clazz)) {
            return Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass);
        }
        if (Long.TYPE.equals(clazz)) {
            return Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass);
        }
        if (Boolean.TYPE.equals(clazz)) {
            return false;
        }
        if (Double.TYPE.equals(clazz)) {
            return false;
        }
        if (Float.TYPE.equals(clazz)) {
            return Double.TYPE.equals(toClass);
        }
        if (Character.TYPE.equals(clazz)) {
            return Integer.TYPE.equals(toClass) || Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass)
                    || Double.TYPE.equals(toClass);
        }
        if (Short.TYPE.equals(clazz)) {
            return Integer.TYPE.equals(toClass) || Long.TYPE.equals(toClass) || Float.TYPE.equals(toClass)
                    || Double.TYPE.equals(toClass);
        }
        if (Byte.TYPE.equals(clazz)) {
            return Short.TYPE.equals(toClass) || Integer.TYPE.equals(toClass) || Long.TYPE.equals(toClass)
                    || Float.TYPE.equals(toClass) || Double.TYPE.equals(toClass);
        }
        // should never get here
        return false;
    }

    return toClass.isAssignableFrom(clazz);
}

From source file:com.github.mybatis.spring.MapperFactoryBean.java

private static boolean isPrimitive(Class clz) {
    return clz.isPrimitive() || NAMES.contains(clz.getSimpleName());
}

From source file:net.solarnetwork.node.util.ClassUtils.java

/**
 * Get a Map of non-null <em>simple</em> bean properties for an object.
 * //from  w w  w .j  a v a2  s  . c o m
 * @param o
 *        the object to inspect
 * @param ignore
 *        a set of property names to ignore (optional)
 * @return Map (never <em>null</em>)
 * @since 1.1
 */
public static Map<String, Object> getSimpleBeanProperties(Object o, Set<String> ignore) {
    if (ignore == null) {
        ignore = DEFAULT_BEAN_PROP_NAME_IGNORE;
    }
    Map<String, Object> result = new LinkedHashMap<String, Object>();
    BeanWrapper bean = new BeanWrapperImpl(o);
    PropertyDescriptor[] props = bean.getPropertyDescriptors();
    for (PropertyDescriptor prop : props) {
        if (prop.getReadMethod() == null) {
            continue;
        }
        String propName = prop.getName();
        if (ignore != null && ignore.contains(propName)) {
            continue;
        }
        Class<?> propType = bean.getPropertyType(propName);
        if (!(propType.isPrimitive() || propType.isEnum() || String.class.isAssignableFrom(propType)
                || Number.class.isAssignableFrom(propType) || Character.class.isAssignableFrom(propType)
                || Byte.class.isAssignableFrom(propType) || Date.class.isAssignableFrom(propType))) {
            continue;
        }
        Object propValue = bean.getPropertyValue(propName);
        if (propValue == null) {
            continue;
        }
        if (propType.isEnum()) {
            propValue = propValue.toString();
        } else if (Date.class.isAssignableFrom(propType)) {
            propValue = ((Date) propValue).getTime();
        }
        result.put(propName, propValue);
    }
    return result;
}

From source file:com.facebook.stetho.json.ObjectMapper.java

private static boolean isWrapperOrPrimitiveType(Class<?> clazz) {
    return clazz.isPrimitive() || clazz.equals(Boolean.class) || clazz.equals(Integer.class)
            || clazz.equals(Character.class) || clazz.equals(Byte.class) || clazz.equals(Short.class)
            || clazz.equals(Double.class) || clazz.equals(Long.class) || clazz.equals(Float.class);
}

From source file:com.yukthi.utils.beans.PropertyMapper.java

/**
 * Called to check when property copy has to be done mismatching fields. Simple types like primitives, java core classes and arrays will be skipped.
 * @param type Type to be checked/* ww w . j a  v  a  2  s.  c o  m*/
 * @return
 */
private static boolean isIgnorableType(Class<?> type) {
    if (type.isPrimitive() || type.getName().startsWith("java") || type.isArray()) {
        return true;
    }

    return false;
}

From source file:info.novatec.testit.livingdoc.util.ClassUtils.java

private static Class<?> getEquivalentNonPrimitiveType(Class<?> type) {
    if (type == null) {
        return null;
    }//  w w  w . j a v  a2  s .c o  m

    if (type.isPrimitive()) {
        Class<?> resultType = null;
        if (type == int.class) {
            resultType = Integer.class;
        } else if (type == float.class) {
            resultType = Float.class;
        } else if (type == boolean.class) {
            resultType = Boolean.class;
        } else if (type == double.class) {
            resultType = Double.class;
        } else if (type == long.class) {
            resultType = Long.class;
        } else if (type == char.class) {
            resultType = Character.class;
        } else if (type == byte.class) {
            resultType = Byte.class;
        } else if (type == short.class) {
            resultType = Short.class;
        }
        return resultType;
    }
    return type;
}

From source file:de.alpharogroup.lang.object.CloneObjectExtensions.java

/**
 * Try to clone the given object.//from  w w w. ja v a 2  s  .  c  o m
 *
 * @param object
 *            The object to clone.
 * @return The cloned object or null if the clone process failed.
 * @throws NoSuchMethodException
 *             Thrown if a matching method is not found or if the name is "&lt;init&gt;"or
 *             "&lt;clinit&gt;".
 * @throws SecurityException
 *             Thrown if the security manager indicates a security violation.
 * @throws IllegalAccessException
 *             Thrown if this {@code Method} object is enforcing Java language access control
 *             and the underlying method is inaccessible.
 * @throws IllegalArgumentException
 *             Thrown if an illegal argument is given
 * @throws InvocationTargetException
 *             Thrown if the property accessor method throws an exception
 * @throws ClassNotFoundException
 *             occurs if a given class cannot be located by the specified class loader
 * @throws InstantiationException
 *             Thrown if one of the following reasons: the class object
 *             <ul>
 *             <li>represents an abstract class</li>
 *             <li>represents an interface</li>
 *             <li>represents an array class</li>
 *             <li>represents a primitive type</li>
 *             <li>represents {@code void}</li>
 *             <li>has no nullary constructor</li>
 *             </ul>
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static Object cloneObject(final Object object)
        throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException,
        InvocationTargetException, ClassNotFoundException, InstantiationException, IOException {
    Object clone = null;
    // Try to clone the object if it implements Serializable.
    if (object instanceof Serializable) {
        clone = SerializedObjectExtensions.copySerializedObject((Serializable) object);
        if (clone != null) {
            return clone;
        }
    }
    // Try to clone the object if it is Cloneble.
    if (clone == null && object instanceof Cloneable) {

        if (object.getClass().isArray()) {
            final Class<?> componentType = object.getClass().getComponentType();
            if (componentType.isPrimitive()) {
                int length = Array.getLength(object);
                clone = Array.newInstance(componentType, length);
                while (length-- > 0) {
                    Array.set(clone, length, Array.get(object, length));
                }
            } else {
                clone = ((Object[]) object).clone();
            }
            if (clone != null) {
                return clone;
            }
        }
        final Class<?> clazz = object.getClass();
        final Method cloneMethod = clazz.getMethod("clone", (Class[]) null);
        clone = cloneMethod.invoke(object, (Object[]) null);
        if (clone != null) {
            return clone;
        }
    }
    // Try to clone the object by copying all his properties with
    // the BeanUtils.copyProperties() method.
    if (clone == null) {
        clone = ReflectionExtensions.getNewInstance(object);
        BeanUtils.copyProperties(clone, object);
    }
    return clone;
}

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

private static void loadObject(Object retObj, Field f, JSONObject jsonObj) throws JSONSerializationException {
    String key = f.getName();//from ww w.  ja  v a2s.c om
    Class type = f.getType();
    try {
        if (type.isPrimitive()) {
            if (type == Integer.TYPE) {
                f.setInt(retObj, jsonObj.getInt(key));
            } else if (type == Long.TYPE) {
                f.setLong(retObj, jsonObj.getInt(key));
            } else if (type == Short.TYPE) {
                f.setShort(retObj, (short) jsonObj.getInt(key));
            } else if (type == Boolean.TYPE) {
                f.setBoolean(retObj, jsonObj.getBoolean(key));
            } else if (type == Double.TYPE) {
                f.setDouble(retObj, jsonObj.getDouble(key));
            } else if (type == Float.TYPE) {
                f.setFloat(retObj, (float) jsonObj.getDouble(key));
            } else if (type == Character.TYPE) {
                char ch = jsonObj.getString(key).charAt(0);
                f.setChar(retObj, ch);
            } else if (type == Byte.TYPE) {
                f.setByte(retObj, (byte) jsonObj.getInt(key));
            } else {
                throw new JSONSerializationException("Unknown primitive: " + type);
            }
        } else if (type == String.class) {
            f.set(retObj, jsonObj.getString(key));
        } else if (JSONSerializable.class.isAssignableFrom(type)) {
            JSONObject jObj = jsonObj.getJSONObject(key);
            JSONSerializable serObj = deSerialize(type, jObj);
            f.set(retObj, serObj);
        }
    } catch (Exception e) {
        throw new JSONSerializationException(e.getMessage(), e);
    }
}

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

private static void dumpObject(JSONObject jsonObj, Field f, JSONSerializable srcObj)
        throws JSONSerializationException, JSONException {
    Object value;//from w w  w.j  a  v a2s  . com
    try {
        value = f.get(srcObj);
    } catch (Exception e) {
        throw new JSONSerializationException(e.getMessage(), e);
    }

    Class type = f.getType();

    String name = f.getName();

    if (type.isPrimitive()) {
        jsonObj.put(name, String.valueOf(value));
    } else if (type == String.class) {
        if (value != null) {
            jsonObj.put(name, String.valueOf(value));
        }
    } else if (JSONSerializable.class.isInstance(value)) {
        jsonObj.put(name, serializeJSONObject((JSONSerializable) value));
    }
}