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 <T> ArrayList<HashMap<String, String>> prepareDataToSave(

        ArrayList<T> source, Class<T> classType)
        throws NoSuchMethodException, SecurityException, IllegalArgumentException, IllegalAccessException {

    ArrayList<HashMap<String, String>> destination = new ArrayList<HashMap<String, String>>();
    ArrayList<Field> savedFieds = new ArrayList<Field>();
    Field[] fields;/*from  ww w .  j  av  a 2  s .c o m*/
    Object value = null;
    HashMap<String, String> aux;
    Field auxField;

    fields = classType.getDeclaredFields();

    for (int j = 0; j < fields.length; j++) {

        Class<?> type = fields[j].getType();

        if (!(Modifier.isStatic(fields[j].getModifiers()) || Modifier.isFinal(fields[j].getModifiers())
                || type.isArray() || Collection.class.isAssignableFrom(type))) {

            savedFieds.add(fields[j]);

        }
    }

    if (classType == null || fields == null) {
        return null;
    }

    for (int i = 0; i < source.size(); i++) {
        aux = new HashMap<String, String>();

        for (int j = 0; j < savedFieds.size(); j++) {

            auxField = savedFieds.get(j);
            auxField.setAccessible(true);
            value = auxField.get(source.get(i));
            aux.put(auxField.getName(), value.toString());

        }

        destination.add(aux);
    }

    return destination;

}

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. ja  v  a2 s.  c  om*/

    return null;
}

From source file:Main.java

public static Object copyOf(Object src) {
    int srcLength = Array.getLength(src);
    Class<?> srcComponentType = src.getClass().getComponentType();
    Object dest = Array.newInstance(srcComponentType, srcLength);
    if (srcComponentType.isArray()) {
        for (int i = 0; i < Array.getLength(src); i++) {
            Array.set(dest, i, copyOf(Array.get(src, i)));
        }/*  ww w .  j a v  a 2 s  .c o  m*/
    } else {
        System.arraycopy(src, 0, dest, 0, srcLength);
    }
    return dest;
}

From source file:Main.java

/**
 * Checks if a class is a Java type: Map, Collection,arrays, Number (extensions and primitives), String, Boolean..
 * /*from   w ww .ja  va  2  s  .  c o m*/
 * @param clazz
 *          Class<?> to examine
 * @return true if clazz is Java type, false otherwise
 */
public static boolean isJavaType(Class<?> clazz) {
    if (clazz.isPrimitive())
        return true;
    else if (clazz.getName().startsWith("java.lang"))
        return true;
    else if (clazz.getName().startsWith("java.util"))
        return true;
    else if (clazz.isArray())
        return true;
    return false;
}

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.
 *//*from www  . j a v a2 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 + "[]"./*from ww w.  java2s  .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();
}

From source file:de.pribluda.android.jsonmarshaller.JSONUnmarshaller.java

/**
 * recursively retrieve base array class
 *
 * @param clazz//from www .  j  a va 2  s .c  om
 * @return
 */
@SuppressWarnings({ "unused", "rawtypes" })
private static Class retrieveArrayBase(Class clazz) {
    if (clazz.isArray())
        return retrieveArrayBase(clazz.getComponentType());
    return clazz;
}

From source file:Main.java

/**
 * Transform the class-relative resource name into a global name by appending
 * it to the classes package name. If the name is already a global name (the
 * name starts with a "/"), then the name is returned unchanged.
 * //from ww  w . jav  a  2s . c  om
 * @param name
 *          the resource name
 * @param c
 *          the class which the resource is relative to
 * @return the tranformed name.
 */
private static String convertName(final String name, Class c) {
    if (name.startsWith("/")) {
        // strip leading slash..
        return name.substring(1);
    }

    // we cant work on arrays, so remove them ...
    while (c.isArray()) {
        c = c.getComponentType();
    }
    // extract the package ...
    final String baseName = c.getName();
    final int index = baseName.lastIndexOf('.');
    if (index == -1) {
        return name;
    }

    final String pkgName = baseName.substring(0, index);
    return pkgName.replace('.', '/') + "/" + name;
}

From source file:com.qmetry.qaf.automation.util.JSONUtil.java

public static <T> T getJsonObjectFromFile(final String file, final Class<T> cls) {

    File f = new File(file);
    String json;/*  ww  w. j  a va2  s. co  m*/
    String defVal = List.class.isAssignableFrom(cls) || cls.isArray() ? "[]"
            : ClassUtil.isPrimitiveOrWrapperType(cls) ? "" : "{}";
    try {
        json = FileUtil.readFileToString(f, "UTF-8");
    } catch (IOException e) {
        json = defVal;
    } catch (Throwable e) {
        logger.error("unable to load [" + cls.getName() + "] from file[ " + file + "] - " + e.getMessage());
        json = defVal;
    }
    Gson gson = new GsonBuilder().disableHtmlEscaping().create();
    return gson.fromJson(json, cls);

}

From source file:edu.umich.flowfence.common.QMDescriptor.java

private static List<String> toNames(Class<?>... classes) {
    if (!ParceledPayload.canParcelTypes(classes)) {
        throw new ParcelFormatException("Can't parcel arguments");
    }// w w w  . j  av  a 2s  .  com
    // ClassUtils.classesTaClassNames doesn't work here, because it emits arrays
    // in their JNI form ([Ljava.lang.String;). Handle array type conversion manually.
    List<String> classNames = new ArrayList<>(classes.length);
    for (Class<?> clazz : classes) {
        int arrayDepth = 0;
        while (clazz.isArray()) {
            arrayDepth++;
            clazz = clazz.getComponentType();
        }
        classNames.add(clazz.getName() + StringUtils.repeat("[]", arrayDepth));
    }
    return classNames;
}