Commits

Anonymous committed 772ef45

Issue number: XW-322
Obtained from:
Submitted by:
Reviewed by:
CVS: ----------------------------------------------------------------------
CVS: Issue number:
CVS: If this change addresses one or more issues,
CVS: then enter the issue number(s) here.
CVS: Obtained from:
CVS: If this change has been taken from another system,
CVS: then name the system in this line, otherwise delete it.
CVS: Submitted by:
CVS: If this code has been contributed to the project by someone else; i.e.,
CVS: they sent us a patch or a set of diffs, then include their name/email
CVS: address here. If this is your work then delete this line.
CVS: Reviewed by:
CVS: If we are doing pre-commit code reviews and someone else has
CVS: reviewed your changes, include their name(s) here.
CVS: If you have not had it reviewed then delete this line.

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@763e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (5)

src/java/com/opensymphony/xwork/util/OgnlContextState.java

 
     public static final String CURRENT_PROPERTY_PATH="current.property.path";
     public static final String FULL_PROPERTY_PATH="current.property.path";
+    private static final String GETTING_BY_KEY_PROPERTY="xwork.getting.by.key.property";
 
     private static final String SET_MAP_KEY="set.map.key";
 
 		setBooleanValue(InstantiatingNullHandler.CREATE_NULL_OBJECTS, context, creatingNullObjects);
 	}
 
+	public static boolean isGettingByKeyProperty(Map context) {
+		return getBooleanProperty(GETTING_BY_KEY_PROPERTY, context);
+	}
+
+	public static void setGettingByKeyProperty(Map context, boolean gettingByKeyProperty) {
+		setBooleanValue(GETTING_BY_KEY_PROPERTY, context, gettingByKeyProperty);
+	}	
+	
 	public static boolean isReportingConversionErrors(Map context) {
 		return getBooleanProperty(XWorkConverter.REPORT_CONVERSION_ERRORS, context);
 	}

src/java/com/opensymphony/xwork/util/XWorkCollectionPropertyAccessor.java

     private static final Log LOG = LogFactory.getLog(XWorkCollectionPropertyAccessor.class);
     private static final String CONTEXT_COLLECTION_MAP = "xworkCollectionPropertyAccessorContextSetMap";
 
-    private static final String KEY_PROPERTY_FOR_CREATION = "makeNew";
+    public static final String KEY_PROPERTY_FOR_CREATION = "makeNew";
 
     //use a basic object Ognl property accessor here
     //to access properties of the objects in the Set
         //check if it is a generic type property.
         //if so, return the value from the
         //superclass which will determine this.
-        if (key instanceof String &&
-                (key.equals("isEmpty")
-                        || key.equals("size")
-                        || key.equals("iterator"))) {
+        if (!OgnlContextState.isGettingByKeyProperty(context)
+                && !key.equals(KEY_PROPERTY_FOR_CREATION)) {
             return super.getProperty(context, target, key);
+        }	else {
+            //reset context property
+            OgnlContextState.setGettingByKeyProperty(context,false);
         }
         Collection c = (Collection) target;
 

src/java/com/opensymphony/xwork/util/XWorkListPropertyAccessor.java

     public Object getProperty(Map context, Object target, Object name)
             throws OgnlException {
 
-        if (name instanceof String) {
-            if (name.equals("isEmpty")
-                        || name.equals("size")
-                        || name.equals("iterator")) {
-                return super.getProperty(context, target, name);
-            }
+        if (OgnlContextState.isGettingByKeyProperty(context)
+                || name.equals(XWorkCollectionPropertyAccessor.KEY_PROPERTY_FOR_CREATION)) {
             return _sAcc.getProperty(context, target, name);
+        }	else if (name instanceof String) {
+            return super.getProperty(context, target, name);
         }
         OgnlContextState.updateCurrentPropertyPath(context, name);
         //System.out.println("Entering XWorkListPropertyAccessor. Name: " + name);

src/java/com/opensymphony/xwork/util/XWorkMethodAccessor.java

                   	    //so that property strings are not cleared
                   	    //i.e. OgnlUtil should be used initially, OgnlRuntime
                   	    //thereafter
-
+                  	    
                   	    Object propVal=OgnlRuntime.getProperty(ogContext, object, string);
                   	    //use the Collection property accessor instead of the individual property accessor, because 
                   	    //in the case of Lists otherwise the index property could be used
                   	    PropertyAccessor accessor=OgnlRuntime.getPropertyAccessor(Collection.class);
-                  	    
+                  	    OgnlContextState.setGettingByKeyProperty(ogContext,true);
                   	    return accessor.getProperty(ogContext,propVal,objects[0]);
                   	}
               }

src/test/com/opensymphony/xwork/util/GetPropertesTest.java

+/*
+ * Created on Jan 23, 2006
+ *
+ * TODO To change the template for this generated file go to
+ * Window - Preferences - Java - Code Style - Code Templates
+ */
+package com.opensymphony.xwork.util;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+
+import junit.framework.TestCase;
+
+/**
+ * @author Gabe
+ *
+ * TODO To change the template for this generated type comment go to
+ * Window - Preferences - Java - Code Style - Code Templates
+ */
+public class GetPropertesTest extends TestCase {
+
+    public void testGetCollectionProperties()  {
+        doGetCollectionPropertiesTest(new ArrayList());
+        doGetCollectionPropertiesTest(new HashSet());
+        
+    }
+    
+    public void doGetCollectionPropertiesTest(Collection c) {
+        OgnlValueStack vs = new OgnlValueStack();
+        Foo foo = new Foo();
+        foo.setBarCollection(c);
+        vs.push(foo);
+        assertEquals(Boolean.TRUE, vs.findValue("barCollection.isEmpty"));
+        assertEquals(Boolean.TRUE, vs.findValue("barCollection.empty"));
+        assertEquals(new Integer(0), vs.findValue("barCollection.size"));
+        assertTrue(vs.findValue("barCollection.iterator") instanceof java.util.Iterator);
+    }
+}