Return-Path: Delivered-To: apmail-incubator-bval-commits-archive@minotaur.apache.org Received: (qmail 30839 invoked from network); 9 Sep 2010 23:23:21 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 9 Sep 2010 23:23:21 -0000 Received: (qmail 45875 invoked by uid 500); 9 Sep 2010 23:23:21 -0000 Delivered-To: apmail-incubator-bval-commits-archive@incubator.apache.org Received: (qmail 45843 invoked by uid 500); 9 Sep 2010 23:23:21 -0000 Mailing-List: contact bval-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: bval-dev@incubator.apache.org Delivered-To: mailing list bval-commits@incubator.apache.org Received: (qmail 45836 invoked by uid 99); 9 Sep 2010 23:23:20 -0000 Received: from Unknown (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 09 Sep 2010 23:23:20 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 09 Sep 2010 23:23:02 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 252FD23888DD; Thu, 9 Sep 2010 23:22:41 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r995609 - /incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/util/PropertyAccess.java Date: Thu, 09 Sep 2010 23:22:41 -0000 To: bval-commits@incubator.apache.org From: mbenson@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100909232241.252FD23888DD@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: mbenson Date: Thu Sep 9 23:22:40 2010 New Revision: 995609 URL: http://svn.apache.org/viewvc?rev=995609&view=rev Log: missed format merge Modified: incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/util/PropertyAccess.java Modified: incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/util/PropertyAccess.java URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/util/PropertyAccess.java?rev=995609&r1=995608&r2=995609&view=diff ============================================================================== --- incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/util/PropertyAccess.java (original) +++ incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/util/PropertyAccess.java Thu Sep 9 23:22:40 2010 @@ -26,171 +26,172 @@ import java.lang.reflect.Type; import java.util.Map; /** - * Description: Undefined dynamic strategy (FIELD or METHOD access) - * Uses PropertyUtils or tries to determine - * field to access the value
+ * Description: Undefined dynamic strategy (FIELD or METHOD access) Uses + * PropertyUtils or tries to determine field to access the value
*/ public class PropertyAccess extends AccessStrategy { - private final Class beanClass; - private final String propertyName; - private Field rememberField; - - /** - * Create a new PropertyAccess instance. - * @param clazz - * @param propertyName - */ - public PropertyAccess(Class clazz, String propertyName) { - this.beanClass = clazz; - this.propertyName = propertyName; - } - - /** - * {@inheritDoc} - */ - public ElementType getElementType() { - return (rememberField != null) ? ElementType.FIELD : ElementType.METHOD; - } - - private static Object getPublicProperty(Object bean, String property) throws - InvocationTargetException, NoSuchMethodException, IllegalAccessException { - if (bean instanceof Map) { - return ((Map) bean).get(property); - } else { // supports DynaBean and standard Objects - return PropertyUtils.getSimpleProperty(bean, property); - } - } - - /** - * Get a named property from bean. - * @param bean - * @param propertyName - * @return Object found - * @throws InvocationTargetException - * @throws NoSuchMethodException - * @throws IllegalAccessException - */ - public static Object getProperty(Object bean, String propertyName) throws - InvocationTargetException, NoSuchMethodException, IllegalAccessException { - return new PropertyAccess(bean.getClass(), propertyName).get(bean); - } - - /** - * {@inheritDoc} - */ - public String toString() { - return "Property{" + beanClass.getName() + '.' + propertyName + '}'; - } - - /** - * {@inheritDoc} - */ - public Type getJavaType() { - /*if(Map.class.isAssignableFrom(beanClass)) { - return beanClass. - }*/ - if (rememberField != null) { // use cached field of previous access - return rememberField.getGenericType(); - } - for (PropertyDescriptor each : PropertyUtils.getPropertyDescriptors(beanClass)) { - if (each.getName().equals(propertyName) && each.getReadMethod() != null) { - return each.getReadMethod().getGenericReturnType(); - } - } - try { // try public field - return beanClass.getField(propertyName).getGenericType(); - } catch (NoSuchFieldException ex2) { - // search for private/protected field up the hierarchy - Class theClass = beanClass; - while (theClass != null) { - try { - return theClass.getDeclaredField(propertyName).getGenericType(); - } catch (NoSuchFieldException ex3) { - // do nothing - } - theClass = theClass.getSuperclass(); - } - } - return Object.class; // unknown type: allow any type?? - } - - /** - * {@inheritDoc} - */ - public String getPropertyName() { - return propertyName; - } - - /** - * {@inheritDoc} - */ - public Object get(Object bean) { - try { - if (rememberField != null) { // cache field of previous access - return rememberField.get(bean); - } - - try { // try public method - return getPublicProperty(bean, propertyName); - } catch (NoSuchMethodException ex) { - return getFieldValue(bean); - } - } catch (IllegalArgumentException e) { - throw e; - } catch (Exception e) { - throw new IllegalArgumentException("cannot access " + propertyName, e); - } - } - - private Object getFieldValue(Object bean) throws IllegalAccessException { - Object value; - try { // try public field - Field aField = bean.getClass().getField(propertyName); - value = aField.get(bean); - rememberField = aField; - return value; - } catch (NoSuchFieldException ex2) { - // search for private/protected field up the hierarchy - Class theClass = bean.getClass(); - while (theClass != null) { + private final Class beanClass; + private final String propertyName; + private Field rememberField; + + /** + * Create a new PropertyAccess instance. + * + * @param clazz + * @param propertyName + */ + public PropertyAccess(Class clazz, String propertyName) { + this.beanClass = clazz; + this.propertyName = propertyName; + } + + /** + * {@inheritDoc} + */ + public ElementType getElementType() { + return (rememberField != null) ? ElementType.FIELD : ElementType.METHOD; + } + + private static Object getPublicProperty(Object bean, String property) throws InvocationTargetException, + NoSuchMethodException, IllegalAccessException { + if (bean instanceof Map) { + return ((Map) bean).get(property); + } else { // supports DynaBean and standard Objects + return PropertyUtils.getSimpleProperty(bean, property); + } + } + + /** + * Get a named property from bean. + * + * @param bean + * @param propertyName + * @return Object found + * @throws InvocationTargetException + * @throws NoSuchMethodException + * @throws IllegalAccessException + */ + public static Object getProperty(Object bean, String propertyName) throws InvocationTargetException, + NoSuchMethodException, IllegalAccessException { + return new PropertyAccess(bean.getClass(), propertyName).get(bean); + } + + /** + * {@inheritDoc} + */ + public String toString() { + return "Property{" + beanClass.getName() + '.' + propertyName + '}'; + } + + /** + * {@inheritDoc} + */ + public Type getJavaType() { + /* + * if(Map.class.isAssignableFrom(beanClass)) { return beanClass. } + */ + if (rememberField != null) { // use cached field of previous access + return rememberField.getGenericType(); + } + for (PropertyDescriptor each : PropertyUtils.getPropertyDescriptors(beanClass)) { + if (each.getName().equals(propertyName) && each.getReadMethod() != null) { + return each.getReadMethod().getGenericReturnType(); + } + } + try { // try public field + return beanClass.getField(propertyName).getGenericType(); + } catch (NoSuchFieldException ex2) { + // search for private/protected field up the hierarchy + Class theClass = beanClass; + while (theClass != null) { + try { + return theClass.getDeclaredField(propertyName).getGenericType(); + } catch (NoSuchFieldException ex3) { + // do nothing + } + theClass = theClass.getSuperclass(); + } + } + return Object.class; // unknown type: allow any type?? + } + + /** + * {@inheritDoc} + */ + public String getPropertyName() { + return propertyName; + } + + /** + * {@inheritDoc} + */ + public Object get(Object bean) { try { - Field aField = theClass - .getDeclaredField(propertyName); - if (!aField.isAccessible()) { - aField.setAccessible(true); - } - value = aField.get(bean); - rememberField = aField; - return value; - } catch (NoSuchFieldException ex3) { - // do nothing - } - theClass = theClass.getSuperclass(); - } - throw new IllegalArgumentException( - "cannot access field " + propertyName); - } - } - - /** - * {@inheritDoc} - */ - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - PropertyAccess that = (PropertyAccess) o; - - return beanClass.equals(that.beanClass) && propertyName.equals(that.propertyName); - } - - /** - * {@inheritDoc} - */ - public int hashCode() { - int result; - result = beanClass.hashCode(); - result = 31 * result + propertyName.hashCode(); - return result; - } + if (rememberField != null) { // cache field of previous access + return rememberField.get(bean); + } + + try { // try public method + return getPublicProperty(bean, propertyName); + } catch (NoSuchMethodException ex) { + return getFieldValue(bean); + } + } catch (IllegalArgumentException e) { + throw e; + } catch (Exception e) { + throw new IllegalArgumentException("cannot access " + propertyName, e); + } + } + + private Object getFieldValue(Object bean) throws IllegalAccessException { + Object value; + try { // try public field + Field aField = bean.getClass().getField(propertyName); + value = aField.get(bean); + rememberField = aField; + return value; + } catch (NoSuchFieldException ex2) { + // search for private/protected field up the hierarchy + Class theClass = bean.getClass(); + while (theClass != null) { + try { + Field aField = theClass.getDeclaredField(propertyName); + if (!aField.isAccessible()) { + aField.setAccessible(true); + } + value = aField.get(bean); + rememberField = aField; + return value; + } catch (NoSuchFieldException ex3) { + // do nothing + } + theClass = theClass.getSuperclass(); + } + throw new IllegalArgumentException("cannot access field " + propertyName); + } + } + + /** + * {@inheritDoc} + */ + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + + PropertyAccess that = (PropertyAccess) o; + + return beanClass.equals(that.beanClass) && propertyName.equals(that.propertyName); + } + + /** + * {@inheritDoc} + */ + public int hashCode() { + int result; + result = beanClass.hashCode(); + result = 31 * result + propertyName.hashCode(); + return result; + } }