Parse Nested User-Defined Functions using Spring Expression Language (SpEL)
If we want to get the result of the function in java,
what is the tool or solution we can refer to? The answer is here.
SpEL, or Spring Expression Language, is a potent expression language that allows for the runtime querying and manipulation of an object network. Although SpEL is a foundation for expression evaluation within the Spring portfolio, it is unrelated to Spring and can be utilized separately. The usage of SpEL to evaluate variables and parse expressions is covered in a plethora of educational articles and blog postings. However, a unique application of it is explained here. The ability to execute user-defined functions is one of the many applications that SpEL offers. The method registerFunction of the StandardEvaluationContext can be used to register a pre-written function.
public void registerFunction(String name, Method m)
Two files will be used to demonstrate the use case.
- StringUtils.java – where all the functions are written.
- Main.java – main class of the application.
Here, The subString function is being overloaded and other functions are created normally.
All the functions are registered in the context using,
- Method name
- Method declared class
- Datatype of parameters of the method
As a result of the information provided above, method overloading is possible because the context may differentiate between methods not only by their names but also by the types of their parameters. In case of method overloading, the name in registerFunction must be distinct as shown above. The parseExpression method gives you the final result.
Flow of the Execution
When you do not want to pass the functions as parameters and break the execution manually, this dynamic solution can save your day.
Please Login to comment...