views:

104

answers:

3

So basically I would like my app to read info from a database, this info would be a string that is valid java code. Once read, I would like my app to execute this string as if it were code. Is there any functionality packaged with the android sdk that supports this?

Essentially I want the phone to populate some data with information queried from a database. One of these pieces of information would be a statement like:

"return data.ZombieKillTotal >= 100000;"

Which would be used inside a statement like:

registerAchievement(new Achievement("Zombie Killer", new AchievementValidator() { 
    public boolean isSatisfied(Data data) { ExecStringAsCode(query result) } 
    });

I just don't know what to use for 'ExecStringAsCode' :(

A: 

The Java programming language doesn't really provide a feature for the execution of code in text strings.

You could embed an interpreter for a more script-friendly language (perhaps Python) and execute code that way.

fadden
A: 

if you want to execute code loaded dynamicly you will have to use somthing like COdeDOM to create code and compile it then use reflection to load and excute it.

Jason
+1  A: 

I ended up using the JRE library and imported javax.script.*;

http://java.sun.com/developer/technicalArticles/J2SE/Desktop/scripting/

FlyingStreudel