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:Main.java

public static String getType(Class<?> paramClass) {
    if (paramClass.isArray()) {
        return "[" + getDesc(paramClass.getComponentType());
    }/*from w  w  w.  j  a  v  a 2 s .  c om*/
    if (!paramClass.isPrimitive()) {
        return paramClass.getName().replaceAll("\\.", "/");
    }
    return getPrimitiveLetter(paramClass);
}

From source file:ShowClass.java

public static String typeName(Class t) {
    String brackets = "";
    while (t.isArray()) {
        brackets += "[]";
        t = t.getComponentType();
    }//from   w  w  w. ja va2  s. c o m
    String name = t.getName();
    int pos = name.lastIndexOf('.');
    if (pos != -1)
        name = name.substring(pos + 1);
    return name + brackets;
}

From source file:Main.java

public static String typename(Class t) {
    String brackets = "";
    while (t.isArray()) {
        brackets += "[]";
        t = t.getComponentType();
    }//from   w w  w  .  j  a  va  2s  . c o  m
    String name = t.getName();
    int pos = name.lastIndexOf('.');
    if (pos != -1)
        name = name.substring(pos + 1);
    return name + brackets;
}

From source file:Main.java

public static String canonicalName(Class clazz) {
    StringBuilder name = new StringBuilder();

    if (clazz.isArray()) {
        name.append(canonicalName(clazz.getComponentType()));
        name.append("[]");
    } else if (clazz.getDeclaringClass() == null) {
        name.append(clazz.getName());//from  w w  w  .  j  av a  2 s .  co m
    } else {
        name.append(canonicalName(clazz.getDeclaringClass()));
        name.append(".");
        name.append(clazz.getName().substring(clazz.getDeclaringClass().getName().length() + 1));
    }

    return name.toString();
}

From source file:ArrayExpander.java

public static Object merge(Object array1, Object array2) {
    if (array1 == null) {
        return null;
    }/*from ww w. j a va2s .co  m*/
    if (array2 == null) {
        return array1;
    }
    Class c = array1.getClass();
    if (c.isArray() && array2.getClass().isArray()) {
        Class cc = c.getComponentType();
        Object newArray = Array.newInstance(cc, Array.getLength(array1) + Array.getLength(array2));
        System.arraycopy(array1, 0, newArray, 0, Array.getLength(array1));
        System.arraycopy(array2, 0, newArray, Array.getLength(array1), Array.getLength(array2));
        return newArray;
    } else {
        throw new ClassCastException("need  array");
    }

}

From source file:Main.java

public static int getArrayDimension(Object array) {
    int dimension = 0;
    Class c = array.getClass();
    if (!c.isArray()) {
        throw new IllegalArgumentException("Object is not  an  array");
    }//  w  w  w . j a v a2 s .c om
    while (c.isArray()) {
        dimension++;
        c = c.getComponentType();
    }
    return dimension;
}

From source file:Main.java

/**
 * Creates a class identifier of form Labc/abc;, from a Class.
 *//*w  ww . ja v a2s. co  m*/
public static String ci(Class n) {
    if (n.isArray()) {
        n = n.getComponentType();
        if (n.isPrimitive()) {
            if (n == Byte.TYPE) {
                return "[B";
            } else if (n == Boolean.TYPE) {
                return "[Z";
            } else if (n == Short.TYPE) {
                return "[S";
            } else if (n == Character.TYPE) {
                return "[C";
            } else if (n == Integer.TYPE) {
                return "[I";
            } else if (n == Float.TYPE) {
                return "[F";
            } else if (n == Double.TYPE) {
                return "[D";
            } else if (n == Long.TYPE) {
                return "[J";
            } else {
                throw new RuntimeException("Unrecognized type in compiler: " + n.getName());
            }
        } else {
            return "[" + ci(n);
        }
    } else {
        if (n.isPrimitive()) {
            if (n == Byte.TYPE) {
                return "B";
            } else if (n == Boolean.TYPE) {
                return "Z";
            } else if (n == Short.TYPE) {
                return "S";
            } else if (n == Character.TYPE) {
                return "C";
            } else if (n == Integer.TYPE) {
                return "I";
            } else if (n == Float.TYPE) {
                return "F";
            } else if (n == Double.TYPE) {
                return "D";
            } else if (n == Long.TYPE) {
                return "J";
            } else if (n == Void.TYPE) {
                return "V";
            } else {
                throw new RuntimeException("Unrecognized type in compiler: " + n.getName());
            }
        } else {
            return "L" + p(n) + ";";
        }
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T, U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
    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 w w . jav a2  s . c o m*/
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static final <T, U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
    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;//  www  . j  a va 2  s.  co  m
}

From source file:Main.java

protected static <T, U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
    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 ww  w  .ja  v a 2s. c  om*/
}