You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "recyclebin5385 (JIRA)" <ji...@apache.org> on 2014/04/29 10:47:14 UTC

[jira] [Comment Edited] (OGNL-238) OGNLRuntime doesn't handle parameterized method arguments correctly for a grandchild class of a generic class

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

recyclebin5385 edited comment on OGNL-238 at 4/29/14 8:45 AM:
--------------------------------------------------------------

Sorry for not responding for a long time....

I recreated a test case using Maven and attached a zipped file "ognl238test.zip" including a whole Maven project.
Running "mvn test" will show test failures like below.

***start of mvn output***
Results :

Failed tests:   test1(Ognl238Test): expected:<class java.lang.Short> but was:<class java.lang.String>

Tests in error:
  test3(Ognl238Test): java.lang.String cannot be cast to java.lang.Integer
  test4(Ognl238Test): java.lang.String cannot be cast to java.lang.Integer

Tests run: 4, Failures: 1, Errors: 2, Skipped: 0
***end of mvn output***

My environment is shown below. (This is the output of "mvn -version".)
Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9;2014-02-15T02:37:52+09:00)
Maven home: C:\ProgramFiles2\maven\bin\..
Java version: 1.7.0, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.7.0\jre
Default locale: ja_JP, platform encoding: MS932
OS name: "windows vista", version: "6.0", arch: "x86", family: "windows"


was (Author: recyclebin5385@yahoo.co.jp):
Sorry for not responding for a long time....

I recreated a test case using Maven and attached a zipped file "ognl238test.zip" including a whole Maven project.
Running "mvn test" will show test failures like below.

---start of mvn output---
Results :

Failed tests:   test1(Ognl238Test): expected:<class java.lang.Short> but was:<class java.lang.String>

Tests in error:
  test3(Ognl238Test): java.lang.String cannot be cast to java.lang.Integer
  test4(Ognl238Test): java.lang.String cannot be cast to java.lang.Integer

Tests run: 4, Failures: 1, Errors: 2, Skipped: 0
---end of mvn output---

My environment is shown below. (This is the output of "mvn -version".)
Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9;2014-02-15T02:37:52+09:00)
Maven home: C:\ProgramFiles2\maven\bin\..
Java version: 1.7.0, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.7.0\jre
Default locale: ja_JP, platform encoding: MS932
OS name: "windows vista", version: "6.0", arch: "x86", family: "windows"

> OGNLRuntime doesn't handle parameterized method arguments correctly for a grandchild class of a generic class
> -------------------------------------------------------------------------------------------------------------
>
>                 Key: OGNL-238
>                 URL: https://issues.apache.org/jira/browse/OGNL-238
>             Project: Commons OGNL
>          Issue Type: Bug
>          Components: Core Runtime
>    Affects Versions: 3.0
>         Environment: Windows 7 64bit, Java Runtime Environment 7
>            Reporter: recyclebin5385
>             Fix For: 4.0
>
>         Attachments: ognl238test.zip
>
>
> Suppose there are classes as described below.
> Class Foo<T> is a generic class with a type parameter T.
> It has a property named "x" whose type is T; Foo has methods "T getX()" and "void setX(T x)".
> Class Bar is a direct derived class of Foo<Integer>.
> Class Baz is a direct derived class of Bar and class Qux is a direct derived class of Baz.
> In short, there is an inheritance tree like this: Foo<T> -> Bar -> Baz -> Qux.
> Static method ognl.Ognl.setValue(String expression, Object root, Object value) has an ability to resolve a type parameter of an argument of a method.
> But it does not work well for a derived class of a generic class.
> If you execute Ognl.setValue("tmp.x", root, "123") where "tmp" is an instance of class Bar, "123" is converted to an Integer and tmp's property X is set to an Integer.
> It is a natural behavior because Bar extends Foo<Integer> and tmp's property x should be an Integer.
> But, if tmp is an instance of class Baz or Qux, which are both derived classes of Bar, the conversion is never performed and tmp's property X is set to String "123".
> It seems that type parameter handling works only when the class is DIRECTLY derived from a generic class.
> Here is a test code.
> You will need to add ognl-3.0.6.jar and javassist-3.11.0.GA.jar to the classpath in order to execute the code. They are bundled in Struts2.3.15.1.
> ------
> import java.text.MessageFormat;
> import java.util.LinkedHashMap;
> import java.util.Map;
> import java.util.Map.Entry;
> import ognl.Ognl;
> class Foo<T> {
>     private T m_x;
>     public T getX() {
>         return m_x;
>     }
>     public void setX(T x) {
>         m_x = x;
>     }
> }
> class Bar extends Foo<Integer> {
> }
> class Baz extends Bar {
> }
> class Qux extends Baz {
> }
> public class OgnlBugTest {
>     public static void main(String[] args) {
>         try {
>             Map<String, Foo<?>> root = new LinkedHashMap<String, Foo<?>>();
>             root.put("foo", new Foo<Short>());
>             root.put("bar", new Bar());
>             root.put("baz", new Baz());
>             root.put("qux", new Qux());
>             Ognl.setValue("foo.x", root, "1");
>             Ognl.setValue("bar.x", root, "2");
>             Ognl.setValue("baz.x", root, "3");
>             Ognl.setValue("qux.x", root, "4");
>             for (Entry<String, Foo<?>> entry : root.entrySet()) {
>                 System.out.println(MessageFormat.format("{0} = {1} ({2})", entry.getKey(), entry.getValue().getX(), entry.getValue().getX().getClass()));
>             }
>         } catch (Exception exception) {
>             exception.printStackTrace();
>         }
>     }
> }



--
This message was sent by Atlassian JIRA
(v6.2#6252)