List of usage examples for java.beans IndexedPropertyDescriptor getIndexedWriteMethod
public synchronized Method getIndexedWriteMethod()
From source file:es.caib.zkib.jxpath.util.ValueUtils.java
/** * Modifies the index'th element of the bean's property represented by * the supplied property descriptor. Converts the value to the required * type if necessary./*from www .java 2 s. c om*/ * @param bean to edit * @param propertyDescriptor indicating what to set * @param index int * @param value to set */ public static void setValue(Object bean, PropertyDescriptor propertyDescriptor, int index, Object value) { if (propertyDescriptor instanceof IndexedPropertyDescriptor) { try { IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) propertyDescriptor; Method method = ipd.getIndexedWriteMethod(); if (method != null) { method.invoke(bean, new Object[] { new Integer(index), convert(value, ipd.getIndexedPropertyType()) }); return; } } catch (Exception ex) { throw new RuntimeException( "Cannot access property: " + propertyDescriptor.getName() + ", " + ex.getMessage()); } } // We will fall through if there is no indexed read Object collection = getValue(bean, propertyDescriptor); if (isCollection(collection)) { setValue(collection, index, value); } else if (index == 0) { setValue(bean, propertyDescriptor, value); } else { throw new RuntimeException("Not a collection: " + propertyDescriptor.getName()); } }
From source file:com.sun.faces.el.impl.BeanInfoManager.java
/** * Initializes by mapping property names to BeanInfoProperties *///from w ww.j a va 2s.com void initialize() throws ElException { try { mBeanInfo = Introspector.getBeanInfo(mBeanClass); mPropertyByName = new HashMap(); mIndexedPropertyByName = new HashMap(); PropertyDescriptor[] pds = mBeanInfo.getPropertyDescriptors(); for (int i = 0; pds != null && i < pds.length; i++) { // Treat as both an indexed property and a normal property PropertyDescriptor pd = pds[i]; if (pd instanceof IndexedPropertyDescriptor) { IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd; Method readMethod = getPublicMethod(ipd.getIndexedReadMethod()); Method writeMethod = getPublicMethod(ipd.getIndexedWriteMethod()); BeanInfoIndexedProperty property = new BeanInfoIndexedProperty(readMethod, writeMethod, ipd); mIndexedPropertyByName.put(ipd.getName(), property); } Method readMethod = getPublicMethod(pd.getReadMethod()); Method writeMethod = getPublicMethod(pd.getWriteMethod()); BeanInfoProperty property = new BeanInfoProperty(readMethod, writeMethod, pd); mPropertyByName.put(pd.getName(), property); } mEventSetByName = new HashMap(); EventSetDescriptor[] esds = mBeanInfo.getEventSetDescriptors(); for (int i = 0; esds != null && i < esds.length; i++) { EventSetDescriptor esd = esds[i]; mEventSetByName.put(esd.getName(), esd); } } catch (IntrospectionException exc) { if (log.isWarnEnabled()) { log.warn(MessageUtil.getMessageWithArgs(Constants.EXCEPTION_GETTING_BEANINFO, mBeanClass.getName()), exc); } } }
From source file:eu.qualityontime.commons.DefaultBeanIntrospector.java
/** * This method fixes an issue where IndexedPropertyDescriptor behaves * differently in different versions of the JDK for 'indexed' properties * which use java.util.List (rather than an array). It implements a * workaround for Bug 28358. If you have a Bean with the following * getters/setters for an indexed property: * * <pre>/*w w w. j a v a 2 s . c om*/ * public List getFoo() * public Object getFoo(int index) * public void setFoo(List foo) * public void setFoo(int index, Object foo) * </pre> * * then the IndexedPropertyDescriptor's getReadMethod() and getWriteMethod() * behave as follows: * <ul> * <li>JDK 1.3.1_04: returns valid Method objects from these methods.</li> * <li>JDK 1.4.2_05: returns null from these methods.</li> * </ul> * * @param beanClass * the current class to be inspected * @param descriptors * the array with property descriptors */ private void handleIndexedPropertyDescriptors(final Class<?> beanClass, final PropertyDescriptor[] descriptors) { for (final PropertyDescriptor pd : descriptors) { if (pd instanceof IndexedPropertyDescriptor) { final IndexedPropertyDescriptor descriptor = (IndexedPropertyDescriptor) pd; final String propName = descriptor.getName().substring(0, 1).toUpperCase() + descriptor.getName().substring(1); if (descriptor.getReadMethod() == null) { final String methodName = descriptor.getIndexedReadMethod() != null ? descriptor.getIndexedReadMethod().getName() : "get" + propName; final Method readMethod = MethodUtils.getMatchingAccessibleMethod(beanClass, methodName, EMPTY_CLASS_PARAMETERS); if (readMethod != null) { try { descriptor.setReadMethod(readMethod); } catch (final Exception e) { log.error("Error setting indexed property read method", e); } } } if (descriptor.getWriteMethod() == null) { final String methodName = descriptor.getIndexedWriteMethod() != null ? descriptor.getIndexedWriteMethod().getName() : "set" + propName; Method writeMethod = MethodUtils.getMatchingAccessibleMethod(beanClass, methodName, LIST_CLASS_PARAMETER); if (writeMethod == null) { for (final Method m : beanClass.getMethods()) { if (m.getName().equals(methodName)) { final Class<?>[] parameterTypes = m.getParameterTypes(); if (parameterTypes.length == 1 && List.class.isAssignableFrom(parameterTypes[0])) { writeMethod = m; break; } } } } if (writeMethod != null) { try { descriptor.setWriteMethod(writeMethod); } catch (final Exception e) { log.error("Error setting indexed property write method", e); } } } } } }
From source file:org.apache.axis.utils.BeanPropertyDescriptor.java
/** * Set an indexed property value//w w w . j a v a 2s . c o m * @param obj is the object * @param i the index * @param newValue is the new value */ public void set(Object obj, int i, Object newValue) throws InvocationTargetException, IllegalAccessException { // Set the new value if (isIndexed()) { IndexedPropertyDescriptor id = (IndexedPropertyDescriptor) myPD; growArrayToSize(obj, id.getIndexedPropertyType(), i); id.getIndexedWriteMethod().invoke(obj, new Object[] { new Integer(i), newValue }); } else { // Not calling 'growArrayToSize' to avoid an extra call to the // property's setter. The setter will be called at the end anyway. // growArrayToSize(obj, myPD.getPropertyType().getComponentType(), i); Object array = get(obj); if (array == null || Array.getLength(array) <= i) { Class componentType = getType().getComponentType(); Object newArray = Array.newInstance(componentType, i + 1); // Copy over the old elements if (array != null) { System.arraycopy(array, 0, newArray, 0, Array.getLength(array)); } array = newArray; } Array.set(array, i, newValue); // Fix for non-indempondent array-type propertirs. // Make sure we call the property's setter. set(obj, array); } }
From source file:org.evergreen.web.utils.beanutils.PropertyUtilsBean.java
/** * <p>Retrieve the property descriptors for the specified class, * introspecting and caching them the first time a particular bean class * is encountered.</p>//from w w w .j ava2 s .c om * * <p><strong>FIXME</strong> - Does not work with DynaBeans.</p> * * @param beanClass Bean class for which property descriptors are requested * @return the property descriptors * * @exception IllegalArgumentException if <code>beanClass</code> is null */ public PropertyDescriptor[] getPropertyDescriptors(Class beanClass) { if (beanClass == null) { throw new IllegalArgumentException("No bean class specified"); } // Look up any cached descriptors for this bean class PropertyDescriptor[] descriptors = null; descriptors = (PropertyDescriptor[]) descriptorsCache.get(beanClass); if (descriptors != null) { return (descriptors); } // Introspect the bean and cache the generated descriptors BeanInfo beanInfo = null; try { beanInfo = Introspector.getBeanInfo(beanClass); } catch (IntrospectionException e) { return (new PropertyDescriptor[0]); } descriptors = beanInfo.getPropertyDescriptors(); if (descriptors == null) { descriptors = new PropertyDescriptor[0]; } // ----------------- Workaround for Bug 28358 --------- START ------------------ // // The following code fixes an issue where IndexedPropertyDescriptor behaves // Differently in different versions of the JDK for 'indexed' properties which // use java.util.List (rather than an array). // // If you have a Bean with the following getters/setters for an indexed property: // // public List getFoo() // public Object getFoo(int index) // public void setFoo(List foo) // public void setFoo(int index, Object foo) // // then the IndexedPropertyDescriptor's getReadMethod() and getWriteMethod() // behave as follows: // // JDK 1.3.1_04: returns valid Method objects from these methods. // JDK 1.4.2_05: returns null from these methods. // for (int i = 0; i < descriptors.length; i++) { if (descriptors[i] instanceof IndexedPropertyDescriptor) { IndexedPropertyDescriptor descriptor = (IndexedPropertyDescriptor) descriptors[i]; String propName = descriptor.getName().substring(0, 1).toUpperCase() + descriptor.getName().substring(1); if (descriptor.getReadMethod() == null) { String methodName = descriptor.getIndexedReadMethod() != null ? descriptor.getIndexedReadMethod().getName() : "get" + propName; Method readMethod = MethodUtils.getMatchingAccessibleMethod(beanClass, methodName, EMPTY_CLASS_PARAMETERS); if (readMethod != null) { try { descriptor.setReadMethod(readMethod); } catch (Exception e) { log.error("Error setting indexed property read method", e); } } } if (descriptor.getWriteMethod() == null) { String methodName = descriptor.getIndexedWriteMethod() != null ? descriptor.getIndexedWriteMethod().getName() : "set" + propName; Method writeMethod = MethodUtils.getMatchingAccessibleMethod(beanClass, methodName, LIST_CLASS_PARAMETER); if (writeMethod == null) { Method[] methods = beanClass.getMethods(); for (int j = 0; j < methods.length; j++) { if (methods[j].getName().equals(methodName)) { Class[] parameterTypes = methods[j].getParameterTypes(); if (parameterTypes.length == 1 && List.class.isAssignableFrom(parameterTypes[0])) { writeMethod = methods[j]; break; } } } } if (writeMethod != null) { try { descriptor.setWriteMethod(writeMethod); } catch (Exception e) { log.error("Error setting indexed property write method", e); } } } } } // ----------------- Workaround for Bug 28358 ---------- END ------------------- descriptorsCache.put(beanClass, descriptors); return (descriptors); }
From source file:org.mypsycho.beans.PropertyUtilsBean.java
/** * <p>//w ww . j av a 2s . co m * Retrieve the property descriptors for the specified class, introspecting * and caching them the first time a particular bean class is encountered. * </p> * * @param beanClass Bean class for which property descriptors are requested * @return the property descriptors * @exception IllegalArgumentException if <code>beanClass</code> is null */ public PropertyDescriptor[] createDescriptorsCache(Class<?> beanClass) throws IntrospectionException { // Introspect the bean and cache the generated descriptors BeanInfo beanInfo = Introspector.getBeanInfo(beanClass); PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors(); if (descriptors == null) { return new PropertyDescriptor[0]; } // ----------------- Workaround for Bug 28358 --------- START ------------------ // // The following code fixes an issue where IndexedPropertyDescriptor // behaves // Differently in different versions of the JDK for 'indexed' properties // which use java.util.List (rather than an array). // // If you have a Bean with the following getters/setters for an indexed // property: // // public List getFoo() // public Object getFoo(int index) // public void setFoo(List foo) // public void setFoo(int index, Object foo) // // then the IndexedPropertyDescriptor's getReadMethod() and // getWriteMethod() // behave as follows: // // JDK 1.3.1_04: returns valid Method objects from these methods. // JDK 1.4.2_05: returns null from these methods. // for (PropertyDescriptor descriptor2 : descriptors) { if (!(descriptor2 instanceof IndexedPropertyDescriptor)) { continue; } IndexedPropertyDescriptor descriptor = (IndexedPropertyDescriptor) descriptor2; String propName = descriptor.getName().substring(0, 1).toUpperCase() + descriptor.getName().substring(1); if (descriptor.getReadMethod() == null) { String methodName = (descriptor.getIndexedReadMethod() != null) ? descriptor.getIndexedReadMethod().getName() : "get" + propName; Method readMethod = MethodUtils.getMatchingAccessibleMethod(beanClass, methodName, EMPTY_CLASS_PARAMETERS); if (readMethod != null) { try { descriptor.setReadMethod(readMethod); } catch (Exception e) { notify("copy", "Fail to set indexed property" + propName, e); } } } if (descriptor.getWriteMethod() == null) { Method indexedMethod = descriptor.getIndexedWriteMethod(); String methodName = indexedMethod != null ? indexedMethod.getName() : "set" + propName; Method writeMethod = MethodUtils.getMatchingAccessibleMethod(beanClass, methodName, LIST_CLASS_PARAMETER); if (writeMethod == null) { Method[] methods = beanClass.getMethods(); for (Method method : methods) { if (method.getName().equals(methodName)) { Class<?>[] parameterTypes = method.getParameterTypes(); if (parameterTypes.length == 1 && List.class.isAssignableFrom(parameterTypes[0])) { writeMethod = method; break; } } } } if (writeMethod != null) { try { descriptor.setWriteMethod(writeMethod); } catch (Exception e) { notify("copy", "Fail to set indexed property" + propName, e); } } } } // ----------------- Workaround for Bug 28358 ---------- END ------------------- return descriptors; }
From source file:org.pentaho.reporting.engine.classic.core.util.beans.BeanUtility.java
private void setProperty(final PropertySpecification name, final Object o) throws BeanException { final PropertyDescriptor pd = properties.get(name.getName()); if (pd == null) { throw new BeanException("No such property:" + name); }/*from w w w . j a v a2 s .co m*/ if (pd instanceof IndexedPropertyDescriptor && name.getIndex() != null) { final IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd; final Method writeMethod = ipd.getIndexedWriteMethod(); if (writeMethod != null) { try { writeMethod.invoke(bean, new Integer(name.getIndex()), o); } catch (Exception e) { throw BeanException.getInstance("InvokationError", e); } // we've done the job ... return; } } final Method writeMethod = pd.getWriteMethod(); if (writeMethod == null) { throw BeanException.getInstance("Property is not writeable: " + name, null); } if (name.getIndex() != null) { // this is a indexed access, but no indexWrite method was found ... updateArrayProperty(pd, name, o); } else { try { writeMethod.invoke(bean, o); } catch (Exception e) { throw BeanException.getInstance( "InvokationError on property '" + name + "' on bean type " + bean.getClass(), e); } } }