Your SlideShare is downloading. ×
0
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Smoothing Your Java with DSLs
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Smoothing Your Java with DSLs

2,400

Published on

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

No Downloads
Views
Total Views
2,400
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
5
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Smoothing Your Java with DSLs Dmit ry Jemerov Development Lead JetBrains, Inc. ht tp://www.jetbrains.com/
  • 2. Learn about the multiple ways you can use domain-specific languages in your Java project to make your code more readable and maintainable
  • 3. Agenda <ul><li>DSL Overview </li></ul><ul><li>DSLs in Java </li></ul><ul><li>DSLs in Scripting Languages </li></ul><ul><li>JetBrains MPS </li></ul>
  • 4. What is a DSL? <ul><li>Computer programming language </li></ul><ul><li>Limited expressiveness </li></ul><ul><li>Domain focus </li></ul>
  • 5. DSL Classification <ul><li>External </li></ul>Regular Expressions SQL CSS
  • 6. DSL Classification <ul><li>External </li></ul><ul><ul><li>XML-based </li></ul></ul>Ant XSLT
  • 7. DSL Classification <ul><li>External </li></ul><ul><ul><li>XML-based </li></ul></ul><ul><li>Internal </li></ul>Query integration: Squill Testing: JMock, EasyMock
  • 8. DSL Classification <ul><li>External </li></ul><ul><ul><li>XML-based </li></ul></ul><ul><li>Internal </li></ul><ul><ul><li>Scripting </li></ul></ul>Build: Gradle, Buildr Testing: easyb UI: Glimmer, Monkeybars
  • 9. DSL Classification <ul><li>External </li></ul><ul><ul><li>XML-based </li></ul></ul><ul><li>Internal </li></ul><ul><ul><li>Scripting </li></ul></ul><ul><li>Language Workbench </li></ul>MPS “Oslo”
  • 10. Why Use DSLs? <ul><li>Raising level of abstraction </li></ul><ul><ul><li>Separating the “what” from the “how” </li></ul></ul><ul><li>Separating stable and frequently changing parts of the system </li></ul><ul><li>Enabling domain expert contributions </li></ul>
  • 11. DSLs: Code or Data? <ul><li>Depends on where you look from </li></ul><ul><ul><li>TrueType fonts: actually code for a rather powerful VM </li></ul></ul>
  • 12. Integrating DSLs <ul><li>Interpretation </li></ul><ul><ul><li>Model is built by executing internal DSL or parsing external DSL </li></ul></ul><ul><ul><li>Interpreter executes operations described by DSL </li></ul></ul><ul><li>Code Generation </li></ul><ul><ul><li>DSL drives generation of host language code </li></ul></ul><ul><ul><li>DSL-generated code compiled with rest of app </li></ul></ul>
  • 13. Agenda <ul><li>DSL Overview </li></ul><ul><li>DSLs in Java </li></ul><ul><li>DSLs in Scripting Languages </li></ul><ul><li>JetBrains MPS </li></ul>
  • 14. Squill <ul><li>squill .from(pets) .where(eq(pets.species, “Cat”)) .orderBy(asc(pets.name)) .selectList(pets.name, pets.birthDate)); </li></ul><ul><li>https://squill.dev.java.net/ </li></ul>
  • 15. EasyMock <ul><li>expect(mock.voteForRemoval(“A&quot;)) .andReturn((byte) 42) .atLeastOnce(); </li></ul><ul><li>http://www.easymock.org/ </li></ul>
  • 16. IntelliJ IDEA PSI Patterns
  • 17. Regular vs Fluent API <ul><li>Regular: a method call executes an operation </li></ul><ul><ul><li>object.verbNoun(param1, …, paramN) </li></ul></ul><ul><li>Fluent: a method call tells one thing about an operation that will be executed </li></ul><ul><ul><li>object.noun1(param1).noun2(param2)… </li></ul></ul>
  • 18. Fluent API Patterns <ul><li>Method chaining </li></ul><ul><li>Builder objects </li></ul><ul><li>Static factory methods </li></ul><ul><li>Static imports </li></ul><ul><li>Initializer blocks in anonymous classes </li></ul>
  • 19. Method Chaining <ul><li>class C { </li></ul><ul><li>C value1(Object v1) { </li></ul><ul><li>this.value1 = v1; </li></ul><ul><li>return this; </li></ul><ul><li>} </li></ul><ul><li>C value2(Object v2) { … } </li></ul><ul><li>} </li></ul><ul><li>c.value1(o1).value2(o2) </li></ul>
  • 20. Builder Objects <ul><li>class CBuilder { </li></ul><ul><li>CBuilder value1(Object v1) { … } </li></ul><ul><li>CBuilder value2(Object v2) { … } </li></ul><ul><li>C build() { </li></ul><ul><li>return new C(value1, value2); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  • 21. Factory Methods, Static Imports <ul><li>class CFactory { </li></ul><ul><li>static <T> C<T> newC() { </li></ul><ul><li>return new C<T>(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>import static CFactory.*; </li></ul><ul><li>C<String> c = newC(); </li></ul>
  • 22. Initializer Blocks (JMock) <ul><li>context.checking(new Expectations() {{ oneOf (subscriber).receive(message); }}); </li></ul>
  • 23. Java Internal DSL Advantages <ul><li>Low entry barrier, no external dependencies </li></ul><ul><li>Full IDE support (code completion etc.) </li></ul><ul><li>Strong typing and compile-time error checking </li></ul>
  • 24. Java Internal DSL Drawbacks <ul><li>Readability is not as good as with other solutions </li></ul>
  • 25. Agenda <ul><li>DSL Overview </li></ul><ul><li>DSLs in Java </li></ul><ul><li>DSLs in Scripting Languages </li></ul><ul><li>JetBrains MPS </li></ul>
  • 26. easyb <ul><li>given &quot;a stack with one pushed value&quot;, { stack = new Stack() pushVal = &quot;foo&quot; stack.push(pushVal) } when &quot;pop is called&quot;, { popVal = stack.pop() } then &quot;that object should be returned&quot;, { popVal.shouldBe pushVal } </li></ul><ul><li>http://easyb.org/ </li></ul>
  • 27. Glimmer <ul><li>shell { </li></ul><ul><li>text &quot;Contact Manager“ </li></ul><ul><li>composite { </li></ul><ul><li>layout GridLayout.new(2, false) </li></ul><ul><li>label { text &quot;First &Name: &quot;} </li></ul><ul><li>text { </li></ul><ul><li>text bind(presenter, :first_name) </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>http://rubyforge.org/projects/glimmer/ </li></ul>
  • 28. RubyMine Parameter Definitions <ul><li>define_params 'validates_presence_of' , migration_ref , [migration_ref , { :message => nil , :on => one_of(:save , :create , :update) , :if => method_ref( 'ActiveRecord::Base' ) }] </li></ul>
  • 29. Scripting Language Advantages <ul><li>Closures </li></ul><ul><li>Properties </li></ul><ul><li>List/map literals </li></ul><ul><li>Operator overloading </li></ul><ul><li>Handling of missing methods </li></ul>
  • 30. Integrating DSL Scripts <ul><li>Joint compilation </li></ul><ul><ul><li>Script statically compiled to bytecode </li></ul></ul><ul><ul><li>Allows direct calls of script code from Java </li></ul></ul><ul><li>Dynamic invocation </li></ul><ul><ul><li>Script invoked via javax.scripting or language-specific embedding API </li></ul></ul><ul><ul><li>Allows dynamic discovery of scripts </li></ul></ul><ul><ul><li>Allows modifying scripts without app restart or recompilation </li></ul></ul>
  • 31. Agenda <ul><li>DSL Overview </li></ul><ul><li>DSLs in Java </li></ul><ul><li>DSLs in Scripting Languages </li></ul><ul><li>JetBrains MPS </li></ul>
  • 32. MPS History <ul><li>Started as a research project back in 2004 </li></ul><ul><li>JetBrains issue tracker project being developed entirely in MPS </li></ul><ul><ul><li>Web framework </li></ul></ul><ul><ul><li>Data access framework </li></ul></ul><ul><ul><li>End-user functionality </li></ul></ul><ul><li>Released as open-source public beta in December 2008 </li></ul>
  • 33. Key Concepts of MPS <ul><li>Language defines a set of concepts and relationships between them </li></ul><ul><li>Models are instances of a particular concept </li></ul><ul><li>Editors define how models of a particular concept are represented in the MPS IDE </li></ul><ul><li>Generators define how models are translated into a different language </li></ul>
  • 34. What MPS doesn’t have <ul><li>No text representation for code in a particular language </li></ul><ul><li>No grammar for languages </li></ul><ul><li>No way to import code in existing text-based language </li></ul><ul><li>No dependency on specific output language generated </li></ul><ul><ul><li>Could be Java, JavaScript, CSS, XML… </li></ul></ul>
  • 35. Advanced MPS <ul><li>Reusing languages </li></ul><ul><ul><li>Business rules need formulas </li></ul></ul><ul><ul><li>Can reuse MPS expression language: editor, generation etc. </li></ul></ul><ul><li>Extending languages </li></ul><ul><ul><li>Closures, ‘using’ blocks, complex numbers… </li></ul></ul>
  • 36. Summary <ul><li>You can use the fluent programming style in Java to make your code easier to read and less verbose </li></ul><ul><li>DSLs built with scripting languages like Groovy and JRuby bring even further readability benefits </li></ul><ul><li>JetBrains MPS is a whole new approach to DSLs </li></ul>
  • 37. For More Information <ul><li>Martin Fowler’s DSL book: </li></ul><ul><ul><li>http://martinfowler.com/dslwip/ </li></ul></ul><ul><li>JetBrains MPS: </li></ul><ul><ul><li>http://www.jetbrains.com/mps/ </li></ul></ul>
  • 38. Q&A Dmitry Jemerov [email_address]
  • 39. Smoothing Your Java with DSLs Dmit ry Jemerov Development Lead JetBrains, Inc. ht tp://www.jetbrains.com/

×