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:com.astamuse.asta4d.data.DefaultContextDataFinder.java

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override//from   w  w  w .  j a v a2 s . c o  m
public ContextDataHolder findDataInContext(Context context, String scope, String name, Class<?> targetType)
        throws DataOperationException {
    ContextDataHolder dataHolder = findByType(context, scope, name, targetType);

    if (dataHolder != null) {
        return dataHolder;
    }

    if (StringUtils.isEmpty(scope)) {
        dataHolder = findDataByScopeOrder(context, 0, name);
    } else {
        dataHolder = context.getDataHolder(scope, name);
    }

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

    Object foundData = dataHolder.getValue();
    Object transformedData = null;
    UnsupportedValueException usve = null;

    Class<?> srcType = new TypeInfo(foundData.getClass()).getType();
    if (targetType.isAssignableFrom(srcType)) {
        transformedData = foundData;
    } else if (srcType.isArray() && targetType.isAssignableFrom(srcType.getComponentType())) {
        transformedData = Array.get(foundData, 0);
    } else if (targetType.isArray() && targetType.getComponentType().isAssignableFrom(srcType)) {
        Object array = Array.newInstance(srcType, 1);
        Array.set(array, 0, foundData);
        transformedData = array;
    } else {
        try {
            transformedData = Configuration.getConfiguration().getDataTypeTransformer().transform(srcType,
                    targetType, foundData);
        } catch (UnsupportedValueException ex) {
            usve = ex;
        }
    }
    if (usve == null) {
        dataHolder.setData(dataHolder.getName(), dataHolder.getScope(), foundData, transformedData);
    } else {
        dataHolder.setData(dataHolder.getName(), InjectUtil.ContextDataTypeUnMatchScope, foundData,
                transformedData);
    }
    return dataHolder;
}

From source file:gda.device.scannable.ScannableGetPositionWrapper.java

Object[] calcElements() {
    if (scannableGetPositionVal == null)
        return new Object[] {};

    Object[] elements = new Object[] { scannableGetPositionVal };
    if (scannableGetPositionVal instanceof Object[]) {
        elements = (Object[]) scannableGetPositionVal;
    } else if (scannableGetPositionVal instanceof PySequence) {
        PySequence seq = (PySequence) scannableGetPositionVal;
        int len = seq.__len__();
        elements = new Object[len];
        for (int i = 0; i < len; i++) {
            elements[i] = seq.__finditem__(i);
        }//  w w  w .  j  a v  a2s .  co  m
    } else if (scannableGetPositionVal instanceof PyList) {
        PyList seq = (PyList) scannableGetPositionVal;
        int len = seq.__len__();
        elements = new Object[len];
        for (int i = 0; i < len; i++) {
            elements[i] = seq.__finditem__(i);
        }
    } else if (scannableGetPositionVal.getClass().isArray()) {
        int len = ArrayUtils.getLength(scannableGetPositionVal);
        elements = new Object[len];
        for (int i = 0; i < len; i++) {
            elements[i] = Array.get(scannableGetPositionVal, i);
        }
    } else if (scannableGetPositionVal instanceof PlottableDetectorData) {
        elements = ((PlottableDetectorData) scannableGetPositionVal).getDoubleVals();
    }
    return elements;
}

From source file:net.sf.excelutils.webwork.OgnlDynaBean.java

public Object get(String name, int index) {
    Object value = super.get(name, index);
    if (value == null) {
        Object array = valueStack.findValue(name);
        if (array.getClass().isArray()) {
            return Array.get(array, index);
        } else if (array instanceof List) {
            return ((List) array).get(index);
        }/*from www  . j  a  v  a 2 s.  c  o  m*/
    }
    return value;
}

From source file:org.directwebremoting.filter.AuditLogAjaxFilter.java

/**
 * Similar to {@link Object#toString()} except that we ensure we don't
 * return long strings, and format strings with quotes. We're probably more
 * interested in what it is rather than the exact detail.
 *//*  w ww  .ja v  a2  s . co  m*/
private String toString(Object reply) {
    if (reply == null) {
        return "null";
    }

    if (reply.getClass().isArray()) {
        if (reply.getClass().getComponentType().isPrimitive()) {
            StringBuilder builder = new StringBuilder();
            builder.append("[");
            for (int i = 0; i < Array.getLength(reply); i++) {
                if (i != 0) {
                    builder.append(",");
                }
                builder.append(Array.get(reply, i));
            }
            builder.append("]");
            reply = builder.toString();
        } else {
            reply = Arrays.deepToString((Object[]) reply);
        }
    }

    if (reply instanceof String) {
        return shorten((String) reply, maxStringLength);
    }

    if (toStringClasses.contains(reply.getClass())) {
        return reply.toString();
    }

    return shorten(reply.toString(), maxStringLength);
    //return reply.getClass().getSimpleName() + "(...)";
}

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

public static double[] convertToDoubleArray(final Object array) {
    if (array == null) {
        return null;
    }//w w  w  .  j  a v a2s  .c o  m
    if (array instanceof double[]) {
        return (double[]) array;
    }
    if (array instanceof Double[]) {
        return ArrayUtils.toPrimitive((Double[]) array);
    }
    final double[] newArray = new double[Array.getLength(array)];
    for (int i = 0; i < newArray.length; i++) {
        final Number val = (Number) Array.get(array, i);
        newArray[i] = val.doubleValue();
    }
    return newArray;
}

From source file:org.openTwoFactor.clientExt.net.sf.ezmorph.array.ByteArrayMorpher.java

public Object morph(Object array) {
    if (array == null) {
        return null;
    }//from   w w  w  .ja  v  a 2 s  .c o m

    if (BYTE_ARRAY_CLASS.isAssignableFrom(array.getClass())) {
        // no conversion needed
        return (byte[]) array;
    }

    if (array.getClass().isArray()) {
        int length = Array.getLength(array);
        int dims = getDimensions(array.getClass());
        int[] dimensions = createDimensions(dims, length);
        Object result = Array.newInstance(byte.class, dimensions);
        ByteMorpher morpher = isUseDefault() ? new ByteMorpher(defaultValue) : new ByteMorpher();
        if (dims == 1) {
            for (int index = 0; index < length; index++) {
                Array.set(result, index, new Byte(morpher.morph(Array.get(array, index))));
            }
        } else {
            for (int index = 0; index < length; index++) {
                Array.set(result, index, morph(Array.get(array, index)));
            }
        }
        return result;
    } else {
        throw new MorphException("argument is not an array: " + array.getClass());
    }
}

From source file:com.nfwork.dbfound.json.converter.NumberArrayConverter.java

public Object convert(Object array) {
    if (array == null) {
        return null;
    }/*from  ww w . j a v  a2s. c  om*/

    if (array.getClass().isArray()) {
        int length = Array.getLength(array);
        int dims = getDimensions(array.getClass());
        int[] dimensions = createDimensions(dims, length);
        Object result = Array.newInstance(type, dimensions);
        AbstractPrimitiveConverter converter = null;
        if (Byte.class.isAssignableFrom(type)) {
            converter = defaultValue != null && isUseDefault() ? new ByteConverter(defaultValue.byteValue())
                    : new ByteConverter();
        } else if (Short.class.isAssignableFrom(type)) {
            converter = defaultValue != null && isUseDefault() ? new ShortConverter(defaultValue.shortValue())
                    : new ShortConverter();
        } else if (Integer.class.isAssignableFrom(type)) {
            converter = defaultValue != null && isUseDefault() ? new IntConverter(defaultValue.intValue())
                    : new IntConverter();
        } else if (Long.class.isAssignableFrom(type)) {
            converter = defaultValue != null && isUseDefault() ? new LongConverter(defaultValue.longValue())
                    : new LongConverter();
        } else if (Float.class.isAssignableFrom(type)) {
            converter = defaultValue != null && isUseDefault() ? new FloatConverter(defaultValue.floatValue())
                    : new FloatConverter();
        } else if (Double.class.isAssignableFrom(type)) {
            converter = defaultValue != null && isUseDefault() ? new DoubleConverter(defaultValue.doubleValue())
                    : new DoubleConverter();
        }

        if (dims == 1) {
            for (int index = 0; index < length; index++) {
                try {
                    Object value = Array.get(array, index);
                    Object converted = MethodUtils.invokeMethod(converter, "convert", value);
                    Array.set(result, index, converted);
                } catch (Exception e) {
                    throw new ConversionException(e);
                }
            }
        } else {
            for (int index = 0; index < length; index++) {
                Array.set(result, index, convert(Array.get(array, index)));
            }
        }
        return result;
    } else {
        throw new ConversionException("argument is not an array: " + array.getClass());
    }
}

From source file:de.micromata.genome.util.matcher.cls.AnnotationAttributeClassMatcher.java

/**
 * Contains value.// ww  w.  j a  v  a  2  s  .c om
 *
 * @param array the array
 * @return true, if successful
 */
protected boolean containsValue(Object array) {
    int len = Array.getLength(array);
    for (int i = 0; i < len; ++i) {
        Object val = Array.get(array, i);
        if (compareSimpleType(val) == true) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

private static Object find(Map<?, ?> map, String key, int fromIndex) {
    final int indexOfDot = key.indexOf('.', fromIndex);
    final int indexOfBracket = key.indexOf('[', fromIndex);
    int indexOfEndBracket = -1;
    int arrayIndex = -1;

    boolean hasDot = indexOfDot != -1;
    boolean hasBracket = indexOfBracket != -1;

    if (hasBracket) {
        // also check that there is an end-bracket
        indexOfEndBracket = key.indexOf("]", indexOfBracket);
        hasBracket = indexOfEndBracket != -1;
        if (hasBracket) {
            final String indexString = key.substring(indexOfBracket + 1, indexOfEndBracket);
            try {
                arrayIndex = Integer.parseInt(indexString);
            } catch (NumberFormatException e) {
                // not a valid array/list index
                hasBracket = false;//  ww  w  .j  a  v a2s  .  c o  m
            }
        }
    }

    if (hasDot && hasBracket) {
        if (indexOfDot > indexOfBracket) {
            hasDot = false;
        } else {
            hasBracket = false;
        }
    }

    if (hasDot) {
        final String prefix = key.substring(0, indexOfDot);
        final Object nestedObject = map.get(prefix);
        if (nestedObject == null) {
            return find(map, key, indexOfDot + 1);
        }
        if (nestedObject instanceof Map) {
            final String remainingPart = key.substring(indexOfDot + 1);
            @SuppressWarnings("unchecked")
            final Map<String, ?> nestedMap = (Map<String, ?>) nestedObject;
            return find(nestedMap, remainingPart);
        }
    }

    if (hasBracket) {
        final String prefix = key.substring(0, indexOfBracket);
        final Object nestedObject = map.get(prefix);
        if (nestedObject == null) {
            return find(map, key, indexOfBracket + 1);
        }

        String remainingPart = key.substring(indexOfEndBracket + 1);

        try {
            final Object valueAtIndex;
            if (nestedObject instanceof List) {
                valueAtIndex = ((List<?>) nestedObject).get(arrayIndex);
            } else if (nestedObject.getClass().isArray()) {
                valueAtIndex = Array.get(nestedObject, arrayIndex);
            } else {
                // no way to extract from a non-array and non-list
                valueAtIndex = null;
            }

            if (valueAtIndex != null) {
                if (remainingPart.startsWith(".")) {
                    remainingPart = remainingPart.substring(1);
                }

                if (remainingPart.isEmpty()) {
                    return valueAtIndex;
                }

                if (valueAtIndex instanceof Map) {
                    @SuppressWarnings("unchecked")
                    final Map<String, ?> nestedMap = (Map<String, ?>) valueAtIndex;
                    return find(nestedMap, remainingPart);
                } else {
                    // not traversing any further. Should we want to add
                    // support for double-sided arrays, we could do it here.
                }
            }

        } catch (IndexOutOfBoundsException e) {
            return null;
        }
    }

    return null;
}

From source file:org.openTwoFactor.clientExt.net.sf.ezmorph.array.IntArrayMorpher.java

public Object morph(Object array) {
    if (array == null) {
        return null;
    }/*from  w  w  w .  j  a va2s  .com*/

    if (INT_ARRAY_CLASS.isAssignableFrom(array.getClass())) {
        // no conversion needed
        return (int[]) array;
    }

    if (array.getClass().isArray()) {
        int length = Array.getLength(array);
        int dims = getDimensions(array.getClass());
        int[] dimensions = createDimensions(dims, length);
        Object result = Array.newInstance(int.class, dimensions);
        IntMorpher morpher = isUseDefault() ? new IntMorpher(defaultValue) : new IntMorpher();
        if (dims == 1) {
            for (int index = 0; index < length; index++) {
                Array.set(result, index, new Integer(morpher.morph(Array.get(array, index))));
            }
        } else {
            for (int index = 0; index < length; index++) {
                Array.set(result, index, morph(Array.get(array, index)));
            }
        }
        return result;
    } else {
        throw new MorphException("argument is not an array: " + array.getClass());
    }
}