List of usage examples for java.lang.reflect Array getLength
@HotSpotIntrinsicCandidate public static native int getLength(Object array) throws IllegalArgumentException;
From source file:net.sf.juffrou.reflect.JuffrouTypeConverterDelegate.java
private Object convertToTypedArray(Object input, String propertyName, Class<?> componentType) { if (input instanceof Collection) { // Convert Collection elements to array elements. Collection coll = (Collection) input; Object result = Array.newInstance(componentType, coll.size()); int i = 0; for (Iterator it = coll.iterator(); it.hasNext(); i++) { Object value = convertIfNecessary(buildIndexedPropertyName(propertyName, i), null, it.next(), componentType);/*from ww w .j a v a2s . co m*/ Array.set(result, i, value); } return result; } else if (input.getClass().isArray()) { // Convert array elements, if necessary. if (componentType.equals(input.getClass().getComponentType()) && !this.propertyEditorRegistry.hasCustomEditorForElement(componentType, propertyName)) { return input; } int arrayLength = Array.getLength(input); Object result = Array.newInstance(componentType, arrayLength); for (int i = 0; i < arrayLength; i++) { Object value = convertIfNecessary(buildIndexedPropertyName(propertyName, i), null, Array.get(input, i), componentType); Array.set(result, i, value); } return result; } else { // A plain value: convert it to an array with a single component. Object result = Array.newInstance(componentType, 1); Object value = convertIfNecessary(buildIndexedPropertyName(propertyName, 0), null, input, componentType); Array.set(result, 0, value); return result; } }
From source file:javadz.beanutils.BeanUtilsBean.java
/** * Return the value of the specified array property of the specified * bean, as a String array./*ww w. ja va2s . co m*/ * * @param bean Bean whose property is to be extracted * @param name Name of the property to be extracted * @return The array property value * * @exception IllegalAccessException if the caller does not have * access to the property accessor method * @exception InvocationTargetException if the property accessor method * throws an exception * @exception NoSuchMethodException if an accessor method for this * property cannot be found */ public String[] getArrayProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Object value = getPropertyUtils().getProperty(bean, name); if (value == null) { return (null); } else if (value instanceof Collection) { ArrayList values = new ArrayList(); Iterator items = ((Collection) value).iterator(); while (items.hasNext()) { Object item = items.next(); if (item == null) { values.add((String) null); } else { // convert to string using convert utils values.add(getConvertUtils().convert(item)); } } return ((String[]) values.toArray(new String[values.size()])); } else if (value.getClass().isArray()) { int n = Array.getLength(value); String[] results = new String[n]; for (int i = 0; i < n; i++) { Object item = Array.get(value, i); if (item == null) { results[i] = null; } else { // convert to string using convert utils results[i] = getConvertUtils().convert(item); } } return (results); } else { String[] results = new String[1]; results[0] = getConvertUtils().convert(value); return (results); } }
From source file:jef.database.DbUtils.java
/** * /*www .j a v a 2s . co m*/ * * @param data * * @param pk * ?Map? */ public static void setPrimaryKeyValue(IQueryableEntity data, Object pk) throws PersistenceException { List<ColumnMapping> fields = MetaHolder.getMeta(data).getPKFields(); if (fields.isEmpty()) return; Assert.notNull(pk); // if (pk instanceof Map) { // Map<String, Object> pkMap = (Map<String, Object>) pk; // BeanWrapper wrapper = BeanWrapper.wrap(data, BeanWrapper.FAST); // for (Field f : fields) { // if (wrapper.isWritableProperty(f.name())) { // wrapper.setPropertyValue(f.name(), pkMap.get(f.name())); // } // } // } else if (pk.getClass().isArray()) { int length = Array.getLength(pk); int n = 0; Assert.isTrue(length == fields.size()); for (ColumnMapping f : fields) { f.getFieldAccessor().set(data, Array.get(pk, n++)); } } else { if (fields.size() != 1) { throw new PersistenceException("No Proper PK fields!"); } fields.get(0).getFieldAccessor().set(data, pk); } }
From source file:javadz.beanutils.LazyDynaBean.java
/** * Grow the size of an indexed property/* www. j a va 2 s .c o m*/ * @param name The name of the property * @param indexedProperty The current property value * @param index The indexed value to grow the property to (i.e. one less than * the required size) * @return The new property value (grown to the appropriate size) */ protected Object growIndexedProperty(String name, Object indexedProperty, int index) { // Grow a List to the appropriate size if (indexedProperty instanceof List) { List list = (List) indexedProperty; while (index >= list.size()) { Class contentType = getDynaClass().getDynaProperty(name).getContentType(); Object value = null; if (contentType != null) { value = createProperty(name + "[" + list.size() + "]", contentType); } list.add(value); } } // Grow an Array to the appropriate size if ((indexedProperty.getClass().isArray())) { int length = Array.getLength(indexedProperty); if (index >= length) { Class componentType = indexedProperty.getClass().getComponentType(); Object newArray = Array.newInstance(componentType, (index + 1)); System.arraycopy(indexedProperty, 0, newArray, 0, length); indexedProperty = newArray; set(name, indexedProperty); int newLength = Array.getLength(indexedProperty); for (int i = length; i < newLength; i++) { Array.set(indexedProperty, i, createProperty(name + "[" + i + "]", componentType)); } } } return indexedProperty; }
From source file:ArrayUtils.java
/** * Removes all contents of <code>array2</code> from <code>array1</code>. All * instances of <code>array2</code> will also be removed from * <code>array1</code>./*from www . j av a 2s .com*/ * * @param <T> * The type of the array * @param array1 * The array to remove elements from * @param array2 * The array containing the elements to remove; or the element to * remove itself * @return <code>array1</code> missing all the contents of * <code>array2</code> */ public static <T> T[] removeAll(T[] array1, Object array2) { if (array1 == null || array2 == null) return array1; if (!array1.getClass().isArray()) return null; if (!array2.getClass().isArray()) array2 = new Object[] { array2 }; java.util.BitSet remove = new java.util.BitSet(); int len1 = array1.length; int len2 = Array.getLength(array2); int i, j; for (i = 0; i < len1; i++) { for (j = 0; j < len2; j++) { if (equals(array1[i], Array.get(array2, j))) { remove.set(i); break; } } } T[] ret = (T[]) Array.newInstance(array1.getClass().getComponentType(), len1 - remove.cardinality()); // This copying section might be replaced by a more efficient version // using System.arraycopy()--this would be much faster than reflection, // especially for large arrays needing only a few elements removed for (i = 0, j = 0; i < len1; i++) { if (!remove.get(i)) { ret[j] = array1[i]; j++; } } return ret; }
From source file:com.sun.faces.config.ManagedBeanFactory.java
/** * <li><p> Call the property getter, if it exists.</p></li> * <p/>//from w w w . ja v a 2 s . c om * <li><p>If the getter returns null or doesn't exist, create a * java.util.ArrayList(), otherwise use the returned Object (an array or * a java.util.List).</p></li> * <p/> * <li><p>If a List was returned or created in step 2), add all * elements defined by nested <value> elements in the order * they are listed, converting values defined by nested * <value> elements to the type defined by * <value-class>. If a <value-class> is not defined, use * the value as-is (i.e., as a java.lang.String). Add null for each * <null-value> element.</p></li> * <p/> * <li><p> If an array was returned in step 2), create a * java.util.ArrayList and copy all elements from the returned array to * the new List, auto-boxing elements of a primitive type. Add all * elements defined by nested <value> elements as described in step * 3).</p></li> * <p/> * <li><p> If a new java.util.List was created in step 2) and the * property is of type List, set the property by calling the setter * method, or log an error if there is no setter method.</p></li> * <p/> * <li><p> If a new java.util.List was created in step 4), convert * the * List to array of the same type as the property and set the * property by * calling the setter method, or log an error if there * is no setter * method.</p></li> */ private void setArrayOrListPropertiesIntoBean(Object bean, ManagedPropertyBean property) throws Exception { Object result = null; boolean getterIsNull = true, getterIsArray = false; List valuesForBean = null; Class valueType = java.lang.String.class, propertyType = null; String propertyName = property.getPropertyName(); try { // see if there is a getter result = PropertyUtils.getProperty(bean, propertyName); getterIsNull = (null == result) ? true : false; propertyType = PropertyUtils.getPropertyType(bean, propertyName); getterIsArray = propertyType.isArray(); } catch (NoSuchMethodException nsme) { // it's valid to not have a getter. } // the property has to be either a List or Array if (!getterIsArray) { if (null != propertyType && !java.util.List.class.isAssignableFrom(propertyType)) { throw new FacesException( Util.getExceptionMessageString(Util.MANAGED_BEAN_CANNOT_SET_LIST_ARRAY_PROPERTY_ID, new Object[] { propertyName, managedBean.getManagedBeanName() })); } } // // Deal with the possibility of the getter returning existing // values. // // if the getter returned non-null if (!getterIsNull) { // if what it returned was an array if (getterIsArray) { valuesForBean = new ArrayList(); for (int i = 0, len = Array.getLength(result); i < len; i++) { // add the existing values valuesForBean.add(Array.get(result, i)); } } else { // if what it returned was not a List if (!(result instanceof List)) { // throw an exception throw new FacesException( Util.getExceptionMessageString(Util.MANAGED_BEAN_EXISTING_VALUE_NOT_LIST_ID, new Object[] { propertyName, managedBean.getManagedBeanName() })); } valuesForBean = (List) result; } } else { // getter returned null result = valuesForBean = new ArrayList(); } // at this point valuesForBean contains the existing values from // the bean, or no values if the bean had no values. In any // case, we can proceed to add values from the config file. valueType = copyListEntriesFromConfigToList(property.getListEntries(), valuesForBean); // at this point valuesForBean has the values to be set into the // bean. if (getterIsArray) { // convert back to Array result = Array.newInstance(valueType, valuesForBean.size()); for (int i = 0, len = valuesForBean.size(); i < len; i++) { if (valueType == Boolean.TYPE) { Array.setBoolean(result, i, ((Boolean) valuesForBean.get(i)).booleanValue()); } else if (valueType == Byte.TYPE) { Array.setByte(result, i, ((Byte) valuesForBean.get(i)).byteValue()); } else if (valueType == Double.TYPE) { Array.setDouble(result, i, ((Double) valuesForBean.get(i)).doubleValue()); } else if (valueType == Float.TYPE) { Array.setFloat(result, i, ((Float) valuesForBean.get(i)).floatValue()); } else if (valueType == Integer.TYPE) { Array.setInt(result, i, ((Integer) valuesForBean.get(i)).intValue()); } else if (valueType == Character.TYPE) { Array.setChar(result, i, ((Character) valuesForBean.get(i)).charValue()); } else if (valueType == Short.TYPE) { Array.setShort(result, i, ((Short) valuesForBean.get(i)).shortValue()); } else if (valueType == Long.TYPE) { Array.setLong(result, i, ((Long) valuesForBean.get(i)).longValue()); } else { Array.set(result, i, valuesForBean.get(i)); } } } else { result = valuesForBean; } if (getterIsNull || getterIsArray) { PropertyUtils.setProperty(bean, propertyName, result); } }
From source file:javadz.beanutils.PropertyUtilsBean.java
/** * Return 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 extracted * @param name Simple property name of the property value to be extracted * @param index Index of the property value to be extracted * @return the indexed property value// www .j a v a 2 s. co m * * @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 * @exception NoSuchMethodException if an accessor method for this * propety cannot be found */ public Object getIndexedProperty(Object bean, String name, int index) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { if (bean == null) { throw new IllegalArgumentException("No bean specified"); } if (name == null || name.length() == 0) { if (bean.getClass().isArray()) { return Array.get(bean, index); } else if (bean instanceof List) { return ((List) bean).get(index); } } 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() + "'"); } return (((DynaBean) bean).get(name, index)); } // 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 getter method if there is one if (descriptor instanceof IndexedPropertyDescriptor) { Method readMethod = ((IndexedPropertyDescriptor) descriptor).getIndexedReadMethod(); readMethod = MethodUtils.getAccessibleMethod(bean.getClass(), readMethod); if (readMethod != null) { Object[] subscript = new Object[1]; subscript[0] = new Integer(index); try { return (invokeMethod(readMethod, bean, subscript)); } catch (InvocationTargetException e) { if (e.getTargetException() instanceof IndexOutOfBoundsException) { throw (IndexOutOfBoundsException) e.getTargetException(); } else { throw e; } } } } // Otherwise, the underlying property must be an array 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 and return the value Object value = invokeMethod(readMethod, bean, EMPTY_OBJECT_ARRAY); if (!value.getClass().isArray()) { if (!(value instanceof java.util.List)) { throw new IllegalArgumentException( "Property '" + name + "' is not indexed on bean class '" + bean.getClass() + "'"); } else { //get the List's value return ((java.util.List) value).get(index); } } else { //get the array's value try { return (Array.get(value, index)); } catch (ArrayIndexOutOfBoundsException e) { throw new ArrayIndexOutOfBoundsException( "Index: " + index + ", Size: " + Array.getLength(value) + " for property '" + name + "'"); } } }
From source file:flex.messaging.services.http.proxy.RequestFilter.java
/** * Process the request body and return its content. * * @param context the context//from w ww . j av a 2s. c om * @return the body content */ protected Object processBody(ProxyContext context) { //FIXME: Should we also send on URL params that were used to contact the message broker servlet? Object body = context.getBody(); if (body == null) { return ""; } else if (body instanceof String) { return body; } else if (body instanceof Map) { Map params = (Map) body; StringBuffer postData = new StringBuffer(); boolean formValues = false; for (Object entry : params.entrySet()) { String name = (String) ((Map.Entry) entry).getKey(); if (!formValues) { formValues = true; } else { postData.append('&'); } Object vals = ((Map.Entry) entry).getValue(); if (vals == null) { encodeParam(postData, name, ""); } else if (vals instanceof String) { String val = (String) vals; encodeParam(postData, name, val); } else if (vals instanceof List) { List valLists = (List) vals; for (int i = 0; i < valLists.size(); i++) { Object o = valLists.get(i); String val = ""; if (o != null) val = o.toString(); if (i > 0) postData.append('&'); encodeParam(postData, name, val); } } else if (vals.getClass().isArray()) { for (int i = 0; i < Array.getLength(vals); i++) { Object o = Array.get(vals, i); String val = ""; if (o != null) val = o.toString(); if (i > 0) postData.append('&'); encodeParam(postData, name, val); } } } return postData.toString(); } else if (body.getClass().isArray()) { return body; } else if (body instanceof InputStream) { return body; } else { return body.toString(); } }
From source file:ArrayUtils.java
/** * Searches an array (possibly primitive) for an element * //from ww w .j a v a2 s .c om * @param anArray * The array to search * @param anElement * The element to search for * @return True if <code>anArray</code> contains <code>anElement</code>, * false otherwise */ public static boolean containsP(Object anArray, Object anElement) { if (anArray == null) return false; if (anArray instanceof Object[]) { Object[] oa = (Object[]) anArray; for (int i = 0; i < oa.length; i++) { if (equals(oa[i], anElement)) return true; } } else { int len = Array.getLength(anArray); for (int i = 0; i < len; i++) { if (equals(Array.get(anArray, i), anElement)) return true; } } return false; }
From source file:org.crank.javax.faces.component.MenuRenderer.java
boolean isSelected(Object itemValue, Object valueArray) { if (null != valueArray) { if (!valueArray.getClass().isArray()) { logger.warning("valueArray is not an array, the actual type is " + valueArray.getClass()); return valueArray.equals(itemValue); }/*from w w w .jav a 2 s . co m*/ int len = Array.getLength(valueArray); for (int i = 0; i < len; i++) { Object value = Array.get(valueArray, i); if (value == null) { if (itemValue == null) { return true; } } else if (value.equals(itemValue)) { return true; } } } return false; }