List of usage examples for java.lang.reflect Array set
public static native void set(Object array, int index, Object value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
From source file:eu.qualityontime.commons.QPropertyUtilsBean.java
public void _setIndexedProperty(final Object bean, final String name, final int index, final Object value) throws Exception { if (bean == null) { throw new IllegalArgumentException("No bean specified"); }/*w w w . j av a2 s.com*/ if (name == null || name.length() == 0) { if (bean.getClass().isArray()) { Array.set(bean, index, value); return; } else if (bean instanceof List) { final List<Object> list = toObjectList(bean); list.set(index, value); return; } } if (name == null) { throw new IllegalArgumentException("No name specified for bean class '" + bean.getClass() + "'"); } if (name.startsWith("@")) { Object f = FieldUtils.readField(bean, name.substring(1)); if (null == f) throw new NestedNullException(); if (f.getClass().isArray()) { Array.set(f, index, value); return; } else if (f instanceof List) { final List<Object> list = toObjectList(f); list.set(index, value); return; } return; } // Retrieve the property descriptor for the specified property final PropertyDescriptor descriptor = getPropertyDescriptor(bean, name); if (descriptor == null) { throw new NoSuchMethodException( "Unknown property '" + name + "' on bean class '" + bean.getClass() + "'"); } // Call the indexed setter method if there is one if (descriptor instanceof IndexedPropertyDescriptor) { Method writeMethod = ((IndexedPropertyDescriptor) descriptor).getIndexedWriteMethod(); writeMethod = MethodUtils.getAccessibleMethod(bean.getClass(), writeMethod); if (writeMethod != null) { final Object[] subscript = new Object[2]; subscript[0] = new Integer(index); subscript[1] = value; try { if (log.isTraceEnabled()) { final String valueClassName = value == null ? "<null>" : value.getClass().getName(); log.trace("setSimpleProperty: Invoking method " + writeMethod + " with index=" + index + ", value=" + value + " (class " + valueClassName + ")"); } invokeMethod(writeMethod, bean, subscript); } catch (final InvocationTargetException e) { if (e.getTargetException() instanceof IndexOutOfBoundsException) { throw (IndexOutOfBoundsException) e.getTargetException(); } else { throw e; } } return; } } // Otherwise, the underlying property must be an array or a list final Method readMethod = getReadMethod(bean.getClass(), descriptor); if (readMethod == null) { throw new NoSuchMethodException( "Property '" + name + "' has no getter method on bean class '" + bean.getClass() + "'"); } // Call the property getter to get the array or list final Object array = invokeMethod(readMethod, bean, EMPTY_OBJECT_ARRAY); if (!array.getClass().isArray()) { if (array instanceof List) { // Modify the specified value in the List final List<Object> list = toObjectList(array); list.set(index, value); } else { throw new IllegalArgumentException( "Property '" + name + "' is not indexed on bean class '" + bean.getClass() + "'"); } } else { // Modify the specified value in the array Array.set(array, index, value); } }
From source file:org.evergreen.web.utils.beanutils.PropertyUtilsBean.java
/** * Set the value of the specified indexed property of the specified * bean, with no type conversions. In addition to supporting the JavaBeans * specification, this method has been extended to support * <code>List</code> objects as well. * * @param bean Bean whose property is to be set * @param name Simple property name of the property value to be set * @param index Index of the property value to be set * @param value Value to which the indexed property element is to be set * * @exception IndexOutOfBoundsException if the specified index * is outside the valid range for the underlying property * @exception IllegalAccessException if the caller does not have * access to the property accessor method * @exception IllegalArgumentException if <code>bean</code> or * <code>name</code> is null * @exception InvocationTargetException if the property accessor method * throws an exception/* w w w . java2 s .co m*/ * @exception NoSuchMethodException if an accessor method for this * propety cannot be found */ public void setIndexedProperty(Object bean, String name, int index, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { if (bean == null) { throw new IllegalArgumentException("No bean specified"); } if (name == null || name.length() == 0) { if (bean.getClass().isArray()) { Array.set(bean, index, value); return; } else if (bean instanceof List) { ((List) bean).set(index, value); return; } } if (name == null) { throw new IllegalArgumentException("No name specified for bean class '" + bean.getClass() + "'"); } // Handle DynaBean instances specially if (bean instanceof DynaBean) { DynaProperty descriptor = ((DynaBean) bean).getDynaClass().getDynaProperty(name); if (descriptor == null) { throw new NoSuchMethodException( "Unknown property '" + name + "' on bean class '" + bean.getClass() + "'"); } ((DynaBean) bean).set(name, index, value); return; } // Retrieve the property descriptor for the specified property PropertyDescriptor descriptor = getPropertyDescriptor(bean, name); if (descriptor == null) { throw new NoSuchMethodException( "Unknown property '" + name + "' on bean class '" + bean.getClass() + "'"); } // Call the indexed setter method if there is one if (descriptor instanceof IndexedPropertyDescriptor) { Method writeMethod = ((IndexedPropertyDescriptor) descriptor).getIndexedWriteMethod(); writeMethod = MethodUtils.getAccessibleMethod(bean.getClass(), writeMethod); if (writeMethod != null) { Object[] subscript = new Object[2]; subscript[0] = new Integer(index); subscript[1] = value; try { if (log.isTraceEnabled()) { String valueClassName = value == null ? "<null>" : value.getClass().getName(); log.trace("setSimpleProperty: Invoking method " + writeMethod + " with index=" + index + ", value=" + value + " (class " + valueClassName + ")"); } invokeMethod(writeMethod, bean, subscript); } catch (InvocationTargetException e) { if (e.getTargetException() instanceof IndexOutOfBoundsException) { throw (IndexOutOfBoundsException) e.getTargetException(); } else { throw e; } } return; } } // Otherwise, the underlying property must be an array or a list Method readMethod = getReadMethod(bean.getClass(), descriptor); if (readMethod == null) { throw new NoSuchMethodException( "Property '" + name + "' has no getter method on bean class '" + bean.getClass() + "'"); } // Call the property getter to get the array or list Object array = invokeMethod(readMethod, bean, EMPTY_OBJECT_ARRAY); if (!array.getClass().isArray()) { if (array instanceof List) { // Modify the specified value in the List //((List) array).set(index, value); //? //((List) array).add(value); List list = (List) array; Type returnType = readMethod.getGenericReturnType(); Type acturalType = ((ParameterizedType) returnType).getActualTypeArguments()[0]; list.add(ConvertUtils.convert(value, (Class) acturalType)); } else if (array instanceof Set) { //?set?? //((Set) array).add(value); Set set = (Set) array; Type returnType = readMethod.getGenericReturnType(); Type acturalType = ((ParameterizedType) returnType).getActualTypeArguments()[0]; set.add(ConvertUtils.convert(value, (Class) acturalType)); } else { throw new IllegalArgumentException( "Property '" + name + "' is not indexed on bean class '" + bean.getClass() + "'"); } } else { // Modify the specified value in the array Array.set(array, index, value); } }
From source file:org.jakz.common.JSONObject.java
/** * DO NOT USE! This is just a sketch in development. * @param source/*from w ww . j a va2s .c om*/ * @param target * @return * @throws IllegalArgumentException * @throws IllegalAccessException * @throws InstantiationException * @throws NoSuchMethodException * @throws SecurityException * @throws InvocationTargetException */ public static Object injectIntoPOJO(Object source, Object target) throws IllegalArgumentException, IllegalAccessException, InstantiationException, NoSuchMethodException, SecurityException, InvocationTargetException { if (source instanceof Byte || source instanceof Character || source instanceof Short || source instanceof Integer || source instanceof Long || source instanceof Boolean || source instanceof Float || source instanceof Double || source instanceof String || source instanceof BigInteger || source instanceof BigDecimal) { return source; } Class<?> pojoClass = null; if (target != null) pojoClass = target.getClass(); else { pojoClass = source.getClass(); target = pojoClass.newInstance(); } Field[] field = pojoClass.getFields(); Object toReturn = pojoClass.newInstance(); for (int i = 0; i < field.length; i++) { String fieldName = field[i].getName(); Class<?> fieldType = field[i].getType(); ParameterizedType fieldParameterizedType = (ParameterizedType) field[i].getGenericType(); if (source instanceof JSONObject) { JSONObject sjson = (JSONObject) source; if (fieldType == Integer.class || fieldType == Byte.class) field[i].set(target, sjson.getInt(fieldName)); else if (fieldType == BigInteger.class || fieldType == Long.class) field[i].set(target, sjson.getBigInteger(fieldName)); else if (fieldType == Boolean.class) field[i].set(target, sjson.getBoolean(fieldName)); else if (fieldType == Double.class) field[i].set(target, sjson.getDouble(fieldName)); else if (fieldType == String.class || fieldType == Character.class) field[i].set(target, sjson.getString(fieldName)); else if (fieldType.isArray()) { JSONArray ajson = sjson.getJSONArray(fieldName); Object apojo = Array.newInstance(fieldType, ajson.length()); Class<?> componentType = fieldType.getComponentType(); for (int ai = 0; ai < ajson.length(); ai++) { Object toAddValue = ajson.get(i); Object componentPOJO = componentType.newInstance(); Array.set(apojo, ai, injectIntoPOJO(toAddValue, componentPOJO)); } field[i].set(target, apojo); } else if (Collection.class.isAssignableFrom(fieldType)) { JSONArray ajson = sjson.getJSONArray(fieldName); Collection<?> cpojo = (Collection<?>) fieldType.newInstance(); for (int ai = 0; ai < ajson.length(); ai++) { Object toAddValue = ajson.get(i); Class<?> genericType = (Class<?>) fieldParameterizedType.getActualTypeArguments()[0]; System.out.println("generic type: " + genericType.getName()); Object componentPOJO = genericType.newInstance(); //TODO - funkar inte Method addMethod = fieldType.getMethod("add", Object.class); addMethod.invoke(cpojo, injectIntoPOJO(toAddValue, componentPOJO)); } field[i].set(target, cpojo); } else { try { Object pojoInstance = fieldType.newInstance(); field[i].set(target, injectIntoPOJO(sjson.get(fieldName), pojoInstance)); } catch (Exception e) { throw new JSONException("Could not inject field " + fieldName + " of type " + fieldType.getName() + " into POJO.", e); } } } //add more sources } return toReturn; }
From source file:org.openTwoFactor.client.util.TwoFactorClientCommonUtils.java
/** * helper method to clone the value of a field. just returns the same * reference for primitives and immutables. Will subclone GrouperCloneables, * and will throw exception if not expecting the type. Will clone sets, lists, maps. * @param <T> template//from w w w .j a v a2 s .co m * * @param value * @return the cloned value */ @SuppressWarnings("unchecked") public static <T> T cloneValue(T value) { Object clonedValue = value; if (value == null || value instanceof String || value.getClass().isPrimitive() || value instanceof Number || value instanceof Boolean || value instanceof Date) { //clone things //for strings, and immutable classes, just assign //nothing to do, just assign the value } else if (value instanceof Map) { clonedValue = new LinkedHashMap(); Map mapValue = (Map) value; Map clonedMapValue = (Map) clonedValue; for (Object key : mapValue.keySet()) { clonedMapValue.put(cloneValue(key), cloneValue(mapValue.get(key))); } } else if (value instanceof Set) { clonedValue = new LinkedHashSet(); Set setValue = (Set) value; Set clonedSetValue = (Set) clonedValue; for (Object each : setValue) { clonedSetValue.add(cloneValue(each)); } } else if (value instanceof List) { clonedValue = new ArrayList(); List listValue = (List) value; List clonedListValue = (List) clonedValue; for (Object each : listValue) { clonedListValue.add(cloneValue(each)); } } else if (value.getClass().isArray()) { clonedValue = Array.newInstance(value.getClass().getComponentType(), Array.getLength(value)); for (int i = 0; i < Array.getLength(value); i++) { Array.set(clonedValue, i, cloneValue(Array.get(value, i))); } } else { //this means lets add support for a new type of object throw new RuntimeException("Unexpected class in clone method: " + value.getClass()); } return (T) clonedValue; }
From source file:org.kchine.r.server.DirectJNI.java
public static Object getJavaArrayFromRArray__(RArray array) { int[] dim = array.getDim(); RVector vector = array.getValue();//from www. j a v a2 s. c o m Class<?> componentType = null; if (vector instanceof RInteger) componentType = int.class; else if (vector instanceof RNumeric) componentType = double.class; else if (vector instanceof RChar) componentType = String.class; else if (vector instanceof RLogical) componentType = boolean.class; Object result = null; try { result = Array.newInstance(componentType, dim); } catch (Exception e) { e.printStackTrace(); } Vector<Integer> v = new Vector<Integer>(); int p = 1; for (int i = dim.length - 1; i > 0; --i) { p = p * dim[i]; v.add(0, p); } for (int bi = 0; bi < p * dim[0]; ++bi) { int bindex = bi; int[] indexes = new int[dim.length]; for (int i = 0; i < indexes.length - 1; ++i) { indexes[i] = bindex / v.elementAt(i); bindex = bindex % v.elementAt(i); } indexes[indexes.length - 1] = bindex; Object arrayTail = null; if (dim.length == 1) { arrayTail = result; } else { arrayTail = Array.get(result, indexes[0]); for (int i = 1; i < indexes.length - 1; ++i) arrayTail = Array.get(arrayTail, indexes[i]); } if (vector instanceof RInteger) Array.setInt(arrayTail, indexes[indexes.length - 1], ((RInteger) vector).getValue()[bi]); else if (vector instanceof RNumeric) Array.setDouble(arrayTail, indexes[indexes.length - 1], ((RNumeric) vector).getValue()[bi]); else if (vector instanceof RChar) Array.set(arrayTail, indexes[indexes.length - 1], ((RChar) vector).getValue()[bi]); else if (vector instanceof RLogical) Array.setBoolean(arrayTail, indexes[indexes.length - 1], ((RLogical) vector).getValue()[bi]); } return result; }
From source file:org.kchine.r.server.DirectJNI.java
public static Object getJavaArrayFromRArray(RArray array) { int[] dim = array.getDim(); RVector vector = array.getValue();//from w ww. j a va 2 s.co m Class<?> componentType = null; if (vector instanceof RInteger) componentType = int.class; else if (vector instanceof RNumeric) componentType = double.class; else if (vector instanceof RChar) componentType = String.class; else if (vector instanceof RLogical) componentType = boolean.class; Object result = null; try { result = Array.newInstance(componentType, dim); } catch (Exception e) { e.printStackTrace(); } Vector<Integer> v1 = new Vector<Integer>(); int p1 = 1; for (int i = dim.length - 1; i > 0; --i) { p1 = p1 * dim[i]; v1.add(0, p1); } Vector<Integer> v2 = new Vector<Integer>(); int p2 = 1; for (int i = 0; i < dim.length - 1; ++i) { p2 = p2 * dim[i]; v2.add(0, p2); } for (int bi = 0; bi < p1 * dim[0]; ++bi) { int bindex = bi; int[] indexes = new int[dim.length]; for (int i = 0; i < indexes.length - 1; ++i) { indexes[i] = bindex / v1.elementAt(i); bindex = bindex % v1.elementAt(i); } indexes[indexes.length - 1] = bindex; Object arrayTail = null; if (dim.length == 1) { arrayTail = result; } else { arrayTail = Array.get(result, indexes[0]); for (int i = 1; i < indexes.length - 1; ++i) arrayTail = Array.get(arrayTail, indexes[i]); } int linearVectorIndex = 0; for (int i = (indexes.length - 1); i > 0; --i) linearVectorIndex += indexes[i] * v2.elementAt((indexes.length - 1) - i); linearVectorIndex += indexes[0]; // System.out.println("linearVectorIndex:"+linearVectorIndex); if (vector instanceof RInteger) Array.setInt(arrayTail, indexes[indexes.length - 1], ((RInteger) vector).getValue()[linearVectorIndex]); else if (vector instanceof RNumeric) Array.setDouble(arrayTail, indexes[indexes.length - 1], ((RNumeric) vector).getValue()[linearVectorIndex]); else if (vector instanceof RChar) Array.set(arrayTail, indexes[indexes.length - 1], ((RChar) vector).getValue()[linearVectorIndex]); else if (vector instanceof RLogical) Array.setBoolean(arrayTail, indexes[indexes.length - 1], ((RLogical) vector).getValue()[linearVectorIndex]); } return result; }
From source file:org.openTwoFactor.client.util.TwoFactorClientCommonUtils.java
/** * Convert a list to an array with the type of the first element e.g. if it * is a list of Person objects, then the array is Person[] * //from w w w .j ava 2s . c o m * @param objectOrArrayOrCollection * is a list * @return the array of objects with type of the first element in the list */ public static Object toArray(Object objectOrArrayOrCollection) { // do this before length since if array with null in it, we want ti get // it back if (objectOrArrayOrCollection != null && objectOrArrayOrCollection.getClass().isArray()) { return objectOrArrayOrCollection; } int length = length(objectOrArrayOrCollection); if (length == 0) { return null; } if (objectOrArrayOrCollection instanceof Collection) { Collection collection = (Collection) objectOrArrayOrCollection; Object first = collection.iterator().next(); return toArray(collection, first == null ? Object.class : first.getClass()); } // make an array of the type of object passed in, size one Object array = Array.newInstance(objectOrArrayOrCollection.getClass(), 1); Array.set(array, 0, objectOrArrayOrCollection); return array; }
From source file:com.evolveum.midpoint.provisioning.ucf.impl.ConnectorInstanceIcfImpl.java
private Object[] convertToIcfArray(PrismProperty prismProperty, Class<?> componentType) throws ConfigurationException { List<PrismPropertyValue> values = prismProperty.getValues(); Object valuesArrary = Array.newInstance(componentType, values.size()); for (int j = 0; j < values.size(); ++j) { Object icfValue = convertToIcf(values.get(j), componentType); Array.set(valuesArrary, j, icfValue); }//from www . j a v a 2s . c o m return (Object[]) valuesArrary; }
From source file:com.processing.core.PApplet.java
static public Object append(Object b, Object value) { int length = Array.getLength(b); b = expand(b, length + 1);/*ww w . j a v a 2 s. c o m*/ Array.set(b, length, value); return b; }
From source file:com.processing.core.PApplet.java
static final public Object splice(Object list, Object v, int index) { Object[] outgoing = null;//from www .ja va 2s . c om int length = Array.getLength(list); // check whether item being spliced in is an array if (v.getClass().getName().charAt(0) == '[') { int vlength = Array.getLength(v); outgoing = new Object[length + vlength]; System.arraycopy(list, 0, outgoing, 0, index); System.arraycopy(v, 0, outgoing, index, vlength); System.arraycopy(list, index, outgoing, index + vlength, length - index); } else { outgoing = new Object[length + 1]; System.arraycopy(list, 0, outgoing, 0, index); Array.set(outgoing, index, v); System.arraycopy(list, index, outgoing, index + 1, length - index); } return outgoing; }