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:org.baswell.routes.ResponseTypeMapper.java

static ResponseType mapResponseType(Method method, RouteConfiguration routeConfiguration) {
    Class returnType = method.getReturnType();
    if ((returnType == void.class) || (returnType == Void.class)) {
        return ResponseType.VOID;
    } else if (returnType.isArray() && (returnType.getComponentType() == byte.class)) {
        return ResponseType.BYTES_CONTENT;
    } else if (returnType == InputStream.class) {
        return ResponseType.STREAM_CONTENT;
    } else if ((returnType == String.class) && !routeConfiguration.returnedStringIsContent) {
        return ResponseType.FORWARD_DISPATCH;
    } else {//  w  w  w  .  ja  va2 s  .  c o m
        return ResponseType.STRING_CONTENT;
    }
}

From source file:ArrayExpander.java

public static Object expand(Object array, int newSize) {
    if (array == null) {
        return null;
    }/* w w w.ja v  a  2  s. c om*/
    Class c = array.getClass();
    if (c.isArray()) {
        int len = Array.getLength(array);
        if (len >= newSize) {
            return array;
        } else {
            Class cc = c.getComponentType();
            Object newArray = Array.newInstance(cc, newSize);
            System.arraycopy(array, 0, newArray, 0, len);
            return newArray;
        }
    } else {
        throw new ClassCastException("need  array");
    }
}

From source file:ArrayExpander.java

public static Object expandAtHead(Object array, int newSize) {
    if (array == null) {
        return null;
    }// w  w  w .  j av a  2  s.  c  o  m
    Class c = array.getClass();
    if (c.isArray()) {
        int len = Array.getLength(array);
        if (len >= newSize) {
            return array;
        } else {
            Class cc = c.getComponentType();
            Object newArray = Array.newInstance(cc, newSize);
            System.arraycopy(array, 0, newArray, newSize - len, len);
            return newArray;
        }
    } else {
        throw new ClassCastException("need  array");
    }
}

From source file:com.adobe.acs.commons.mcp.util.AnnotatedFieldDeserializer.java

private static Object convertValue(String value, Class<?> type) throws ParseException {
    Class clazz = type.isArray() ? type.getComponentType() : type;
    if (clazz.isPrimitive() || Number.class.isAssignableFrom(clazz) || clazz == Boolean.class) {
        return convertPrimitiveValue(value, clazz);
    } else if (clazz == String.class) {
        return value;
    } else if (clazz.isEnum()) {
        return Enum.valueOf((Class<Enum>) clazz, value);
    }/*  ww w . j  ava 2  s . co m*/

    return null;
}

From source file:Main.java

private static Object[] copyOf(Object[] obj, int newSize, Class<? extends Object> newType) {
    Object[] copy = ((Object) newType == (Object) Object[].class) ? (Object[]) new Object[newSize]
            : (Object[]) Array.newInstance(newType.getComponentType(), newSize);
    return copy;//from  w w w  . j  a  va  2s .  c  o m
}

From source file:com.britesnow.snow.util.ObjectUtil.java

/**
 * <div class="notes"> <strong>Notes:</strong>
 * <ul>/*  ww w . ja  va 2s  .  c om*/
 * <li>So far does not safeguard any type conversion (will throw an runtime exception if parsing fail)</li>
 * </ul>
 * </div>
 * 
 * @param <T>
 * @param values
 *            String arrays of the value to be converted
 * @param cls
 * @param defaultValues
 * @return The typed array given a value of the String.
 */
public static final <T> T getValue(String[] values, Class<T> cls, T defaultValues) {
    if (values != null && cls.isArray()) {
        Class compCls = cls.getComponentType();
        T resultArray = (T) Array.newInstance(compCls, values.length);
        int i = 0;
        for (String v : values) {
            Object r = getValue(v, compCls, null);
            Array.set(resultArray, i++, r);
        }
        return resultArray;
    } else {
        return defaultValues;
    }

}

From source file:it.greenvulcano.util.ArrayUtils.java

public static final List<?> arrayToList(Object arr) {
    List<?> list = null;//from   ww  w .j a v a2s  .com

    Class<?> ac = arr.getClass();
    if (!ac.isArray()) {
        throw new IllegalArgumentException("The input parameter isn't an array");
    }

    Class<?> act = ac.getComponentType();
    if (act.isPrimitive()) {
        if (act.equals(boolean.class)) {
            list = Arrays.asList(org.apache.commons.lang3.ArrayUtils.toObject((boolean[]) arr));
        } else if (act.equals(byte.class)) {
            list = Arrays.asList(org.apache.commons.lang3.ArrayUtils.toObject((byte[]) arr));
        } else if (act.equals(char.class)) {
            list = Arrays.asList(org.apache.commons.lang3.ArrayUtils.toObject((char[]) arr));
        } else if (act.equals(short.class)) {
            list = Arrays.asList(org.apache.commons.lang3.ArrayUtils.toObject((short[]) arr));
        } else if (act.equals(int.class)) {
            list = Arrays.asList(org.apache.commons.lang3.ArrayUtils.toObject((int[]) arr));
        } else if (act.equals(long.class)) {
            list = Arrays.asList(org.apache.commons.lang3.ArrayUtils.toObject((long[]) arr));
        } else if (act.equals(float.class)) {
            list = Arrays.asList(org.apache.commons.lang3.ArrayUtils.toObject((float[]) arr));
        } else if (act.equals(double.class)) {
            list = Arrays.asList(org.apache.commons.lang3.ArrayUtils.toObject((double[]) arr));
        }
    } else {
        list = arrayToList((Object[]) arr);
    }

    return list;
}

From source file:Main.java

/**
 * renderArray returns an array similar to a List. If the array type is an
 * object they are rendered with "render(object)" for each. If the array
 * type is a primitive each element is added directly to the string buffer
 * collecting the result.//from   ww w. ja v a  2 s  .c  o m
 * 
 * @param o
 * @param objectClass
 * @return
 */
private static StringBuffer renderArray(Object o, Class<?> objectClass) {
    Class<?> componentType = objectClass.getComponentType();
    StringBuffer sb = new StringBuffer(ARRAY_PREFIX);

    if (componentType.isPrimitive() == false) {
        Object[] oa = (Object[]) o;
        for (int i = 0; i < oa.length; i++) {
            if (i > 0) {
                sb.append(ELEMENT_SEPARATOR);
            }
            sb.append(render(oa[i]));
        }
    } else {
        if (Boolean.TYPE.equals(componentType)) {
            boolean[] ba = (boolean[]) o;
            for (int i = 0; i < ba.length; i++) {
                if (i > 0) {
                    sb.append(ELEMENT_SEPARATOR);
                }
                sb.append(ba[i]);
            }
        } else if (Integer.TYPE.equals(componentType)) {
            int[] ia = (int[]) o;
            for (int i = 0; i < ia.length; i++) {
                if (i > 0) {
                    sb.append(ELEMENT_SEPARATOR);
                }
                sb.append(ia[i]);
            }

        } else if (Long.TYPE.equals(componentType)) {
            long[] ia = (long[]) o;
            for (int i = 0; i < ia.length; i++) {
                if (i > 0) {
                    sb.append(ELEMENT_SEPARATOR);
                }
                sb.append(ia[i]);
            }
        } else if (Double.TYPE.equals(componentType)) {
            double[] ia = (double[]) o;
            for (int i = 0; i < ia.length; i++) {
                if (i > 0) {
                    sb.append(ELEMENT_SEPARATOR);
                }
                sb.append(ia[i]);
            }
        } else if (Float.TYPE.equals(componentType)) {
            float[] ia = (float[]) o;
            for (int i = 0; i < ia.length; i++) {
                if (i > 0) {
                    sb.append(ELEMENT_SEPARATOR);
                }
                sb.append(ia[i]);
            }
        } else if (Character.TYPE.equals(componentType)) {
            char[] ia = (char[]) o;
            for (int i = 0; i < ia.length; i++) {
                if (i > 0) {
                    sb.append(ELEMENT_SEPARATOR);
                }
                sb.append(ia[i]);
            }
        } else if (Short.TYPE.equals(componentType)) {
            short[] ia = (short[]) o;
            for (int i = 0; i < ia.length; i++) {
                if (i > 0) {
                    sb.append(ELEMENT_SEPARATOR);
                }
                sb.append(ia[i]);
            }
        } else if (Byte.TYPE.equals(componentType)) {
            byte[] ia = (byte[]) o;
            for (int i = 0; i < ia.length; i++) {
                if (i > 0) {
                    sb.append(ELEMENT_SEPARATOR);
                }
                sb.append(ia[i]);
            }
        }
    }
    sb.append(ARRAY_SUFFIX);
    return sb;
}

From source file:br.msf.commons.util.ArrayUtils.java

/**
 * Returns the dimension count of the given array.
 *
 * @param array The array to be evaluated.
 * @return The dimension count of the given array.
 *///w  ww.  j a  v  a 2  s . co m
public static int getDimension(final Object array) {
    if (array == null) {
        return 0;
    }
    int dim = 0;
    Class<?> c = array.getClass();
    while (c.isArray()) {
        c = c.getComponentType();
        dim++;
    }
    return dim;
}

From source file:ClassUtils.java

/**
 * Build a nice qualified name for an array:
 * component type class name + "[]"./*w  ww.j a va 2s .c  o m*/
 * @param clazz the array class
 * @return a qualified name for the array class
 */
private static String getQualifiedNameForArray(Class clazz) {
    StringBuffer buffer = new StringBuffer();
    while (clazz.isArray()) {
        clazz = clazz.getComponentType();
        buffer.append(ClassUtils.ARRAY_SUFFIX);
    }
    buffer.insert(0, clazz.getName());
    return buffer.toString();
}