struts-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "ASF GitHub Bot (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (WW-4932) Conversion fails when generic type is an interface
Date Mon, 21 May 2018 05:25:00 GMT

    [ https://issues.apache.org/jira/browse/WW-4932?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16482207#comment-16482207
] 

ASF GitHub Bot commented on WW-4932:
------------------------------------

lukaszlenart closed pull request #221:  WW-4932 honor .properties file before the generic
parametrics
URL: https://github.com/apache/struts/pull/221
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/core/src/main/java/com/opensymphony/xwork2/conversion/impl/DefaultObjectTypeDeterminer.java
b/core/src/main/java/com/opensymphony/xwork2/conversion/impl/DefaultObjectTypeDeterminer.java
index c375ce147..2ef75e88c 100644
--- a/core/src/main/java/com/opensymphony/xwork2/conversion/impl/DefaultObjectTypeDeterminer.java
+++ b/core/src/main/java/com/opensymphony/xwork2/conversion/impl/DefaultObjectTypeDeterminer.java
@@ -75,11 +75,11 @@ public DefaultObjectTypeDeterminer(@Inject XWorkConverter converter, @Inject
Ref
 
     /**
      * Determines the key class by looking for the value of @Key annotation for the given
class.
-     * If no annotation is found, the key class is determined by using the generic parametrics.
-     *
-     * As fallback, it determines the key class by looking for the value of Key_${property}
in the properties
+     * If no annotation is found, it determines the key class by looking for the value of
Key_${property} in the properties
      * file for the given class.
      *
+     * As fallback, the key class is determined by using the generic parametrics.
+     *
      * @param parentClass the Class which contains as a property the Map or Collection we
are finding the key for.
      * @param property    the property of the Map or Collection for the given parent class
      * @see com.opensymphony.xwork2.conversion.ObjectTypeDeterminer#getKeyClass(Class, String)
@@ -89,21 +89,21 @@ public Class getKeyClass(Class parentClass, String property) {
         if (annotation != null) {
             return annotation.value();
         }
-        Class clazz = getClass(parentClass, property, false);
+        Class clazz = (Class) xworkConverter.getConverter(parentClass, KEY_PREFIX + property);
         if (clazz != null) {
             return clazz;
         }
-        return (Class) xworkConverter.getConverter(parentClass, KEY_PREFIX + property);
+        return getClass(parentClass, property, false);
     }
 
     /**
      * Determines the element class by looking for the value of @Element annotation for the
given
      * class.
-     * If no annotation is found, the element class is determined by using the generic parametrics.
-     *
-     * As fallback, it determines the key class by looking for the value of Element_${property}
in the properties
+     * If no annotation is found, it determines the key class by looking for the value of
Element_${property} in the properties
      * file for the given class. Also looks for the deprecated Collection_${property}
      *
+     * As fallback, the element class is determined by using the generic parametrics.
+     *
      * @param parentClass the Class which contains as a property the Map or Collection we
are finding the key for.
      * @param property    the property of the Map or Collection for the given parent class
      * @see com.opensymphony.xwork2.conversion.ObjectTypeDeterminer#getElementClass(Class,
String, Object)
@@ -113,17 +113,17 @@ public Class getElementClass(Class parentClass, String property, Object
key) {
         if (annotation != null) {
             return annotation.value();
         }
-        Class clazz = getClass(parentClass, property, true);
-        if (clazz != null) {
-            return clazz;
-        }
-        clazz = (Class) xworkConverter.getConverter(parentClass, ELEMENT_PREFIX + property);
+        Class clazz = (Class) xworkConverter.getConverter(parentClass, ELEMENT_PREFIX + property);
         if (clazz == null) {
             clazz = (Class) xworkConverter.getConverter(parentClass, DEPRECATED_ELEMENT_PREFIX
+ property);
             if (clazz != null) {
                 LOG.info("The Collection_xxx pattern for collection type conversion is deprecated.
Please use Element_xxx!");
             }
         }
+        if (clazz != null) {
+            return clazz;
+        }
+        clazz = getClass(parentClass, property, true);
         return clazz;
     }
 
diff --git a/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlUtilTest.java b/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlUtilTest.java
index dec0cc0ff..87578a7a3 100644
--- a/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlUtilTest.java
+++ b/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlUtilTest.java
@@ -582,7 +582,7 @@ public void testGetBeanMap() throws Exception {
         // just do some of the 15 tests
         Map beans = ognlUtil.getBeanMap(foo);
         assertNotNull(beans);
-        assertEquals(21, beans.size());
+        assertEquals(22, beans.size());
         assertEquals("Hello Santa", beans.get("title"));
         assertEquals(new Long("123"), beans.get("ALong"));
         assertEquals(new Integer("44"), beans.get("number"));
diff --git a/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlValueStackTest.java b/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlValueStackTest.java
index 012122085..514b31325 100644
--- a/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlValueStackTest.java
+++ b/core/src/test/java/com/opensymphony/xwork2/ognl/OgnlValueStackTest.java
@@ -733,6 +733,14 @@ public void testSetNullList() {
         assertEquals("Cat One", ((Cat) foo.getCats().get(0)).getName());
         assertEquals("Cat Two", ((Cat) foo.getCats().get(1)).getName());
 
+        //test when both Key and Value types of Map are interfaces but concrete classes are
defined in .properties file
+        vs.setValue("animalMap[3].name", "Cat Three by interface");
+        vs.setValue("animalMap[6].name", "Cat Six by interface");
+        assertNotNull(foo.getAnimalMap());
+        assertEquals(2, foo.getAnimalMap().size());
+        assertEquals("Cat Three by interface", foo.getAnimalMap().get(new Long(3)).getName());
+        assertEquals("Cat Six by interface", foo.getAnimalMap().get(new Long(6)).getName());
+
         vs.setValue("annotatedCats[0].name", "Cat One By Annotation");
         vs.setValue("annotatedCats[1].name", "Cat Two By Annotation");
         assertNotNull(foo.getAnnotatedCats());
diff --git a/core/src/test/java/com/opensymphony/xwork2/util/Animal.java b/core/src/test/java/com/opensymphony/xwork2/util/Animal.java
new file mode 100644
index 000000000..cc26a4524
--- /dev/null
+++ b/core/src/test/java/com/opensymphony/xwork2/util/Animal.java
@@ -0,0 +1,23 @@
+/*
+ * 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 com.opensymphony.xwork2.util;
+
+public interface Animal {
+    String getName();
+}
diff --git a/core/src/test/java/com/opensymphony/xwork2/util/Cat.java b/core/src/test/java/com/opensymphony/xwork2/util/Cat.java
index 626c78b8e..2ebdd136a 100644
--- a/core/src/test/java/com/opensymphony/xwork2/util/Cat.java
+++ b/core/src/test/java/com/opensymphony/xwork2/util/Cat.java
@@ -26,7 +26,7 @@
  * @author $Author$
  * @version $Revision$
  */
-public class Cat {
+public class Cat implements Animal {
 
     public static final String SCIENTIFIC_NAME = "Feline";
 
diff --git a/core/src/test/java/com/opensymphony/xwork2/util/Foo.java b/core/src/test/java/com/opensymphony/xwork2/util/Foo.java
index b14270a2e..f8b67c71a 100644
--- a/core/src/test/java/com/opensymphony/xwork2/util/Foo.java
+++ b/core/src/test/java/com/opensymphony/xwork2/util/Foo.java
@@ -53,6 +53,7 @@
     long aLong;
     Calendar calendar;
     BarJunior barJunior;
+    Map<MyNumber, Animal> animalMap;
 
     public BarJunior getBarJunior() {
         return barJunior;
@@ -242,5 +243,13 @@ public Calendar getCalendar() {
 
     public void setCalendar(Calendar calendar) {
         this.calendar = calendar;
-    }     
+    }
+
+    public Map<MyNumber, Animal> getAnimalMap() {
+        return animalMap;
+    }
+
+    public void setAnimalMap(Map<MyNumber, Animal> animalMap) {
+        this.animalMap = animalMap;
+    }
 }
diff --git a/core/src/test/java/com/opensymphony/xwork2/util/MyNumber.java b/core/src/test/java/com/opensymphony/xwork2/util/MyNumber.java
new file mode 100644
index 000000000..218747234
--- /dev/null
+++ b/core/src/test/java/com/opensymphony/xwork2/util/MyNumber.java
@@ -0,0 +1,22 @@
+/*
+ * 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 com.opensymphony.xwork2.util;
+
+abstract class MyNumber extends Number {
+}
diff --git a/core/src/test/resources/com/opensymphony/xwork2/util/Foo-conversion.properties
b/core/src/test/resources/com/opensymphony/xwork2/util/Foo-conversion.properties
index 945f69e2c..b4960c4c1 100644
--- a/core/src/test/resources/com/opensymphony/xwork2/util/Foo-conversion.properties
+++ b/core/src/test/resources/com/opensymphony/xwork2/util/Foo-conversion.properties
@@ -26,4 +26,5 @@ KeyProperty_barCollection=id
 Element_barCollection=com.opensymphony.xwork2.util.Bar
 KeyProperty_barList=id
 Element_barList=com.opensymphony.xwork2.util.Bar
-
+Key_animalMap=java.lang.Long
+Element_animalMap=com.opensymphony.xwork2.util.Cat


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


> Conversion fails when generic type is an interface
> --------------------------------------------------
>
>                 Key: WW-4932
>                 URL: https://issues.apache.org/jira/browse/WW-4932
>             Project: Struts 2
>          Issue Type: Improvement
>          Components: Other
>            Reporter: Nuno Oliveira
>            Assignee: Yasser Zamani
>            Priority: Minor
>             Fix For: 2.6
>
>
> Hi, I was asked to create this issue after exchanging some emails in the struts mailing
list.
> I had the following problem:
> {noformat}
> Hi,
> I am having problems populating an action variable Set by request using
> struts type conversion if the Set has the element type defined as an
> interface.
> I have a UserAction-conversion.properties file with the following
> configuration using the class implementation:
> KeyProperty_roles=id
> Element_roles=path.to.class.Roles
> CreateIfNull_roles=true
> And UserAction has the collection Set<RolesInterface>. Roles obviously
> implements RolesInterface.
> If the Set element type <RolesInterface> is deleted it works just fine.
> Am I doing something wrong or is this not possible?
> Thanks{noformat}
> And this was the response, which fixed my problem. :
> {noformat}
> I reproduced it and seems it's because it tries to instantiate a new
> element but the interface cannot being instantiated. Please see [1] to
> know why it tries to instantiate interface instead of class.
> Annotating the getRoles or setRoles method with @Element(value =
> path.to.class.Roles.class) may fix this issue. (shouldn't it be
> path.to.class.Role instead? it seems Roles is a list not element)
> Regards.
> [1]
> github.com/apache/struts/blob/05829e3faadd15cfa67eb234cd1775f2b98918cb/core/src/main/java/com/opensymphony/xwork2/conversion/impl/DefaultObjectTypeDeterminer.java#L100{noformat}
> After acknowledging the issue is fixed, the fix suggestion:
> {noformat}
> You're welcome :) glad to hear this. However, I think it's better that
> Struts honor .properties file (where element class has been defined
> strictly by user) before the generic parametrics [1]. Could you please
> register an issue at [2] with title "conversion fails when generic type
> is an interface" then paste these emails contents in it's description.
> Thanks for your report!
> [1]
> github.com/apache/struts/blob/05829e3faadd15cfa67eb234cd1775f2b98918cb/core/src/main/java/com/opensymphony/xwork2/conversion/impl/DefaultObjectTypeDeterminer.java#L100
> [2] issues.apache.org/jira/projects/WW/
> {noformat}
>  



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Mime
View raw message