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.cloudata.core.common.io.CObjectWritable.java

private static void writeColumnValue(DataOutput out, Object instance, Class declaredClass, CloudataConf conf,
        int length) throws IOException {
    int shortByteSize = CWritableUtils.getShortByteSize();
    int intByteSize = CWritableUtils.getIntByteSize();

    int totalByteSize = 0;

    long startTime = System.currentTimeMillis();
    for (int i = 0; i < length; i++) {
        ColumnValue columnValue = (ColumnValue) Array.get(instance, i);
        totalByteSize += shortByteSize;/*from  ww  w .j a v a  2 s  .com*/
        totalByteSize += columnValue.size();
    }
    //    long endTime = System.currentTimeMillis();
    //LOG.fatal("writeColumnValue1:length=" + length + ",bytes=" + totalByteSize + ",time=" + (endTime - startTime));

    out.writeInt(totalByteSize);

    //    startTime = System.currentTimeMillis();
    for (int i = 0; i < length; i++) {
        writeObject(out, Array.get(instance, i), declaredClass.getComponentType(), conf, true);
    }
    long endTime = System.currentTimeMillis();
    //LOG.fatal("writeColumnValue2:time=" + (endTime - startTime));
}

From source file:com.freetmp.common.util.ClassUtils.java

private static String getQualifiedNameForArray(Class<?> clazz) {
    StringBuilder result = new StringBuilder();
    while (clazz.isArray()) {
        clazz = clazz.getComponentType();
        result.append(ClassUtils.ARRAY_SUFFIX);
    }//from   w w w.j a va  2s . co m
    result.insert(0, clazz.getName());
    return result.toString();
}

From source file:com.freetmp.common.util.ClassUtils.java

public static boolean isPrimitiveWrapperArray(Class<?> clazz) {
    Assert.notNull(clazz, "Class must not be null");
    return (clazz.isArray() && isPrimitiveWrapper(clazz.getComponentType()));
}

From source file:jef.tools.ArrayUtils.java

/**
 * ??//w w w .j  ava  2 s  .co  m
 * java.util.Arrays??????9???
 * 
 * @param a1
 *            Object
 * @param a2
 *            Object,
 * @return
 * @see java.util.Arrays#equals(boolean[], boolean[])
 * @see java.util.Arrays#equals(byte[], byte[])
 * @see java.util.Arrays#equals(char[], char[])
 * @see java.util.Arrays#equals(double[], double[])
 * @see java.util.Arrays#equals(float[], float[])
 * @see java.util.Arrays#equals(int[], int[])
 * @see java.util.Arrays#equals(long[], long[])
 * @see java.util.Arrays#equals(short[], short[])
 * @see java.util.Arrays#equals(Object[], Object[])
 * @throws IllegalArgumentException
 *             ?
 */
public static boolean equals(Object a1, Object a2) {
    if (a1 == a2)
        return true;
    if (a1 == null || a2 == null)
        return false;
    Class<?> clz1 = a1.getClass();
    Class<?> clz2 = a2.getClass();
    if (!clz1.isArray() || !clz2.isArray()) {
        throw new IllegalArgumentException("must comapre between two Array.");
    }
    clz1 = clz1.getComponentType();
    clz2 = clz2.getComponentType();
    if (clz1.isPrimitive() != clz2.isPrimitive()) {
        return false;
    }
    if (clz1 == int.class) {
        return Arrays.equals((int[]) a1, (int[]) a2);
    } else if (clz1 == short.class) {
        return Arrays.equals((short[]) a1, (short[]) a2);
    } else if (clz1 == long.class) {
        return Arrays.equals((long[]) a1, (long[]) a2);
    } else if (clz1 == float.class) {
        return Arrays.equals((float[]) a1, (float[]) a2);
    } else if (clz1 == double.class) {
        return Arrays.equals((double[]) a1, (double[]) a2);
    } else if (clz1 == boolean.class) {
        return Arrays.equals((boolean[]) a1, (boolean[]) a2);
    } else if (clz1 == byte.class) {
        return Arrays.equals((byte[]) a1, (byte[]) a2);
    } else if (clz1 == char.class) {
        return Arrays.equals((char[]) a1, (char[]) a2);
    } else {
        return Arrays.equals((Object[]) a1, (Object[]) a2);
    }
}

From source file:egovframework.rte.itl.integration.type.ListType.java

@Override
public boolean isAssignableFrom(Class<?> clazz) {
    if (super.isAssignableFrom(clazz)) {
        return true;
    }//  w  w w.j ava 2 s. co m
    if (clazz.isArray()) {
        return elementType.isAssignableFrom(clazz.getComponentType());
    }
    return false;
}

From source file:com.metaparadigm.jsonrpc.ArraySerializer.java

public Object unmarshall(SerializerState state, Class clazz, Object o) throws UnmarshallException {
    JSONArray jso = (JSONArray) o;/*from w  w  w .ja v a 2 s.co  m*/
    Class cc = clazz.getComponentType();
    int i = 0;
    try {
        if (clazz == int[].class) {
            int arr[] = new int[jso.length()];
            for (; i < jso.length(); i++)
                arr[i] = ((Number) ser.unmarshall(state, cc, jso.get(i))).intValue();
            return arr;
        } else if (clazz == byte[].class) {
            byte arr[] = new byte[jso.length()];
            for (; i < jso.length(); i++)
                arr[i] = ((Number) ser.unmarshall(state, cc, jso.get(i))).byteValue();
            return arr;
        } else if (clazz == short[].class) {
            short arr[] = new short[jso.length()];
            for (; i < jso.length(); i++)
                arr[i] = ((Number) ser.unmarshall(state, cc, jso.get(i))).shortValue();
            return arr;
        } else if (clazz == long[].class) {
            long arr[] = new long[jso.length()];
            for (; i < jso.length(); i++)
                arr[i] = ((Number) ser.unmarshall(state, cc, jso.get(i))).longValue();
            return arr;
        } else if (clazz == float[].class) {
            float arr[] = new float[jso.length()];
            for (; i < jso.length(); i++)
                arr[i] = ((Number) ser.unmarshall(state, cc, jso.get(i))).floatValue();
            return arr;
        } else if (clazz == double[].class) {
            double arr[] = new double[jso.length()];
            for (; i < jso.length(); i++)
                arr[i] = ((Number) ser.unmarshall(state, cc, jso.get(i))).doubleValue();
            return arr;
        } else if (clazz == char[].class) {
            char arr[] = new char[jso.length()];
            for (; i < jso.length(); i++)
                arr[i] = ((String) ser.unmarshall(state, cc, jso.get(i))).charAt(0);
            return arr;
        } else if (clazz == boolean[].class) {
            boolean arr[] = new boolean[jso.length()];
            for (; i < jso.length(); i++)
                arr[i] = ((Boolean) ser.unmarshall(state, cc, jso.get(i))).booleanValue();
            return arr;
        } else {
            Object arr[] = (Object[]) Array.newInstance(clazz.getComponentType(), jso.length());
            for (; i < jso.length(); i++)
                arr[i] = ser.unmarshall(state, cc, jso.get(i));
            return arr;
        }
    } catch (UnmarshallException e) {
        throw new UnmarshallException("element " + i + " " + e.getMessage());
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
}

From source file:org.crazydog.util.spring.ClassUtils.java

/**
 * Check if the given class represents an array of primitives,
 * i.e. boolean, byte, char, short, int, long, float, or double.
 * @param clazz the class to check//from   www.j ava  2 s . co  m
 * @return whether the given class is a primitive array class
 */
public static boolean isPrimitiveArray(Class<?> clazz) {
    org.springframework.util.Assert.notNull(clazz, "Class must not be null");
    return (clazz.isArray() && clazz.getComponentType().isPrimitive());
}

From source file:com.bstek.dorado.data.type.manager.DefaultDataTypeManager.java

/**
 * ????DataType???Class<br>/*from   w  ww  . jav a 2s .c  o  m*/
 * ???DataTypeDefinitionManager??
 * 
 * @param type
 *            Class
 * @throws Exception
 */
@Override
protected DataTypeDefinition getDataTypeDefinition(Type type) throws Exception {
    DataTypeDefinition dataType = null;
    if (type instanceof Class<?> && ((Class<?>) type).isArray()) {
        Class<?> cl = (Class<?>) type;
        DataTypeDefinition componentDataType = getDataTypeDefinitionByType(cl.getComponentType());
        String name = ARRAY + '[' + componentDataType.getName() + ']';
        dataType = getDataTypeDefinitionManager().getDefinition(name);
    } else if (type instanceof ParameterizedType) {
        Class<?> elementType = null;
        Type[] actualTypeArguments = ((ParameterizedType) type).getActualTypeArguments();
        if (actualTypeArguments.length > 0) {
            elementType = (Class<?>) actualTypeArguments[0];
        }
        if (elementType != null) {
            DataTypeDefinition elementDataType = getDataTypeDefinitionByType(elementType);
            if (elementDataType != null) {
                String name = '[' + elementDataType.getName() + ']';
                dataType = getDataTypeDefinitionManager().getDefinition(name);
            }
        }
    }
    if (dataType == null && type instanceof Class<?>) {
        dataType = getDataTypeDefinitionByType((Class<?>) type);
    }
    return dataType;
}

From source file:name.ikysil.beanpathdsl.codegen.Context.java

private Class<?> resolveClass(Class<?> clazz) {
    if (clazz.isArray()) {
        return clazz.getComponentType();
    }//  w  w w .  j av  a2 s. com
    return clazz;
}

From source file:com.dinstone.ut.faststub.internal.StubMethodInvocation.java

/**
 * {@inheritDoc}//from   ww  w  .j a v a 2  s. c  om
 * 
 */
public Object invoke(Method method, Object[] args) throws Throwable {
    ApplicationContext stubContext = getStubContext(method);
    Object retObj = stubContext.getBean(method.getName());
    // handle exception
    handleException(retObj);

    // handle array object
    Class<?> retType = method.getReturnType();
    if (retType.isArray() && retObj instanceof List<?>) {
        List<?> list = (List<?>) retObj;
        int len = list.size();
        Object arrObj = Array.newInstance(retType.getComponentType(), len);
        for (int i = 0; i < len; i++) {
            Array.set(arrObj, i, list.get(i));
        }
        return arrObj;
    }

    return retObj;
}