Example usage for java.lang Class getComponentType

List of usage examples for java.lang Class getComponentType

Introduction

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

Prototype

public Class<?> getComponentType() 

Source Link

Document

Returns the Class representing the component type of an array.

Usage

From source file:com.wordnik.swagger.testframework.APITestRunner.java

/**
 * Converts JSON string to object.// ww  w  . j a v  a2  s.  c  o m
 */
public static Object convertJSONStringToObject(String inputJSON, Class objectType) throws Exception {
    boolean isArray = false;
    boolean isList = false;
    Class className = objectType;
    String ObjectTypeName = objectType.getName();

    //identify if the input is a array
    if (ObjectTypeName.startsWith("[")) {
        isArray = true;
        className = objectType.getComponentType();
    }

    //identify if the input is a list
    if (List.class.isAssignableFrom(objectType)) {
        isList = true;
    }

    if (isArray || isList) {
        Object responseObject = mapper.readValue(inputJSON, TypeFactory.type(objectType));
        return responseObject;
    } else {
        return APIInvoker.deserialize(inputJSON, className);
    }
}

From source file:com.jilk.ros.rosbridge.implementation.JSON.java

private static Object convertJSONArrayToArray(JSONArray ja, Class c, Registry<Class> r) {
    Object result = Array.newInstance(c, ja.size());
    for (int i = 0; i < ja.size(); i++) {
        Object lookup = ja.get(i);
        Object value = null;/*from   ww w  .j  a  va  2 s.c  o m*/
        if (lookup != null) {
            if (lookup.getClass().equals(JSONObject.class))
                value = convertJSONObjectToMessage((JSONObject) lookup, c, r);
            else if (lookup.getClass().equals(JSONArray.class)) // this is not actually allowed in ROS
                value = convertJSONArrayToArray((JSONArray) lookup, c.getComponentType(), r);
            else
                value = convertJSONPrimitiveToPrimitive(lookup, c);
            Array.set(result, i, value);
        }
    }

    return result;
}

From source file:ArrayUtils.java

@SuppressWarnings("unchecked")
public static <T, U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
    final T[] copy = ((Object) newType == (Object) Object[].class) ? (T[]) new Object[newLength]
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
    System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
    return copy;/*from w ww. java2 s  .  co  m*/
}

From source file:com.sunchenbin.store.feilong.core.lang.ClassUtil.java

/**
 *  class info map for LOGGER./*from   w w w. j  a va 2  s .c o m*/
 *
 * @param klass
 *            the clz
 * @return the map for log
 */
public static Map<String, Object> getClassInfoMapForLog(Class<?> klass) {
    if (Validator.isNullOrEmpty(klass)) {
        return Collections.emptyMap();
    }

    Map<String, Object> map = new LinkedHashMap<String, Object>();

    map.put("clz.getCanonicalName()", klass.getCanonicalName());//"com.sunchenbin.store.feilong.core.date.DatePattern"
    map.put("clz.getName()", klass.getName());//"com.sunchenbin.store.feilong.core.date.DatePattern"
    map.put("clz.getSimpleName()", klass.getSimpleName());//"DatePattern"

    map.put("clz.getComponentType()", klass.getComponentType());
    // ?? ,voidboolean?byte?char?short?int?long?float  double?
    map.put("clz.isPrimitive()", klass.isPrimitive());

    // ??,
    map.put("clz.isLocalClass()", klass.isLocalClass());
    // ????,?,?????
    map.put("clz.isMemberClass()", klass.isMemberClass());

    //isSynthetic()?Class????java??false,?true,JVM???,java??????
    map.put("clz.isSynthetic()", klass.isSynthetic());
    map.put("clz.isArray()", klass.isArray());
    map.put("clz.isAnnotation()", klass.isAnnotation());

    //??true
    map.put("clz.isAnonymousClass()", klass.isAnonymousClass());
    map.put("clz.isEnum()", klass.isEnum());

    return map;
}

From source file:me.xiaopan.android.gohttp.requestobject.RequestParser.java

/**
 * ?type/* w w  w .ja v  a  2  s .  c  o m*/
 */
private static boolean isArrayByType(Field field, Class<?> type) {
    Class<?> fieldType = field.getType();
    return fieldType.isArray() && type.isAssignableFrom(fieldType.getComponentType());
}

From source file:com.dsj.core.beans.ObjectUtils.java

/**
 * Convert a primitive array to an object array of primitive wrapper
 * objects./*  w  ww  .  j a  va2  s  .c  o  m*/
 * 
 * @param primitiveArray
 *            the primitive array
 * @return the object array
 * @throws IllegalArgumentException
 *             if the parameter is not a primitive array
 */
public static Object[] toObjectArray(Object primitiveArray) {
    if (primitiveArray == null) {
        return new Object[0];
    }
    Class clazz = primitiveArray.getClass();
    if (!clazz.isArray() || !clazz.getComponentType().isPrimitive()) {
        throw new IllegalArgumentException("The specified parameter is not a primitive array");
    }
    int length = Array.getLength(primitiveArray);
    if (length == 0) {
        return new Object[0];
    }
    Class wrapperType = Array.get(primitiveArray, 0).getClass();
    Object[] newArray = (Object[]) Array.newInstance(wrapperType, length);
    for (int i = 0; i < length; i++) {
        newArray[i] = Array.get(primitiveArray, i);
    }
    return newArray;
}

From source file:com.xwtec.xwserver.util.json.util.JSONUtils.java

/**
 * Returns the inner-most component type of an Array.
 */// w  ww.  ja  v a2s  . c  o  m
public static Class getInnerComponentType(Class type) {
    if (!type.isArray()) {
        return type;
    }
    return getInnerComponentType(type.getComponentType());
}

From source file:org.springmodules.util.Objects.java

/**
 * Returns <code>true</code> if the given object is an array of primitives.
 * // w  w  w .  j a  v  a 2s  .  com
 * @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.github.dozermapper.core.util.MappingUtils.java

public static Class<?> determineCustomConverter(FieldMap fieldMap, Cache converterByDestTypeCache,
        CustomConverterContainer customConverterContainer, Class<?> srcClass, Class<?> destClass) {
    if (customConverterContainer == null) {
        return null;
    }/*from   w ww .j a  va  2s .  co  m*/

    // This method is messy. Just trying to isolate the junk into this one method instead of spread across the mapping
    // processor until a better solution can be put into place
    // For indexed mapping, need to use the actual class at index to determine the custom converter.
    if (fieldMap != null && fieldMap.isDestFieldIndexed()) {
        if (destClass.isArray()) {
            destClass = destClass.getComponentType();
        } else if (destClass.isAssignableFrom(Collection.class) && fieldMap.getDestHintContainer() != null
                && !fieldMap.getDestHintContainer().hasMoreThanOneHint()) {
            // use hint when trying to find a custom converter
            destClass = fieldMap.getDestHintContainer().getHint();
        }
    }

    return findCustomConverter(converterByDestTypeCache, customConverterContainer, srcClass, destClass);
}

From source file:jef.tools.collection.CollectionUtil.java

/**
 * ?/*  w w  w  .ja va2s  .  com*/
 * 
 * @param type
 * @return ??null,???
 */
public static Type getComponentType(Type type) {
    if (type instanceof GenericArrayType) {
        return ((GenericArrayType) type).getGenericComponentType();
    } else if (type instanceof Class) {
        Class<?> rawType = (Class<?>) type;
        if (rawType.isArray()) {
            return rawType.getComponentType();
        } else if (Collection.class.isAssignableFrom(rawType)) {
            // ??Object
            return Object.class;
        }
    } else if (type instanceof ParameterizedType) {
        ParameterizedType pType = (ParameterizedType) type;
        Type rawType = pType.getRawType();
        if (isCollection(rawType)) {
            return pType.getActualTypeArguments()[0];
        }
    }
    return null;
}