You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When doing number arithmetic in SpEL the result type is apparently one of double, long or int.
This has the unwanted effect that for example float or BigDecimal values will be changed to int.
new java.math.BigDecimal("12.34")
evaluates to 12.34
-(new java.math.BigDecimal("12.34"))
evaluates to -12
see org.springframework.expression.spel.ast
OpPlus, OpMinus, OpMultiply, OpDivide
This does need doing. I feel like widening byte/short/int to long value if not necessary might be overkill but I don't feel strongly enough that it shouldn't be done. Supporting BigDecimal/BigInteger in those referenced Op* classes should be added. Although not hard, care would need taking to do it in a way such that the code doesn't get too bloated with instanceof checks. Also needs test coverage, ideally for all cases/combinations. Don't think we have to take the further step of handling custom Number subtypes, that could be looked at when the scenario comes up.
public class FunctionsClass {
public int abs(int value) {
return Math.abs(value);
}
public float abs(float value) {
return Math.abs(value);
}
}
and when I execute the test with following code:
FunctionsClass testObject = new FunctionsClass();
StandardEvaluationContext context = new StandardEvaluationContext();
org.springframework.expression.Expression expression = parser.parseExpression("abs(-10.2f)");
Number result = expression.getValue(context, testObject, Number.class);
it fails with:
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1033E:(pos 0): Method call of 'abs' is ambiguous, supported type conversions allow multiple variants to match
I think that your problem (SpEL unable to find the correct method) should be described in a new issue.
This report deals with the problems with numeric operators, but you speak of indecisiveness in the resolution of the method to be applied.
Regarding your problem, it seems that (at first glance) SPEL tries to find a function with the required signature, if not, try to find one that might fit (using converters), finds two candidates and does not know which one to apply.
You might think to put in place a priority system depending on the type to convert.
For example, when I have a float, I try first to see if there is a function that accepts a double, and if it finds it then use that, otherwise I try to see if there is one that accept long, int, etc..
...but pay attention, imagine you have this situation:
public int someFunction (int a, double b);
public int someFunction (double a, int b);
Then you go and evaluate
"someFunction (1f, 1f)"
Which one of the two functions would you choose?
So I think that it is necessary to open a new issue, and discuss it carefully in there.
Thanks for the feedback. I will open a new bug. The possible fix that I tried locally (and that resolved my issue) involved introducing FloatLiteral.java and adding additional instanceof for Float.
I just reviewed the PR Giovanni again - made a couple of comments on it but in principal it is in reasonable shape. There seems to be some code duplication in the tests and the coding standards aren't quite right for the if blocks, but it works and all the existing expression tests are passing for me. Chris/Phil - would you expect the submitter to fix the code duplication and styling thing in the PR and resubmit or would you do that?
Activity
spring-projects-issues commentedon Feb 26, 2012
Chris Beams commented
Added Andy Clement as watcher
@Andy, could you take a look at this? Any comments would be appreciated; please assign to yourself if you can spare the time to fix it directly.Thanks!
spring-projects-issues commentedon Mar 2, 2012
Oliver Becker commented
I think the algorithm should be something along
spring-projects-issues commentedon Mar 5, 2012
Andy Clement commented
This does need doing. I feel like widening byte/short/int to long value if not necessary might be overkill but I don't feel strongly enough that it shouldn't be done. Supporting BigDecimal/BigInteger in those referenced Op* classes should be added. Although not hard, care would need taking to do it in a way such that the code doesn't get too bloated with instanceof checks. Also needs test coverage, ideally for all cases/combinations. Don't think we have to take the further step of handling custom Number subtypes, that could be looked at when the scenario comes up.
spring-projects-issues commentedon Mar 8, 2012
Chris Beams commented
Andy, is this something you're willing to commit to for 3.2 M1 / M2? I'll assign to you if so.
spring-projects-issues commentedon Mar 15, 2012
Stevo Slavić commented
#13358 is somewhat related issue.
spring-projects-issues commentedon Apr 16, 2012
Giovanni Dall'Oglio Risso commented
Hi...
I'm the reporter for the #13358 issue (related to this).
We managed to solve this problem, adding BigDecimals; we also made a pull request: #80
You can take that pull request, or start by that code (we added quite test cases).
PS: maybe you can consider to add also the BigIntegers support...
spring-projects-issues commentedon May 14, 2012
Chris Beams commented
I've asked Giovanni to touch up his pull request per the contributor guidelines. We can start with that support at least, which covers
BigDecimal.spring-projects-issues commentedon Jun 6, 2012
Satyapal Reddy commented
I have two functions:
public class FunctionsClass {
public int abs(int value) {
return Math.abs(value);
}
public float abs(float value) {
return Math.abs(value);
}
}
and when I execute the test with following code:
it fails with:
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1033E:(pos 0): Method call of 'abs' is ambiguous, supported type conversions allow multiple variants to match
spring-projects-issues commentedon Jun 6, 2012
Satyapal Reddy commented
When I have two functions with different inputs int and float, eventhough I pass float, it is complaining about this being ambiguous
spring-projects-issues commentedon Jun 6, 2012
Satyapal Reddy commented
test case
spring-projects-issues commentedon Jun 8, 2012
Giovanni Dall'Oglio Risso commented
@SatyapalI think that your problem (SpEL unable to find the correct method) should be described in a new issue.
This report deals with the problems with numeric operators, but you speak of indecisiveness in the resolution of the method to be applied.
Regarding your problem, it seems that (at first glance) SPEL tries to find a function with the required signature, if not, try to find one that might fit (using converters), finds two candidates and does not know which one to apply.
You might think to put in place a priority system depending on the type to convert.
For example, when I have a float, I try first to see if there is a function that accepts a double, and if it finds it then use that, otherwise I try to see if there is one that accept long, int, etc..
...but pay attention, imagine you have this situation:
Then you go and evaluate
Which one of the two functions would you choose?
So I think that it is necessary to open a new issue, and discuss it carefully in there.
spring-projects-issues commentedon Jun 8, 2012
Satyapal Reddy commented
@GiovanniThanks for the feedback. I will open a new bug. The possible fix that I tried locally (and that resolved my issue) involved introducing FloatLiteral.java and adding additional instanceof for Float.
Satyapal
spring-projects-issues commentedon Nov 13, 2013
Andy Clement commented
I just reviewed the PR Giovanni again - made a couple of comments on it but in principal it is in reasonable shape. There seems to be some code duplication in the tests and the coding standards aren't quite right for the if blocks, but it works and all the existing expression tests are passing for me. Chris/Phil - would you expect the submitter to fix the code duplication and styling thing in the PR and resubmit or would you do that?
spring-projects-issues commentedon Nov 14, 2013
Giovanni Dall'Oglio Risso commented
This weekend I'm out, if you can wait, the first days of the next week I'll rebase and clean everything...
Thanks
spring-projects-issues commentedon Nov 19, 2013
Giovanni Dall'Oglio Risso commented
Hello.
Everything is now OK, plus some additions:
I rebased on master, hope is OK...
Thanks
spring-projects-issues commentedon Nov 21, 2013
Oliver Becker commented
Big thanks to Giovanni and Andy for fixing this!