List of usage examples for java.beans IndexedPropertyDescriptor getIndexedReadMethod
public synchronized Method getIndexedReadMethod()
From source file:es.caib.zkib.jxpath.util.ValueUtils.java
/** * Returns the index'th element of the bean's property represented by * the supplied property descriptor./*from w ww. j ava 2s. c om*/ * @param bean to read * @param propertyDescriptor indicating what to read * @param index int * @return Object */ public static Object getValue(Object bean, PropertyDescriptor propertyDescriptor, int index) { if (propertyDescriptor instanceof IndexedPropertyDescriptor) { try { IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) propertyDescriptor; Method method = ipd.getIndexedReadMethod(); if (method != null) { return method.invoke(bean, new Object[] { new Integer(index) }); } } catch (InvocationTargetException ex) { Throwable t = ex.getTargetException(); if (t instanceof IndexOutOfBoundsException) { return null; } throw new JXPathException("Cannot access property: " + propertyDescriptor.getName(), t); } catch (Throwable ex) { throw new JXPathException("Cannot access property: " + propertyDescriptor.getName(), ex); } } // We will fall through if there is no indexed read return getValue(getValue(bean, propertyDescriptor), index); }
From source file:es.caib.zkib.jxpath.util.ValueUtils.java
/** * If there is a regular non-indexed read method for this property, * uses this method to obtain the collection and then returns its * length./* www . j a v a 2 s . co m*/ * Otherwise, attempts to guess the length of the collection by * calling the indexed get method repeatedly. The method is supposed * to throw an exception if the index is out of bounds. * @param object collection * @param pd IndexedPropertyDescriptor * @return int */ public static int getIndexedPropertyLength(Object object, IndexedPropertyDescriptor pd) { if (pd.getReadMethod() != null) { return getLength(getValue(object, pd)); } Method readMethod = pd.getIndexedReadMethod(); if (readMethod == null) { throw new JXPathException("No indexed read method for property " + pd.getName()); } for (int i = 0; i < UNKNOWN_LENGTH_MAX_COUNT; i++) { try { readMethod.invoke(object, new Object[] { new Integer(i) }); } catch (Throwable t) { return i; } } throw new JXPathException("Cannot determine the length of the indexed property " + pd.getName()); }
From source file:com.sun.faces.el.impl.BeanInfoManager.java
/** * Initializes by mapping property names to BeanInfoProperties *///from w ww .j a va 2 s . co m 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>//from w w w. ja va 2s . 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
/** * Get an indexed property//from ww w . ja va2s . c om * @param obj is the object * @param i the index * @return the object at the indicated index */ public Object get(Object obj, int i) throws InvocationTargetException, IllegalAccessException { if (!isIndexed()) { return Array.get(get(obj), i); } else { IndexedPropertyDescriptor id = (IndexedPropertyDescriptor) myPD; return id.getIndexedReadMethod().invoke(obj, new Object[] { new Integer(i) }); } }
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 a v a 2 s . com * * <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 w w .j a v a2 s .c o 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 Object getPropertyForSpecification(final PropertySpecification name) 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 a 2 s . co m*/ if (pd instanceof IndexedPropertyDescriptor && name.getIndex() != null) { final IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd; final Method readMethod = ipd.getIndexedReadMethod(); if (readMethod == null) { throw new BeanException("Property is not readable: " + name); } try { return readMethod.invoke(bean, new Integer(name.getIndex())); } catch (Exception e) { throw BeanException.getInstance("InvokationError", e); } } else { final Method readMethod = pd.getReadMethod(); if (readMethod == null) { throw BeanException.getInstance("Property is not readable: " + name, null); } if (name.getIndex() != null) { // handle access to array-only properties .. try { // System.out.println(readMethod); final Object value = readMethod.invoke(bean); // we have (possibly) an array. if (value == null) { // noinspection ThrowCaughtLocally throw new IndexOutOfBoundsException("No such index, property is null"); } if (value.getClass().isArray() == false) { // noinspection ThrowCaughtLocally throw new BeanException("The property contains no array."); } final int index = Integer.parseInt(name.getIndex()); return Array.get(value, index); } catch (BeanException be) { throw be; } catch (IndexOutOfBoundsException iob) { throw iob; } catch (Exception e) { throw new BeanException("Failed to read indexed property."); } } try { return readMethod.invoke(bean); } catch (Exception e) { throw BeanException.getInstance("InvokationError", e); } } }