Hi, I am having a formula which I want to make configurable. As in, this formula will be kept in the properties file. The formula is something like - value = (class1.getField1()+class2.getField2())*class3.getField3()
Now if I keep the RHS of the formula in the properties file and get it in my code at runtime, it will just be a normal string and JVM won't understand that it has to process it and get the value. How can I make JVM to process it at runtime? Any pointers in this regard will be of great help.
The JEP library can interpret formulas with respect to parameter passed in from the outside. But if your code is actual Java code, i.e. you need to access object and methods, then you'll need to create actual class files. The Javassist library makes it easy to create and use classes at runtime. (Never mind the references to JBoss - Javassist is independent of it.) [ March 24, 2007: Message edited by: Ulf Dittmer ]
But if this needs to create classes, is there any use if suppose tomorrow the formula is going to change and it still has impact on code level? That's all is the purpose of making it configurable, right? It shouldn't have any impact on code even if the formula is changed. I should just modify the properties (or whatever configurable file is) and that's it... code is ready to run with the new formula...
Joined: Mar 22, 2005
Note that I said "at runtime". The class that will do the calculation never exists as a physical class file - it is created from the formula while the code is running. And, being a regular class, it is subject to being compiled by the JIT, so it's fast too - very cool stuff. If during the runtime of the program you need to evaluate several different formulas, no problem - just create several classes (which need to have different class names, of course, or the class loader won't recognize them as being different).
Another possible solution is to use a scripting language for the formula, for example Groovy.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus