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.astamuse.asta4d.web.form.flow.base.BasicFormFlowSnippetTrait.java

/**
 * Sub classes could override this method to customize how to handle the injection trace data for type unmatch errors.
 * //from   w  ww. j  a va  2 s  .  c o  m
 * @param fieldName
 * @param fieldDataType
 * @param rawTraceData
 * @return
 */
default Object convertRawInjectionTraceDataToRenderingData(String fieldName, Class<?> fieldDataType,
        Object rawTraceData) {
    if (fieldDataType.isArray() && rawTraceData.getClass().isArray()) {
        return rawTraceData;
    } else if (rawTraceData.getClass().isArray()) {// but field data type is
                                                   // not array
        if (Array.getLength(rawTraceData) > 0) {
            return Array.get(rawTraceData, 0);
        } else {
            return null;
        }
    } else {
        return rawTraceData;
    }
}

From source file:jenkins.plugins.shiningpanda.ShiningPandaTestCase.java

/**
 * Same as assertEqualBeans, but works on protected and private fields.
 * //from w  w  w .j  a va2  s . co m
 * @param lhs
 *            The initial object.
 * @param rhs
 *            The final object.
 * @param properties
 *            The properties to check.
 * @throws Exception
 */
public void assertEqualBeans2(Object lhs, Object rhs, String properties) throws Exception {
    assertNotNull("lhs is null", lhs);
    assertNotNull("rhs is null", rhs);
    for (String p : properties.split(",")) {
        PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(lhs, p);
        Object lp, rp;
        if (pd == null) {
            Field f = getField(lhs.getClass(), p);
            assertNotNull("No such property " + p + " on " + lhs.getClass(), f);
            boolean accessible = f.isAccessible();
            if (!accessible)
                f.setAccessible(true);
            lp = f.get(lhs);
            rp = f.get(rhs);
            f.setAccessible(accessible);
        } else {
            lp = PropertyUtils.getProperty(lhs, p);
            rp = PropertyUtils.getProperty(rhs, p);
        }

        if (lp != null && rp != null && lp.getClass().isArray() && rp.getClass().isArray()) {
            // deep array equality comparison
            int m = Array.getLength(lp);
            int n = Array.getLength(rp);
            assertEquals("Array length is different for property " + p, m, n);
            for (int i = 0; i < m; i++)
                assertEquals(p + "[" + i + "] is different", Array.get(lp, i), Array.get(rp, i));
            return;
        }

        assertEquals("Property " + p + " is different", lp, rp);
    }
}

From source file:com.jms.notify.utils.httpclient.SimpleHttpUtils.java

/**
 *
 * @param value/*  w  w w  . ja  v a 2  s.  com*/
 * @return
 */
private static List<String> makeStringList(Object value) {
    if (value == null) {
        value = "";
    }
    List<String> result = new ArrayList<String>();
    if (value.getClass().isArray()) {
        for (int j = 0; j < Array.getLength(value); j++) {
            Object obj = Array.get(value, j);
            result.add(obj != null ? obj.toString() : "");
        }
        return result;
    }

    if (value instanceof Iterator) {
        Iterator it = (Iterator) value;
        while (it.hasNext()) {
            Object obj = it.next();
            result.add(obj != null ? obj.toString() : "");
        }
        return result;
    }

    if (value instanceof Collection) {
        for (Object obj : (Collection) value) {
            result.add(obj != null ? obj.toString() : "");
        }
        return result;
    }

    if (value instanceof Enumeration) {
        Enumeration enumeration = (Enumeration) value;
        while (enumeration.hasMoreElements()) {
            Object obj = enumeration.nextElement();
            result.add(obj != null ? obj.toString() : "");
        }
        return result;
    }
    result.add(value.toString());
    return result;
}

From source file:org.paxml.util.ReflectUtils.java

public static int collectArray(Object array, Collection col) {
    int len = Array.getLength(array);
    for (int i = 0; i < len; i++) {
        col.add(Array.get(array, i));
    }/*from   w  w  w  .j  a va 2s. com*/
    return len;
}

From source file:org.jfree.chart.demo.SampleXYSymbolicDataset.java

/**
 * Returns a clone of the array.//from  ww  w  .  j  a  va2  s.c o m
 * 
 * @param arr
 *           the array.
 * @return a clone.
 */
private static Object cloneArray(final Object arr) {

    if (arr == null) {
        return arr;
    }

    final Class cls = arr.getClass();
    if (!cls.isArray()) {
        return arr;
    }

    final int length = Array.getLength(arr);
    final Object[] newarr = (Object[]) Array.newInstance(cls.getComponentType(), length);

    Object obj;

    for (int i = 0; i < length; i++) {
        obj = Array.get(arr, i);
        if (obj.getClass().isArray()) {
            newarr[i] = cloneArray(obj);
        } else {
            newarr[i] = obj;
        }
    }

    return newarr;
}

From source file:net.sf.excelutils.ExcelParser.java

/**
 * get Iterator from the object//from   ww w  .j  av a 2s.  c o  m
 * 
 * @param collection
 * @return Iterator of the object
 */
public static Iterator getIterator(Object collection) {
    Iterator iterator = null;
    if (collection.getClass().isArray()) {
        try {
            // If we're lucky, it is an array of objects
            // that we can iterate over with no copying
            iterator = Arrays.asList((Object[]) collection).iterator();
        } catch (ClassCastException e) {
            // Rats -- it is an array of primitives
            int length = Array.getLength(collection);
            ArrayList c = new ArrayList(length);
            for (int i = 0; i < length; i++) {
                c.add(Array.get(collection, i));
            }
            iterator = c.iterator();
        }
    } else if (collection instanceof Collection) {
        iterator = ((Collection) collection).iterator();
    } else if (collection instanceof Iterator) {
        iterator = (Iterator) collection;
    } else if (collection instanceof Map) {
        iterator = ((Map) collection).entrySet().iterator();
    }
    return iterator;
}

From source file:org.jfree.chart.demo.SymbolicXYPlotDemo.java

/**
 * Transform an primitive array to an object array.
 * //from  www  .  j av a2  s.c  o m
 * @param arr
 *           the array.
 * @return an array.
 */
private static Object toArray(final Object arr) {

    if (arr == null) {
        return arr;
    }

    final Class cls = arr.getClass();
    if (!cls.isArray()) {
        return arr;
    }

    Class compType = cls.getComponentType();
    int dim = 1;
    while (!compType.isPrimitive()) {
        if (!compType.isArray()) {
            return arr;
        } else {
            dim++;
            compType = compType.getComponentType();
        }
    }

    final int[] length = new int[dim];
    length[0] = Array.getLength(arr);
    Object[] newarr = null;

    try {
        if (compType.equals(Integer.TYPE)) {
            newarr = (Object[]) Array.newInstance(Class.forName("java.lang.Integer"), length);
        } else if (compType.equals(Double.TYPE)) {
            newarr = (Object[]) Array.newInstance(Class.forName("java.lang.Double"), length);
        } else if (compType.equals(Long.TYPE)) {
            newarr = (Object[]) Array.newInstance(Class.forName("java.lang.Long"), length);
        } else if (compType.equals(Float.TYPE)) {
            newarr = (Object[]) Array.newInstance(Class.forName("java.lang.Float"), length);
        } else if (compType.equals(Short.TYPE)) {
            newarr = (Object[]) Array.newInstance(Class.forName("java.lang.Short"), length);
        } else if (compType.equals(Byte.TYPE)) {
            newarr = (Object[]) Array.newInstance(Class.forName("java.lang.Byte"), length);
        } else if (compType.equals(Character.TYPE)) {
            newarr = (Object[]) Array.newInstance(Class.forName("java.lang.Character"), length);
        } else if (compType.equals(Boolean.TYPE)) {
            newarr = (Object[]) Array.newInstance(Class.forName("java.lang.Boolean"), length);
        }
    } catch (ClassNotFoundException ex) {
        System.out.println(ex);
    }

    for (int i = 0; i < length[0]; i++) {
        if (dim != 1) {
            newarr[i] = toArray(Array.get(arr, i));
        } else {
            newarr[i] = Array.get(arr, i);
        }
    }
    return newarr;
}

From source file:com.link_intersystems.lang.reflect.Member2.java

private Object createVarargsArray(Parameter varargsParameter, int varargsParamIndex, Object[] callParameters) {
    Class2<?> parameterClass2 = varargsParameter.getParameterClass2();
    Class<?> type = parameterClass2.getType();
    Class<?> componentType = type.getComponentType();
    Object varargsArray = Array.newInstance(type.getComponentType(), callParameters.length - varargsParamIndex);
    if (componentType.isPrimitive()) {
        PrimitiveArrayCallback primitiveCallback = new PrimitiveArrayCallback(varargsArray);
        for (int varargsIndex = varargsParamIndex; varargsIndex < callParameters.length; varargsIndex++) {
            Object object = callParameters[varargsIndex];
            Conversions.unbox(object, primitiveCallback);
        }/*  ww  w  . j a  va2s . c o m*/
    } else {
        System.arraycopy(callParameters, varargsParamIndex, varargsArray, 0, Array.getLength(varargsArray));
    }
    return varargsArray;
}

From source file:com.jaspersoft.jasperserver.ws.axis2.scheduling.ReportJobBeanTraslator.java

protected Object toCollectionValue(Class parameterType, Object valueArray) {
    Object reportValue;/*from   w ww.j a v a2 s.  c  o  m*/
    int valueCount = Array.getLength(valueArray);
    if (parameterType.equals(Object.class) || parameterType.equals(Collection.class)
            || parameterType.equals(Set.class)) {
        Collection values = new ListOrderedSet();
        for (int i = 0; i < valueCount; ++i) {
            values.add(Array.get(valueArray, i));
        }
        reportValue = values;
    } else if (parameterType.equals(List.class)) {
        Collection values = new ArrayList(valueCount);
        for (int i = 0; i < valueCount; ++i) {
            values.add(Array.get(valueArray, i));
        }
        reportValue = values;
    } else if (parameterType.isArray()) {
        Class componentType = parameterType.getComponentType();
        if (componentType.equals(valueArray.getClass().getComponentType())) {
            reportValue = valueArray;
        } else {
            reportValue = Array.newInstance(componentType, valueCount);
            for (int i = 0; i < valueCount; ++i) {
                Array.set(reportValue, i, Array.get(valueArray, i));
            }
        }
    } else {
        throw new JSException("report.scheduling.ws.collection.parameter.type.not.supported",
                new Object[] { parameterType.getName() });
    }
    return reportValue;
}

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

/**
 * ??? ?Map?Collection//from   ww  w.ja v a  2s.c  o m
 * 
 * @param ?
 */
public static Object findElementInstance(Object collection) {
    if (collection == null)
        return null;
    if (collection.getClass().isArray()) {
        for (int i = 0; i < Array.getLength(collection); i++) {
            Object o = Array.get(collection, i);
            if (o != null) {
                return o;
            }
        }
    } else if (collection instanceof Collection) {
        for (Object o : ((Collection<?>) collection)) {
            if (o != null) {
                return o;
            }
        }
    }
    return null;
}