SlideShare a Scribd company logo
1 of 51
Embedded Typesafe DSLs for Java Jevgeni Kabanov Tech Lead, ZeroTurnaround  jevgeni@zeroturnaround.com
ABOUT ME ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Based on ,[object Object],[object Object],[object Object]
OUTLINE ,[object Object],[object Object],[object Object]
Domain Specific Language ,[object Object],[object Object],[object Object],[object Object]
Why bother? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Fluent Interface ,[object Object]
Java 5 features and EDSLs ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
BUILDING SQL QUERIES
SQL Example (4 errors) StringBuffer sql =  new   StringBuffer() ; sql.append( "SELECT o.sum,(SELECT first_name,last_name" ); sql.append( "  FROM person p" ); sql.append( "  WHERE o.person_id=p.id) AS client" ); sql.append( " FROM order o" ); sql.append( "WHERE o.id = " +orderId); sql.append( "  AND o.status_code IN (?,?)" ); PreparedStatement stmt = conn.prepareStatement(sql.toString()); stmt.setString(1,  "PAID" ); //...
Typesafe SQL Example Person p =  new   Person() ;   List<Tuple3<String, Integer, Date>> rows = new   QueryBuilder(datasource) .from(p) .where( gt(p. height , 170)) .select(p. name , p. height , p. birthday ) .list(); for   (Tuple3<String, Integer, Date> row : rows) { String name = row. v1 ; Integer height = row. v2 ; Date birthday = row. v3 ; System. out .println( name +  &quot; &quot;  + height +  &quot; &quot;  + birthday); }
Tuples ,[object Object],[object Object]
Tuple2 public   class   Tuple2<T1, T2>  implements   Tuple { public   final   T1  v1 ; public   final   T2  v2 ; public   Tuple2(T1 v1, T2 v2) { this . v1  = v1; this . v2   = v2; } public   T1 v1() {  return   v1 ; } public   T2 v2() {  return   v2 ; } }
Typesafe SQL Example Person p =  new   Person() ;   List<Tuple3<String, Integer, Date>> rows = new   QueryBuilder(datasource) .from(p) .where( gt(p. height , 170)) .select(p. name , p. height , p. birthday ) .list(); for   (Tuple3<String, Integer, Date> row : rows) { String name = row. v1 ; Integer height = row. v2 ; Date birthday = row. v3 ; System. out .println( name +  &quot; &quot;  + height +  &quot; &quot;  + birthday); }
Typesafe Metadata ,[object Object],[object Object]
Metadata dictionary public   class   Person   implements   Table   { public   String   getName() {   return   &quot;person&quot; ;  } ; public   Column<Person, String> name = new   Column<Person, String>( this ,  &quot;name&quot; , String . class ) ; public   Column<Person, Integer> height = new   Column<Person, Integer>( this ,  &quot;height&quot; , Integer. class ) ; public   Column<Person, Date> birthday = new   Column<Person, Date>( this ,  &quot;birthday&quot; , Date. class ) ; }
Typesafe SQL Example Person p =  new   Person() ;   List<Tuple3<String, Integer, Date>> rows = new   QueryBuilder(datasource) .from(p) .where( gt(p. height , 170)) .select(p. name , p. height , p. birthday ) .list(); for   (Tuple3<String, Integer, Date> row : rows) { String name = row. v1 ; Integer height = row. v2 ; Date birthday = row. v3 ; System. out .println( name +  &quot; &quot;  + height +  &quot; &quot;  + birthday); }
Restricting Syntax ,[object Object],[object Object],[object Object],[object Object]
QueryBuilder class   QueryBuilder   extends   Builder   { ... <T  extends   Table> FromBuilder<T> from(T table); }
FromBuilder class   FromBuilder<T  extends  Table> extends   Builder { ... <C1> SelectBuilder1<T, C1> select(Col<T, C1> c1); <C1, C2> SelectBuilder2<T, C1, C2> select(Col<T, C1> c1, Col<T, C2> c2); ... }
SelectBuilder class   SelectBuilder2<T   extends   Table,C1,C2>  extends   SelectBuilder<T> { ... List<Tuple2<C1,C2>> list(); ... }
Typesafe SQL Example Person p =  new   Person() ;   List<Tuple3<String, Integer, Date>> rows = new   QueryBuilder(datasource) .from(p) .where( gt(p. height , 170)) .select(p. name , p. height , p. birthday ) .list(); for   (Tuple3<String, Integer, Date> row : rows) { String name = row. v1 ; Integer height = row. v2 ; Date birthday = row. v3 ; System. out .println( name +  &quot; &quot;  + height +  &quot; &quot;  + birthday); }
Hierarchical Expressions ,[object Object],[object Object],[object Object],or( eq(p.name, &quot;Peter&quot;), gt(p.height, 170) )
Expression public   interface   Expression<E> { String getSqlString(); List<Object> getSqlArguments(); Class<E> getType(); }
Expressions class   Expressions { Expr<Bool> and(Expr<Bool>... e) <E> Expr<Bool> eq(Expr<E> e1, Expr<E> e2) Expr<Bool> like(Expr<?> e, Expr<String> pattern) <E> Expr<E> constant(E value) Expr<String> concat(Expr... e) ... }
Closures interface   Closure   {   void   apply(Builder b); } class   SelectBuilderC2<C1,C2> extends   SelectBuilder { SelectBuilderC2<C1,C2> closure(Closure closure) { closure.apply( this ); return   this ; } } }
Unsafe Assumptions ,[object Object],Expression<Integer> count = unchecked(Integer. class , &quot;util.countChildren(id)&quot; );
Used Patterns ,[object Object],[object Object],[object Object],[object Object],[object Object]
Case Study 2: Typesafe Bytecode
Java  Class Definition Modifiers, name, super class, interfaces Enclosing class reference Annotation* Inner class* Name Field* Modifiers, name, type Annotation* Method* Modifiers, name, return and parameter types Annotation* Compiled code
Java Execution Model Local variables Operand stack Execution stack Frame Calling a method Throwing an exception
Instruction Example opcode argumen ts apply Operands stack when applying the instruction : Instruction :
Bytecode Engineering ,[object Object],[object Object],[object Object],[object Object],[object Object]
Hello, World! public   class   HelloWorld { public   static   void   main(String[] args) { System. out .println( &quot;Hello, World!&quot; ); } }
Hello, World! in Bytecode public   class   HelloWorld { public   <init>()V ALOAD 0 INVOKESPECIAL  Object.<init>()V RETURN public   static   main([LString;)V GETSTATIC System.out : LPrintStream; LDC  &quot;Hello, World!&quot; INVOKEVIRTUAL PrintStream.println(LString;)V RETURN }
Hello, World! in ASM ClassWriter cw =  new   ClassWriter(0); MethodVisitor  mv; cw.visit( V1_6 ,  ACC_PUBLIC  +  ACC_SUPER ,  &quot;HelloWorld&quot; ,  null ,  &quot;java/lang/Object&quot; ,  null ); { mv = cw.visitMethod( ACC_PUBLIC  +  ACC_STATIC ,  &quot;main&quot; ,  &quot;([Ljava/lang/String;)V&quot; ,  null ,  null ); mv.visitCode(); mv.visitFieldInsn( GETSTATIC ,  &quot;java/lang/System&quot; ,  &quot;out&quot; ,  &quot;Ljava/io/PrintStream;&quot; ); mv.visitLdcInsn( &quot;Hello, World!&quot; ); mv.visitMethodInsn( INVOKEVIRTUAL ,  &quot;java/io/PrintStream&quot; ,  &quot;println&quot; ,  &quot;(Ljava/lang/String;)V&quot; ); mv.visitInsn( RETURN ); mv.visitMaxs(2, 1); mv.visitEnd(); } cw.visitEnd();
Hello, World! in DSL
Possible mistakes ,[object Object],[object Object],[object Object],[object Object]
Similar  Patterns ,[object Object],[object Object]
Different Patterns ,[object Object],[object Object],[object Object],[object Object],[object Object]
Tracking stack ,[object Object],[object Object],[object Object]
Tracking stack
Hello, World! in DSL
Type History ,[object Object],[object Object]
Tracking stack types
Tracking stack types
Hello, World! in DSL
Unsafe Assumptions ,[object Object]
Unsafe Assumptions
Reusable genSayHello() private   static   void   genSayHello(MethodBuilder0 mb) { mb .assumePush(String. class ) .getStatic(System. class ,  &quot;out&quot; ,  PrintStream . class ) .swap()  .invoke()   .param(String. class ) .virtVoid(PrintStream. class ,  &quot;println&quot; );  }
Questions

More Related Content

What's hot

JavaScript - Chapter 4 - Types and Statements
 JavaScript - Chapter 4 - Types and Statements JavaScript - Chapter 4 - Types and Statements
JavaScript - Chapter 4 - Types and StatementsWebStackAcademy
 
JavaScript Arrays
JavaScript Arrays JavaScript Arrays
JavaScript Arrays Reem Alattas
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesGanesh Samarthyam
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to JavascriptAnjan Banda
 
Contracts in-clojure-pete
Contracts in-clojure-peteContracts in-clojure-pete
Contracts in-clojure-petejessitron
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesGanesh Samarthyam
 
JavaScript Functions
JavaScript Functions JavaScript Functions
JavaScript Functions Reem Alattas
 
DConf 2016 std.database (a proposed interface & implementation)
DConf 2016 std.database (a proposed interface & implementation)DConf 2016 std.database (a proposed interface & implementation)
DConf 2016 std.database (a proposed interface & implementation)cruisercoder
 
Clean code _v2003
 Clean code _v2003 Clean code _v2003
Clean code _v2003R696
 
Functional Principles for OO Developers
Functional Principles for OO DevelopersFunctional Principles for OO Developers
Functional Principles for OO Developersjessitron
 
JavaScript Tutorial
JavaScript  TutorialJavaScript  Tutorial
JavaScript TutorialBui Kiet
 
Introduction to web programming with JavaScript
Introduction to web programming with JavaScriptIntroduction to web programming with JavaScript
Introduction to web programming with JavaScriptT11 Sessions
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to JavascriptAmit Tyagi
 
Charles Sharp: Java 8 Streams
Charles Sharp: Java 8 StreamsCharles Sharp: Java 8 Streams
Charles Sharp: Java 8 Streamsjessitron
 
Clean Code: Chapter 3 Function
Clean Code: Chapter 3 FunctionClean Code: Chapter 3 Function
Clean Code: Chapter 3 FunctionKent Huang
 
C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8Christian Nagel
 

What's hot (20)

JavaScript - Chapter 4 - Types and Statements
 JavaScript - Chapter 4 - Types and Statements JavaScript - Chapter 4 - Types and Statements
JavaScript - Chapter 4 - Types and Statements
 
JavaScript Arrays
JavaScript Arrays JavaScript Arrays
JavaScript Arrays
 
Linq intro
Linq introLinq intro
Linq intro
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on Examples
 
Javascript basics
Javascript basicsJavascript basics
Javascript basics
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to Javascript
 
Contracts in-clojure-pete
Contracts in-clojure-peteContracts in-clojure-pete
Contracts in-clojure-pete
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional Interfaces
 
JavaScript Functions
JavaScript Functions JavaScript Functions
JavaScript Functions
 
DConf 2016 std.database (a proposed interface & implementation)
DConf 2016 std.database (a proposed interface & implementation)DConf 2016 std.database (a proposed interface & implementation)
DConf 2016 std.database (a proposed interface & implementation)
 
Clean code _v2003
 Clean code _v2003 Clean code _v2003
Clean code _v2003
 
Javascript
JavascriptJavascript
Javascript
 
Functional Principles for OO Developers
Functional Principles for OO DevelopersFunctional Principles for OO Developers
Functional Principles for OO Developers
 
JavaScript Tutorial
JavaScript  TutorialJavaScript  Tutorial
JavaScript Tutorial
 
Introduction to web programming with JavaScript
Introduction to web programming with JavaScriptIntroduction to web programming with JavaScript
Introduction to web programming with JavaScript
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 
Charles Sharp: Java 8 Streams
Charles Sharp: Java 8 StreamsCharles Sharp: Java 8 Streams
Charles Sharp: Java 8 Streams
 
Clean code
Clean codeClean code
Clean code
 
Clean Code: Chapter 3 Function
Clean Code: Chapter 3 FunctionClean Code: Chapter 3 Function
Clean Code: Chapter 3 Function
 
C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8
 

Similar to Embedded Typesafe Domain Specific Languages for Java

Micro-ORM Introduction - Don't overcomplicate
Micro-ORM Introduction - Don't overcomplicateMicro-ORM Introduction - Don't overcomplicate
Micro-ORM Introduction - Don't overcomplicateKiev ALT.NET
 
Groovy Introduction - JAX Germany - 2008
Groovy Introduction - JAX Germany - 2008Groovy Introduction - JAX Germany - 2008
Groovy Introduction - JAX Germany - 2008Guillaume Laforge
 
Executing Sql Commands
Executing Sql CommandsExecuting Sql Commands
Executing Sql Commandsphanleson
 
Executing Sql Commands
Executing Sql CommandsExecuting Sql Commands
Executing Sql Commandsleminhvuong
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharpg_hemanth17
 
Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1Sachin Singh
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharpRaga Vahini
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharpSatish Verma
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharpsinghadarsh
 
Introduction to CSharp
Introduction to CSharpIntroduction to CSharp
Introduction to CSharpMody Farouk
 
Working effectively with legacy code
Working effectively with legacy codeWorking effectively with legacy code
Working effectively with legacy codeShriKant Vashishtha
 
Javazone 2010-lift-framework-public
Javazone 2010-lift-framework-publicJavazone 2010-lift-framework-public
Javazone 2010-lift-framework-publicTimothy Perrett
 
Java Intro
Java IntroJava Intro
Java Introbackdoor
 
30 5 Database Jdbc
30 5 Database Jdbc30 5 Database Jdbc
30 5 Database Jdbcphanleson
 
Introduction To Groovy 2005
Introduction To Groovy 2005Introduction To Groovy 2005
Introduction To Groovy 2005Tugdual Grall
 

Similar to Embedded Typesafe Domain Specific Languages for Java (20)

PostThis
PostThisPostThis
PostThis
 
Micro-ORM Introduction - Don't overcomplicate
Micro-ORM Introduction - Don't overcomplicateMicro-ORM Introduction - Don't overcomplicate
Micro-ORM Introduction - Don't overcomplicate
 
TechTalk - Dotnet
TechTalk - DotnetTechTalk - Dotnet
TechTalk - Dotnet
 
Groovy Introduction - JAX Germany - 2008
Groovy Introduction - JAX Germany - 2008Groovy Introduction - JAX Germany - 2008
Groovy Introduction - JAX Germany - 2008
 
Executing Sql Commands
Executing Sql CommandsExecuting Sql Commands
Executing Sql Commands
 
Executing Sql Commands
Executing Sql CommandsExecuting Sql Commands
Executing Sql Commands
 
Coding Ajax
Coding AjaxCoding Ajax
Coding Ajax
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharp
 
Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
 
Introduction to CSharp
Introduction to CSharpIntroduction to CSharp
Introduction to CSharp
 
Coding Ajax
Coding AjaxCoding Ajax
Coding Ajax
 
Working effectively with legacy code
Working effectively with legacy codeWorking effectively with legacy code
Working effectively with legacy code
 
Javazone 2010-lift-framework-public
Javazone 2010-lift-framework-publicJavazone 2010-lift-framework-public
Javazone 2010-lift-framework-public
 
Java Intro
Java IntroJava Intro
Java Intro
 
30 5 Database Jdbc
30 5 Database Jdbc30 5 Database Jdbc
30 5 Database Jdbc
 
Introduction To Groovy 2005
Introduction To Groovy 2005Introduction To Groovy 2005
Introduction To Groovy 2005
 
My java file
My java fileMy java file
My java file
 

Recently uploaded

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 

Recently uploaded (20)

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 

Embedded Typesafe Domain Specific Languages for Java

  • 1. Embedded Typesafe DSLs for Java Jevgeni Kabanov Tech Lead, ZeroTurnaround jevgeni@zeroturnaround.com
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 10. SQL Example (4 errors) StringBuffer sql = new StringBuffer() ; sql.append( &quot;SELECT o.sum,(SELECT first_name,last_name&quot; ); sql.append( &quot; FROM person p&quot; ); sql.append( &quot; WHERE o.person_id=p.id) AS client&quot; ); sql.append( &quot; FROM order o&quot; ); sql.append( &quot;WHERE o.id = &quot; +orderId); sql.append( &quot; AND o.status_code IN (?,?)&quot; ); PreparedStatement stmt = conn.prepareStatement(sql.toString()); stmt.setString(1, &quot;PAID&quot; ); //...
  • 11. Typesafe SQL Example Person p = new Person() ; List<Tuple3<String, Integer, Date>> rows = new QueryBuilder(datasource) .from(p) .where( gt(p. height , 170)) .select(p. name , p. height , p. birthday ) .list(); for (Tuple3<String, Integer, Date> row : rows) { String name = row. v1 ; Integer height = row. v2 ; Date birthday = row. v3 ; System. out .println( name + &quot; &quot; + height + &quot; &quot; + birthday); }
  • 12.
  • 13. Tuple2 public class Tuple2<T1, T2> implements Tuple { public final T1 v1 ; public final T2 v2 ; public Tuple2(T1 v1, T2 v2) { this . v1 = v1; this . v2 = v2; } public T1 v1() { return v1 ; } public T2 v2() { return v2 ; } }
  • 14. Typesafe SQL Example Person p = new Person() ; List<Tuple3<String, Integer, Date>> rows = new QueryBuilder(datasource) .from(p) .where( gt(p. height , 170)) .select(p. name , p. height , p. birthday ) .list(); for (Tuple3<String, Integer, Date> row : rows) { String name = row. v1 ; Integer height = row. v2 ; Date birthday = row. v3 ; System. out .println( name + &quot; &quot; + height + &quot; &quot; + birthday); }
  • 15.
  • 16. Metadata dictionary public class Person implements Table { public String getName() { return &quot;person&quot; ; } ; public Column<Person, String> name = new Column<Person, String>( this , &quot;name&quot; , String . class ) ; public Column<Person, Integer> height = new Column<Person, Integer>( this , &quot;height&quot; , Integer. class ) ; public Column<Person, Date> birthday = new Column<Person, Date>( this , &quot;birthday&quot; , Date. class ) ; }
  • 17. Typesafe SQL Example Person p = new Person() ; List<Tuple3<String, Integer, Date>> rows = new QueryBuilder(datasource) .from(p) .where( gt(p. height , 170)) .select(p. name , p. height , p. birthday ) .list(); for (Tuple3<String, Integer, Date> row : rows) { String name = row. v1 ; Integer height = row. v2 ; Date birthday = row. v3 ; System. out .println( name + &quot; &quot; + height + &quot; &quot; + birthday); }
  • 18.
  • 19. QueryBuilder class QueryBuilder extends Builder { ... <T extends Table> FromBuilder<T> from(T table); }
  • 20. FromBuilder class FromBuilder<T extends Table> extends Builder { ... <C1> SelectBuilder1<T, C1> select(Col<T, C1> c1); <C1, C2> SelectBuilder2<T, C1, C2> select(Col<T, C1> c1, Col<T, C2> c2); ... }
  • 21. SelectBuilder class SelectBuilder2<T extends Table,C1,C2> extends SelectBuilder<T> { ... List<Tuple2<C1,C2>> list(); ... }
  • 22. Typesafe SQL Example Person p = new Person() ; List<Tuple3<String, Integer, Date>> rows = new QueryBuilder(datasource) .from(p) .where( gt(p. height , 170)) .select(p. name , p. height , p. birthday ) .list(); for (Tuple3<String, Integer, Date> row : rows) { String name = row. v1 ; Integer height = row. v2 ; Date birthday = row. v3 ; System. out .println( name + &quot; &quot; + height + &quot; &quot; + birthday); }
  • 23.
  • 24. Expression public interface Expression<E> { String getSqlString(); List<Object> getSqlArguments(); Class<E> getType(); }
  • 25. Expressions class Expressions { Expr<Bool> and(Expr<Bool>... e) <E> Expr<Bool> eq(Expr<E> e1, Expr<E> e2) Expr<Bool> like(Expr<?> e, Expr<String> pattern) <E> Expr<E> constant(E value) Expr<String> concat(Expr... e) ... }
  • 26. Closures interface Closure { void apply(Builder b); } class SelectBuilderC2<C1,C2> extends SelectBuilder { SelectBuilderC2<C1,C2> closure(Closure closure) { closure.apply( this ); return this ; } } }
  • 27.
  • 28.
  • 29. Case Study 2: Typesafe Bytecode
  • 30. Java Class Definition Modifiers, name, super class, interfaces Enclosing class reference Annotation* Inner class* Name Field* Modifiers, name, type Annotation* Method* Modifiers, name, return and parameter types Annotation* Compiled code
  • 31. Java Execution Model Local variables Operand stack Execution stack Frame Calling a method Throwing an exception
  • 32. Instruction Example opcode argumen ts apply Operands stack when applying the instruction : Instruction :
  • 33.
  • 34. Hello, World! public class HelloWorld { public static void main(String[] args) { System. out .println( &quot;Hello, World!&quot; ); } }
  • 35. Hello, World! in Bytecode public class HelloWorld { public <init>()V ALOAD 0 INVOKESPECIAL Object.<init>()V RETURN public static main([LString;)V GETSTATIC System.out : LPrintStream; LDC &quot;Hello, World!&quot; INVOKEVIRTUAL PrintStream.println(LString;)V RETURN }
  • 36. Hello, World! in ASM ClassWriter cw = new ClassWriter(0); MethodVisitor mv; cw.visit( V1_6 , ACC_PUBLIC + ACC_SUPER , &quot;HelloWorld&quot; , null , &quot;java/lang/Object&quot; , null ); { mv = cw.visitMethod( ACC_PUBLIC + ACC_STATIC , &quot;main&quot; , &quot;([Ljava/lang/String;)V&quot; , null , null ); mv.visitCode(); mv.visitFieldInsn( GETSTATIC , &quot;java/lang/System&quot; , &quot;out&quot; , &quot;Ljava/io/PrintStream;&quot; ); mv.visitLdcInsn( &quot;Hello, World!&quot; ); mv.visitMethodInsn( INVOKEVIRTUAL , &quot;java/io/PrintStream&quot; , &quot;println&quot; , &quot;(Ljava/lang/String;)V&quot; ); mv.visitInsn( RETURN ); mv.visitMaxs(2, 1); mv.visitEnd(); } cw.visitEnd();
  • 38.
  • 39.
  • 40.
  • 41.
  • 44.
  • 48.
  • 50. Reusable genSayHello() private static void genSayHello(MethodBuilder0 mb) { mb .assumePush(String. class ) .getStatic(System. class , &quot;out&quot; , PrintStream . class ) .swap() .invoke() .param(String. class ) .virtVoid(PrintStream. class , &quot;println&quot; ); }