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:jef.database.DbUtils.java

private static boolean isValidPKValue(IQueryableEntity obj, ITableMetadata meta, ColumnMapping field) {
    Class<?> type = field.getFieldAccessor().getType();
    Object value = field.getFieldAccessor().get(obj);
    if (field.isUnsavedValueDeclared()) {
        return !field.isUnsavedValue(value);
    } else if (type.isPrimitive()) {
        if (field.isUnsavedValue(value)) {
            if (!obj.isUsed(field.field()))
                return false;
        }//from  www.j a va  2 s .co m
        return true;
    } else {
        return value != null;
    }
}

From source file:info.magnolia.content2bean.impl.TypeMappingImpl.java

@Override
public TypeDescriptor getTypeDescriptor(Class<?> beanClass) {
    TypeDescriptor dscr = types.get(beanClass);
    // eh, we know about this type, don't bother resolving any further.
    if (dscr != null) {
        return dscr;
    }// w  w  w .j  a va2 s  . c o m
    dscr = new TypeDescriptor();
    dscr.setType(beanClass);
    dscr.setMap(Map.class.isAssignableFrom(beanClass));
    dscr.setCollection(beanClass.isArray() || Collection.class.isAssignableFrom(beanClass));
    types.put(beanClass, dscr);

    if (!beanClass.isArray() && !beanClass.isPrimitive()) { // don't bother looking for a transformer if the property is an array or a primitive type
        Content2BeanTransformer transformer = null; // TODO ? transformerProvider.getTransformerFor(beanClass);
        try {
            if (transformer == null) {
                transformer = findTransformerByNamingConvention(beanClass);
            }
            if (transformer == null) {
                transformer = findTransformerViaProperty(beanClass);
            }
        } catch (Exception e) {
            // this is fine because having a transformer class is optional
            log.debug("No custom transformer class {}Transformer class found", beanClass.getName());
        }
        dscr.setTransformer(transformer);
    }
    return dscr;
}

From source file:com.haulmont.chile.core.loader.ChileAnnotationsLoader.java

protected Range.Cardinality getCardinality(Field field) {
    Class<?> type = field.getType();
    if (Collection.class.isAssignableFrom(type)) {
        return Range.Cardinality.ONE_TO_MANY;
    } else if (type.isPrimitive() || type.equals(String.class) || Number.class.isAssignableFrom(type)
            || Date.class.isAssignableFrom(type) || UUID.class.isAssignableFrom(type)) {
        return Range.Cardinality.NONE;
    } else/*from w w  w  .  j a  v a 2 s. co  m*/
        return Range.Cardinality.MANY_TO_ONE;
}

From source file:com.trafficspaces.api.model.Resource.java

public void setJSONObject(JSONObject jsonObject) {
    Iterator itr = jsonObject.keys();
    while (itr.hasNext()) {
        String key = (String) itr.next();
        Object value = jsonObject.opt(key);

        try {/*w  w  w.ja  v  a  2 s. c o m*/
            Field field = this.getClass().getField(key);
            Class type = field.getType();

            int fieldModifiers = field.getModifiers();
            //System.out.println("key=" + key + ", name=" + field.getName() + ", value=" + value + ", type=" +type + ", componentType=" +type.getComponentType() + 
            //      ", ispublic="+Modifier.isPublic(fieldModifiers) + ", isstatic="+Modifier.isStatic(fieldModifiers) + ", isnative="+Modifier.isNative(fieldModifiers) +
            //      ", isprimitive="+type.isPrimitive() + ", isarray="+type.isArray() + ", isResource="+Resource.class.isAssignableFrom(type));

            if (type.isPrimitive()) {
                if (type.equals(int.class)) {
                    field.setInt(this, jsonObject.getInt(key));
                } else if (type.equals(double.class)) {
                    field.setDouble(this, jsonObject.getDouble(key));
                }
            } else if (type.isArray()) {
                JSONArray jsonArray = null;
                if (value instanceof JSONArray) {
                    jsonArray = (JSONArray) value;
                } else if (value instanceof JSONObject) {
                    JSONObject jsonSubObject = (JSONObject) value;
                    jsonArray = jsonSubObject.optJSONArray(key.substring(0, key.length() - 1));
                }
                if (jsonArray != null && jsonArray.length() > 0) {
                    Class componentType = type.getComponentType();
                    Object[] values = (Object[]) Array.newInstance(componentType, jsonArray.length());
                    for (int j = 0; j < jsonArray.length(); j++) {
                        Resource resource = (Resource) componentType.newInstance();
                        resource.setJSONObject(jsonArray.getJSONObject(j));
                        values[j] = resource;
                    }
                    field.set(this, values);
                }
            } else if (Resource.class.isAssignableFrom(type) && value instanceof JSONObject) {
                Resource resource = (Resource) type.newInstance();
                resource.setJSONObject((JSONObject) value);
                field.set(this, resource);
            } else if (type.equals(String.class) && value instanceof String) {
                field.set(this, (String) value);
            }
        } catch (NoSuchFieldException nsfe) {
            System.err.println("warning: field does not exist. key=" + key + ",value=" + value);
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("error: key=" + key + ",value=" + value + ", error=" + e.getMessage());
        }
    }
}

From source file:com.evolveum.midpoint.prism.PrismPropertyValue.java

void checkValue() {
    if (isRaw()) {
        // Cannot really check raw values
        return;//from  w ww. j ava  2 s . co  m
    }
    if (value == null) {
        // can be used not because of prism forms in gui (will be fixed later [lazyman]
        // throw new IllegalArgumentException("Null value in "+this);
        return;
    }
    if (value instanceof PolyStringType) {
        // This is illegal. PolyString should be there instead.
        throw new IllegalArgumentException("PolyStringType found where PolyString should be in " + this);
    }
    Class<? extends Object> valueClass = value.getClass();
    if (value instanceof Serializable) {
        // This is OK
        return;
    }
    if (valueClass.isPrimitive()) {
        // This is OK
        return;
    }
    if (value instanceof SchemaDefinitionType) {
        return;
    }

    if (value instanceof RawType) {
        return;
    }

    throw new IllegalArgumentException("Unsupported value " + value + " (" + valueClass + ") in " + this);
}

From source file:com.p5solutions.core.utils.ReflectionUtility.java

public static Boolean isBasicClass(Class<?> clazz) {
    if (clazz != null) {
        if (clazz.isPrimitive()) {
            return true;
        }// w  w w  . ja  va2 s  . co m

        if (isNumberClass(clazz)) {
            return true;
        }

        if (Boolean.class.equals(clazz)) {
            return true;
        }

        // check for char

        if (String.class.equals(clazz)) {
            return true;
        }

        if (isDate(clazz)) {
            return true;
        }

        // TODO check for other types.. such as enums
    }
    return false;
}

From source file:com.amazon.carbonado.repo.jdbc.JDBCStorableIntrospector.java

/**
 * Figures out how to best access the given property, or returns null if
 * not supported. An adapter is not applied.
 *
 * @return null if not supported/*from   w  w w . j  av  a 2  s . c om*/
 */
private static AccessInfo getAccessInfo(Class desiredClass, int dataType, String dataTypeName, int columnSize,
        int decimalDigits) {
    if (!desiredClass.isPrimitive()) {
        TypeDesc desiredType = TypeDesc.forClass(desiredClass);
        if (desiredType.toPrimitiveType() != null) {
            desiredType = desiredType.toPrimitiveType();
            desiredClass = desiredType.toClass();
        }
    }

    if (desiredClass == Object.class) {
        return new AccessInfo("Object", Object.class);
    }

    Class actualClass;
    String suffix;

    switch (dataType) {
    default:
        return null;

    case BIT:
    case BOOLEAN:
        if (desiredClass == boolean.class) {
            actualClass = boolean.class;
            suffix = "Boolean";
        } else if (desiredClass == String.class) {
            actualClass = String.class;
            suffix = "String";
        } else {
            return null;
        }
        break;

    case TINYINT:
        if (desiredClass == byte.class) {
            actualClass = byte.class;
            suffix = "Byte";
        } else if (desiredClass == short.class) {
            actualClass = short.class;
            suffix = "Short";
        } else if (desiredClass == String.class) {
            actualClass = String.class;
            suffix = "String";
        } else {
            return null;
        }
        break;

    case SMALLINT:
        if (desiredClass == short.class) {
            actualClass = short.class;
            suffix = "Short";
        } else if (desiredClass == int.class) {
            actualClass = int.class;
            suffix = "Int";
        } else if (desiredClass == String.class) {
            actualClass = String.class;
            suffix = "String";
        } else {
            return null;
        }
        break;

    case INTEGER:
        if (desiredClass == int.class) {
            actualClass = int.class;
            suffix = "Int";
        } else if (desiredClass == long.class) {
            actualClass = long.class;
            suffix = "Long";
        } else if (desiredClass == String.class) {
            actualClass = String.class;
            suffix = "String";
        } else {
            return null;
        }
        break;

    case BIGINT:
        if (desiredClass == long.class) {
            actualClass = long.class;
            suffix = "Long";
        } else if (desiredClass == String.class) {
            actualClass = String.class;
            suffix = "String";
        } else {
            return null;
        }
        break;

    case REAL:
    case FLOAT:
    case DOUBLE:
        if (desiredClass == float.class) {
            actualClass = float.class;
            suffix = "Float";
        } else if (desiredClass == double.class) {
            actualClass = double.class;
            suffix = "Double";
        } else if (desiredClass == String.class) {
            actualClass = String.class;
            suffix = "String";
        } else {
            return null;
        }
        break;

    case NUMERIC:
    case DECIMAL:
        if (desiredClass == int.class) {
            if (decimalDigits == 0) {
                actualClass = int.class;
                suffix = "Int";
            } else {
                return null;
            }
        } else if (desiredClass == long.class) {
            if (decimalDigits == 0) {
                actualClass = long.class;
                suffix = "Long";
            } else {
                return null;
            }
        } else if (desiredClass == double.class) {
            actualClass = double.class;
            suffix = "Double";
        } else if (desiredClass == BigDecimal.class) {
            actualClass = BigDecimal.class;
            suffix = "BigDecimal";
        } else if (desiredClass == short.class) {
            if (decimalDigits == 0) {
                actualClass = short.class;
                suffix = "Short";
            } else {
                return null;
            }
        } else if (desiredClass == byte.class) {
            if (decimalDigits == 0) {
                actualClass = byte.class;
                suffix = "Byte";
            } else {
                return null;
            }
        } else if (desiredClass == String.class) {
            actualClass = String.class;
            suffix = "String";
        } else {
            return null;
        }
        break;

    case CHAR:
    case VARCHAR:
    case LONGVARCHAR:
        if (desiredClass == String.class) {
            actualClass = String.class;
            suffix = "String";
        } else if (desiredClass == char.class && columnSize == 1) {
            actualClass = String.class;
            suffix = "String";
        } else {
            return null;
        }
        break;

    case DATE:
        // Treat Date as a Timestamp since some databases make no
        // distinction.  The DateTimeAdapter can be used to provide
        // more control over the desired precision.
        if (desiredClass == java.sql.Date.class) {
            actualClass = java.sql.Timestamp.class;
            suffix = "Timestamp";
        } else {
            return null;
        }
        break;

    case TIME:
        if (desiredClass == java.sql.Time.class) {
            actualClass = java.sql.Time.class;
            suffix = "Time";
        } else {
            return null;
        }
        break;

    case TIMESTAMP:
        if (desiredClass == java.sql.Timestamp.class) {
            actualClass = java.sql.Timestamp.class;
            suffix = "Timestamp";
        } else {
            return null;
        }
        break;

    case BINARY:
    case VARBINARY:
    case LONGVARBINARY:
        if (desiredClass == byte[].class) {
            actualClass = byte[].class;
            suffix = "Bytes";
        } else {
            return null;
        }
        break;

    case BLOB:
        if (desiredClass == com.amazon.carbonado.lob.Blob.class) {
            actualClass = java.sql.Blob.class;
            suffix = "Blob";
        } else {
            return null;
        }
        break;

    case CLOB:
        if (desiredClass == com.amazon.carbonado.lob.Clob.class) {
            actualClass = java.sql.Clob.class;
            suffix = "Clob";
        } else {
            return null;
        }
        break;
    }

    return new AccessInfo(suffix, actualClass);
}

From source file:com.opensymphony.xwork2.conversion.impl.XWorkBasicConverter.java

private Object doConvertToNumber(Map<String, Object> context, Object value, Class toType) {
    if (value instanceof String) {
        if (toType == BigDecimal.class) {
            return new BigDecimal((String) value);
        } else if (toType == BigInteger.class) {
            return new BigInteger((String) value);
        } else if (toType.isPrimitive()) {
            Object convertedValue = super.convertValue(context, value, toType);
            String stringValue = (String) value;
            if (!isInRange((Number) convertedValue, stringValue, toType))
                throw new XWorkException("Overflow or underflow casting: \"" + stringValue + "\" into class "
                        + convertedValue.getClass().getName());

            return convertedValue;
        } else {/*from   w ww .  j a v  a 2  s .co m*/
            String stringValue = (String) value;
            if (!toType.isPrimitive() && (stringValue == null || stringValue.length() == 0)) {
                return null;
            }
            NumberFormat numFormat = NumberFormat.getInstance(getLocale(context));
            ParsePosition parsePos = new ParsePosition(0);
            if (isIntegerType(toType)) {
                numFormat.setParseIntegerOnly(true);
            }
            numFormat.setGroupingUsed(true);
            Number number = numFormat.parse(stringValue, parsePos);

            if (parsePos.getIndex() != stringValue.length()) {
                throw new XWorkException(
                        "Unparseable number: \"" + stringValue + "\" at position " + parsePos.getIndex());
            } else {
                if (!isInRange(number, stringValue, toType))
                    throw new XWorkException("Overflow or underflow casting: \"" + stringValue
                            + "\" into class " + number.getClass().getName());

                value = super.convertValue(context, number, toType);
            }
        }
    } else if (value instanceof Object[]) {
        Object[] objArray = (Object[]) value;

        if (objArray.length == 1) {
            return doConvertToNumber(context, objArray[0], toType);
        }
    }

    // pass it through DefaultTypeConverter
    return super.convertValue(context, value, toType);
}

From source file:net.lightbody.bmp.proxy.jetty.xml.XmlConfiguration.java

private void set(Object obj, XmlParser.Node node) throws ClassNotFoundException, NoSuchMethodException,
        InvocationTargetException, IllegalAccessException {
    String attr = node.getAttribute("name");
    String name = "set" + attr.substring(0, 1).toUpperCase() + attr.substring(1);
    Object value = value(obj, node);
    Object[] arg = { value };//from  w  w  w  .  j a  va  2s  .  co m

    Class oClass = nodeClass(node);
    if (oClass != null)
        obj = null;
    else
        oClass = obj.getClass();

    Class[] vClass = { Object.class };
    if (value != null)
        vClass[0] = value.getClass();

    if (log.isDebugEnabled())
        log.debug(obj + "." + name + "(" + vClass[0] + " " + value + ")");

    // Try for trivial match
    try {
        Method set = oClass.getMethod(name, vClass);
        set.invoke(obj, arg);
        return;
    } catch (IllegalArgumentException e) {
        LogSupport.ignore(log, e);
    } catch (IllegalAccessException e) {
        LogSupport.ignore(log, e);
    } catch (NoSuchMethodException e) {
        LogSupport.ignore(log, e);
    }

    // Try for native match
    try {
        Field type = vClass[0].getField("TYPE");
        vClass[0] = (Class) type.get(null);
        Method set = oClass.getMethod(name, vClass);
        set.invoke(obj, arg);
        return;
    } catch (NoSuchFieldException e) {
        LogSupport.ignore(log, e);
    } catch (IllegalArgumentException e) {
        LogSupport.ignore(log, e);
    } catch (IllegalAccessException e) {
        LogSupport.ignore(log, e);
    } catch (NoSuchMethodException e) {
        LogSupport.ignore(log, e);
    }

    // Try a field
    try {
        Field field = oClass.getField(attr);
        if (Modifier.isPublic(field.getModifiers())) {
            field.set(obj, value);
            return;
        }
    } catch (NoSuchFieldException e) {
        LogSupport.ignore(log, e);
    }

    // Search for a match by trying all the set methods
    Method[] sets = oClass.getMethods();
    Method set = null;
    for (int s = 0; sets != null && s < sets.length; s++) {
        if (name.equals(sets[s].getName()) && sets[s].getParameterTypes().length == 1) {
            // lets try it
            try {
                set = sets[s];
                sets[s].invoke(obj, arg);
                return;
            } catch (IllegalArgumentException e) {
                LogSupport.ignore(log, e);
            } catch (IllegalAccessException e) {
                LogSupport.ignore(log, e);
            }
        }
    }

    // Try converting the arg to the last set found.
    if (set != null) {
        try {
            Class sClass = set.getParameterTypes()[0];
            if (sClass.isPrimitive()) {
                for (int t = 0; t < __primitives.length; t++) {
                    if (sClass.equals(__primitives[t])) {
                        sClass = __primitiveHolders[t];
                        break;
                    }
                }
            }
            Constructor cons = sClass.getConstructor(vClass);
            arg[0] = cons.newInstance(arg);
            set.invoke(obj, arg);
            return;
        } catch (NoSuchMethodException e) {
            LogSupport.ignore(log, e);
        } catch (IllegalAccessException e) {
            LogSupport.ignore(log, e);
        } catch (InstantiationException e) {
            LogSupport.ignore(log, e);
        }
    }

    // No Joy
    throw new NoSuchMethodException(oClass + "." + name + "(" + vClass[0] + ")");
}

From source file:org.amplafi.flow.flowproperty.FlowPropertyDefinitionImpl.java

/**
 * @return/*from  w  w  w  .  ja v  a 2 s .co  m*/
 */
private boolean isDefaultByClassAvailable() {
    if (this.autoCreate != null) {
        return getBoolean(this.autoCreate);
    }
    if (!getDataClassDefinition().isCollection()) {
        Class<?> dataClass = getDataClassDefinition().getDataClass();
        if (dataClass.isPrimitive()) {
            return true;
        } else if (dataClass.isInterface() || dataClass.isEnum()) {
            return false;
        }
    }
    return false;
}