Created
April 16, 2013 17:27
-
-
Save schup/5397811 to your computer and use it in GitHub Desktop.
Implementing an Interface in Groovy and using it in Java through javax.script / JSR-223
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def start(){ | |
println "Start the engines!" | |
} | |
def stop(){ | |
println "Stop at once!" | |
} | |
this as Engine |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Engine.java | |
public interface Engine { | |
void start(); | |
void stop(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// invoke the script using JSR | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.Reader; | |
import javax.script.ScriptContext; | |
import javax.script.ScriptEngine; | |
import javax.script.ScriptEngineManager; | |
import javax.script.SimpleScriptContext; | |
public class Main { | |
public static void main(String[] args) { | |
ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); | |
ScriptEngine groovy = scriptEngineManager.getEngineByExtension("groovy"); | |
try (Reader reader = new FileReader(new File("engine.groovy"))) { | |
ScriptContext scriptContext = new SimpleScriptContext(); | |
Engine result = (Engine) groovy.eval(reader, scriptContext); | |
result.start(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do we handle pakage if, for exemple, Engine was in com.somePakage.Engine.java