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

"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 

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; ); }