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.dianping.resource.io.util.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// w  w  w . j  a  v a  2 s  . c om
 * @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.dianping.resource.io.util.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 ww . j a v  a 2s.c om*/
 * @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:edu.usc.irds.sparkler.solr.schema.FieldMapper.java

/**
 * Maps field name to a dynamic field name based on value content type
 * The map function takes (input field name, value type,  configs, overrides) into consideration
 * for deciding field names/*from w  w w  .  j  av a2s  .c  om*/
 * @param fieldName name of field.
 * @param value value
 * @return mapped solr field name. returns {@code null} when mapping fails
 * @throws IllegalStateException when {@code this.failOnError} is set and an error occurs
 */
public String mapField(String fieldName, Object value) {
    if (overrides.containsKey(fieldName)) {
        return overrides.get(fieldName);
    }
    if (value == null) {
        return null; // null cant be mapped
    }
    Class<?> valType = value.getClass();

    boolean multiValued = false;
    if (valType.isArray()) {
        multiValued = true;
        valType = valType.getComponentType();
        //it could be primitive type, so mapping to Boxed Type
        valType = PRIMITIVE_MAP.getOrDefault(valType, valType);
    } else if (Collection.class.isAssignableFrom(valType)) {
        multiValued = true;
        Collection items = (Collection) value;
        if (items.isEmpty()) {
            return null;
            //cant determine an empty collection
        }
        valType = items.iterator().next().getClass();
    }
    String suffix = typeSuffix.get(valType);
    if (suffix == null) {
        LOG.warn("{} type is not mapped, field name = {}", valType.getName(), fieldName);
        throw new IllegalStateException(valType + " type is not known");
    }

    if (multiValued) {
        suffix += multiValSuffix;
    }
    //is it already mapped
    if (fieldName.endsWith(suffix)) {
        return fieldName;
    }
    fieldName = fieldName.toLowerCase().replaceAll("\\s+", "");
    return fieldName + suffix;
}

From source file:microsoft.exchange.webservices.data.core.EwsUtilities.java

public static int getDim(Object array) {
    int dim = 0;//from  ww w.jav  a2 s  .  c  o m
    Class<?> c = array.getClass();
    while (c.isArray()) {
        c = c.getComponentType();
        dim++;
    }
    return (dim);
}

From source file:com.freetmp.common.util.ClassUtils.java

private static String getQualifiedNameForArray(Class<?> clazz) {
    StringBuilder result = new StringBuilder();
    while (clazz.isArray()) {
        clazz = clazz.getComponentType();
        result.append(ClassUtils.ARRAY_SUFFIX);
    }/*from   w  w w .  j a v  a  2s .c  o m*/
    result.insert(0, clazz.getName());
    return result.toString();
}

From source file:com.freetmp.common.util.ClassUtils.java

public static String getQualifiedName(Class<?> clazz) {
    Assert.notNull(clazz, "Class must not be null");
    if (clazz.isArray()) {
        return getQualifiedNameForArray(clazz);
    } else {//from  w  w w.  j av a 2 s  .  c  o  m
        return clazz.getName();
    }
}

From source file:com.freetmp.common.util.ClassUtils.java

public static boolean isPrimitiveArray(Class<?> clazz) {
    Assert.notNull(clazz, "Class must not be null");
    return (clazz.isArray() && clazz.getComponentType().isPrimitive());
}

From source file:com.freetmp.common.util.ClassUtils.java

public static boolean isPrimitiveWrapperArray(Class<?> clazz) {
    Assert.notNull(clazz, "Class must not be null");
    return (clazz.isArray() && isPrimitiveWrapper(clazz.getComponentType()));
}

From source file:de.fau.cs.osr.utils.NameAbbrevService.java

/**
 * Return the abbreviated variant of the given class' full name.
 * //  w w  w  . java 2s.  c o  m
 * @throws IllegalArgumentException
 *             Thrown if the given class is not part of a packge from the
 *             package list.
 */
public String abbrev(Class<?> clazz) {
    String suffix = "";
    if (clazz.isArray()) {
        ArrayInfo info = ReflectionUtils.arrayDimension(clazz);
        clazz = info.elementClass;
        suffix = StringUtils.strrep("[]", info.dim);
    }

    String shortName = (String) cache.get(clazz);
    if (shortName != null)
        return shortName + suffix;

    // clazz.getSimpleName(); doesn't work for nested classes!
    String simpleName = clazz.getName();
    {
        int i = simpleName.lastIndexOf('.');
        if (i >= 0)
            simpleName = simpleName.substring(i + 1);
    }

    // Maybe the abbreviated name was already used for another class of the 
    // same name.
    if (cache.containsValue(simpleName)) {
        // Cannot abbreviate any more :(
        shortName = clazz.getName();
        cache.put(clazz, shortName);
        return shortName + suffix;
    }

    final String dotSimpleName = "." + simpleName;
    for (String pkg : packages) {
        try {
            Class<?> otherClazz = Class.forName(pkg + dotSimpleName);
            // At this point a class with this simple name has not been 
            // abbreviated. The first package that contains a class with 
            // this simple name will be the one we abbreviate. All others 
            // have to use the full name.
            cache.put(otherClazz, simpleName);

            if (otherClazz != clazz) {
                // Cannot abbreviate any more :(
                shortName = clazz.getName();
                cache.put(clazz, shortName);
                return shortName + suffix;
            } else {
                shortName = simpleName;
                return shortName + suffix;
            }
        } catch (ClassNotFoundException e) {
        }
    }

    if (!strict)
        return clazz.getName() + suffix;

    throw new IllegalArgumentException("Given class is not part of the package list: " + clazz.getName());
}

From source file:org.nabucco.alfresco.enhScriptEnv.common.script.converter.general.ArrayConverter.java

/**
 * {@inheritDoc}/*from   ww  w  .  ja  v  a2s . c  om*/
 */
@Override
public boolean canConvertValueForScript(final Object value, final ValueConverter globalDelegate,
        final Class<?> expectedClass) {
    boolean canConvert = expectedClass.isArray();

    if (canConvert) {
        final Class<?> componentClass = expectedClass.getComponentType();
        if (value instanceof Iterable<?>) {
            final Collection<?> coll = (Collection<?>) value;
            for (final Object element : coll) {
                canConvert = canConvert && globalDelegate.canConvertValueForScript(element, componentClass);

                if (!canConvert) {
                    break;
                }
            }
        } else if (value.getClass().isArray()) {
            final int length = Array.getLength(value);
            for (int idx = 0; idx < length && canConvert; idx++) {
                canConvert = canConvert
                        && globalDelegate.canConvertValueForScript(Array.get(value, idx), componentClass);
            }
        } else {
            canConvert = false;
        }
    }

    return canConvert;
}