Shamseer PC Shamseer PC - 6 months ago 11
Java Question

How to evaluate "(1.25 > 8)? (2.3125 * sin(90) * 1.25) : (2.3125 * sin(90) * 8)" in Java

I am using

org.springframework.expression.spel.standard.SpelExpressionParser
for parsing a
String
expression but when I add
sin(90)
to this expression I am getting the following error:


EL1011E:(pos 50): Method call: Attempted to call method sin(java.lang.Integer) on null context object error.


Any idea how to solve this?

Answer

Read the Spring Expression Language (SpEL) manual.

Option 1: §9.5.12 Functions:

You can extend SpEL by registering user defined functions that can be called within the expression string.

Option 2: §9.5.9 Types:

The special T operator can be used to specify an instance of java.lang.Class (the type). Static methods are invoked using this operator as well.

If you go with option 1, you can register the sin() function, and the expression will work unchanged.

If you go with option 2, then this is how the expression should look:

(1.25 > 8) ? (2.3125 * T(Math).sin(90) * 1.25) : (2.3125 * T(Math).sin(90) * 8)