Java Code Examples for java.lang.reflect.Array#get()

The following examples show how to use java.lang.reflect.Array#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OffHeapEncoder.java    From Concurnas with MIT License 6 votes vote down vote up
private int putObjectArray(Object object, int levels, boolean tag){
	int size = 0;
	if(tag){
		size = this.tagObject(object, null, true, true, true);
	}
	if(object==null){
		return size;
	}//no vid needed
	int len = Array.getLength(object);
	size += put(len);
	for(int n=0; n < len; n++){
		Object got = Array.get(object, n);
		if(levels == 1){
			size += put(got);
		}else{
			size += putObjectArray(got, levels-1);
		}
	}
	
	return size;		
}
 
Example 2
Source File: BasicAttribute.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
  * Calculates the hash code of this attribute.
  *<p>
  * The hash code is computed by adding the hash code of
  * the attribute's id and that of all of its values except for
  * values that are arrays.
  * For an array, the hash code of each element of the array is summed.
  * If a subclass overrides <tt>hashCode()</tt>, it should override
  * <tt>equals()</tt>
  * as well so that two attributes that are equal have the same hash code.
  *
  * @return an int representing the hash code of this attribute.
  * @see #equals
  */
public int hashCode() {
    int hash = attrID.hashCode();
    int num = values.size();
    Object val;
    for (int i = 0; i < num; i ++) {
        val = values.elementAt(i);
        if (val != null) {
            if (val.getClass().isArray()) {
                Object it;
                int len = Array.getLength(val);
                for (int j = 0 ; j < len ; j++) {
                    it = Array.get(val, j);
                    if (it != null) {
                        hash += it.hashCode();
                    }
                }
            } else {
                hash += val.hashCode();
            }
        }
    }
    return hash;
}
 
Example 3
Source File: GraphComparator.java    From java-util with Apache License 2.0 6 votes vote down vote up
private static String getStringValue(Object foo)
{
    if (foo == null)
    {
        return "null";
    }
    else if (foo.getClass().isArray())
    {
        StringBuilder s = new StringBuilder();
        s.append('[');
        int len = Array.getLength(foo);
        for (int i=0; i < len; i++)
        {
            Object element = Array.get(foo, i);
            s.append(element == null ? "null" : element.toString());
            if (i < len - 1)
            {
                s.append(',');
            }
        }
        s.append(']');
        return s.toString();
    }
    return foo.toString();
}
 
Example 4
Source File: BasicAttribute.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Determines whether two arrays are equal by comparing each of their
 * elements using <tt>Object.equals()</tt>.
 */
private static boolean arrayEquals(Object a1, Object a2) {
    int len;
    if ((len = Array.getLength(a1)) != Array.getLength(a2))
        return false;

    for (int j = 0; j < len; j++) {
        Object i1 = Array.get(a1, j);
        Object i2 = Array.get(a2, j);
        if (i1 == null || i2 == null) {
            if (i1 != i2)
                return false;
        } else if (!i1.equals(i2)) {
            return false;
        }
    }
    return true;
}
 
Example 5
Source File: CollectionDecoder.java    From flex-blazeds with Apache License 2.0 6 votes vote down vote up
protected Collection decodeCollection(Collection collectionShell, Object encodedObject)
{
    Object obj = null;

    if (encodedObject instanceof String)
    {
        encodedObject = ((String)encodedObject).toCharArray();
    }
    else if (encodedObject instanceof Collection)
    {
        encodedObject = ((Collection)encodedObject).toArray();
    }

    int len = Array.getLength(encodedObject);

    for (int i = 0; i < len; i++)
    {
        obj = Array.get(encodedObject, i);
        collectionShell.add(obj);
    }

    return collectionShell;
}
 
Example 6
Source File: AbstractConverter.java    From commons-beanutils with Apache License 2.0 6 votes vote down vote up
/**
 * Return the first element from an Array (or Collection)
 * or the value unchanged if not an Array (or Collection).
 *
 * N.B. This needs to be overridden for array/Collection converters.
 *
 * @param value The value to convert
 * @return The first element in an Array (or Collection)
 * or the value unchanged if not an Array (or Collection)
 */
protected Object convertArray(final Object value) {
    if (value == null) {
        return null;
    }
    if (value.getClass().isArray()) {
        if (Array.getLength(value) > 0) {
            return Array.get(value, 0);
        }
        return null;
    }
    if (value instanceof Collection) {
        final Collection<?> collection = (Collection<?>)value;
        if (collection.size() > 0) {
            return collection.iterator().next();
        }
        return null;
    }
    return value;
}
 
Example 7
Source File: ObjectUtils.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
public static Object[] toObjectArray(Object source) {
    if (source instanceof Object[]) {
        return (Object[]) source;
    } else if (source == null) {
        return new Object[0];
    } else if (!source.getClass().isArray()) {
        throw new IllegalArgumentException("Source is not an array: " + source);
    } else {
        int length = Array.getLength(source);
        if (length == 0) {
            return new Object[0];
        } else {
            Class wrapperType = Array.get(source, 0).getClass();
            Object[] newArray = (Object[]) Array.newInstance(wrapperType, length);

            for (int i = 0; i < length; ++i) {
                newArray[i] = Array.get(source, i);
            }

            return newArray;
        }
    }
}
 
Example 8
Source File: UserConfigurationDictionary.java    From ews-java-api with MIT License 6 votes vote down vote up
/**
 * Validates the dictionary object (key or entry value).
 *
 * @param dictionaryObject Object to validate.
 * @throws Exception the exception
 */
private void validateObject(Object dictionaryObject) throws Exception {
  // Keys may not be null but we rely on the internal dictionary to throw
  // if the key is null.
  if (dictionaryObject != null) {
    if (dictionaryObject.getClass().isArray()) {
      int length = Array.getLength(dictionaryObject);
      Class<?> wrapperType = Array.get(dictionaryObject, 0).getClass();
      Object[] newArray = (Object[]) Array.
          newInstance(wrapperType, length);
      for (int i = 0; i < length; i++) {
        newArray[i] = Array.get(dictionaryObject, i);
      }
      this.validateArrayObject(newArray);
    } else {
      this.validateObjectType(dictionaryObject);

    }

  } else {
    throw new NullPointerException();
  }
}
 
Example 9
Source File: DatasetUtils.java    From january with Eclipse Public License 1.0 6 votes vote down vote up
private static void setRow(Object row, Dataset a, int... pos) {
	final int l = Array.getLength(row);
	final int rank = pos.length;
	final int[] npos = Arrays.copyOf(pos, rank+1);
	Object r;
	if (rank+1 < a.getRank()) {
		for (int i = 0; i < l; i++) {
			npos[rank] = i;
			r = Array.get(row, i);
			setRow(r, a, npos);
		}
	} else {
		for (int i = 0; i < l; i++) {
			npos[rank] = i;
			r = a.getObject(npos);
			Array.set(row, i, r);
		}
	}
}
 
Example 10
Source File: ActivationSerializer.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public ArrayFieldStorage create(Object objectToStore, Class type) {
    FieldStorage[] fields = new FieldStorage[Array.getLength(objectToStore)];
    type = type.isArray()? type.getComponentType():type;
    for(int i=0;i<fields.length;i++){
        Object o = Array.get(objectToStore,i);
        if(o!=null){
            fields[i] = getFieldStorage(o, type);
        }
    }
    return new ArrayFieldStorage(type,fields);
}
 
Example 11
Source File: BeanDiff.java    From jaxb2-rich-contract-plugin with MIT License 5 votes vote down vote up
public static Object[] toObjectArray(Object array) {
	int length = Array.getLength(array);
	Object[] ret = new Object[length];
	for (int i = 0; i < length; i++)
		ret[i] = Array.get(array, i);
	return ret;

}
 
Example 12
Source File: ValueListBeanInfoImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public final void serializeURIs(Object array, XMLSerializer target) throws SAXException {
    if(xducer.useNamespace()) {
        int len = Array.getLength(array);
        for( int i=0; i<len; i++ )  {
            Object item = Array.get(array,i);
            try {
                xducer.declareNamespace(item,target);
            } catch (AccessorException e) {
                target.reportError("arrayItem",e);
            }
        }
    }
}
 
Example 13
Source File: MXBeanTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static String deepToString(Object o) {
    StringBuffer buf = new StringBuffer();
    buf.append("[");
    int len = Array.getLength(o);
    for (int i = 0; i < len; i++) {
        if (i > 0)
            buf.append(", ");
        Object e = Array.get(o, i);
        buf.append(string(e));
    }
    buf.append("]");
    return buf.toString();
}
 
Example 14
Source File: ArrayTypeAdapter.java    From sagetv with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override public void write(JsonWriter out, Object array) throws IOException {
  if (array == null) {
    out.nullValue();
    return;
  }

  out.beginArray();
  for (int i = 0, length = Array.getLength(array); i < length; i++) {
    E value = (E) Array.get(array, i);
    componentTypeAdapter.write(out, value);
  }
  out.endArray();
}
 
Example 15
Source File: AttributesTest.java    From jhdf with MIT License 5 votes vote down vote up
Class<?> getArrayType(Object array) {
	Object element = Array.get(array, 0);
	if (element.getClass().isArray()) {
		return getArrayType(element);
	} else {
		return array.getClass().getComponentType();
	}
}
 
Example 16
Source File: ArrayConverter.java    From hasor with Apache License 2.0 5 votes vote down vote up
/**
 * Handles conversion to a String.
 *
 * @param value The value to be converted.
 * @return the converted String value.
 * @throws Throwable if an error occurs converting to a String
 */
@Override
protected String convertToString(final Object value) throws Throwable {
    int size = 0;
    Iterator iterator = null;
    Class type = value.getClass();
    if (type.isArray()) {
        size = Array.getLength(value);
    } else {
        Collection collection = this.convertToCollection(type, value);
        size = collection.size();
        iterator = collection.iterator();
    }
    if (size == 0) {
        return (String) this.getDefault(String.class);
    }
    if (this.onlyFirstToString) {
        size = 1;
    }
    // Create a StringBuffer containing a delimited list of the values
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < size; i++) {
        if (i > 0) {
            buffer.append(this.delimiter);
        }
        Object element = iterator == null ? Array.get(value, i) : iterator.next();
        element = this.elementConverter.convert(String.class, element);
        if (element != null) {
            buffer.append(element);
        }
    }
    return buffer.toString();
}
 
Example 17
Source File: ArrayIterator.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public Object next() {
    i++; // move to next element
    if ( i>= n ) {
        throw new NoSuchElementException();
    }
    return Array.get(array, i);
}
 
Example 18
Source File: AutoDisposer.java    From libgdx-snippets with MIT License 4 votes vote down vote up
private static void disposeArray(Object object) {

		int length = Array.getLength(object);

		for (int i = 0; i < length; i++) {

			Object element = Array.get(object, i);
			Class<?> elementType = element.getClass();

			if (elementType.isArray()) {
				disposeArray(element);
			} else {
				dispose(element, elementType);
			}

		}

	}
 
Example 19
Source File: MethodExitReturnValuesTest.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
static void doit(MethodExitReturnValuesTarg xx) {
    s_show("==========  Testing static methods ================");
    s_bytef();
    s_charf();
    s_doublef();
    s_floatf();
    s_intf();
    s_longf();
    s_shortf();
    s_booleanf();

    s_stringf();
    s_classf();
    s_classLoaderf();
    s_threadf();
    s_threadGroupf();
    s_intArrayf();
    s_nullObjectf();
    s_objectf();
    s_voidf();

    s_show("==========  Testing instance methods ================");
    xx.i_bytef();
    xx.i_charf();
    xx.i_doublef();
    xx.i_floatf();
    xx.i_intf();
    xx.i_longf();
    xx.i_shortf();
    xx.i_booleanf();
    xx.i_stringf();
    xx.i_intArrayf();
    xx.i_classf();
    xx.i_classLoaderf();
    xx.i_threadf();
    xx.i_threadGroupf();
    xx.i_nullObjectf();
    xx.i_objectf();
    xx.i_voidf();

    // Prove it works for native methods too
    s_show("==========  Testing native methods ================");
    StrictMath.sin(doubleValue);
    Array.getByte(arrByte, 0);
    Array.getChar(arrChar, 0);
    Array.getDouble(arrDouble, 0);
    Array.getFloat(arrFloat, 0);
    Array.getInt(arrInt, 0);
    Array.getLong(arrLong, 0);
    Array.getShort(arrShort, 0);
    Array.getBoolean(arrBoolean, 0);
    Array.get(arrObject, 0);
    stringValue.intern();
}
 
Example 20
Source File: PropertyElementHandler.java    From hottub with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Performs the search of the getter for the property
 * with specified {@code name} in specified class
 * and returns value of the property.
 *
 * @param bean   the context bean that contains property
 * @param name   the name of the property
 * @param index  the index of the indexed property
 * @return the value of the property
 * @throws IllegalAccessException    if the property is not accesible
 * @throws IntrospectionException    if the bean introspection is failed
 * @throws InvocationTargetException if the getter cannot be invoked
 * @throws NoSuchMethodException     if the getter is not found
 */
private static Object getPropertyValue(Object bean, String name, Integer index) throws IllegalAccessException, IntrospectionException, InvocationTargetException, NoSuchMethodException {
    Class<?> type = bean.getClass();
    if (index == null) {
        return MethodUtil.invoke(findGetter(type, name), bean, new Object[] {});
    } else if (type.isArray() && (name == null)) {
        return Array.get(bean, index);
    } else {
        return MethodUtil.invoke(findGetter(type, name, int.class), bean, new Object[] {index});
    }
}