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:es.pode.soporte.auditoria.registrar.BeanDescripcion.java

/** 
 * Mtodo para recuperar informacin de un objeto a travs de reflexin 
 *   //w w w . j a  va2s  . c o m
 * @param objeto Objeto al cual se le realiza la reflexin 
 * @param atributo Valor que se recupera del objeto
 * @return valor Se devuelve el valor del atributo buscado
 */
public static String describe(Object objeto, String atributo) {

    if (objeto == null)
        return null;

    Object valor = null;

    Class clase = objeto.getClass();
    if (clase.isArray() || java.util.Collection.class.isAssignableFrom(clase))
        log.warn("El atributo es un array y debera ser un String");
    else {
        log("Reflexin del objeto: " + objeto);

        BeanWrapper wrapper = new BeanWrapperImpl(objeto);
        PropertyDescriptor descriptors[] = wrapper.getPropertyDescriptors();

        for (int i = 0; i < descriptors.length; i++) {
            PropertyDescriptor pd = descriptors[i];

            if (pd.getReadMethod() != null && pd.getWriteMethod() != null) {
                String name = pd.getName();

                /* Capturamos el valor del atributo que nos interesa */
                if (name.equals(atributo)) {
                    log("Nombre atributo: " + name);
                    valor = wrapper.getPropertyValue(name);

                    /* Si el valor es nulo registramos un "" */
                    if (valor == null) {
                        log("El valor del atributo interceptado es nulo");
                        return null;
                    } else
                        return valor.toString();
                }
            }
        }
    }

    return null;
}

From source file:ShowClass.java

/** Return the name of an interface or primitive type, handling arrays. */
public static String typename(Class t) {
    String brackets = "";
    while (t.isArray()) {
        brackets += "[]";
        t = t.getComponentType();// w w  w  .ja  va2s .  c om
    }
    String name = t.getName();
    int pos = name.lastIndexOf('.');
    if (pos != -1)
        name = name.substring(pos + 1);
    return name + brackets;
}

From source file:Main.java

/**
 * Creates a class identifier of form Labc/abc;, from a Class.
 */// w w  w.  ja v  a 2s .c  om
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

public static boolean isCollectionOrMap(Class type) {
    if (List.class.isAssignableFrom(type) || Set.class.isAssignableFrom(type)
            || Map.class.isAssignableFrom(type) || type.isArray()) {
        return true;
    }// ww  w.  j a v a2 s  .  c o m
    return false;
}

From source file:Main.java

private static boolean isSimpleObjectClass(Class<?> objectClass) {
    if (objectClass.isArray()) {
        return isSimpleObjectClass(objectClass.getComponentType());
    } else {/*w w w.j a  v a2  s. c  o m*/
        return objectClass.isPrimitive() || CharSequence.class.isAssignableFrom(objectClass)
                || Character.class.isAssignableFrom(objectClass) || Boolean.class.isAssignableFrom(objectClass)
                || Number.class.isAssignableFrom(objectClass);
    }
}

From source file:it.greenvulcano.util.ArrayUtils.java

public static final List<?> arrayToList(Object arr) {
    List<?> list = null;// www.j  a  va 2 s .  c  o  m

    Class<?> ac = arr.getClass();
    if (!ac.isArray()) {
        throw new IllegalArgumentException("The input parameter isn't an array");
    }

    Class<?> act = ac.getComponentType();
    if (act.isPrimitive()) {
        if (act.equals(boolean.class)) {
            list = Arrays.asList(org.apache.commons.lang3.ArrayUtils.toObject((boolean[]) arr));
        } else if (act.equals(byte.class)) {
            list = Arrays.asList(org.apache.commons.lang3.ArrayUtils.toObject((byte[]) arr));
        } else if (act.equals(char.class)) {
            list = Arrays.asList(org.apache.commons.lang3.ArrayUtils.toObject((char[]) arr));
        } else if (act.equals(short.class)) {
            list = Arrays.asList(org.apache.commons.lang3.ArrayUtils.toObject((short[]) arr));
        } else if (act.equals(int.class)) {
            list = Arrays.asList(org.apache.commons.lang3.ArrayUtils.toObject((int[]) arr));
        } else if (act.equals(long.class)) {
            list = Arrays.asList(org.apache.commons.lang3.ArrayUtils.toObject((long[]) arr));
        } else if (act.equals(float.class)) {
            list = Arrays.asList(org.apache.commons.lang3.ArrayUtils.toObject((float[]) arr));
        } else if (act.equals(double.class)) {
            list = Arrays.asList(org.apache.commons.lang3.ArrayUtils.toObject((double[]) arr));
        }
    } else {
        list = arrayToList((Object[]) arr);
    }

    return list;
}

From source file:io.fabric8.jolokia.support.JolokiaHelpers.java

public static Object convertJolokiaToJavaType(Class<?> clazz, Object value) throws IOException {
    if (clazz.isArray()) {
        if (value instanceof JSONArray) {
            JSONArray jsonArray = (JSONArray) value;
            Object[] javaArray = (Object[]) Array.newInstance(clazz.getComponentType(), jsonArray.size());
            int idx = 0;
            for (Object element : jsonArray) {
                Array.set(javaArray, idx++, convertJolokiaToJavaType(clazz.getComponentType(), element));
            }// ww  w.jav  a  2  s  . c o m
            return javaArray;
        } else {
            return null;
        }
    } else if (String.class.equals(clazz)) {
        return (value != null) ? value.toString() : null;
    } else if (clazz.equals(Byte.class) || clazz.equals(byte.class)) {
        Number number = asNumber(value);
        return number != null ? number.byteValue() : null;
    } else if (clazz.equals(Short.class) || clazz.equals(short.class)) {
        Number number = asNumber(value);
        return number != null ? number.shortValue() : null;
    } else if (clazz.equals(Integer.class) || clazz.equals(int.class)) {
        Number number = asNumber(value);
        return number != null ? number.intValue() : null;
    } else if (clazz.equals(Long.class) || clazz.equals(long.class)) {
        Number number = asNumber(value);
        return number != null ? number.longValue() : null;
    } else if (clazz.equals(Float.class) || clazz.equals(float.class)) {
        Number number = asNumber(value);
        return number != null ? number.floatValue() : null;
    } else if (clazz.equals(Double.class) || clazz.equals(double.class)) {
        Number number = asNumber(value);
        return number != null ? number.doubleValue() : null;
    } else if (value instanceof JSONObject) {
        JSONObject jsonObject = (JSONObject) value;
        if (!JSONObject.class.isAssignableFrom(clazz)) {
            String json = jsonObject.toJSONString();
            return getObjectMapper().readerFor(clazz).readValue(json);
        }
    }
    return value;
}

From source file:Main.java

/**
 * Another helper method to deal with rest of [JACKSON-103]
 *///from   w w w.  j av  a  2s . c  o m
protected static boolean isGroovyMetaClassGetter(AnnotatedMethod am) {
    Class<?> rt = am.getRawType();
    if (rt == null || rt.isArray()) {
        return false;
    }
    Package pkg = rt.getPackage();
    if (pkg != null && pkg.getName().startsWith("groovy.lang")) {
        return true;
    }
    return false;
}

From source file:ArrayConverter.java

public static int getDimensionCount(Class arrayClass) {
    int count = 0;
    while (arrayClass != null && arrayClass.isArray()) {
        count += 1;//from ww w  . jav  a 2 s  . c  om
        arrayClass = arrayClass.getComponentType();
    }
    return count;
}

From source file:com.wavemaker.tools.apidocs.tools.parser.util.DataTypeUtil.java

/**
 * It will return {@link PrimitiveType} if given type is known else return null.
 *
 * @param type data type// w  w w.j a va  2s  .  com
 * @return {@link PrimitiveType} if type is in known list, else returns null.
 */
public static PrimitiveType getPrimitiveType(Class<?> type) {
    if (type != null && type.isArray()) {
        return PrimitiveType.ARRAY;
    } else {
        return primitiveTypesMap.get(type);
    }
}