How to parse a mathematical expression given as a string and return a number? [duplicate]

You can pass it to a BeanShell bsh.Interpreter, something like this: Interpreter interpreter = new Interpreter(); interpreter.eval(“result = 5+4*(7-15)”); System.out.println(interpreter.get(“result”)); You’ll want to ensure the string you evaluate is from a trusted source and the usual precautions but otherwise it’ll work straight off. If you want to go a more complicated (but safer) approach you … Read more

Overriding grails.views.default.codec=’html’ config back to ‘none’

To summarize the various levels at which the codec can be applied: Set Config.groovy’s grails.views.default.codec=”html” to get HTML escaping by default on all ${expressions} in the application. Then when you want to default a whole page back to none, use the directive: <%@page defaultCodec=”none” %> or <%@ defaultCodec=”none” %> To disable HTML encoding for one … Read more

Expression for Type members results in different Expressions (MemberExpression, UnaryExpression)

The reason this happens is that Age is a value type. In order to coerce an expression returning a value type into Func<Person,object> the compiler needs to insert a Convert(expr, typeof(object)), a UnaryExpression. For strings and other reference types, however, there is no need to box, so a “straight” member expression is returned. If you … Read more

Expression references a method that does not belong to the mocked object

This problem occurs because you are trying to mock Select method, which is an extension method, not an instance method of IEnumerable<T>. Basically, there is no way to mock an extension method. Have a look at this question for some ideas that you may find useful. UPD (12/11/2014): To gain more understanding on mocking extension … Read more

Why isn’t a final variable always a constant expression?

From the JLS A blank final is a final variable whose declaration lacks an initializer. A constant variable is a final variable of primitive type or type String that is initialized with a constant expression (ยง15.28). Your variable final int a; is a blank final variable. It lacks an initializer. The second paragraph doesn’t apply … Read more