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:com.ydjy.util.StringUtils.java

/**
  * <br>/*  www .j a v a2  s  .c  om*/
  * <br>
  * @param pInput   ?<br>
  * @return boolean ,?,<br>
  */
public static boolean objectIsNullOrEmpty(Object pInput) {
    // ??''
    if (pInput == null || "".equals(pInput)) {
        return true;
    } else if ("java.lang.String".equals(pInput.getClass().getName())) {
        // ?String
        // ???
        String tmpInput = Pattern.compile("//r|//n|//u3000").matcher((String) pInput).replaceAll("");
        // ?
        return Pattern.compile("^(//s)*$").matcher(tmpInput).matches();
    } else {
        // 
        Method method = null;
        String newInput = "";
        try {
            // ?size
            method = pInput.getClass().getMethod("size");
            // size?
            // ?String
            newInput = String.valueOf(method.invoke(pInput));
            // size0?
            return Integer.parseInt(newInput) == 0 ? true : false;
        } catch (Exception e) {
            // 
            try {
                // ?getItemCount
                method = pInput.getClass().getMethod("getItemCount");
                // size?
                // ?String
                newInput = String.valueOf(method.invoke(pInput));
                // getItemCount0?
                return Integer.parseInt(newInput) == 0 ? true : false;
            } catch (Exception ex) {
                // 
                try {
                    // ?
                    // 0?
                    return Array.getLength(pInput) == 0 ? true : false;
                } catch (Exception exx) {
                    // 
                    try {
                        // ?hasNext
                        method = Iterator.class.getMethod("hasNext");
                        // ?String
                        newInput = String.valueOf(method.invoke(pInput));
                        // ?hasNext
                        return Boolean.valueOf(newInput) == false ? true : false;
                    } catch (Exception exxx) {
                        // ??
                        return false;
                    }
                }
            }
        }
    }
}

From source file:ArrayUtils.java

/**
 * Replaces <code>toReplace</code> with <code>replacement</code> in
 * <code>array</code> as many times as it occurs
 * /*w  ww.  ja  v  a  2 s.c  o m*/
 * @param array
 *            The array to search and replace in
 * @param toReplace
 *            The object to replace
 * @param replacement
 *            The object to replace <code>toReplace</code> with each time it
 *            is found
 * @return The number of times <code>toReplace</code> was found and replaced
 */
public static int replaceAll(Object array, Object toReplace, Object replacement) {
    int count = 0;
    if (array == null)
        return count;
    if (array instanceof Object[]) {
        Object[] array2 = (Object[]) array;
        for (int i = 0; i < array2.length; i++) {
            if (equals(array2[i], toReplace)) {
                array2[i] = replacement;
                count++;
            }
        }
    } else {
        int i, len;
        len = Array.getLength(array);
        for (i = 0; i < len; i++) {
            if (equals(Array.get(array, i), toReplace)) {
                put(array, replacement, i);
                count++;
            }
        }
    }
    return count;
}

From source file:net.yasion.common.core.bean.wrapper.impl.ExtendedBeanWrapperImpl.java

private Object growArrayIfNecessary(Object array, int index, String name) {
    if (!isAutoGrowNestedPaths()) {
        return array;
    }//from   w w w .  j  av  a2s  .  c o m
    int length = Array.getLength(array);
    if (index >= length && index < this.autoGrowCollectionLimit) {
        Class<?> componentType = array.getClass().getComponentType();
        Object newArray = Array.newInstance(componentType, index + 1);
        System.arraycopy(array, 0, newArray, 0, length);
        for (int i = length; i < Array.getLength(newArray); i++) {
            Array.set(newArray, i, newValue(componentType, null, name));
        }
        // #TO#DO# this is not efficient because conversion may create a copy ... set directly because we know it is assignable.
        setPropertyValue(name, newArray);
        return getPropertyValue(name);
    } else {
        return array;
    }
}

From source file:com.github.dozermapper.core.MappingProcessor.java

private Object addArrayContentCopy(FieldMap fieldMap, int size, Object srcCollectionValue, Object destObj,
        Class destEntryType) {//from   w w  w  . ja  v a 2  s .com
    Object result;
    Object field = fieldMap.getDestValue(destObj);
    int arraySize = 0;
    if (field == null) {
        result = Array.newInstance(destEntryType, size);
    } else {
        result = Array.newInstance(destEntryType, size + Array.getLength(field));
        arraySize = Array.getLength(field);
        System.arraycopy(field, 0, result, 0, arraySize);
    }
    System.arraycopy(srcCollectionValue, 0, result, arraySize, size);
    return result;
}

From source file:com.github.dozermapper.core.MappingProcessor.java

private Object addToPrimitiveArray(Object srcObj, FieldMap fieldMap, int size, Object srcCollectionValue,
        Object destObj, Class<?> destEntryType) {

    Object result;//from   ww w  .j a  v a  2 s  .co  m
    Object field = fieldMap.getDestValue(destObj);
    int arraySize = 0;
    if (field == null) {
        result = Array.newInstance(destEntryType, size);
    } else {
        result = Array.newInstance(destEntryType, size + Array.getLength(field));
        arraySize = Array.getLength(field);
        System.arraycopy(field, 0, result, 0, arraySize);
    }
    // primitive arrays are ALWAYS cumulative
    for (int i = 0; i < size; i++) {
        CopyByReferenceContainer copyByReferences = globalConfiguration.getCopyByReferences();
        Object toValue;
        if (srcCollectionValue != null && copyByReferences.contains(srcCollectionValue.getClass())) {
            toValue = srcCollectionValue;
        } else {
            toValue = mapOrRecurseObject(srcObj, Array.get(srcCollectionValue, i), destEntryType, fieldMap,
                    destObj);
        }
        Array.set(result, arraySize, toValue);
        arraySize++;
    }
    return result;
}

From source file:ArrayUtils.java

/**
 * Gets the elements that the two arrays have in common. Elements are
 * retrieved from the first array./* w w  w . j  a v  a  2  s.c  o m*/
 * 
 * @param array1
 *            The first array
 * @param array2
 *            The second array
 * @return The elements in <code>array1</code> that occur at least once in
 *         <code>array2</code>
 */
public static Object[] commonElementsP(Object array1, Object array2) {
    int count = 0;
    if (array1 == null || array2 == null)
        return new Object[0];
    if (!array1.getClass().isArray() || array2.getClass().isArray())
        return new Object[0];
    int len1 = Array.getLength(array1);
    int len2 = Array.getLength(array2);
    int i, j;
    for (i = 0; i < len1; i++)
        for (j = 0; j < len2; j++)
            if (equals(Array.get(array1, i), Array.get(array2, j))) {
                count++;
                break;
            }
    Object[] ret = new Object[count];
    count = 0;
    for (i = 0; i < len1; i++)
        for (j = 0; j < len2; j++)
            if (equals(Array.get(array1, i), Array.get(array2, j))) {
                ret[count] = Array.get(array1, i);
                count++;
                break;
            }
    return ret;
}

From source file:br.msf.commons.text.EnhancedStringBuilder.java

protected static void format(final Object object, final StringBuilder builder) {
    if (object == null) {
        return;//from   w w  w  . java 2  s.co  m
    }
    if (ObjectUtils.isCharSequence(object)) {
        builder.append((CharSequence) object);
    } else if (ObjectUtils.isCharacter(object)) {
        builder.append((Character) object);
    } else if (ObjectUtils.isNumber(object)) {
        builder.append(NumberUtils.format((Number) object));
    } else if (ObjectUtils.isDate(object) || ObjectUtils.isCalendar(object)) {
        builder.append(DateUtils.formatDateTime(object));
    } else if (ObjectUtils.isArray(object)) {
        int size = Array.getLength(object);
        builder.ensureCapacity(builder.length() + (size));
        for (int i = 0; i < size; i++) {
            format(Array.get(object, i), builder);
        }
    } else if (ObjectUtils.isCollection(object)) {
        Collection<?> collection = (Collection) object;
        for (Object item : collection) {
            format(item, builder);
        }
    } else {
        builder.append(object);
    }
}

From source file:gedi.util.ArrayUtils.java

/**
 * Removes an item from an array and returns a new array.
 * @param <T> the class/*from   ww w  .j a  v a 2 s.co m*/
 * @param array the array
 * @param index the index of the item to remove
 * @return the new array without the item
 */
@SuppressWarnings("unchecked")
public static <T> T[] removeItemFromArray(T[] array, int index) {
    T[] re = (T[]) Array.newInstance(array.getClass().getComponentType(), Array.getLength(array) - 1);
    System.arraycopy(array, 0, re, 0, index);
    System.arraycopy(array, index + 1, re, index, re.length - index);
    return re;
}

From source file:com.betfair.cougar.netutil.nio.marshalling.SocketRMIMarshallerTest.java

/**
 * Equals methods in generated idd classes don't handle delegates
 * @param result//from  w  w w  .j a  va  2 s. c  om
 * @return
 * @throws Exception
 */
private Object removeDelegates(Object result) throws Exception {
    if (!(result instanceof Transcribable)) {
        return result;
    }
    Transcribable transcribable = (Transcribable) result;
    final Object[] objects = new Object[transcribable.getParameters().length];
    final int[] index = new int[1];
    transcribable.transcribe(new TranscriptionOutput() {

        @Override
        public void writeObject(Object obj, Parameter param, boolean client) throws Exception {
            if (obj == null) {
                objects[index[0]++] = null;
            } else if (param.getParameterType().getType() == Type.OBJECT) {
                objects[index[0]++] = removeDelegates(obj);
            } else if (param.getParameterType().getType() == Type.LIST) {
                if (obj.getClass().isArray()) {
                    objects[index[0]] = Array.newInstance(
                            param.getParameterType().getComponentTypes()[0].getImplementationClass(),
                            Array.getLength(obj));
                    for (int i = 0, limit = Array.getLength(obj); i < limit; i++) {
                        Array.set(objects[index[0]], i, removeDelegates(Array.get(obj, i)));
                    }
                    index[0]++;
                } else {
                    List list = (List) obj;
                    objects[index[0]] = new ArrayList();
                    for (Object o : list) {
                        ((List) objects[index[0]]).add(removeDelegates(o));
                    }
                    index[0]++;
                }
            } else if (param.getParameterType().getType() == Type.SET) {
                Set set = (Set) obj;
                objects[index[0]] = new HashSet();
                for (Object o : set) {
                    ((Set) objects[index[0]]).add(removeDelegates(o));
                }
                index[0]++;
            } else if (param.getParameterType().getType() == Type.MAP) {
                Map<Object, Object> map = (Map) obj;
                objects[index[0]] = new HashMap();
                for (Entry entry : map.entrySet()) {
                    ((Map) objects[index[0]]).put(removeDelegates(entry.getKey()),
                            removeDelegates(entry.getValue()));
                }
                index[0]++;
            } else {
                objects[index[0]++] = obj;
            }

        }
    }, TranscribableParams.getAll(), false);

    Transcribable newObject = (Transcribable) result.getClass().newInstance();
    index[0] = 0;
    newObject.transcribe(new TranscriptionInput() {

        @Override
        public <T> T readObject(Parameter param, boolean client) throws Exception {
            return (T) objects[index[0]++];

        }
    }, TranscribableParams.getAll(), false);

    return newObject;
}

From source file:gedi.util.ArrayUtils.java

/**
 * Removes an item from an array and returns a new array.
 * @param <T> the class/*from   w  ww  .ja va2 s  . co m*/
 * @param array the array
 * @param index the index of the item to remove
 * @return the new array without the item
 */
@SuppressWarnings("unchecked")
public static <T> T[] removeItemFromArray(T[] array, T item) {
    int index = find(array, item);
    if (index == -1)
        throw new IllegalArgumentException("Item not in array!");
    T[] re = (T[]) Array.newInstance(array.getClass().getComponentType(), Array.getLength(array) - 1);
    System.arraycopy(array, 0, re, 0, index);
    System.arraycopy(array, index + 1, re, index, re.length - index);
    return re;
}