1

I wrote an el function with following signature which compares the double value:

java.lang.Number numberMinimum(java.lang.Number, java.lang.Number)

This works even using primitive data types,
but calling it using a value from a resource bundle results in:

java.lang.IllegalArgumentException: Cannot convert 99 of type class java.lang.String to class java.lang.Number
at com.sun.el.lang.ELSupport.coerceToType(ELSupport.java:412)
at com.sun.el.parser.AstFunction.getValue(AstFunction.java:123)

whereas changing the parameter to primitive int which the resource value is assigned to works?

int intMinimum(int, int)

Why is the expression-language autoconverting it to an int if desired target is of type int but not working for number?

djmj
  • 5,579
  • 5
  • 54
  • 92

3 Answers3

0

Add another function:

java.lang.Number numberMinimum(java.lang.String, java.lang.String)

In that function convert your strings to numbers (Convert a String to Double - Java) and call your java.lang.Number numberMinimum(java.lang.Number, java.lang.Number)

Community
  • 1
  • 1
piokuc
  • 25,594
  • 11
  • 72
  • 102
0

String will not get converted to Number automatically. Consider converting your string to Number by overloading your numberMinimum() method.

Hope this helps!

Anugoonj
  • 575
  • 3
  • 9
  • It does get autoconverted by el to primitive int. Aslong as the target is an int. But not if target is a number. Else the second method would still not work. – djmj Feb 01 '13 at 18:11
0

El cannot convert to Number directly only to inheriting classes:

com.sun.el.lang.ELSupport.coerceToType(ELSupport.java:412) checks if target type is of any type that implements Number but not Number itself.

There is no default implementation like using double if target type is of type Number.

http://grepcode.com/file/repo1.maven.org/maven2/org.mortbay.jetty/jsp-2.1/6.1.1rc1/com/sun/el/lang/ELArithmetic.java#ELArithmetic.isNumberType%28java.lang.Class%29

djmj
  • 5,579
  • 5
  • 54
  • 92