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' :(

5
  • 2
    Not really a good idea, it's even a terrible idea to execute string. Commented Aug 24, 2010 at 17:53
  • I don't know if you'd want to do this on Android or even if it's supported, but in Java you'd probably have to use dynamically compiled/loaded/executed classes. Commented Aug 24, 2010 at 17:56
  • @Colin: How would you recommend getting that information into the class from the database if not executing the string? Commented Aug 24, 2010 at 18:03
  • This is a really dangerous idea. Why not just parse the string and do what you need to do with it? Commented Aug 24, 2010 at 18:13
  • I am trying not to limit the functionality of the achievement validation. If I am misunderstanding perhaps you could provide an example of parsed string to code? Commented Aug 24, 2010 at 18:22

3 Answers 3

3

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

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.