Example usage for java.lang.reflect Array get

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

Introduction

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

Prototype

public static native Object get(Object array, int index)
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;

Source Link

Document

Returns the value of the indexed component in the specified array object.

Usage

From source file:org.jbpm.formModeler.service.bb.mvc.taglib.formatter.ForFormatter.java

public void service(HttpServletRequest request, HttpServletResponse response) throws FormatterException {
    log.debug("Servicing ForFormatter.");
    Object array = getParameter("array");
    if (array == null) {
        Object componentName = getParameter("bean");
        Object propertyName = getParameter("property");
        if (componentName != null) {
            Object component = CDIBeanLocator.getBeanByNameOrType((String) componentName);
            array = component;//from w  w  w.  ja v  a2 s  . c o  m
            if (propertyName != null) {
                JXPathContext ctx = JXPathContext.newContext(component);
                try {
                    array = ctx.getValue((String) propertyName);
                } catch (Exception e) {
                    log.debug("Error:", e);
                }
            }
        }
    }
    String sortProperties = (String) getParameter("sortProperties");

    Iterator iterator = null;
    if (array == null) {
        renderFragment("empty");
        return;
    }

    if (array instanceof Collection) {
        iterator = ((Collection) array).iterator();
    } else if (array.getClass().isArray()) {
        final Object theArray = array;
        iterator = new Iterator() {
            int index = 0;

            public void remove() {
                throw new UnsupportedOperationException();
            }

            public boolean hasNext() {
                return Array.getLength(theArray) > index;
            }

            public Object next() {
                return Array.get(theArray, index++);
            }
        };
    } else if (array instanceof Iterator) {
        iterator = (Iterator) array;
    } else if (array instanceof Enumeration) {
        List l = new ArrayList();
        while (((Enumeration) array).hasMoreElements()) {
            l.add(((Enumeration) array).nextElement());
        }
        iterator = l.iterator();
    }

    if (sortProperties != null) {
        iterator = getSortedIterator(iterator, sortProperties);
    }

    if (iterator != null && iterator.hasNext()) {
        renderFragment("outputStart");
        int i = 0;
        while (iterator.hasNext()) {
            Object o = iterator.next();
            setAttribute("index", new Integer(i));
            setAttribute("count", new Integer(++i));
            if (o != null)
                setAttribute("element", o);
            else
                setAttribute("element", getParameter("nullValue"));
            renderFragment("output");
        }
        renderFragment("outputEnd");
    } else {
        renderFragment("empty");
    }
}

From source file:com.proctorcam.proctorserv.HashedAuthenticator.java

protected static Object[] getArray(Object arr) {
    // Does not convert if not necessary
    if (arr instanceof Object[])
        return (Object[]) arr;

    int arrlength = Array.getLength(arr);
    Object[] outputArray = new Object[arrlength];
    for (int i = 0; i < arrlength; ++i) {
        outputArray[i] = Array.get(arr, i);
    }/*w  w  w .  j  a  v  a 2  s  . c  om*/
    return outputArray;
}

From source file:com.feilong.core.lang.ObjectUtilTemp.java

/**
 * ?? {@link java.util.Iterator}.//  w  w  w. j  a va 2s.  c  o  m
 * <p>
 * ??,,???with no copying<br>
 * ?, ClassCastException  ,Rats -- ,arrayList ?arrayList.iterator()
 * </p>
 * <p>
 * <b>:</b>{@link Arrays#asList(Object...)} list {@link Array}  ArrayList,
 * {@link java.util.AbstractList#add(int, Object)} ,<br>
 * listadd?, {@link java.lang.UnsupportedOperationException}
 * </p>
 * 
 * @param <T>
 *            the generic type
 * @param arrays
 *            ,? , 
 * @return  (null == arrays)  null;<br>
 *         ?arrays?Object[], {@link Arrays#asList(Object...)}?list,? {@link List#iterator()
 *         t}<br>
 *         ,? Object[],?, {@link Array#getLength(Object)}?, {@link Array#get(Object, int)} list
 * @see Arrays#asList(Object...)
 * @see Array#getLength(Object)
 * @see Array#get(Object, int)
 * @see List#iterator()
 * @deprecated
 */
@Deprecated
@SuppressWarnings({ "unchecked" })
public static <T> Iterator<T> toIterator(Object arrays) {
    if (null == arrays) {
        return null;
    }
    List<T> list = null;
    try {
        // ??,,???with no copying
        Object[] objArrays = (Object[]) arrays;
        list = (List<T>) toList(objArrays);
    } catch (ClassCastException e) {
        LOGGER.debug("arrays can not cast to Object[],maybe primitive type,values is:{},{}", arrays,
                e.getMessage());
        // Rats -- 
        int length = Array.getLength(arrays);
        list = new ArrayList<T>(length);
        for (int i = 0; i < length; ++i) {
            Object object = Array.get(arrays, i);
            list.add((T) object);
        }
    }
    return list.iterator();
}

From source file:org.gradle.caching.internal.DefaultBuildCacheKeyBuilder.java

public BuildCacheKeyBuilder appendToCacheKey(Object value) {

    if (value == null) {
        this.putString("$NULL");
        return this;
    }//  www  . j  ava  2  s .  c om

    if (value.getClass().isArray()) {
        this.putString("Array");
        for (int idx = 0, len = Array.getLength(value); idx < len; idx++) {
            this.putInt(idx);
            this.appendToCacheKey(Array.get(value, idx));
        }
        return this;
    }

    if (value instanceof Iterable) {
        this.putString("Iterable");
        int idx = 0;
        for (Object elem : (Iterable<?>) value) {
            this.putInt(idx);
            this.appendToCacheKey(elem);
            idx++;
        }
        return this;
    }

    if (value instanceof Map) {
        this.putString("Map");
        int idx = 0;
        for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
            this.putInt(idx);
            this.appendToCacheKey(entry.getKey());
            this.appendToCacheKey(entry.getValue());
            idx++;
        }
        return this;
    }

    if (value instanceof Boolean) {
        this.putBoolean((Boolean) value);
    } else if (value instanceof Long) {
        this.putLong((Long) value);
    } else if (value instanceof Integer) {
        this.putInt((Integer) value);
    } else if (value instanceof Short) {
        this.putInt((Short) value);
    } else if (value instanceof Byte) {
        this.putInt((Byte) value);
    } else if (value instanceof Double) {
        this.putDouble((Double) value);
    } else if (value instanceof Float) {
        this.putDouble((Float) value);
    } else if (value instanceof BigInteger) {
        this.putBytes(((BigInteger) value).toByteArray());
    } else if (value instanceof CharSequence) {
        this.putString((CharSequence) value);
    } else if (value instanceof Enum) {
        this.putString(value.getClass().getName());
        this.putString(((Enum) value).name());
    } else {
        byte[] bytes = SerializationUtils.serialize((Serializable) value);
        this.putBytes(bytes);
    }
    return this;
}

From source file:org.gradle.api.internal.changedetection.state.TaskExecution.java

private static void appendToCacheKey(TaskCacheKeyBuilder builder, Object value) {
    if (value == null) {
        builder.putString("$NULL");
        return;/* w  w w  .ja v a 2s . c  om*/
    }

    if (value.getClass().isArray()) {
        builder.putString("Array");
        for (int idx = 0, len = Array.getLength(value); idx < len; idx++) {
            builder.putInt(idx);
            appendToCacheKey(builder, Array.get(value, idx));
        }
        return;
    }

    if (value instanceof Iterable) {
        builder.putString("Iterable");
        int idx = 0;
        for (Object elem : (Iterable<?>) value) {
            builder.putInt(idx);
            appendToCacheKey(builder, elem);
            idx++;
        }
        return;
    }

    if (value instanceof Map) {
        builder.putString("Map");
        int idx = 0;
        for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
            builder.putInt(idx);
            appendToCacheKey(builder, entry.getKey());
            appendToCacheKey(builder, entry.getValue());
            idx++;
        }
        return;
    }

    if (value instanceof Boolean) {
        builder.putBoolean((Boolean) value);
    } else if (value instanceof Integer) {
        builder.putInt((Integer) value);
    } else if (value instanceof Short) {
        builder.putInt((Short) value);
    } else if (value instanceof Byte) {
        builder.putInt((Byte) value);
    } else if (value instanceof Double) {
        builder.putDouble((Double) value);
    } else if (value instanceof Float) {
        builder.putDouble((Float) value);
    } else if (value instanceof BigInteger) {
        builder.putBytes(((BigInteger) value).toByteArray());
    } else if (value instanceof CharSequence) {
        builder.putString((CharSequence) value);
    } else if (value instanceof Enum) {
        builder.putString(value.getClass().getName());
        builder.putString(((Enum) value).name());
    } else {
        byte[] bytes = SerializationUtils.serialize((Serializable) value);
        builder.putBytes(bytes);
    }
}

From source file:br.msf.commons.util.ArrayUtils.java

@SuppressWarnings("SuspiciousSystemArraycopy")
public static <T extends Object> T[] merge(final Class<T> itemClass, final boolean ignoreNulls,
        final Object... arrays) {
    if (getSize(arrays) == 0) {
        return null;
    }//w w  w  . j  ava  2 s. co m
    final T[] ret = (T[]) Array.newInstance(itemClass, getTotalSize(arrays));
    int pos = 0;
    for (Object array : arrays) {
        final int len = getSize(array);
        if (len > 0) {
            if (ignoreNulls) {
                /* trata manualmente cada posio, ignorando os nulls */
                for (int i = 0; i < len; i++) {
                    T item = (T) Array.get(array, i);
                    if (item != null) {
                        ret[pos] = item;
                        pos++;
                    }
                }
            } else {
                /* faz a cpia nativa */
                System.arraycopy(array, 0, ret, pos, len);
                pos += len;
            }
        }
    }
    return ret;
}

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

/**
 * {@inheritDoc}/*from w  w  w.  j  a  v  a2  s.co  m*/
 */
@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;
}

From source file:com.smallchill.core.toolbox.Func.java

/**
 * ??//from   w  ww. j  av  a2 s  .  c  o  m
 * 
 * @param obj
 *            
 * @param element
 *            
 * @return ??
 */
public static boolean contains(Object obj, Object element) {
    if (obj == null) {
        return false;
    }
    if (obj instanceof String) {
        if (element == null) {
            return false;
        }
        return ((String) obj).contains(element.toString());
    }
    if (obj instanceof Collection) {
        return ((Collection<?>) obj).contains(element);
    }
    if (obj instanceof Map) {
        return ((Map<?, ?>) obj).values().contains(element);
    }

    if (obj instanceof Iterator) {
        Iterator<?> iter = (Iterator<?>) obj;
        while (iter.hasNext()) {
            Object o = iter.next();
            if (equals(o, element)) {
                return true;
            }
        }
        return false;
    }
    if (obj instanceof Enumeration) {
        Enumeration<?> enumeration = (Enumeration<?>) obj;
        while (enumeration.hasMoreElements()) {
            Object o = enumeration.nextElement();
            if (equals(o, element)) {
                return true;
            }
        }
        return false;
    }
    if (obj.getClass().isArray() == true) {
        int len = Array.getLength(obj);
        for (int i = 0; i < len; i++) {
            Object o = Array.get(obj, i);
            if (equals(o, element)) {
                return true;
            }
        }
    }
    return false;
}

From source file:com.jilk.ros.rosbridge.implementation.JSON.java

private static JSONArray convertArrayToJSONArray(Object array) {
    JSONArray result = new JSONArray();
    for (int i = 0; i < Array.getLength(array); i++) {
        Object elementObject = Array.get(array, i);
        if (elementObject != null) {
            Object resultObject = convertElementToJSON(elementObject);
            result.add(resultObject);/*from www  .  j av a2  s . c om*/
        }
    }
    return result;
}

From source file:com.jaspersoft.jasperserver.export.modules.common.DefaultReportParametersTranslator.java

protected Object[] toBeanParameterValues(Object value) {
    Object[] values;//w w w .  j  a v a 2 s.  c  om
    if (value == null) {
        values = new Object[] { toBeanParameterValue(null) };
    } else if (value.getClass().isArray()) {
        int count = Array.getLength(value);
        values = new Object[count];
        for (int idx = 0; idx < count; idx++) {
            values[idx] = toBeanParameterValue(Array.get(value, idx));
        }
    } else if (value instanceof Collection) {
        Collection valueCollection = (Collection) value;
        values = new Object[valueCollection.size()];

        Iterator iterator = valueCollection.iterator();
        for (int i = 0; iterator.hasNext(); i++) {
            values[i] = toBeanParameterValue(iterator.next());
        }
    } else {
        values = new Object[] { toBeanParameterValue(value) };
    }

    return values;
}