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.inaetics.remote.EndpointDescriptorWriter.java

private static void appendMultiValueProperty(Writer writer, String key, Object value) throws IOException {

    Class<?> componentType = determineComponentType(value);
    if (ValueTypes.get(componentType) == null) {
        throw new IllegalStateException();
    }// w w w. jav  a 2  s  .co m
    if (componentType.equals(String.class)) {
        writer.append("   <property name=\"").append(escapeXml(key)).append("\">\n");
    } else {
        writer.append("   <property name=\"").append(escapeXml(key)).append("\" value-type=\"")
                .append(componentType.getSimpleName()).append("\">\n");
    }
    if (value.getClass().isArray()) {
        List<Object> objectList = new ArrayList<Object>();
        int length = Array.getLength(value);
        for (int i = 0; i < length; i++) {
            objectList.add(Array.get(value, i));
        }
        writer.append("      <array>").append("\n");
        appendMultiValues(writer, objectList);
        writer.append("      </array>\n");
    } else if (value instanceof List<?>) {
        writer.append("      <list>").append("\n");
        appendMultiValues(writer, (List<?>) value);
        writer.append("      </list>\n");
    } else if (value instanceof Set<?>) {
        writer.append("      <set>").append("\n");
        appendMultiValues(writer, (Set<?>) value);
        writer.append("      </set>\n");
    }
    writer.append("   </property>\n");
}

From source file:org.kordamp.ezmorph.array.BooleanArrayMorpher.java

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

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

    if (array.getClass().isArray()) {
        int length = Array.getLength(array);
        int dims = getDimensions(array.getClass());
        int[] dimensions = createDimensions(dims, length);
        Object result = Array.newInstance(boolean.class, dimensions);
        BooleanMorpher morpher = isUseDefault() ? new BooleanMorpher(defaultValue) : new BooleanMorpher();
        if (dims == 1) {
            for (int index = 0; index < length; index++) {
                Array.set(result, index, morpher.morph(Array.get(array, index)) ? Boolean.TRUE : Boolean.FALSE);
            }
        } 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:org.kordamp.ezmorph.array.CharArrayMorpher.java

public Object morph(Object array) {
    if (array == null) {
        return null;
    }//from ww  w  . j  a  v  a  2  s . c om

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

    if (array.getClass().isArray()) {
        int length = Array.getLength(array);
        int dims = getDimensions(array.getClass());
        int[] dimensions = createDimensions(dims, length);
        Object result = Array.newInstance(char.class, dimensions);
        CharMorpher morpher = isUseDefault() ? new CharMorpher(defaultValue) : new CharMorpher();
        if (dims == 1) {
            for (int index = 0; index < length; index++) {
                Array.set(result, index, 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:org.kordamp.ezmorph.array.LongArrayMorpher.java

public Object morph(Object array) {
    if (array == null) {
        return null;
    }/*  w  ww.ja  v a  2s  .  c  om*/

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

    if (array.getClass().isArray()) {
        int length = Array.getLength(array);
        int dims = getDimensions(array.getClass());
        int[] dimensions = createDimensions(dims, length);
        Object result = Array.newInstance(long.class, dimensions);
        LongMorpher morpher = isUseDefault() ? new LongMorpher(defaultValue) : new LongMorpher();
        if (dims == 1) {
            for (int index = 0; index < length; index++) {
                Array.set(result, index, 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:org.gradle.caching.internal.DefaultBuildCacheHasher.java

@Override
public DefaultBuildCacheHasher putObject(Object value) {
    if (value == null) {
        this.putString("$NULL");
        return this;
    }/*from   w  w w  .  ja  va2 s .co m*/

    if (value.getClass().isArray()) {
        this.putString("Array");
        for (int idx = 0, len = Array.getLength(value); idx < len; idx++) {
            this.putInt(idx);
            this.putObject(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.putObject(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.putObject(entry.getKey());
            this.putObject(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:net.sf.morph.util.TestUtils.java

public static boolean equals(Object object1, Object object2) {
    if (log.isTraceEnabled()) {
        log.trace("Testing for equality between " + ObjectUtils.getObjectDescription(object1) + " and "
                + ObjectUtils.getObjectDescription(object2));
    }/*w ww.j  ava 2  s .c om*/
    // if both objects are == (incl. null) they are equal
    if (object1 == object2) {
        return true;
    }
    // if one object is null and the other is not, the two objects aren't
    // equal
    if (object1 == null || object2 == null) {
        return false;
    }
    if (object1 instanceof Calendar && object2 instanceof Calendar) {
        return equals(((Calendar) object1).getTime(), ((Calendar) object2).getTime());
    }
    if (object1 instanceof Comparable && object1.getClass() == object2.getClass()) {
        return ((Comparable) object1).compareTo(object2) == 0;
    }
    if (object1 instanceof Map.Entry && object2 instanceof Map.Entry) {
        if (object1.getClass() != object2.getClass()) {
            return false;
        }
        Map.Entry me1 = (Map.Entry) object1;
        Map.Entry me2 = (Map.Entry) object2;
        return equals(me1.getKey(), me2.getKey()) && equals(me1.getValue(), me2.getValue());
    }
    // if both objects are arrays
    if (object1.getClass().isArray() && object2.getClass().isArray()) {
        // if the arrays aren't of the same class, the two objects aren't
        // equal
        if (object1.getClass() != object2.getClass()) {
            return false;
        }
        // else, same type of array
        // if the arrays are different sizes, they aren't equal
        if (Array.getLength(object1) != Array.getLength(object2)) {
            return false;
        }
        // else arrays are the same size
        // iterate through the arrays and check if all elements are
        // equal
        for (int i = 0; i < Array.getLength(object1); i++) {
            // if one item isn't equal to the other
            if (!equals(Array.get(object1, i), Array.get(object2, i))) {
                // the arrays aren't equal
                return false;
            }
        }
        // if we iterated through both arrays and found no items
        // that weren't equal to each other, the collections are
        // equal
        return true;
    }
    if (object1 instanceof Iterator && object2 instanceof Iterator) {
        Iterator iterator1 = (Iterator) object1;
        Iterator iterator2 = (Iterator) object2;
        while (iterator1.hasNext()) {
            if (!iterator2.hasNext()) {
                return false;
            }
            // if one item isn't equal to the other
            if (!equals(iterator1.next(), iterator2.next())) {
                // the arrays aren't equal
                return false;
            }
        }
        // if we iterated through both collections and found
        // no items that weren't equal to each other, the
        // collections are equal
        return !iterator2.hasNext();
    }
    if (object1 instanceof Enumeration && object2 instanceof Enumeration) {
        return equals(new EnumerationIterator((Enumeration) object1),
                new EnumerationIterator((Enumeration) object2));
    }
    if ((object1 instanceof List && object2 instanceof List)
            || (object1 instanceof SortedSet && object2 instanceof SortedSet)) {
        // if the collections aren't of the same type, they aren't equal
        if (object1.getClass() != object2.getClass()) {
            return false;
        }
        // else same type of collection
        return equals(((Collection) object1).iterator(), ((Collection) object2).iterator());
    }
    if (object1 instanceof Set && object2 instanceof Set) {
        // if the sets aren't of the same type, they aren't equal
        if (object1.getClass() != object2.getClass()) {
            return false;
        }
        // else same type of set
        Set set1 = (Set) object1;
        Set set2 = (Set) object2;
        // if the sets aren't the same size, they aren't equal
        if (set1.size() != set2.size()) {
            return false;
        }
        // else sets are the same size
        Iterator iterator1 = set1.iterator();
        while (iterator1.hasNext()) {
            Object next = iterator1.next();
            if (!contains(set2, next)) {
                return false;
            }
        }
        return true;
    }
    if (object1 instanceof Map && object2 instanceof Map) {
        return equals(((Map) object1).entrySet(), ((Map) object2).entrySet());
    }
    if (object1.getClass() == object2.getClass() && object1 instanceof StringBuffer) {
        return object1.toString().equals(object2.toString());
    }
    // for primitives, use their equals methods
    if (object1.getClass() == object2.getClass()
            && (object1 instanceof String || object1 instanceof Number || object1 instanceof Boolean || //object1 instanceof StringBuffer ||
                    object1 instanceof Character)) {
        return object1.equals(object2);
    }
    // for non-primitives, compare field-by-field
    return MorphEqualsBuilder.reflectionEquals(object1, object2);
}

From source file:com.partypoker.poker.engagement.utils.EngagementBundleToJSON.java

/**
 * Recursive function to write a value to JSON.
 * @param json the JSON serializer./*from   ww  w  . ja v a2s. c o  m*/
 * @param value the value to write in JSON.
 */
private static void convert(JSONStringer json, Object value) throws JSONException {
    /* Handle null */
    if (value == null)
        json.value(null);

    /* The function is recursive if it encounters a bundle */
    else if (value instanceof Bundle) {
        /* Cast bundle */
        Bundle bundle = (Bundle) value;

        /* Open object */
        json.object();

        /* Traverse bundle */
        for (String key : bundle.keySet()) {
            /* Write key */
            json.key(key);

            /* Recursive call to write the value */
            convert(json, bundle.get(key));
        }

        /* End object */
        json.endObject();
    }

    /* Handle array, write it as a JSON array */
    else if (value.getClass().isArray()) {
        /* Open array */
        json.array();

        /* Recursive call on each value */
        int length = Array.getLength(value);
        for (int i = 0; i < length; i++)
            convert(json, Array.get(value, i));

        /* Close array */
        json.endArray();
    }

    /* Handle ArrayList, write it as a JSON array */
    else if (value instanceof ArrayList<?>) {
        /* Open array */
        json.array();

        /* Recursive call on each value */
        ArrayList<?> arrayList = (ArrayList<?>) value;
        for (Object val : arrayList)
            convert(json, val);

        /* Close array */
        json.endArray();
    }

    /* Format throwable values with the stack trace */
    else if (value instanceof Throwable) {
        Throwable t = (Throwable) value;
        StringWriter text = new StringWriter();
        t.printStackTrace(new PrintWriter(text));
        json.value(text.toString());
    }

    /* Other values are handled directly by JSONStringer (numerical, boolean and String) */
    else
        json.value(value);
}

From source file:org.kordamp.ezmorph.array.FloatArrayMorpher.java

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

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

    if (array.getClass().isArray()) {
        int length = Array.getLength(array);
        int dims = getDimensions(array.getClass());
        int[] dimensions = createDimensions(dims, length);
        Object result = Array.newInstance(float.class, dimensions);
        FloatMorpher morpher = isUseDefault() ? new FloatMorpher(defaultValue) : new FloatMorpher();
        if (dims == 1) {
            for (int index = 0; index < length; index++) {
                Array.set(result, index, 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:org.kordamp.ezmorph.array.ShortArrayMorpher.java

public Object morph(Object array) {
    if (array == null) {
        return null;
    }//from   w  w  w.j  a v  a2 s. co  m

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

    if (array.getClass().isArray()) {
        int length = Array.getLength(array);
        int dims = getDimensions(array.getClass());
        int[] dimensions = createDimensions(dims, length);
        Object result = Array.newInstance(short.class, dimensions);
        ShortMorpher morpher = isUseDefault() ? new ShortMorpher(defaultValue) : new ShortMorpher();
        if (dims == 1) {
            for (int index = 0; index < length; index++) {
                Array.set(result, index, 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:org.kordamp.ezmorph.array.DoubleArrayMorpher.java

public Object morph(Object array) {
    if (array == null) {
        return null;
    }/*from  w w  w .j a  v  a2  s  . c om*/

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

    if (array.getClass().isArray()) {
        int length = Array.getLength(array);
        int dims = getDimensions(array.getClass());
        int[] dimensions = createDimensions(dims, length);
        Object result = Array.newInstance(double.class, dimensions);
        DoubleMorpher morpher = isUseDefault() ? new DoubleMorpher(defaultValue) : new DoubleMorpher();
        if (dims == 1) {
            for (int index = 0; index < length; index++) {
                Array.set(result, index, 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());
    }
}