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:org.springmodules.util.Objects.java

/**
 * Returns <code>true</code> if the given object is an array of primitives.
 * /*from  www  .  j a va 2 s.co  m*/
 * @param array
 *          the given object to check.
 * @return <code>true</code> if the given object is an array of primitives.
 */
public static boolean isArrayOfPrimitives(Object array) {
    boolean primitiveArray = false;

    if (array != null) {
        Class clazz = array.getClass();

        primitiveArray = clazz.isArray() && clazz.getComponentType().isPrimitive();
    }

    return primitiveArray;
}

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

private static void dumpObject(JSONArray jsonArray, Class type, Object array, int index)
        throws JSONSerializationException, JSONException {
    if (type.isPrimitive()) {
        jsonArray.put(String.valueOf(Array.get(array, index)));
    } else if (type == String.class) {
        String val = (String) Array.get(array, index);
        if (val != null) {
            jsonArray.put(val);
        }/*from  w w  w  .ja  va  2s . co m*/
    } else if (JSONSerializable.class.isAssignableFrom(type)) {
        JSONSerializable o = (JSONSerializable) Array.get(array, index);
        JSONObject jobj = serializeJSONObject(o);
        jsonArray.put(jobj);
    } else if (type.isArray() && array != null) {
        Class compType = type.getComponentType();
        Object subArray = Array.get(array, index);
        int len = Array.getLength(subArray);
        JSONArray arr = new JSONArray();
        for (int k = 0; k < len; ++k) {
            dumpObject(arr, compType, subArray, k);
        }
        jsonArray.put(arr);
    }
}

From source file:edu.umich.flowfence.common.ParceledPayload.java

public static boolean canParcelType(Class<?> clazz, boolean allowVoid) {
    // All primitives and wrapper types are parcelable, except Character and Void.
    if (ClassUtils.isPrimitiveOrWrapper(clazz)) {
        return (clazz != char.class && clazz != Character.class
                && (allowVoid || (clazz != void.class && clazz != Void.class)));
    }/*from  w  w w .  j  a  v  a 2s. c o m*/
    // String and CharSequence are parcelable.
    if (clazz == String.class || ClassUtils.isAssignable(clazz, CharSequence.class)) {
        return true;
    }
    // Object arrays are parcelable if their component type is parcelable.
    // Primitive boolean[], byte[], int[], and long[] arrays are parcelable.
    if (clazz.isArray()) {
        Class<?> componentType = clazz.getComponentType();
        if (componentType.isPrimitive()) {
            return (componentType == int.class || componentType == long.class || componentType == byte.class
                    || componentType == boolean.class);
        } else {
            return canParcelType(componentType, false);
        }
    }
    // Parcelable, obviously, is parcelable.
    // This covers Bundle as well.
    if (ClassUtils.isAssignable(clazz, Parcelable.class)) {
        return true;
    }
    // Map, List, and SparseArray are all parcelable, with restrictions on their component type
    // that we can't check here.
    if (ClassUtils.isAssignable(clazz, Map.class) || ClassUtils.isAssignable(clazz, List.class)
            || ClassUtils.isAssignable(clazz, SparseArray.class)) {
        return true;
    }
    // IBinder is parcelable.
    if (ClassUtils.isAssignable(clazz, IBinder.class)) {
        return true;
    }
    // Serializable is parcelable.
    if (ClassUtils.isAssignable(clazz, Serializable.class)) {
        return true;
    }
    return false;
}

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

/**
 * Convert runtime java.lang.Class to BCEL Type object.
 *
 * @param cl Java class/*  ww w. ja va  2  s .c om*/
 *
 * @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:be.fedict.eid.applet.service.impl.tlv.TlvParser.java

private static <T> T parseThrowing(byte[] file, Class<T> tlvClass) throws InstantiationException,
        IllegalAccessException, DataConvertorException, UnsupportedEncodingException {
    Field[] fields = tlvClass.getDeclaredFields();
    Map<Integer, Field> tlvFields = new HashMap<Integer, Field>();
    for (Field field : fields) {
        TlvField tlvFieldAnnotation = field.getAnnotation(TlvField.class);
        if (null == tlvFieldAnnotation) {
            continue;
        }/*from  w w w  . j  a  va 2s  .com*/
        int tagId = tlvFieldAnnotation.value();
        if (tlvFields.containsKey(new Integer(tagId))) {
            throw new IllegalArgumentException("TLV field duplicate: " + tagId);
        }
        tlvFields.put(new Integer(tagId), field);
    }
    T tlvObject = tlvClass.newInstance();

    int idx = 0;
    while (idx < file.length - 1) {
        byte tag = file[idx];
        idx++;
        byte lengthByte = file[idx];
        int length = lengthByte & 0x7f;
        while ((lengthByte & 0x80) == 0x80) {
            idx++;
            lengthByte = file[idx];
            length = (length << 7) + (lengthByte & 0x7f);
        }
        idx++;
        if (0 == tag) {
            idx += length;
            continue;
        }
        if (tlvFields.containsKey(new Integer(tag))) {
            Field tlvField = tlvFields.get(new Integer(tag));
            Class<?> tlvType = tlvField.getType();
            ConvertData convertDataAnnotation = tlvField.getAnnotation(ConvertData.class);
            byte[] tlvValue = copy(file, idx, length);
            Object fieldValue;
            if (null != convertDataAnnotation) {
                Class<? extends DataConvertor<?>> dataConvertorClass = convertDataAnnotation.value();
                DataConvertor<?> dataConvertor = dataConvertorClass.newInstance();
                fieldValue = dataConvertor.convert(tlvValue);
            } else if (String.class == tlvType) {
                fieldValue = new String(tlvValue, "UTF-8");
            } else if (Boolean.TYPE == tlvType) {
                fieldValue = true;
            } else if (tlvType.isArray() && Byte.TYPE == tlvType.getComponentType()) {
                fieldValue = tlvValue;
            } else {
                throw new IllegalArgumentException("unsupported field type: " + tlvType.getName());
            }
            LOG.debug("setting field: " + tlvField.getName());
            if (null != tlvField.get(tlvObject) && false == tlvField.getType().isPrimitive()) {
                throw new RuntimeException("field was already set: " + tlvField.getName());
            }
            tlvField.setAccessible(true);
            tlvField.set(tlvObject, fieldValue);
        } else {
            LOG.debug("unknown tag: " + (tag & 0xff) + ", length: " + length);
        }
        idx += length;
    }
    return tlvObject;
}

From source file:com.doitnext.jsonschema.generator.SchemaGen.java

private static boolean handleArrayType(Class<?> classz, StringBuilder sb, JsonSchemaProperty propertyDescriptor,
        Map<String, String> declarations, String uriPrefix) {
    boolean result = false;
    Map<String, Boolean> flags = new HashMap<String, Boolean>();
    flags.put("hasTitle", false);
    flags.put("hasDescription", false);
    flags.put("hasType", false);
    flags.put("hasEnum", false);

    Class<?> itemClass = null;
    String prepend = ", ";
    if (classz.isArray()) {
        sb.append("\"type\":\"array\"");
        itemClass = classz.getComponentType();
        flags.put("hasType", true);
        result = true;/*  w w w.  j  a  v  a2s  .c o m*/
    } else if (Collection.class.isAssignableFrom(classz)) {
        if (propertyDescriptor.collectionContains().equals(JsonSchemaProperty.DEFAULT.class))
            throw new IllegalArgumentException(String.format(
                    "The property descriptor is not valid.  You must specify collectionContains for collection type properties. %s",
                    propertyDescriptor));
        itemClass = propertyDescriptor.collectionContains();
        sb.append("\"type\":\"array\"");
        flags.put("hasType", false);
        result = true;
    }
    if (result == true) {

        handlePropertyDescriptor(sb, propertyDescriptor, flags, prepend, result, uriPrefix);

        boolean inline = true;
        StringBuilder sb2 = new StringBuilder();
        sb2.append("{");
        if (!handleSimpleType(itemClass, sb2, null, declarations, uriPrefix)) {
            if (!handleArrayType(itemClass, sb2, propertyDescriptor, declarations, uriPrefix)) {
                inline = false;
                handleObjectType(itemClass, sb2, propertyDescriptor, declarations, false, uriPrefix);
            }
        }
        sb2.append("}");

        if (inline) {
            sb.append(prepend);
            sb.append("\"items\":");
            sb.append(sb2.toString());
        } else {
            String id = null;
            JsonSchemaClass jsc = itemClass.getAnnotation(JsonSchemaClass.class);
            if (jsc != null)
                id = jsc.id();
            else
                id = itemClass.getName();
            declarations.put(id, sb2.toString());

            sb.append(prepend);
            sb.append("\"items\":{\"$ref\":\"" + uriPrefix);
            sb.append(id);
            sb.append("\"}");
        }
    }
    return result;
}

From source file:eu.stratosphere.api.java.typeutils.TypeExtractor.java

@SuppressWarnings("unchecked")
public static <X> TypeInformation<X> getForClass(Class<X> clazz) {
    Validate.notNull(clazz);/* w  ww. j  a v  a  2s.  c o  m*/

    // check for abstract classes or interfaces
    if (Modifier.isInterface(clazz.getModifiers())
            || (Modifier.isAbstract(clazz.getModifiers()) && !clazz.isArray())) {
        throw new InvalidTypesException("Interfaces and abstract classes are not valid types.");
    }

    // check for arrays
    if (clazz.isArray()) {

        // primitive arrays: int[], byte[], ...
        PrimitiveArrayTypeInfo<X> primitiveArrayInfo = PrimitiveArrayTypeInfo.getInfoFor(clazz);
        if (primitiveArrayInfo != null) {
            return primitiveArrayInfo;
        }

        // basic type arrays: String[], Integer[], Double[]
        BasicArrayTypeInfo<X, ?> basicArrayInfo = BasicArrayTypeInfo.getInfoFor(clazz);
        if (basicArrayInfo != null) {
            return basicArrayInfo;
        }

        // object arrays
        else {
            return ObjectArrayTypeInfo.getInfoFor(clazz);
        }
    }

    // check for writable types
    if (Writable.class.isAssignableFrom(clazz)) {
        return (TypeInformation<X>) WritableTypeInfo.getWritableTypeInfo((Class<? extends Writable>) clazz);
    }

    // check for basic types
    TypeInformation<X> basicTypeInfo = BasicTypeInfo.getInfoFor(clazz);
    if (basicTypeInfo != null) {
        return basicTypeInfo;
    }

    // check for subclasses of Value
    if (Value.class.isAssignableFrom(clazz)) {
        Class<? extends Value> valueClass = clazz.asSubclass(Value.class);
        return (TypeInformation<X>) ValueTypeInfo.getValueTypeInfo(valueClass);
    }

    // check for subclasses of Tuple
    if (Tuple.class.isAssignableFrom(clazz)) {
        throw new InvalidTypesException(
                "Type information extraction for tuples cannot be done based on the class.");
    }

    // return a generic type
    return new GenericTypeInfo<X>(clazz);
}

From source file:com.g3net.tool.CollectionUtils.java

/**
 * ?set//from  w w  w . j a va  2s .c  o  m
 * 
 * @param set
 * @return
 */
public static String toString(Set set) {

    if (set == null)
        return "";
    String s = "{";
    for (Object value : set) {
        Class c = value.getClass();
        if (value instanceof Map) {
            s = s + (s.length() == 1 ? "" : ",") + "{" + toString((Map) value) + "}";
        } else if (value instanceof List) {
            s = s + (s.length() == 1 ? "" : ",") + "{" + toString((List) value) + "}";
        } else if (value instanceof Set) {
            s = s + (s.length() == 1 ? "" : ",") + "{" + toString((Set) value) + "}";
        } else {

            if (c.isArray()) {
                s = s + (s.length() == 1 ? "" : ",") + "[" + ArrayUtils.toString(value, ",") + "]";
            } else {
                s = s + (s.length() == 1 ? "" : ",") + value;
            }
        }
    }
    s = s + "}";
    return s;
}

From source file:com.g3net.tool.CollectionUtils.java

/**
 * ?list//from  w  ww.  j a va 2 s .co  m
 * 
 * @param list
 * @return
 */
public static String toString(List list) {

    if (list == null)
        return "";
    String s = "{";
    for (Object value : list) {
        Class c = value.getClass();
        if (value instanceof Map) {

            s = s + (s.length() == 1 ? "" : ",") + "{" + toString((Map) value) + "}";
        } else if (value instanceof List) {
            s = s + (s.length() == 1 ? "" : ",") + "{" + toString((List) value) + "}";
        } else if (value instanceof Set) {
            s = s + (s.length() == 1 ? "" : ",") + "{" + toString((Set) value) + "}";
        } else {

            if (c.isArray()) {
                s = s + (s.length() == 1 ? "" : ",") + "[" + ArrayUtils.toString(value, ",") + "]";
            } else {
                s = s + (s.length() == 1 ? "" : ",") + "" + value;
            }
        }

    }
    s = s + "}";
    return s;
}

From source file:be.fedict.commons.eid.consumer.tlv.TlvParser.java

private static <T> T parseThrowing(final byte[] file, final Class<T> tlvClass) throws InstantiationException,
        IllegalAccessException, DataConvertorException, UnsupportedEncodingException {
    final Field[] fields = tlvClass.getDeclaredFields();
    final Map<Integer, Field> tlvFields = new HashMap<Integer, Field>();
    final T tlvObject = tlvClass.newInstance();
    for (Field field : fields) {
        final TlvField tlvFieldAnnotation = field.getAnnotation(TlvField.class);
        if (null != tlvFieldAnnotation) {
            final int tagId = tlvFieldAnnotation.value();
            if (tlvFields.containsKey(new Integer(tagId))) {
                throw new IllegalArgumentException("TLV field duplicate: " + tagId);
            }//from w  ww .jav  a  2 s .co m
            tlvFields.put(new Integer(tagId), field);
        }
        final OriginalData originalDataAnnotation = field.getAnnotation(OriginalData.class);
        if (null != originalDataAnnotation) {
            field.setAccessible(true);
            field.set(tlvObject, file);
        }
    }

    int idx = 0;
    while (idx < file.length - 1) {
        final byte tag = file[idx];
        idx++;
        byte lengthByte = file[idx];
        int length = lengthByte & 0x7f;
        while ((lengthByte & 0x80) == 0x80) {
            idx++;
            lengthByte = file[idx];
            length = (length << 7) + (lengthByte & 0x7f);
        }
        idx++;
        if (0 == tag) {
            idx += length;
            continue;
        }
        if (tlvFields.containsKey(new Integer(tag))) {
            final Field tlvField = tlvFields.get(new Integer(tag));
            final Class<?> tlvType = tlvField.getType();
            final ConvertData convertDataAnnotation = tlvField.getAnnotation(ConvertData.class);
            final byte[] tlvValue = copy(file, idx, length);
            Object fieldValue;
            if (null != convertDataAnnotation) {
                final Class<? extends DataConvertor<?>> dataConvertorClass = convertDataAnnotation.value();
                final DataConvertor<?> dataConvertor = dataConvertorClass.newInstance();
                fieldValue = dataConvertor.convert(tlvValue);
            } else if (String.class == tlvType) {
                fieldValue = new String(tlvValue, "UTF-8");
            } else if (Boolean.TYPE == tlvType) {
                fieldValue = true;
            } else if (tlvType.isArray() && Byte.TYPE == tlvType.getComponentType()) {
                fieldValue = tlvValue;
            } else {
                throw new IllegalArgumentException("unsupported field type: " + tlvType.getName());
            }
            if (null != tlvField.get(tlvObject) && false == tlvField.getType().isPrimitive()) {
                throw new RuntimeException("field was already set: " + tlvField.getName());
            }
            tlvField.setAccessible(true);
            tlvField.set(tlvObject, fieldValue);
        } else {
            LOG.debug("unknown tag: " + (tag & 0xff) + ", length: " + length);
        }
        idx += length;
    }
    return tlvObject;
}