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.microsoft.tfs.core.internal.db.DBStatement.java

/**
 * Convenience query that returns an array of a primitive component type.
 */// www .j  a  va 2 s.c om
public Object executeQueryForPrimitiveArray(final Object params, final Class primitiveType) {
    if (primitiveType == null || !primitiveType.isPrimitive()) {
        throw new IllegalArgumentException("primitiveType must be a non-null primitive type"); //$NON-NLS-1$
    }

    final List results = new ArrayList();

    executeQuery(params, new ResultHandler() {
        @Override
        public void handleRow(final ResultSet rset) throws SQLException {
            results.add(rset.getObject(1));
        }
    });

    final int len = results.size();
    final Object returnArray = Array.newInstance(primitiveType, len);

    for (int i = 0; i < len; i++) {
        Array.set(returnArray, i, results.get(i));
    }

    return returnArray;
}

From source file:com.nfwork.dbfound.json.JSONDynaBean.java

public Object get(String name) {
    Object value = dynaValues.get(name);
    if (value != null) {
        return value;
    }//w w w .  j a  v  a2  s .  c  o  m

    Class type = getDynaProperty(name).getType();
    if (type == null) {
        throw new NullPointerException("Unspecified property type for " + name);
    }
    if (!type.isPrimitive()) {
        return value;
    }

    if (type == Boolean.TYPE) {
        return Boolean.FALSE;
    } else if (type == Byte.TYPE) {
        return new Byte((byte) 0);
    } else if (type == Character.TYPE) {
        return new Character((char) 0);
    } else if (type == Short.TYPE) {
        return new Short((short) 0);
    } else if (type == Integer.TYPE) {
        return new Integer(0);
    } else if (type == Long.TYPE) {
        return new Long(0);
    } else if (type == Float.TYPE) {
        return new Float(0.0);
    } else if (type == Double.TYPE) {
        return new Double(0);
    }

    return null;
}

From source file:org.cloudifysource.restDoclet.exampleGenerators.DocDefaultExampleGenerator.java

private String generateJSON(final Type type) throws Exception {
    Class<?> clazz = ClassUtils.getClass(type.qualifiedTypeName());
    if (MultipartFile.class.getName().equals(clazz.getName())) {
        return "\"file content\"";
    }//  w ww  . j av a2  s. c o  m
    if (clazz.isInterface()) {
        throw new InstantiationException("the given class is an interface [" + clazz.getName() + "].");
    }
    Object newInstance = null;
    if (clazz.isPrimitive()) {
        newInstance = PrimitiveExampleValues.getValue(clazz);
    }
    Class<?> classToInstantiate = clazz;
    if (newInstance == null) {
        newInstance = classToInstantiate.newInstance();
    }
    return new ObjectMapper().writeValueAsString(newInstance);
}

From source file:com.ms.commons.summer.web.util.json.JsonBeanUtils.java

/**
 * Creates a JSONDynaBean from a JSONObject.
 *//* www  .  ja va 2s .  com*/
public static Object toBean(JSONObject jsonObject) {
    if (jsonObject == null || jsonObject.isNullObject()) {
        return null;
    }

    DynaBean dynaBean = null;

    Map props = JSONUtils.getProperties(jsonObject);
    dynaBean = JSONUtils.newDynaBean(jsonObject, new JsonConfig());
    for (Iterator entries = jsonObject.names().iterator(); entries.hasNext();) {
        String name = (String) entries.next();
        String key = JSONUtils.convertToJavaIdentifier(name, new JsonConfig());
        Class type = (Class) props.get(name);
        Object value = jsonObject.get(name);
        try {
            if (!JSONUtils.isNull(value)) {
                if (value instanceof JSONArray) {
                    dynaBean.set(key, JSONArray.toCollection((JSONArray) value));
                } else if (String.class.isAssignableFrom(type) || Boolean.class.isAssignableFrom(type)
                        || JSONUtils.isNumber(type) || Character.class.isAssignableFrom(type)
                        || JSONFunction.class.isAssignableFrom(type)) {
                    dynaBean.set(key, value);
                } else {
                    dynaBean.set(key, toBean((JSONObject) value));
                }
            } else {
                if (type.isPrimitive()) {
                    // assume assigned default value
                    log.warn("Tried to assign null value to " + key + ":" + type.getName());
                    dynaBean.set(key, JSONUtils.getMorpherRegistry().morph(type, null));
                } else {
                    dynaBean.set(key, null);
                }
            }
        } catch (JSONException jsone) {
            throw jsone;
        } catch (Exception e) {
            throw new JSONException("Error while setting property=" + name + " type" + type, e);
        }
    }

    return dynaBean;
}

From source file:com.examples.with.different.packagename.testcarver.AbstractConverter.java

/**
 * Change primitve Class types to the associated wrapper class.
 * @param type The class type to check./*from  w  w w .  ja  v a2 s  .  c  o m*/
 * @return The converted type.
 */
Class primitive(Class type) {
    if (type == null || !type.isPrimitive()) {
        return type;
    }

    if (type == Integer.TYPE) {
        return Integer.class;
    } else if (type == Double.TYPE) {
        return Double.class;
    } else if (type == Long.TYPE) {
        return Long.class;
    } else if (type == Boolean.TYPE) {
        return Boolean.class;
    } else if (type == Float.TYPE) {
        return Float.class;
    } else if (type == Short.TYPE) {
        return Short.class;
    } else if (type == Byte.TYPE) {
        return Byte.class;
    } else if (type == Character.TYPE) {
        return Character.class;
    } else {
        return type;
    }
}

From source file:de.ufinke.cubaja.sql.ObjectFactoryGenerator.java

ObjectFactory getFactory(Class<?> dataClass, WarnMode warnMode) throws Exception {

    ObjectFactory factory = factoryMap.get(dataClass);
    if (factory != null) {
        return factory;
    }/*from www .j  av a 2s  . c om*/

    if (dataClass.isPrimitive()) {
        throw new SQLException(text.get("primitive"));
    }

    dataClassType = new Type(dataClass);

    builtin = null;
    if (singleColumnSqlType != 0) {
        TypeCombination combination = new TypeCombination(singleColumnSqlType, dataClass);
        builtin = ObjectFactoryType.getType(combination);
    }
    if (builtin == null) {
        createSetterMap(dataClass);
        checkSetterMap(dataClass, warnMode);
    }

    Class<?> factoryClass = Loader.createClass(this, "QueryObjectFactory", dataClass);
    factory = (ObjectFactory) factoryClass.newInstance();
    factoryMap.put(dataClass, factory);

    setterMap = null;

    return factory;
}

From source file:org.amplafi.hivemind.factory.servicessetter.ServicesSetterImpl.java

/**
 * @param propertyType/* www . j av a  2s .  c  om*/
 * @return true class can be wired up as a service
 */
@Override
public boolean isWireableClass(Class<?> propertyType) {
    if (
    // exclude primitives or other things that are not mockable in any form
    propertyType.isPrimitive() || propertyType.isAnnotation() || propertyType.isArray() || propertyType.isEnum()
    // generated classes
            || propertyType.getCanonicalName() == null
            // exclude java classes
            || propertyType.getPackage().getName().startsWith("java")) {
        return false;
    } else {
        // exclude things that are explicitly labeled as not being injectable
        NotService notService = propertyType.getAnnotation(NotService.class);
        return notService == null;
    }
}

From source file:com.haulmont.cuba.core.config.AppPropertiesLocator.java

private void setDataType(Method method, AppPropertyEntity entity) {
    Class<?> returnType = method.getReturnType();
    if (returnType.isPrimitive()) {
        if (returnType == boolean.class)
            entity.setDataTypeName(datatypes.getIdByJavaClass(Boolean.class));
        if (returnType == int.class)
            entity.setDataTypeName(datatypes.getIdByJavaClass(Integer.class));
        if (returnType == long.class)
            entity.setDataTypeName(datatypes.getIdByJavaClass(Long.class));
        if (returnType == double.class || returnType == float.class)
            entity.setDataTypeName(datatypes.getIdByJavaClass(Double.class));
    } else if (returnType.isEnum()) {
        entity.setDataTypeName("enum");
        EnumStore enumStoreAnn = method.getAnnotation(EnumStore.class);
        if (enumStoreAnn != null && enumStoreAnn.value() == EnumStoreMode.ID) {
            //noinspection unchecked
            Class<EnumClass> enumeration = (Class<EnumClass>) returnType;
            entity.setEnumValues(Arrays.asList(enumeration.getEnumConstants()).stream()
                    .map(ec -> String.valueOf(ec.getId())).collect(Collectors.joining(",")));
        } else {// ww w.j a va 2  s. c om
            entity.setEnumValues(Arrays.asList(returnType.getEnumConstants()).stream().map(Object::toString)
                    .collect(Collectors.joining(",")));
        }
    } else {
        Datatype<?> datatype = datatypes.get(returnType);
        if (datatype != null)
            entity.setDataTypeName(datatypes.getId(datatype));
        else
            entity.setDataTypeName(datatypes.getIdByJavaClass(String.class));
    }

    String dataTypeName = entity.getDataTypeName();
    if (!dataTypeName.equals("enum")) {
        Datatype datatype = Datatypes.get(dataTypeName);
        String v = null;
        try {
            v = entity.getDefaultValue();
            datatype.parse(v);
            v = entity.getCurrentValue();
            datatype.parse(v);
        } catch (ParseException e) {
            log.debug("Cannot parse '{}' with {} datatype, using StringDatatype for property {}", v, datatype,
                    entity.getName());
            entity.setDataTypeName(datatypes.getIdByJavaClass(String.class));
        }
    }
}

From source file:fr.obeo.emf.specimen.SpecimenGenerator.java

private Object nextValue(Class<?> instanceClass) {
    final Object value;
    if (instanceClass.isPrimitive() || isWrapperType(instanceClass)) {
        value = nextPrimitive(unwrap(instanceClass));
    } else {//  ww w.ja  va  2  s  . co m
        value = nextObject(instanceClass);
    }
    return value;
}

From source file:com.google.feedserver.util.BeanUtil.java

public boolean isBean(Class<?> valueClass) {
    if (valueClass.isArray()) {
        return isBean(valueClass.getComponentType());
    } else if (valueClass.isPrimitive()) {
        return false;
    } else {//from   w  ww.ja  v a  2 s  .co  m
        return !primitiveTypes.contains(valueClass);
    }
}