Example usage for java.lang.reflect Array getLength

List of usage examples for java.lang.reflect Array getLength

Introduction

In this page you can find the example usage for java.lang.reflect Array getLength.

Prototype

@HotSpotIntrinsicCandidate
public static native int getLength(Object array) throws IllegalArgumentException;

Source Link

Document

Returns the length of the specified array object, as an int .

Usage

From source file:gda.data.scan.datawriter.scannablewriter.StringComponentWriter.java

@Override
public Collection<SelfCreatingLink> makeComponent(final NeXusFileInterface file, int[] dim, final String path,
        final String scannableName, final String componentName, final Object pos, final String unit)
        throws NexusException {

    final String name = enterLocation(file, path);

    stringlength = 127;//from w  w w . j  av  a2  s .  c  o  m

    final Object slab = getComponentSlab(pos);
    final int slablength = Array.getLength(slab);

    if (Arrays.equals(dim, new int[] { 1 })) {
        stringlength = slablength;
    } else if (slablength + 10 > stringlength) { // if strings vary more than that we are in trouble
        stringlength = slablength + 10;
    }

    dim = makedatadimfordim(dim);

    if (dim[dim.length - 1] == 1) {
        dim[dim.length - 1] = stringlength;
    } else {
        dim = ArrayUtils.add(dim, stringlength);
    }
    rank = dim.length;

    file.makedata(name, NexusFile.NX_CHAR, rank, dim);
    file.opendata(name);

    if (componentName != null) {
        file.putattr("local_name", (scannableName + "." + componentName).getBytes(UTF8), NexusFile.NX_CHAR);
    }

    addCustomAttributes(file, scannableName, componentName);
    file.putslab(slab, nulldimfordim(dim), slabsizedimfordim(dim));

    file.closedata();
    leaveLocation(file);

    return Collections.emptySet();
}

From source file:ArrayIterator.java

/**
 * Creates a new iterator instance for the specified array.
 *
 * @param array The array for which an iterator is desired.
 *///from   w w  w  .ja v a  2  s  .c  o m
public ArrayIterator(Object array) {
    /*
     * if this isn't an array, then throw.  Note that this is
     * for internal use - so this should never happen - if it does
     *  we screwed up.
     */

    if (!array.getClass().isArray()) {
        throw new IllegalArgumentException("Programmer error : internal ArrayIterator invoked w/o array");
    }

    this.array = array;
    pos = 0;
    size = Array.getLength(this.array);
}

From source file:Main.java

/**
 * Gets the size of the collection/iterator specified.
 * <p/>/* w ww  . j  a  v a  2  s.c om*/
 * This method can handles objects as follows
 * <ul>
 * <li>Collection - the collection size
 * <li>Map - the map size
 * <li>Array - the array size
 * <li>Iterator - the number of elements remaining in the iterator
 * <li>Enumeration - the number of elements remaining in the enumeration
 * </ul>
 *
 * @param object the object to get the size of
 * @return the size of the specified collection
 * @throws IllegalArgumentException thrown if object is not recognised or null
 * @since Commons Collections 3.1
 */
public static int size(Object object) {
    int total = 0;
    if (object instanceof Map) {
        total = ((Map) object).size();
    } else if (object instanceof Collection) {
        total = ((Collection) object).size();
    } else if (object instanceof Object[]) {
        total = ((Object[]) object).length;
    } else if (object instanceof Iterator) {
        Iterator it = (Iterator) object;
        while (it.hasNext()) {
            total++;
            it.next();
        }
    } else if (object instanceof Enumeration) {
        Enumeration it = (Enumeration) object;
        while (it.hasMoreElements()) {
            total++;
            it.nextElement();
        }
    } else if (object == null) {
        throw new IllegalArgumentException("Unsupported object type: null");
    } else {
        try {
            total = Array.getLength(object);
        } catch (IllegalArgumentException ex) {
            throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName());
        }
    }
    return total;
}

From source file:org.codhaus.groovy.grails.validation.MaxSizeConstraint.java

@Override
protected void processValidate(Object target, Object propertyValue, Errors errors) {
    int length;//from w  w w.j  a  v  a2  s.co m
    if (propertyValue.getClass().isArray()) {
        length = Array.getLength(propertyValue);
    } else if (propertyValue instanceof Collection<?>) {
        length = ((Collection<?>) propertyValue).size();
    } else { // String
        length = ((String) propertyValue).length();
    }

    if (length > maxSize) {
        Object[] args = { constraintPropertyName, constraintOwningClass, propertyValue, maxSize };
        rejectValue(target, errors, ConstrainedPropertyGunn.DEFAULT_INVALID_MAX_SIZE_MESSAGE_CODE,
                ConstrainedPropertyGunn.MAX_SIZE_CONSTRAINT + ConstrainedPropertyGunn.EXCEEDED_SUFFIX, args);
    }
}

From source file:org.codhaus.groovy.grails.validation.MinSizeConstraint.java

@Override
protected void processValidate(Object target, Object propertyValue, Errors errors) {
    int length;/*from   ww w . jav  a  2  s.  c o  m*/
    if (propertyValue.getClass().isArray()) {
        length = Array.getLength(propertyValue);
    } else if (propertyValue instanceof Collection<?>) {
        length = ((Collection<?>) propertyValue).size();
    } else { // String
        length = ((String) propertyValue).length();
    }

    if (length < minSize) {
        Object[] args = { constraintPropertyName, constraintOwningClass, propertyValue, minSize };
        rejectValue(target, errors, ConstrainedPropertyGunn.DEFAULT_INVALID_MIN_SIZE_MESSAGE_CODE,
                ConstrainedPropertyGunn.MIN_SIZE_CONSTRAINT + ConstrainedPropertyGunn.NOTMET_SUFFIX, args);
    }
}

From source file:mil.jpeojtrs.sca.util.PrimitiveArrayUtils.java

public static float[] convertToFloatArray(final Object array) {
    if (array == null) {
        return null;
    }//from  w w  w.  jav a2 s  .  co m
    if (array instanceof float[]) {
        return (float[]) array;
    }
    if (array instanceof Float[]) {
        return ArrayUtils.toPrimitive((Float[]) array);
    }
    final float[] newArray = new float[Array.getLength(array)];
    for (int i = 0; i < newArray.length; i++) {
        final Number val = (Number) Array.get(array, i);
        newArray[i] = val.floatValue();
    }
    return newArray;
}

From source file:ArrayMap.java

/**
 * @param <T>/*from  w  ww  .  jav a  2s  . c  o  m*/
 *            The type of the array
 * @param anArray
 *            The array to extend
 * @param anElement
 *            The element to add into <code>anArray</code>
 * @return A new array of length <code>anArray.length+1</code> whose
 *         contents are those of <code>anArray</code> followed by
 *         <code>anElement</code>
 */
public static <T> T[] add(T[] anArray, T anElement) {
    int len = anArray == null ? 0 : Array.getLength(anArray);
    return add(anArray, anElement, len);
}

From source file:ObjectUtils.java

/**
 * Convert the given array (which may be a primitive array) to an
 * object array (if necessary of primitive wrapper objects).
 * <p>A <code>null</code> source value will be converted to an
 * empty Object array./*www .  j  av  a 2s  .c o m*/
 * @param source the (potentially primitive) array
 * @return the corresponding object array (never <code>null</code>)
 * @throws IllegalArgumentException if the parameter is not an array
 */
public static Object[] toObjectArray(Object source) {
    if (source instanceof Object[]) {
        return (Object[]) source;
    }
    if (source == null) {
        return new Object[0];
    }
    if (!source.getClass().isArray()) {
        throw new IllegalArgumentException("Source is not an array: " + source);
    }
    int length = Array.getLength(source);
    if (length == 0) {
        return new Object[0];
    }
    Class wrapperType = Array.get(source, 0).getClass();
    Object[] newArray = (Object[]) Array.newInstance(wrapperType, length);
    for (int i = 0; i < length; i++) {
        newArray[i] = Array.get(source, i);
    }
    return newArray;
}

From source file:com.astamuse.asta4d.web.form.validation.TypeUnMatchValidator.java

@SuppressWarnings("rawtypes")
private void addMessage(List<FormValidationMessage> msgList, Object form, int[] indexes) {
    List<AnnotatedPropertyInfo> fieldList = AnnotatedPropertyUtil.retrieveProperties(form.getClass());

    try {// w ww . j a v  a2s. c o m
        for (AnnotatedPropertyInfo field : fieldList) {

            CascadeFormField cff = field.getAnnotation(CascadeFormField.class);
            if (cff != null) {
                Object subform = field.retrieveValue(form);
                if (StringUtils.isEmpty(cff.arrayLengthField())) {
                    // simple cascade form
                    addMessage(msgList, subform, indexes);
                } else {
                    // array cascade form
                    int len = Array.getLength(subform);
                    for (int i = 0; i < len; i++) {
                        addMessage(msgList, Array.get(subform, i), ArrayUtils.add(indexes, i));
                    }
                }
                continue;
            }

            ContextDataHolder valueHolder;
            if (field.getField() != null) {
                valueHolder = InjectTrace.getInstanceInjectionTraceInfo(form, field.getField());
            } else {
                valueHolder = InjectTrace.getInstanceInjectionTraceInfo(form, field.getSetter());
            }
            if (valueHolder != null) {
                msgList.add(createTypeUnMatchMessage(form.getClass(), field, valueHolder, indexes));
            }
        }
    } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.codhaus.groovy.grails.validation.SizeConstraint.java

@SuppressWarnings("unchecked")
@Override/* www.j av  a 2 s . c  o m*/
protected void processValidate(Object target, Object propertyValue, Errors errors) {
    Object[] args = { constraintPropertyName, constraintOwningClass, propertyValue, range.getFrom(),
            range.getTo() };

    int size;
    if (propertyValue.getClass().isArray()) {
        size = Array.getLength(propertyValue);
    } else if (propertyValue instanceof Collection<?>) {
        size = ((Collection<?>) propertyValue).size();
    } else { // String
        size = ((String) propertyValue).length();
    }

    if (!range.contains(size)) {
        if (range.getFrom().compareTo(size) == 1) {
            rejectValue(args, errors, target, ConstrainedPropertyGunn.TOOSMALL_SUFFIX);
        } else if (range.getTo().compareTo(size) == -1) {
            rejectValue(args, errors, target, ConstrainedPropertyGunn.TOOBIG_SUFFIX);
        }
    }
}