Shamseer PC Shamseer PC - 2 years ago 58
Java Question

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

I am using

for parsing a
expression but when I add
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 Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download