Author: mgrigorov
Date: Sun Sep 19 13:48:37 2010
New Revision: 998663

URL: http://svn.apache.org/viewvc?rev=998663&view=rev
Log:
WICKET-2937 AbstractPropertyModel getObjectClass don't consider nested 
IObjectClassAwareModel targets

Use the IObjectClassAwareModel's target class if available.
patch-provided-by: Pedro Santos


Added:
    
wicket/branches/wicket-1.4.x/wicket/src/test/java/org/apache/wicket/model/AbstractPropertyModelObjectClassTest.java
   (with props)
Modified:
    
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/model/AbstractPropertyModel.java
    
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/util/lang/PropertyResolver.java

Modified: 
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/model/AbstractPropertyModel.java
URL: 
http://svn.apache.org/viewvc/wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/model/AbstractPropertyModel.java?rev=998663&r1=998662&r2=998663&view=diff
==============================================================================
--- 
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/model/AbstractPropertyModel.java
 (original)
+++ 
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/model/AbstractPropertyModel.java
 Sun Sep 19 13:48:37 2010
@@ -22,6 +22,7 @@ import java.lang.reflect.Method;
 import org.apache.wicket.Application;
 import org.apache.wicket.Component;
 import org.apache.wicket.Session;
+import org.apache.wicket.WicketRuntimeException;
 import org.apache.wicket.util.lang.PropertyResolver;
 import org.apache.wicket.util.lang.PropertyResolverConverter;
 import org.apache.wicket.util.string.Strings;
@@ -226,6 +227,19 @@ public abstract class AbstractPropertyMo
                                // ignore.
                        }
                }
+               else if (this.target instanceof IObjectClassAwareModel)
+               {
+                       try
+                       {
+                               return 
PropertyResolver.getPropertyClass(expression,
+                                       
((IObjectClassAwareModel<?>)this.target).getObjectClass());
+                       }
+                       catch (WicketRuntimeException e)
+                       {
+                               // it was just a try.
+                       }
+
+               }
                return null;
        }
 

Modified: 
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/util/lang/PropertyResolver.java
URL: 
http://svn.apache.org/viewvc/wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/util/lang/PropertyResolver.java?rev=998663&r1=998662&r2=998663&view=diff
==============================================================================
--- 
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/util/lang/PropertyResolver.java
 (original)
+++ 
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/util/lang/PropertyResolver.java
 Sun Sep 19 13:48:37 2010
@@ -28,6 +28,7 @@ import org.apache.wicket.Application;
 import org.apache.wicket.Session;
 import org.apache.wicket.WicketRuntimeException;
 import org.apache.wicket.util.convert.ConversionException;
+import org.apache.wicket.util.lang.PropertyResolver.IClassCache;
 import org.apache.wicket.util.string.Strings;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -154,6 +155,23 @@ public final class PropertyResolver
        }
 
        /**
+        * @param <T>
+        * @param expression
+        * @param clz
+        * @return class of the target Class property expression
+        */
+       public static <T> Class<T> getPropertyClass(String expression, Class<?> 
clz)
+       {
+               ObjectAndGetSetter setter = getObjectAndGetSetter(expression, 
null, RESOLVE_CLASS, clz);
+               if (setter == null)
+               {
+                       throw new WicketRuntimeException("No Class returned for 
expression: " + expression +
+                               " for getting the target classs of: " + clz);
+               }
+               return (Class<T>)setter.getTargetClass();
+       }
+
+       /**
         * @param expression
         * @param object
         * @return Field for the property expression or null if such field 
doesn't exist (only getters
@@ -204,15 +222,40 @@ public final class PropertyResolver
                return setter.getSetter();
        }
 
+       /**
+        * Just delegating the call to the original getObjectAndGetSetter 
passing the object type as
+        * parameter.
+        * 
+        * @param expression
+        * @param object
+        * @param tryToCreateNull
+        * @return {...@link ObjectAndGetSetter}
+        */
        private static ObjectAndGetSetter getObjectAndGetSetter(final String 
expression,
                final Object object, int tryToCreateNull)
        {
+               return getObjectAndGetSetter(expression, object, 
tryToCreateNull, object.getClass());
+       }
+
+
+       /**
+        * Receives the class parameter also, since this method can resolve the 
type for some
+        * expression, only knowing the target class
+        * 
+        * @param expression
+        * @param object
+        * @param tryToCreateNull
+        * @param clz
+        * @return {...@link ObjectAndGetSetter}
+        */
+       private static ObjectAndGetSetter getObjectAndGetSetter(final String 
expression,
+               final Object object, int tryToCreateNull, Class<?> clz)
+       {
                final String expressionBracketsSeperated = 
Strings.replaceAll(expression, "[", ".[")
                        .toString();
                int index = getNextDotIndex(expressionBracketsSeperated, 0);
                int lastIndex = 0;
                Object value = object;
-               Class<?> clz = value.getClass();
                String exp = expressionBracketsSeperated;
                while (index != -1)
                {

Added: 
wicket/branches/wicket-1.4.x/wicket/src/test/java/org/apache/wicket/model/AbstractPropertyModelObjectClassTest.java
URL: 
http://svn.apache.org/viewvc/wicket/branches/wicket-1.4.x/wicket/src/test/java/org/apache/wicket/model/AbstractPropertyModelObjectClassTest.java?rev=998663&view=auto
==============================================================================
--- 
wicket/branches/wicket-1.4.x/wicket/src/test/java/org/apache/wicket/model/AbstractPropertyModelObjectClassTest.java
 (added)
+++ 
wicket/branches/wicket-1.4.x/wicket/src/test/java/org/apache/wicket/model/AbstractPropertyModelObjectClassTest.java
 Sun Sep 19 13:48:37 2010
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.model;
+
+import java.io.Serializable;
+
+import junit.framework.TestCase;
+
+/**
+ * https://issues.apache.org/jira/browse/WICKET-2937
+ * 
+ * <p>
+ * If AbstractPropertyModel has an target that implements the 
IObjectClassAwareModel interface then
+ * the class of that target is used to infer the modeled property type.
+ * 
+ * @author Pedro Santos
+ */
+public class AbstractPropertyModelObjectClassTest extends TestCase
+{
+
+       public void testCompoundPropertyModel()
+       {
+               assertPropertyModelTargetTypeIsInteger(new 
CompoundPropertyModel<CustomType>(
+                       new CustomType()));
+       }
+
+       public void testCompoundPropertyModelBind()
+       {
+               CompoundPropertyModel<CustomType> compoundPropertyModel = new 
CompoundPropertyModel<CustomType>(
+                       new CustomBean());
+               IModel<?> modelForCustomTypeObject = 
compoundPropertyModel.bind("customType");
+               
assertPropertyModelTargetTypeIsInteger(modelForCustomTypeObject);
+       }
+
+       public void testModel()
+       {
+               assertPropertyModelTargetTypeIsInteger(new 
Model<CustomType>(new CustomType()));
+       }
+
+       /**
+        * Just asserting that the the property expression for the somePropety 
is aware of this property
+        * type.
+        * 
+        * @param modelForCustomTypeObject
+        */
+       private void assertPropertyModelTargetTypeIsInteger(IModel<?> 
modelForCustomTypeObject)
+       {
+               assertEquals(Integer.class, new 
PropertyModel<IModel<?>>(modelForCustomTypeObject,
+                       "someProperty").getObjectClass());
+       }
+
+       private static class CustomType implements Serializable
+       {
+               private Integer someProperty;
+
+               public void setSomeProperty(Integer someProperty)
+               {
+                       this.someProperty = someProperty;
+               }
+
+               public Integer getSomeProperty()
+               {
+                       return someProperty;
+               }
+       }
+
+       private static class CustomBean implements Serializable
+       {
+               private CustomType customType;
+
+               public CustomType getCustomType()
+               {
+                       return customType;
+               }
+
+               public void setCustomType(CustomType customType)
+               {
+                       this.customType = customType;
+               }
+       }
+}
\ No newline at end of file

Propchange: 
wicket/branches/wicket-1.4.x/wicket/src/test/java/org/apache/wicket/model/AbstractPropertyModelObjectClassTest.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to