dmitri 2003/06/16 18:39:51 Modified: jxpath/src/java/org/apache/commons/jxpath/util ValueUtils.java jxpath/src/test/org/apache/commons/jxpath/ri/model/beans BeanModelTest.java jxpath/src/java/org/apache/commons/jxpath/ri/model/beans BeanPropertyPointer.java Added: jxpath/src/test/org/apache/commons/jxpath/ri/model/beans TestIndexedPropertyBean.java Log: Added support for indexed properties that don't have a method that returns the whole collection. Revision Changes Path 1.16 +54 -6 jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/util/ValueUtils.java Index: ValueUtils.java =================================================================== RCS file: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/util/ValueUtils.java,v retrieving revision 1.15 retrieving revision 1.16 diff -u -r1.15 -r1.16 --- ValueUtils.java 11 Mar 2003 00:59:34 -0000 1.15 +++ ValueUtils.java 17 Jun 2003 01:39:51 -0000 1.16 @@ -64,6 +64,7 @@ import java.beans.IndexedPropertyDescriptor; import java.beans.PropertyDescriptor; import java.lang.reflect.Array; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.ArrayList; @@ -86,6 +87,7 @@ */ public class ValueUtils { private static Map dynamicPropertyHandlerMap = new HashMap(); + private static final int UNKNOWN_LENGTH_MAX_COUNT = 16000; /** * Returns true if the object is an array or a Collection @@ -131,6 +133,42 @@ return 0; } + + /** + * If there is a regular non-indexed read method for this property, + * uses this method to obtain the collection and then returns its + * length. + * 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. + */ + 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()); + } /** * Returns the length of the supplied collection. If the supplied object @@ -422,8 +460,19 @@ bean, new Object[] { new Integer(index)}); } + } + catch (InvocationTargetException ex) { + Throwable t = + ((InvocationTargetException) ex).getTargetException(); + if (t instanceof ArrayIndexOutOfBoundsException) { + return null; + } + + throw new JXPathException( + "Cannot access property: " + propertyDescriptor.getName(), + t); } - catch (Exception ex) { + catch (Throwable ex) { throw new JXPathException( "Cannot access property: " + propertyDescriptor.getName(), ex); @@ -461,7 +510,6 @@ } } catch (Exception ex) { - ex.printStackTrace(); throw new RuntimeException( "Cannot access property: " + propertyDescriptor.getName() 1.5 +17 -4 jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/beans/BeanModelTest.java Index: BeanModelTest.java =================================================================== RCS file: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/beans/BeanModelTest.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- BeanModelTest.java 11 Mar 2003 00:59:38 -0000 1.4 +++ BeanModelTest.java 17 Jun 2003 01:39:51 -0000 1.5 @@ -65,6 +65,7 @@ import junit.framework.TestSuite; import org.apache.commons.jxpath.AbstractFactory; +import org.apache.commons.jxpath.JXPathContext; import org.apache.commons.jxpath.TestBean; import org.apache.commons.jxpath.ri.model.BeanModelTestCase; @@ -99,5 +100,17 @@ protected AbstractFactory getAbstractFactory() { return new TestBeanFactory(); } + + public void testIndexedProperty() { + JXPathContext context = + JXPathContext.newContext(null, new TestIndexedPropertyBean()); + + assertXPathValueAndPointer( + context, + "indexed[1]", + new Integer(0), + "/indexed[1]"); + } + } 1.1 jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/beans/TestIndexedPropertyBean.java Index: TestIndexedPropertyBean.java =================================================================== /* * $Header: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/model/beans/TestIndexedPropertyBean.java,v 1.1 2003/06/17 01:39:51 dmitri Exp $ * $Revision: 1.1 $ * $Date: 2003/06/17 01:39:51 $ * * ==================================================================== * The Apache Software License, Version 1.1 * * * Copyright (c) 1999-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Commons", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 2001, Plotnix, Inc, * <http://www.plotnix.com/>. * For more information on the Apache Software Foundation, please see * <http://www.apache.org/>. */ package org.apache.commons.jxpath.ri.model.beans; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.commons.jxpath.util.ValueUtils; /** * Test bean for JUnit tests of indexed property handling. * * @author Dmitri Plotnikov * @version $Revision: 1.1 $ $Date: 2003/06/17 01:39:51 $ */ public class TestIndexedPropertyBean { public int getIndexed(int i) { if (i >= 2) { throw new ArrayIndexOutOfBoundsException(i); } return i; } } 1.14 +25 -10 jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/model/beans/BeanPropertyPointer.java Index: BeanPropertyPointer.java =================================================================== RCS file: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/model/beans/BeanPropertyPointer.java,v retrieving revision 1.13 retrieving revision 1.14 diff -u -r1.13 -r1.14 --- BeanPropertyPointer.java 11 Mar 2003 00:59:24 -0000 1.13 +++ BeanPropertyPointer.java 17 Jun 2003 01:39:51 -0000 1.14 @@ -61,6 +61,7 @@ */ package org.apache.commons.jxpath.ri.model.beans; +import java.beans.IndexedPropertyDescriptor; import java.beans.PropertyDescriptor; import org.apache.commons.jxpath.JXPathBeanInfo; @@ -84,6 +85,8 @@ private static final Object UNINITIALIZED = new Object(); private Object baseValue = UNINITIALIZED; private Object value = UNINITIALIZED; + + private static final int UNKNOWN_LENGTH_MAX_COUNT = 10000; public BeanPropertyPointer(NodePointer parent, JXPathBeanInfo beanInfo) { super(parent); @@ -174,15 +177,17 @@ */ public Object getImmediateNode() { if (value == UNINITIALIZED) { - Object baseValue = getBaseValue(); if (index == WHOLE_COLLECTION) { - value = baseValue; - } - else if (value != null && index >= 0 && index < getLength()) { - value = ValueUtils.getValue(baseValue, index); + value = getBaseValue(); } else { - value = null; + PropertyDescriptor pd = getPropertyDescriptor(); + if (pd == null) { + value = null; + } + else { + value = ValueUtils.getValue(getBean(), pd, index); + } } } return value; @@ -198,6 +203,10 @@ return false; } + if (pd instanceof IndexedPropertyDescriptor) { + return true; + } + int hint = ValueUtils.getCollectionHint(pd.getPropertyType()); if (hint == -1) { return false; @@ -218,6 +227,12 @@ PropertyDescriptor pd = getPropertyDescriptor(); if (pd == null) { return 1; + } + + if (pd instanceof IndexedPropertyDescriptor) { + return ValueUtils.getIndexedPropertyLength( + getBean(), + (IndexedPropertyDescriptor) pd); } int hint = ValueUtils.getCollectionHint(pd.getPropertyType());
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]