I have some string with Groovy expression. I need:
- Execute Groovy expression that is contained in java.lang.String
- Get result of the expression as an Object
- Determine type of the resulting object
Is it possible? Also, if I won’t use dynamic features of Groovy, will I be able to determine type of the expression “statically”, i.e. in compile-time without executing expression itself.
Thanks
A very simple and easy way is to use the Eval class. Evaluates the specified String expression and returns the result. The type result of the expression is
Object.You can get the class of an object with the
getClass()method or simply:In this example, is not possible determine the static type checking at compilation time.