Example usage for java.lang Class isArray

List of usage examples for java.lang Class isArray

Introduction

In this page you can find the example usage for java.lang Class isArray.

Prototype

@HotSpotIntrinsicCandidate
public native boolean isArray();

Source Link

Document

Determines if this Class object represents an array class.

Usage

From source file:com.github.dozermapper.core.util.MappingUtils.java

public static Object prepareIndexedCollection(Class<?> collectionType, Object existingCollection,
        Object collectionEntry, int index) {
    Object result = null;/*from ww  w .j  av a  2s  . co  m*/
    if (collectionType.isArray()) {
        result = prepareIndexedArray(collectionType, existingCollection, collectionEntry, index);
    } else if (Collection.class.isAssignableFrom(collectionType)) {
        result = prepareIndexedCollectionType(collectionType, existingCollection, collectionEntry, index);
    } else {
        throwMappingException(
                "Only types java.lang.Object[] and java.util.Collection are supported for indexed properties.");
    }

    return result;
}

From source file:com.wavemaker.json.type.reflect.ReflectTypeUtils.java

/**
 * Returns the FieldDefinition for a field of the specified type.
 * //  w ww  .  j av a2 s .c om
 * @param type
 * @param typeState
 * @param strict True if strict mode is on; not enough information will result in exceptions instead of warnings.
 * @param The name of this field (if known)
 * @return The corresponding fieldDefinition to the type.
 */
public static FieldDefinition getFieldDefinition(Type type, TypeState typeState, boolean strict, String name) {

    GenericFieldDefinition ret = new GenericFieldDefinition();
    ret.setName(name);

    if (type == null) {
        // do nothing, it's null, but do return a FieldDefinition
    } else if (type instanceof Class) {
        Class<?> returnTypeClass = (Class<?>) type;

        if (returnTypeClass.isArray()) {
            Tuple.Two<TypeDefinition, List<ListTypeDefinition>> dimAndClass = getArrayDimensions(
                    returnTypeClass, typeState, strict);
            ret.setTypeDefinition(dimAndClass.v1);
            ret.setArrayTypes(dimAndClass.v2);
        } else if (!strict && Collection.class.isAssignableFrom(returnTypeClass)) {
            if (!JSON.class.isAssignableFrom(returnTypeClass)) {
                logger.warn(MessageResource.JSON_TYPE_NOGENERICS.getMessage(returnTypeClass));
            }

            ret.setArrayTypes(new ArrayList<ListTypeDefinition>(1));
            ret.getArrayTypes().add(getListTypeDefinition(returnTypeClass, typeState, strict));
        } else if (ClassUtils.isPrimitiveOrWrapper(returnTypeClass)) {
            TypeDefinition td = getTypeDefinition(returnTypeClass, typeState, strict);
            ret.setTypeDefinition(td);
        } else {
            TypeDefinition td = getTypeDefinition(returnTypeClass, typeState, strict);
            ret.setTypeDefinition(td);
        }
    } else if (type instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) type;

        if (Class.class == pt.getRawType()) {
            TypeDefinition td = getTypeDefinition(Class.class, typeState, strict);
            ret.setTypeDefinition(td);
        } else if (pt.getRawType() instanceof Class
                && Collection.class.isAssignableFrom((Class<?>) pt.getRawType())) {
            Tuple.Two<TypeDefinition, List<ListTypeDefinition>> dimAndClass = getArrayDimensions(pt, typeState,
                    strict);
            ret.setTypeDefinition(dimAndClass.v1);
            ret.setArrayTypes(dimAndClass.v2);
        } else if (pt.getRawType() instanceof Class && Map.class.isAssignableFrom((Class<?>) pt.getRawType())) {
            TypeDefinition td = getTypeDefinition(pt, typeState, strict);
            ret.setTypeDefinition(td);
        } else {
            if (strict) {
                throw new WMRuntimeException(MessageResource.JSON_TYPE_UNKNOWNRAWTYPE, pt.getOwnerType(), pt);
            } else {
                logger.warn(MessageResource.JSON_TYPE_UNKNOWNRAWTYPE.getMessage(pt.getOwnerType(), pt));
            }
        }
    } else if (type instanceof GenericArrayType) {
        Tuple.Two<TypeDefinition, List<ListTypeDefinition>> dimAndClass = getArrayDimensions(type, typeState,
                strict);
        ret.setTypeDefinition(dimAndClass.v1);
        ret.setArrayTypes(dimAndClass.v2);
    } else {
        throw new WMRuntimeException(MessageResource.JSON_TYPE_UNKNOWNPARAMTYPE, type,
                type != null ? type.getClass() : null);
    }

    return ret;
}

From source file:net.paoding.rose.jade.rowmapper.DefaultRowMapperFactory.java

private static Class<?> getRowType(StatementMetaData statementMetaData) {
    Class<?> returnClassType = statementMetaData.getMethod().getReturnType();
    if (Collection.class.isAssignableFrom(returnClassType)) {
        return getRowTypeFromCollectionType(statementMetaData, returnClassType);
    } else if (Map.class == returnClassType) {
        return getRowTypeFromMapType(statementMetaData, returnClassType);
    } else if (returnClassType.isArray() && returnClassType != byte[].class) {
        // , ??/*  w  ww .j av a2s. co m*/
        return returnClassType.getComponentType();
    }

    // DAO?
    return returnClassType;
}

From source file:com.mawujun.util.AnnotationUtils.java

/**
 * Helper method for checking whether two objects of the given type are
 * equal. This method is used to compare the parameters of two annotation
 * instances./* w ww.  ja  va2  s . co  m*/
 *
 * @param type the type of the objects to be compared
 * @param o1 the first object
 * @param o2 the second object
 * @return a flag whether these objects are equal
 */
private static boolean memberEquals(Class<?> type, Object o1, Object o2) {
    if (o1 == o2) {
        return true;
    }
    if (o1 == null || o2 == null) {
        return false;
    }
    if (type.isArray()) {
        return arrayMemberEquals(type.getComponentType(), o1, o2);
    }
    if (type.isAnnotation()) {
        return equals((Annotation) o1, (Annotation) o2);
    }
    return o1.equals(o2);
}

From source file:com.base.dao.sql.ReflectionUtils.java

public static Object convertValue(Object value, Class toType) {
    Object result = null;//from w  w  w. j av a 2 s. c o  m
    if (value != null) {
        if (value.getClass().isArray() && toType.isArray()) {
            Class componentType = toType.getComponentType();
            result = Array.newInstance(componentType, Array.getLength(value));
            for (int i = 0, icount = Array.getLength(value); i < icount; i++) {
                Array.set(result, i, convertValue(Array.get(value, i), componentType));
            }
        } else {
            if ((toType == Integer.class) || (toType == Integer.TYPE))
                result = Integer.valueOf((int) longValue(value));
            if ((toType == Double.class) || (toType == Double.TYPE))
                result = new Double(doubleValue(value));
            if ((toType == Boolean.class) || (toType == Boolean.TYPE))
                result = booleanValue(value) ? Boolean.TRUE : Boolean.FALSE;
            if ((toType == Byte.class) || (toType == Byte.TYPE))
                result = Byte.valueOf((byte) longValue(value));
            if ((toType == Character.class) || (toType == Character.TYPE))
                result = new Character((char) longValue(value));
            if ((toType == Short.class) || (toType == Short.TYPE))
                result = Short.valueOf((short) longValue(value));
            if ((toType == Long.class) || (toType == Long.TYPE))
                result = Long.valueOf(longValue(value));
            if ((toType == Float.class) || (toType == Float.TYPE))
                result = new Float(doubleValue(value));
            if (toType == BigInteger.class)
                result = bigIntValue(value);
            if (toType == BigDecimal.class)
                result = bigDecValue(value);
            if (toType == String.class)
                result = stringValue(value);
            if (toType == Date.class) {
                result = DateUtils.toDate(stringValue(value));
            }
            if (Enum.class.isAssignableFrom(toType))
                result = enumValue((Class<Enum>) toType, value);
        }
    } else {
        if (toType.isPrimitive()) {
            result = primitiveDefaults.get(toType);
        }
    }
    return result;
}

From source file:Main.java

public static String getTypeForField(Field field) {
    Class type = field.getType();
    // Animation//from w w  w .ja v  a2s  .co  m
    if (type.isAssignableFrom(Animation.class))
        return "anim";
    // Color State List
    else if (type.isAssignableFrom(ColorStateList.class))
        return "color";
    // Drawable
    else if (type.isAssignableFrom(Drawable.class))
        return "drawable";
    // String
    else if (type.isAssignableFrom(String.class))
        return "string";
    // String Array
    else if (type.isArray())
        return "array";
    // TODO: Recognize plural strings if possible
    //        else if (type.isAssignableFrom(String.class))
    //            return "plural";
    // Boolean
    else if (type.isAssignableFrom(Boolean.TYPE) || type.isAssignableFrom(Boolean.class))
        return "bool";
    // Integer
    else if (type.isAssignableFrom(Integer.TYPE) || type.isAssignableFrom(Integer.class))
        return "integer";
    // Dimen
    else if (type.isAssignableFrom(Float.TYPE) || type.isAssignableFrom(Float.class))
        return "dimen";

    throw new RuntimeException("No suitable type found for " + field.getName() + "of class " + type.getName());
}

From source file:org.opencron.server.dao.HibernateDao.java

private static boolean isSimple(Object value) {
    Class<? extends Object> type = value.getClass();
    if (type.isArray() || simpleTypes.contains(type))
        return true;
    for (Class clazz : simpleTypes) {
        if (clazz.isInstance(value))
            return true;
    }/*from  w ww  .  j  a  va 2 s.c  o  m*/
    return false;
}

From source file:org.uimafit.factory.ConfigurationParameterFactory.java

private static boolean isMultiValued(Field field) {
    Class<?> parameterClass = field.getType();
    if (parameterClass.isArray()) {
        return true;
    } else if (Collection.class.isAssignableFrom(parameterClass)) {
        return true;
    }/*  www  . j a  v a2s. com*/
    return false;
}

From source file:fi.foyt.foursquare.api.JSONFieldParser.java

/**
 * Parses single JSON object field into a value. Value might be of type String, Integer, Long, Double, Boolean or FoursquareEntity depending classes field type
 * // w w w .  ja va 2s .  c  o  m
 * @param clazz class
 * @param jsonObject JSON Object
 * @param objectFieldName field to be parsed
 * @param skipNonExistingFields whether parser should ignore non-existing fields
 * @return field's value
 * @throws JSONException when JSON parsing error occures
 * @throws FoursquareApiException when something unexpected happens
 */
private static Object parseValue(Class<?> clazz, JSONObject jsonObject, String objectFieldName,
        boolean skipNonExistingFields) throws JSONException, FoursquareApiException {
    if (clazz.isArray()) {
        Object value = jsonObject.get(objectFieldName);

        JSONArray jsonArray;
        if (value instanceof JSONArray) {
            jsonArray = (JSONArray) value;
        } else {
            if ((value instanceof JSONObject) && (((JSONObject) value).has("items"))) {
                jsonArray = ((JSONObject) value).getJSONArray("items");
            } else {
                throw new FoursquareApiException("JSONObject[\"" + objectFieldName
                        + "\"] is neither a JSONArray nor a {count, items} object.");
            }
        }

        Class<?> arrayClass = clazz.getComponentType();
        Object[] arrayValue = (Object[]) Array.newInstance(arrayClass, jsonArray.length());

        for (int i = 0, l = jsonArray.length(); i < l; i++) {
            if (arrayClass.equals(String.class)) {
                arrayValue[i] = jsonArray.getString(i);
            } else if (arrayClass.equals(Integer.class)) {
                arrayValue[i] = jsonArray.getInt(i);
            } else if (arrayClass.equals(Long.class)) {
                arrayValue[i] = jsonArray.getLong(i);
            } else if (arrayClass.equals(Double.class)) {
                arrayValue[i] = jsonArray.getDouble(i);
            } else if (arrayClass.equals(Boolean.class)) {
                arrayValue[i] = jsonArray.getBoolean(i);
            } else if (isFoursquareEntity(arrayClass)) {
                arrayValue[i] = parseEntity(arrayClass, jsonArray.getJSONObject(i), skipNonExistingFields);
            } else {
                throw new FoursquareApiException("Unknown array type: " + arrayClass);
            }
        }

        return arrayValue;
    } else if (clazz.equals(String.class)) {
        return jsonObject.getString(objectFieldName);
    } else if (clazz.equals(Integer.class)) {
        return jsonObject.getInt(objectFieldName);
    } else if (clazz.equals(Long.class)) {
        return jsonObject.getLong(objectFieldName);
    } else if (clazz.equals(Double.class)) {
        return jsonObject.getDouble(objectFieldName);
    } else if (clazz.equals(Boolean.class)) {
        return jsonObject.getBoolean(objectFieldName);
    } else if (isFoursquareEntity(clazz)) {
        return parseEntity(clazz, jsonObject.getJSONObject(objectFieldName), skipNonExistingFields);
    } else {
        throw new FoursquareApiException("Unknown type: " + clazz);
    }
}

From source file:io.github.moosbusch.lumpi.util.LumpiUtil.java

public static boolean isArray(Class<?> type) {
    return type.isArray();
}