Scripting In Java

480 views

Published on

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.

Published in: Software
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
480
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Scripting In Java

  1. 1. Scripting in Java JavaLand 2014 25.03.2014, Lars Gregori
  2. 2. Scripting in Java JavaLand 2014 25.03.2014, Lars Gregori
  3. 3. © 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
  4. 4. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 4
  5. 5. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 5
  6. 6. © 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
  7. 7. © 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
  8. 8. © 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
  9. 9. © 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
  10. 10. © 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
  11. 11. © 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
  12. 12. © 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
  13. 13. © 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
  14. 14. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 14 Motivation
  15. 15. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 Motivation  cool  mal etwas anderes  macht Spass  JATUMBA 15
  16. 16. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 Motivation  elegant und einfach erweitern  Konfigurationsdatei  Datenbank 16
  17. 17. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 Motivation  elegant und einfach erweitern  Konfigurationsdatei  Datenbank 17
  18. 18. © 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
  19. 19. © 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
  20. 20. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 Motivation  selbe Technologie Frontend und Backend  Javascript Validierung 20
  21. 21. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 Motivation  selbe Technologie Frontend und Backend  Javascript Validierung 21
  22. 22. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 Motivation  langsamer als Java  Entwickler muss andere Sprache lernen 22
  23. 23. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 Motivation  langsamer als Java  Entwickler muss andere Sprache lernen 23
  24. 24. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 24
  25. 25. © 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
  26. 26. © 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
  27. 27. © 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();
  28. 28. © 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
  29. 29. © 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
  30. 30. © 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());
  31. 31. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 31
  32. 32. © 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
  33. 33. © 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"/>
  34. 34. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 JSR-223 – script engines 34
  35. 35. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 35
  36. 36. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 Beispiele  JRuby  Groovy  Clojure 36
  37. 37. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 37
  38. 38. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 JDK 8 – Was ist neu?  Nashorn  Lambda  … 38
  39. 39. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 39
  40. 40. © 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
  41. 41. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 Nashorn 41
  42. 42. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 42
  43. 43. © 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
  44. 44. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 44
  45. 45. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 45
  46. 46. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 invokedynamic  JavaScript  4 + 2  4 + “hallo”   add(a, b) 46
  47. 47. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014  http://niklasschlimm.blogspot.de/2012/02/java-7-complete- invokedynamic-example.html 47
  48. 48. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014  http://niklasschlimm.blogspot.de/2012/02/java-7-complete- invokedynamic-example.html 48
  49. 49. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 49
  50. 50. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 Invokedynamic – Jetzt und Zukunft?  JRuby  Groovy  groovy-indy  Clojure? 50
  51. 51. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 51
  52. 52. © 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
  53. 53. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 53
  54. 54. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 54
  55. 55. © 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
  56. 56. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 56
  57. 57. © 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
  58. 58. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 58
  59. 59. Wir bedanken uns für Ihre Aufmerksamkeit! msgGillardon AG Robert-Bürkle-Str. 1 85737 Ismaning > www.msg-gillardon.de
  60. 60. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 60
  61. 61. © 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)
  62. 62. © 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
  63. 63. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 63
  64. 64. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 64
  65. 65. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 65
  66. 66. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 66
  67. 67. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 67
  68. 68. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 68 JSR 223: https://code.google.com/p/clojure-jsr223/
  69. 69. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 69
  70. 70. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 70
  71. 71. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 71
  72. 72. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 72
  73. 73. © 2014 Lars Gregori, msgGillardon AG | Scripting in Java | 25.03.2014 73

×