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

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

From source file:ArrayDemo.java

/** 
 * Copy an array and return the copy.//from  w  w w  .ja  va2s . co  m
 *
 * @param input The array to copy.
 *
 * @return The coppied array.
 *
 * @throws IllegalArgumentException If input is not an array.
 */
public static Object copyArray(final Object input) {
    final Class type = input.getClass();
    if (!type.isArray()) {
        throw new IllegalArgumentException();
    }
    final int length = Array.getLength(input);
    final Class componentType = type.getComponentType();

    final Object result = Array.newInstance(componentType, length);
    for (int idx = 0; idx < length; idx++) {
        Array.set(result, idx, Array.get(input, idx));
    }
    return result;
}

From source file:Main.java

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

From source file:ArrayExpander.java

public static Object merge(Object array1, Object array2) {
    if (array1 == null) {
        return null;
    }/*from   ww  w.  j  a  va2  s  .c om*/
    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 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  .c  o 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:Main.java

public static String canBeABeanType(Class<?> paramClass) {
    if (paramClass.isAnnotation())
        return "annotation";
    if (paramClass.isArray())
        return "array";
    if (paramClass.isEnum())
        return "enum";
    if (paramClass.isPrimitive())
        return "primitive";
    return null;//from   ww w .j  a  v  a  2 s .c o m
}

From source file:Main.java

public static String idOfClass(Class<?> clazz) {
    Class<?> theClass = clazz;
    StringBuilder sb = new StringBuilder();
    if (theClass.isArray()) {
        do {//from  w w w.j a  va 2  s .co m
            sb.append("Array"); //$NON-NLS-1$
            theClass = theClass.getComponentType();
        } while (theClass.isArray());
    }
    String clazzName = theClass.getName();
    clazzName = clazzName.substring(clazzName.lastIndexOf('.') + 1);
    return clazzName + sb.toString();
}

From source file:SampleArrayReflection.java

static void printArrayNames(Object target) {
    Class targetClass = target.getClass();
    Field[] publicFields = targetClass.getFields();
    for (int i = 0; i < publicFields.length; i++) {
        String fieldName = publicFields[i].getName();
        Class typeClass = publicFields[i].getType();
        String fieldType = typeClass.getName();
        if (typeClass.isArray()) {
            System.out.println("Name: " + fieldName + ", Type: " + fieldType);
        }//from w  w w  .j a v  a 2  s .c  om
    }
}

From source file:Main.java

/**
 * @return an Object array for the given object.
 *
 * @param obj  Object to convert to an array.  Converts primitive
 *             arrays to Object arrays consisting of their wrapper
 *             classes.  If the object is not an array (object or primitve)
 *             then a new array of the given type is created and the
 *             object is set as the sole element.
 *//*from  ww  w.  j av  a  2  s.  co  m*/
public static Object[] toArray(final Object obj) {
    // if the object is an array, the cast and return it.
    if (obj instanceof Object[]) {
        return (Object[]) obj;
    }

    // if the object is an array of primitives then wrap the array
    Class type = obj.getClass();
    Object array;
    if (type.isArray()) {
        int length = Array.getLength(obj);
        Class componentType = type.getComponentType();
        array = Array.newInstance(componentType, length);
        for (int i = 0; i < length; i++) {
            Array.set(array, i, Array.get(obj, i));
        }
    } else {
        array = Array.newInstance(type, 1);
        Array.set(array, 0, obj);
    }

    return (Object[]) array;
}

From source file:Main.java

public static Class<?> inferFromArrayType(final Class<?> type) {
    if (!isArrayType(type)) {
        return null;
    }// w ww  .  j  a  v  a2 s .co m
    if (type.isArray()) {
        final Class<?> componentType = type.getComponentType();
        return componentType;
    }
    return null;
}