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:com.g3net.tool.CollectionUtils.java

/**
 * ?map/*from w  w w  .ja v  a2 s  .c  o m*/
 * 
 * @param map
 * @return
 */
public static String toString(Map map) {
    if (CollectionUtils.isEmpty(map))
        return "";

    Set keys = map.keySet();
    String s = "{";
    for (Object key : keys) {
        Object value = map.get(key);
        Class c = value.getClass();
        if (value instanceof Map) {

            s = s + (s.length() == 1 ? "" : ",") + key + "={" + toString((Map) value) + "}";
        } else if (value instanceof List) {
            s = s + (s.length() == 1 ? "" : ",") + key + "={" + toString((List) value) + "}";
        } else if (value instanceof Set) {
            s = s + (s.length() == 1 ? "" : ",") + key + "={" + toString((Set) value) + "}";
        } else {

            if (c.isArray()) {
                s = s + (s.length() == 1 ? "" : ",") + key + "=[" + ArrayUtils.toString(value, ",") + "]";
            } else {
                s = s + (s.length() == 1 ? "" : ",") + key + "=" + value;
            }
        }
    }
    s = s + "}";
    return s;
}

From source file:br.msf.commons.mocca.ajax.AbstractAjaxService.java

private static void validateTargetClass(final Class<?> targetClass) {
    ArgumentUtils.rejectIfNull(targetClass);
    if (targetClass.isInterface()) {
        throw new IllegalArgumentException("targetClass cannot be an interface.");
    } else if (targetClass.isArray()) {
        throw new IllegalArgumentException("targetClass cannot be an array.");
    } else if (Collection.class.isAssignableFrom(targetClass) || Map.class.isAssignableFrom(targetClass)) {
        throw new IllegalArgumentException("targetClass cannot be a collection.");
    }//from  w w  w .  ja  v a 2s.  c o  m
}

From source file:com.swingtech.commons.testing.JavaBeanTester.java

private static Object buildValue(Class<?> clazz) throws InstantiationException, IllegalAccessException,
        IllegalArgumentException, SecurityException, InvocationTargetException {
    // If we are using a Mocking framework try that first...
    final Object mockedObject = buildMockValue(clazz);
    if (mockedObject != null) {
        return mockedObject;
    }/*from   w  w w.  ja  v  a 2s . c  o  m*/

    // Next check for a no-arg constructor
    final Constructor<?>[] ctrs = clazz.getConstructors();
    for (Constructor<?> ctr : ctrs) {
        if (ctr.getParameterTypes().length == 0) {
            // The class has a no-arg constructor, so just call it
            return ctr.newInstance();
        }
    }

    // Specific rules for common classes
    if (clazz == String.class) {
        return "testvalue";

    } else if (clazz.isArray()) {
        return Array.newInstance(clazz.getComponentType(), 1);

    } else if (clazz == boolean.class || clazz == Boolean.class) {
        return true;

    } else if (clazz == int.class || clazz == Integer.class) {
        return 1;

    } else if (clazz == long.class || clazz == Long.class) {
        return 1L;

    } else if (clazz == double.class || clazz == Double.class) {
        return 1.0D;

    } else if (clazz == float.class || clazz == Float.class) {
        return 1.0F;

    } else if (clazz == char.class || clazz == Character.class) {
        return 'Y';

        // Add your own rules here

    } else {
        fail("Unable to build an instance of class " + clazz.getName() + ", please add some code to the "
                + JavaBeanTester.class.getName() + " class to do this.");
        return null; // for the compiler
    }
}

From source file:ArrayEnumeration.java

public ArrayEnumeration(Object obj) {
    Class type = obj.getClass();
    if (!type.isArray()) {
        throw new IllegalArgumentException("Invalid type: " + type);
    }//from  ww w . jav  a  2  s.co m
    size = Array.getLength(obj);
    array = obj;
}

From source file:jef.tools.collection.CollectionUtils.java

/**
 * ??//  w  w w . j  a  v a2 s.co m
 * 
 * @param type
 * @return true if type is a collection type.
 */
public static boolean isArrayOrCollection(Type type) {
    if (type instanceof GenericArrayType) {
        return true;
    } else if (type instanceof Class) {
        Class<?> rawType = (Class<?>) type;
        return rawType.isArray() || Collection.class.isAssignableFrom(rawType);
    } else {
        return Collection.class.isAssignableFrom(GenericUtils.getRawClass(type));
    }
}

From source file:com.jeeframework.util.classes.ClassUtils.java

/**
 * Return the qualified name of the given class: usually simply
 * the class name, but component type class name + "[]" for arrays.
 * @param clazz the class// w w  w  .  j a va 2 s .  c om
 * @return the qualified name of the class
 */
public static String getQualifiedName(Class clazz) {
    Assert.notNull(clazz, "Class must not be null");
    if (clazz.isArray()) {
        return getQualifiedNameForArray(clazz);
    } else {
        return clazz.getName();
    }
}

From source file:com.jeeframework.util.classes.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   w w  w .j  a v  a2s.  co m*/
 * @return whether the given class is a primitive array class
 */
public static boolean isPrimitiveArray(Class clazz) {
    Assert.notNull(clazz, "Class must not be null");
    return (clazz.isArray() && clazz.getComponentType().isPrimitive());
}

From source file:com.jeeframework.util.classes.ClassUtils.java

/**
 * Check if the given class represents an array of primitive wrappers,
 * i.e. Boolean, Byte, Character, Short, Integer, Long, Float, or Double.
 * @param clazz the class to check/*from   w  w w  . j  a v a2s.  c o  m*/
 * @return whether the given class is a primitive wrapper array class
 */
public static boolean isPrimitiveWrapperArray(Class clazz) {
    Assert.notNull(clazz, "Class must not be null");
    return (clazz.isArray() && isPrimitiveWrapper(clazz.getComponentType()));
}

From source file:com.softwarementors.extjs.djn.router.processor.standard.json.JsonRequestProcessor.java

private static boolean isValidJsonTypeForJavaType(JsonElement jsonElement, Class<?> parameterType) {
    assert jsonElement != null;
    assert parameterType != null;

    // Check json nulls
    if (jsonElement.isJsonNull()) {
        return !parameterType.isPrimitive();
    }/*from  w  ww.j av a2 s. co  m*/

    if (parameterType.isArray()) {
        // This is *always* ok because if the value is not a json array
        // we will instantiate a single item array and attempt conversion
        return true;
    }

    if (parameterType.equals(Boolean.class) || parameterType.equals(boolean.class)) {
        return jsonElement.isJsonPrimitive() && ((JsonPrimitive) jsonElement).isBoolean();
    } else if (parameterType.equals(char.class) || parameterType.equals(Character.class)) {
        if (jsonElement.isJsonPrimitive() && ((JsonPrimitive) jsonElement).isString()) {
            return jsonElement.getAsString().length() == 1;
        }
        return false;
    } else if (parameterType.equals(String.class)) {
        return jsonElement.isJsonPrimitive() && ((JsonPrimitive) jsonElement).isString();
    } else if (ClassUtils.isNumericType(parameterType)) {
        return jsonElement.isJsonPrimitive() && ((JsonPrimitive) jsonElement).isNumber();
    }

    // If we arrived here, assume somebody will know how to handle the json element, maybe customizing Gson's serialization
    return true;
}

From source file:com.angkorteam.framework.swagger.factory.SwaggerFactory.java

public static boolean isSimpleArray(Class<?> type) {
    return type.isArray() && (type == byte[].class || type == Byte[].class || type == short[].class
            || type == Short[].class || type == int[].class || type == Integer[].class || type == float[].class
            || type == Float[].class || type == long[].class || type == Long[].class || type == double[].class
            || type == Double[].class || type == boolean[].class || type == Boolean[].class
            || type == char[].class || type == Character[].class || type == MultipartFile[].class
            || type == String[].class);
}