samedi 20 juin 2015

Compiling/running .java files at runtime vs. reflection

So I've come across this problem. We're using Java as our 'scripting' language, because we already use it for everything else, and it doesn't require any additional 3rd party libs.

Now currently we're executing runtime code via reflection. Something like:

Class triggerAction = null;
    try {
        triggerAction = Class.forName(actionName);
        Gdx.app.log("Info", triggerAction.getName() + " attached to "
                + this.toString());
        try {
            this.triggerAction = (TriggerAction) triggerAction
                    .newInstance();
        } 

This is great, but it's not ideal. Any java files that are meant to be run at runtime must still be compiled into the latest build. Ideally, non developers could play with script files stored in like a '/scripts/' dir, and then just run the program and have the modified .java file execute. This is something I've never done before, so I'm a little at a loss as to how to go about it in a way that will be safe for the enduser.

I've found this approach: Generating, compiling and using Java code at run time?

But I'm not expecting most end users to have PATH setup and all that, so I'm concerned that these solutions could run into problems there. What's the ideal approach here?





Aucun commentaire:

Enregistrer un commentaire