In SPEL projection expression, #this as the same value as #root [SPR-13055] #17647
I think #this is not resolved correctly when it is used as an argument of a static method.
If I try to execute this code :
I get an exception :
In this case #this shouldn't be an ArrayList but a HashMap.
If I don't use #this in the method collectionToCommaDelimitedString, but directly in the projection expression like this :
I don't have any exceptions and the result is the one I was expecting.
Andy Clement commented
Juergen, could you cast an eye over this patch?
With these changes we fix #17647, #15050 and #16651. All variants of the problem of #this (and unqualified) references in projection/selection. I've added test cases for all scenarios and some extra ones (involving maps with projection/selection).
The fix is to do as I mentioned in 12035 - recognize when entering a new scope and remember the object should be used for #this (or for unqualified references) at that nesting level. By remembering it we avoid accidentally using values computed for intermediate computations as we resolve other expression elements at that level. As we exit the scope, we forget the object again.
The downside is, with this fix, some users may find their expressions behave differently if they were relying on the old, wrong, behavior. They could have written something that would work and after this change it will break - to fix it they'd probably need to modify #this references in nested elements of their expressions to #root (or prepend #root to any unqualified references). I think, on balance, it is better to make this change though.
If you are happy with the patch and my comment about affecting existing users, I can push it.
With these fixes I feel I could now tackle SpEL compilation of projection/selection...