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:org.seasar.struts.action.S2RequestProcessor.java
/** * ?????//from w ww. j a v a 2 s .com * * @param array * ? * @param indexes * ?? * @param elementType * ???? * @return ?? */ protected Object getArrayValue(Object array, int[] indexes, Class<?> elementType) { Object value = array; elementType = convertClass(elementType); for (int i = 0; i < indexes.length; i++) { Object value2 = Array.get(value, indexes[i]); if (i == indexes.length - 1 && value2 == null) { value2 = ClassUtil.newInstance(elementType); Array.set(value, indexes[i], value2); } value = value2; } return value; }
From source file:org.seasar.struts.action.S2RequestProcessor.java
/** * ?????/* w ww . ja v a2s. c om*/ * * @param array * ? * @param indexes * ?? * @param value * */ protected void setArrayValue(Object array, int[] indexes, Object value) { for (int i = 0; i < indexes.length - 1; i++) { array = Array.get(array, indexes[i]); } Array.set(array, indexes[indexes.length - 1], value); }
From source file:jef.tools.ArrayUtils.java
/** * ?:set//from ww w.ja v a2s.c o m * * @param: index * ??-1?-2 */ public final static void set(Object obj, int index, Object value) { if (index >= 0) { Array.set(obj, index, value); } else { Array.set(obj, Array.getLength(obj) + index, value); } }
From source file:org.tinygroup.weblayer.webcontext.parser.util.BeanWrapperImpl.java
private Object growArrayIfNecessary(Object array, int index, String name) { if (!this.autoGrowNestedPaths) { return array; }/*from w w w . jav a2 s.c o m*/ int length = Array.getLength(array); if (index >= length) { Class<?> componentType = array.getClass().getComponentType(); Object newArray = Array.newInstance(componentType, index + 1); System.arraycopy(array, 0, newArray, 0, length); for (int i = length; i < Array.getLength(newArray); i++) { Array.set(newArray, i, newValue(componentType, name)); } setPropertyValue(name, newArray); return newArray; } else { return array; } }
From source file:org.springframework.beans.AbstractNestablePropertyAccessor.java
private Object growArrayIfNecessary(Object array, int index, String name) { if (!isAutoGrowNestedPaths()) { return array; }// w w w . j a va 2 s.co m int length = Array.getLength(array); if (index >= length && index < this.autoGrowCollectionLimit) { Class<?> componentType = array.getClass().getComponentType(); Object newArray = Array.newInstance(componentType, index + 1); System.arraycopy(array, 0, newArray, 0, length); for (int i = length; i < Array.getLength(newArray); i++) { Array.set(newArray, i, newValue(componentType, null, name)); } setPropertyValue(name, newArray); Object defaultValue = getPropertyValue(name); Assert.state(defaultValue != null, "Default value must not be null"); return defaultValue; } else { return array; } }
From source file:org.tinygroup.weblayer.webcontext.parser.util.BeanWrapperImpl.java
private void setPropertyValue(PropertyTokenHolder tokens, PropertyValue pv) throws BeansException { String propertyName = tokens.canonicalName; String actualName = tokens.actualName; if (tokens.keys != null) { // Apply indexes and map keys: fetch value for all keys but the last one. PropertyTokenHolder getterTokens = new PropertyTokenHolder(); getterTokens.canonicalName = tokens.canonicalName; getterTokens.actualName = tokens.actualName; getterTokens.keys = new String[tokens.keys.length - 1]; System.arraycopy(tokens.keys, 0, getterTokens.keys, 0, tokens.keys.length - 1); Object propValue = null;//from w w w . j a v a2 s .c o m try { propValue = getPropertyValue(getterTokens); } catch (NotReadablePropertyException ex) { throw new NotWritablePropertyException(getRootClass(), this.nestedPath + propertyName, "Cannot access indexed value in property referenced " + "in indexed property path '" + propertyName + "'", ex); } // Set value for last key. String key = tokens.keys[tokens.keys.length - 1]; if (propValue == null) { throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + propertyName, "Cannot access indexed value in property referenced " + "in indexed property path '" + propertyName + "': returned null"); } else if (propValue.getClass().isArray()) { Class requiredType = propValue.getClass().getComponentType(); int arrayIndex = Integer.parseInt(key); Object oldValue = null; try { if (isExtractOldValueForEditor()) { oldValue = Array.get(propValue, arrayIndex); } Object convertedValue = this.typeConverterDelegate.convertIfNecessary(propertyName, oldValue, pv.getValue(), requiredType); Array.set(propValue, Integer.parseInt(key), convertedValue); } catch (IllegalArgumentException ex) { PropertyChangeEvent pce = new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, pv.getValue()); throw new TypeMismatchException(pce, requiredType, ex); } catch (IndexOutOfBoundsException ex) { throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName, "Invalid array index in property path '" + propertyName + "'", ex); } } else if (propValue instanceof List) { PropertyDescriptor pd = getCachedIntrospectionResults().getPropertyDescriptor(actualName); Class requiredType = null; if (JdkVersion.isAtLeastJava15()) { requiredType = GenericCollectionTypeResolver.getCollectionReturnType(pd.getReadMethod(), tokens.keys.length); } List list = (List) propValue; int index = Integer.parseInt(key); Object oldValue = null; if (isExtractOldValueForEditor() && index < list.size()) { oldValue = list.get(index); } try { Object convertedValue = this.typeConverterDelegate.convertIfNecessary(propertyName, oldValue, pv.getValue(), requiredType); if (index < list.size()) { list.set(index, convertedValue); } else if (index >= list.size()) { for (int i = list.size(); i < index; i++) { try { list.add(null); } catch (NullPointerException ex) { throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName, "Cannot set element with index " + index + " in List of size " + list.size() + ", accessed using property path '" + propertyName + "': List does not support filling up gaps with null elements"); } } list.add(convertedValue); } } catch (IllegalArgumentException ex) { PropertyChangeEvent pce = new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, pv.getValue()); throw new TypeMismatchException(pce, requiredType, ex); } } else if (propValue instanceof Map) { PropertyDescriptor pd = getCachedIntrospectionResults().getPropertyDescriptor(actualName); Class mapKeyType = null; Class mapValueType = null; if (JdkVersion.isAtLeastJava15()) { mapKeyType = GenericCollectionTypeResolver.getMapKeyReturnType(pd.getReadMethod(), tokens.keys.length); mapValueType = GenericCollectionTypeResolver.getMapValueReturnType(pd.getReadMethod(), tokens.keys.length); } Map map = (Map) propValue; Object convertedMapKey = null; Object convertedMapValue = null; try { // IMPORTANT: Do not pass full property name in here - property editors // must not kick in for map keys but rather only for map values. convertedMapKey = this.typeConverterDelegate.convertIfNecessary(key, mapKeyType); } catch (IllegalArgumentException ex) { PropertyChangeEvent pce = new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, null, pv.getValue()); throw new TypeMismatchException(pce, mapKeyType, ex); } Object oldValue = null; if (isExtractOldValueForEditor()) { oldValue = map.get(convertedMapKey); } try { // Pass full property name and old value in here, since we want full // conversion ability for map values. convertedMapValue = this.typeConverterDelegate.convertIfNecessary(propertyName, oldValue, pv.getValue(), mapValueType, null, new MethodParameter(pd.getReadMethod(), -1, tokens.keys.length + 1)); } catch (IllegalArgumentException ex) { PropertyChangeEvent pce = new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, pv.getValue()); throw new TypeMismatchException(pce, mapValueType, ex); } map.put(convertedMapKey, convertedMapValue); } else { throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName, "Property referenced in indexed property path '" + propertyName + "' is neither an array nor a List nor a Map; returned value was [" + pv.getValue() + "]"); } } else { PropertyDescriptor pd = pv.resolvedDescriptor; if (pd == null || !pd.getWriteMethod().getDeclaringClass().isInstance(this.object)) { pd = getCachedIntrospectionResults().getPropertyDescriptor(actualName); if (pd == null || pd.getWriteMethod() == null) { PropertyMatches matches = PropertyMatches.forProperty(propertyName, getRootClass()); throw new NotWritablePropertyException(getRootClass(), this.nestedPath + propertyName, matches.buildErrorMessage(), matches.getPossibleMatches()); } pv.getOriginalPropertyValue().resolvedDescriptor = pd; } Object oldValue = null; try { Object originalValue = pv.getValue(); Object valueToApply = originalValue; if (!Boolean.FALSE.equals(pv.conversionNecessary)) { if (pv.isConverted()) { valueToApply = pv.getConvertedValue(); } else { if (isExtractOldValueForEditor() && pd.getReadMethod() != null) { Method readMethod = pd.getReadMethod(); if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); } try { oldValue = readMethod.invoke(this.object, new Object[0]); } catch (Exception ex) { if (logger.isDebugEnabled()) { logger.debug("Could not read previous value of property '" + this.nestedPath + propertyName + "'", ex); } } } valueToApply = this.typeConverterDelegate.convertIfNecessary(oldValue, originalValue, pd); } pv.getOriginalPropertyValue().conversionNecessary = Boolean .valueOf(valueToApply != originalValue); } Method writeMethod = pd.getWriteMethod(); if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(this.object, new Object[] { valueToApply }); } catch (InvocationTargetException ex) { PropertyChangeEvent propertyChangeEvent = new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, pv.getValue()); if (ex.getTargetException() instanceof ClassCastException) { throw new TypeMismatchException(propertyChangeEvent, pd.getPropertyType(), ex.getTargetException()); } else { throw new MethodInvocationException(propertyChangeEvent, ex.getTargetException()); } } catch (IllegalArgumentException ex) { PropertyChangeEvent pce = new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, pv.getValue()); throw new TypeMismatchException(pce, pd.getPropertyType(), ex); } catch (IllegalAccessException ex) { PropertyChangeEvent pce = new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, pv.getValue()); throw new MethodInvocationException(pce, ex); } } }
From source file:org.springframework.beans.BeanWrapperImpl.java
private Object growArrayIfNecessary(Object array, int index, String name) { if (!isAutoGrowNestedPaths()) { return array; }/*w w w .j a va 2 s . c o m*/ int length = Array.getLength(array); if (index >= length && index < this.autoGrowCollectionLimit) { Class<?> componentType = array.getClass().getComponentType(); Object newArray = Array.newInstance(componentType, index + 1); System.arraycopy(array, 0, newArray, 0, length); for (int i = length; i < Array.getLength(newArray); i++) { Array.set(newArray, i, newValue(componentType, null, name)); } // TODO this is not efficient because conversion may create a copy ... set directly because we know it is assignable. setPropertyValue(name, newArray); return getPropertyValue(name); } else { return array; } }
From source file:net.yasion.common.core.bean.wrapper.impl.ExtendedBeanWrapperImpl.java
private Object growArrayIfNecessary(Object array, int index, String name) { if (!isAutoGrowNestedPaths()) { return array; }//from w w w . j ava2s. co m int length = Array.getLength(array); if (index >= length && index < this.autoGrowCollectionLimit) { Class<?> componentType = array.getClass().getComponentType(); Object newArray = Array.newInstance(componentType, index + 1); System.arraycopy(array, 0, newArray, 0, length); for (int i = length; i < Array.getLength(newArray); i++) { Array.set(newArray, i, newValue(componentType, null, name)); } // #TO#DO# this is not efficient because conversion may create a copy ... set directly because we know it is assignable. setPropertyValue(name, newArray); return getPropertyValue(name); } else { return array; } }
From source file:com.zenesis.qx.remote.RequestHandler.java
/** * Reads an array from JSON, where each value is of the class clazz. Note that while the result * is an array, you cannot assume that it is an array of Object, or use generics because generics * are always Objects - this is because arrays of primitive types are not arrays of Objects * @param jp//from www.j av a 2 s . c o m * @param clazz * @return * @throws IOException */ private Object readArray(JsonParser jp, Class clazz) throws IOException { if (jp.getCurrentToken() == JsonToken.VALUE_NULL) return null; boolean isProxyClass = Proxied.class.isAssignableFrom(clazz); ArrayList result = new ArrayList(); for (; jp.nextToken() != JsonToken.END_ARRAY;) { if (isProxyClass) { Integer id = jp.readValueAs(Integer.class); if (id != null) { Proxied obj = getProxied(id); if (obj == null) log.fatal("Cannot read object of class " + clazz + " from id=" + id); else if (!clazz.isInstance(obj)) throw new ClassCastException( "Cannot cast " + obj + " class " + obj.getClass() + " to " + clazz); else result.add(obj); } else result.add(null); } else { Object obj = readSimpleValue(jp, clazz); result.add(obj); } } Object arr = Array.newInstance(clazz, result.size()); for (int i = 0; i < result.size(); i++) Array.set(arr, i, result.get(i)); return arr; //return result.toArray(Array.newInstance(clazz, result.size())); }
From source file:org.springframework.beans.AbstractNestablePropertyAccessor.java
private Object newValue(Class<?> type, @Nullable TypeDescriptor desc, String name) { try {/*from w w w .j a v a2s . co m*/ if (type.isArray()) { Class<?> componentType = type.getComponentType(); // TODO - only handles 2-dimensional arrays if (componentType.isArray()) { Object array = Array.newInstance(componentType, 1); Array.set(array, 0, Array.newInstance(componentType.getComponentType(), 0)); return array; } else { return Array.newInstance(componentType, 0); } } else if (Collection.class.isAssignableFrom(type)) { TypeDescriptor elementDesc = (desc != null ? desc.getElementTypeDescriptor() : null); return CollectionFactory.createCollection(type, (elementDesc != null ? elementDesc.getType() : null), 16); } else if (Map.class.isAssignableFrom(type)) { TypeDescriptor keyDesc = (desc != null ? desc.getMapKeyTypeDescriptor() : null); return CollectionFactory.createMap(type, (keyDesc != null ? keyDesc.getType() : null), 16); } else { Constructor<?> ctor = type.getDeclaredConstructor(); if (Modifier.isPrivate(ctor.getModifiers())) { throw new IllegalAccessException("Auto-growing not allowed with private constructor: " + ctor); } return BeanUtils.instantiateClass(ctor); } } catch (Throwable ex) { throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + name, "Could not instantiate property type [" + type.getName() + "] to auto-grow nested property path", ex); } }