Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Scripting in Java

Scripting in Java

Anhand der im JDK 8 bestehenden neuen JavaScript-Engine Nashorn werden die Vorteile des JSR 292 (Supporting Dynamically Typed Languages on the Java Platform) für Skriptsprachen auf der JVM aufgezeigt und mit welchen Performanzsteigerungen diese in Zukunft innerhalb von Java rechnen können. Neben einer Einführung in die Lambda Expressions, die ebenfalls Einzug in das JDK 8 haben und bereits Bestandteil einiger Sprachen ist, werden weitere spezielle Sprachkonstrukte von bestimmten Skriptsprachen betrachtet. Einfache Code-Beispiele zu den jeweiligen Sprache, die auf der JVM laufen, heben ihre Vorteile gegenüber Java hervor. Neben JavaScript (Nashorn) werden Beispiele in Groovy, JRuby und Clojure gezeigt.

Lars Gregori

March 25, 2014
Tweet

More Decks by Lars Gregori

Other Decks in Programming

Transcript

  1. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 Lars Gregori  msgGillardon AG  CoC IT-Architekturen  learn a new language every year --- The Pragmatic Programmer 3
  2. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 Was sind Scriptsprachen? Wikipedia: „Scriptsprachen sind Programmiersprachen, die vor allem für kleine, überschaubare Programme gedacht sind. Sie verzichten oft auf bestimmte Sprachelemente, deren Nutzen erst bei der Bearbeitung größerer Projekte zum Tragen kommen. So wird etwa in Skriptsprachen auf den Deklarationszwang von Variablen meist verzichtet - vorteilhaft zur schnellen Erstellung von kleinen Programmen (siehe auch Prototyping), bei großen hingegen von Nachteil, etwa wegen der fehlenden Überprüfungsmöglichkeit von Tippfehlern in Variablennamen.“ 6 http://de.wikipedia.org/wiki/Skriptsprache
  3. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 Was sind Scriptsprachen? Wikipedia: „Scriptsprachen sind Programmiersprachen, die vor allem für kleine, überschaubare Programme gedacht sind. Sie verzichten oft auf bestimmte Sprachelemente, deren Nutzen erst bei der Bearbeitung größerer Projekte zum Tragen kommen. So wird etwa in Skriptsprachen auf den Deklarationszwang von Variablen meist verzichtet - vorteilhaft zur schnellen Erstellung von kleinen Programmen (siehe auch Prototyping), bei großen hingegen von Nachteil, etwa wegen der fehlenden Überprüfungsmöglichkeit von Tippfehlern in Variablennamen.“ 7 http://de.wikipedia.org/wiki/Skriptsprache
  4. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 Static vs. Dynamic Typing  Most scripting languages are dynamically typed  explicit type declarations not required  type information is attached to values, not to variables  Java is static-typed  require variable type (declaration time)  only data of declared type 8 https://confluence.deri.ie:8443/display/romulus/Scripting+Languages+and+java
  5. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 Weak vs. Strong Typing  Java is a static, strongly typed language  strongest possible constraint on the type of object at declaration time  prevents mixing operations between mismatched types  Many scripting languages are weakly typed  allow operations on incompatible types  implicit type conversion or ad-hoc polymorphism 9 https://confluence.deri.ie:8443/display/romulus/Scripting+Languages+and+java
  6. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 Weak vs. Strong Typing  Java is a static, strongly typed language  strongest possible constraint on the type of object at declaration time  prevents mixing operations between mismatched types  Many scripting languages are weakly typed  allow operations on incompatible types  implicit type conversion or ad-hoc polymorphism 10 https://confluence.deri.ie:8443/display/romulus/Scripting+Languages+and+java
  7. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 Quality and Performance  Scripting languages are more compact and readable  less lines of code  weak typing not requiring the overhead of type declaration  Fewer lines of code and less complexity means lower amounts of bugs, thus reducing development and maintenance costs.  The missing type information has some disadvantages.  static, strongly typed languages ensure the robustness  type errors will be detected at compile time 11 https://confluence.deri.ie:8443/display/romulus/Scripting+Languages+and+java
  8. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 Quality and Performance  runtime performance  extra overhead of the interpreter and runtime checks  not performed at compile time 12 http://attractivechaos.github.io/plb/ https://confluence.deri.ie:8443/display/romulus/Scripting+Languages+and+java
  9. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 Quality and Performance  runtime performance  extra overhead of the interpreter and runtime checks  not performed at compile time 13 http://attractivechaos.github.io/plb/ https://confluence.deri.ie:8443/display/romulus/Scripting+Languages+and+java
  10. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 Motivation  cool  mal etwas anderes  macht Spass  JATUMBA 15
  11. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 Motivation  elegant und einfach erweitern  Konfigurationsdatei  Datenbank 16
  12. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 Motivation  elegant und einfach erweitern  Konfigurationsdatei  Datenbank 17
  13. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014  passender  Inner DSL (JRuby)  String Manipulation (Jython, Perl)  map / reduce (Lisp, Clojure)  GroovySQL, GroovyMarkup  Templates Motivation 18
  14. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014  passender  Inner DSL (JRuby)  String Manipulation (Jython, Perl)  map / reduce (Lisp, Clojure)  GroovySQL, GroovyMarkup  Templates Motivation 19
  15. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 Motivation  selbe Technologie Frontend und Backend  Javascript Validierung 20
  16. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 Motivation  selbe Technologie Frontend und Backend  Javascript Validierung 21
  17. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 Motivation  langsamer als Java  Entwickler muss andere Sprache lernen 22
  18. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 Motivation  langsamer als Java  Entwickler muss andere Sprache lernen 23
  19. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 Buch 25 http://www.scriptinginjava.net/ Scripting in Java: Languages, Frameworks, and Patterns by Dejan Bosanac Publisher: Addison Wesley Professional Pub Date: August 31, 2007 (est.) Print ISBN-10: 0-321-32193-6 Print ISBN-13: 978-0-321-32193-0 eText ISBN-10: 0-321-50933-1 eText ISBN-13: 978-0-321-50933-8 Pages: 432
  20. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 Scriptsprachen in Java einbinden  kompilieren  eigene Engine  Bean Scripting Framework (BSF)  JSR 223 – Scripting for the Java Platform 26
  21. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 kompilieren – groovyc 27 package my.groovy.jartest class Answer { def answer() { 42 } } package my.groovy.jartest; import groovy.lang.GroovyObject; public class Answer implements GroovyObject { ... public Object answer() { CallSite[] arrayOfCallSite = $getCallSiteArray(); return Integer.valueOf(42); return null; } } import my.groovy.jartest; ... Answer answer = new Answer(); int a = (Integer) answer.answer();
  22. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 Script Engine 28 GroovyShell gs = new GroovyShell(); String script = "return 42"; int answer = (Integer) gs.evaluate(script); Binding binding = new Binding(); binding.setVariable("foo", new Integer(2)); GroovyShell shell = new GroovyShell(binding); Object value = shell.evaluate( "println 'Hello World!'; x = 123; return foo * 10"); assert value.equals(new Integer(20)); assert binding.getVariable("x").equals(new Integer(123)); http://groovy.codehaus.org/Embedding+Groovy
  23. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 Bean Scripting Framework (BSF)  http://commons.apache.org/proper/commons-bsf/  Bean Scripting Framework (BSF) is a set of Java classes which provides scripting language support within Java applications, and access to Java objects and methods from scripting languages. 29
  24. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 Bean Scripting Framework (BSF) – Beispiel 30 BSFManager manager = new BSFManager(); manager.declareBean("a", 6, Integer.class); manager.declareBean("b", 7, Integer.class); String script = "var answer = a * b;" + "bsf.registerBean(\"answer\", answer)"; manager.eval("javascript", "blah", 0, 0, script); Double answer = (Double) manager.lookupBean("answer"); assertEquals(42, answer.intValue());
  25. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 JSR-223 – Scripting for the Java Platform  The specification describe mechanisms allowing scripting language programs to access information developed in the Java Platform … https://jcp.org/en/jsr/detail?id=223  Java 1.6+  Rhino JavaScript for Java version 1.6R2  javax.script.*  jrunscript  http://docs.oracle.com/javase/6/docs/technotes/tools/share/jrunscript.html 32
  26. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 ServiceLoader  Since: 1.6  http://docs.oracle.com/javase/6/docs/api/java/util/ServiceLoader.html  META-INF/services/javax.script.ScriptEngineFactory  This file contains the single line: de.torq.clojure.jsr223.ClojureScriptEngineFactory  Clojure JSR 223 build.xml  https://github.com/pmf/clojure-jsr223/blob/master/build.xml 33 <jar jarfile="${clojure_jsr223_bundle}" basedir="${build_osgi}"> <service type="javax.script.ScriptEngineFactory“ provider="de.torq.clojure.jsr223.ClojureScriptEngineFactory"/>
  27. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 JSR-223 – script engines 34
  28. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 Beispiele  JRuby  Groovy  Clojure 36
  29. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 JDK 8 – Was ist neu?  Nashorn  Lambda  … 38
  30. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 Nashorn  JEP 174  http://openjdk.java.net/jeps/174  ECMAScript-262 Edition 5.1  http://ecma-international.org/ecma-262/5.1/  javax.script (JSR 223) 40
  31. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 Lambda  JSR 335  Lambda Expressions for the Java Programming Language  https://jcp.org/en/jsr/detail?id=335  Final Specification 43
  32. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 invokedynamic  JavaScript  4 + 2  4 + “hallo”   add(a, b) 46
  33. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014  http://niklasschlimm.blogspot.de/2012/02/java-7-complete- invokedynamic-example.html 47
  34. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014  http://niklasschlimm.blogspot.de/2012/02/java-7-complete- invokedynamic-example.html 48
  35. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 Invokedynamic – Jetzt und Zukunft?  JRuby  Groovy  groovy-indy  Clojure? 50
  36. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 Bytecode  Bytecode Frameworks  ASM : http://asm.ow2.org/  Javassist : http://www.csg.ci.i.u-tokyo.ac.jp/~chiba/javassist/  Jitescript : https://github.com/qmx/jitescript  … 52
  37. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 Da Vinci Machine  a multi-language renaissance for the Java Virtual Machine architecture  http://openjdk.java.net/projects/mlvm/  dynamic invocation  continuations  tail-calls  interface injection 55
  38. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 Credits & making of  Fotos  Anja Gregori  Font  Chopin Script by ClaudeP  http://www.dafont.com/chopin-script.font  Eclipse Plugin  Bytecode Outline plugin for Eclipse  http://andrei.gmxhome.de/bytecode/ 57
  39. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 Range Kata  http://codingdojo.org/cgi-bin/wiki.pl?KataRange 61 integer range contains [2,6) contains {2,4} [2,6) doesn't contain {-1,1,6,10} getAllPoints? [2,6) allPoints = {2,3,4,5} ContainsRange? [2,5) doesn't contain [7,10) [2,5) doesn't contain [3,10) [3,5) doesn't contain [2,10) [2,10) contains [3,5] [3,5] contains [3,5) endPoints [2,6) allPoints = {2,3,4,5} [2,6] allPoints = {2,3,4,5,6} (2,6) allPoints = {3,4,5} (2,6] allPoints = {3,4,5,6} overlapsRange [2,5) doesn't overlap with [7,10) [2,10) overlaps with [3,5) [3,5) overlaps with [3,5) [2,5) overlaps with [3,10) [3,5) overlaps with [2,10) Equals [3,5) equals [3,5) [2,10) neq [3,5) [2,5) neq [3,10) [3,5) neq [2,10)
  40. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 List of freely available programming books  http://stackoverflow.com/questions/194812/list-of-freely-available- programming-books/392926#392926 62
  41. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java

    | 25.03.2014 68 JSR 223: https://code.google.com/p/clojure-jsr223/