SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Tendances

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
 

Tendances (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
 

Similaire à 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
 

Similaire à 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
 

Dernier

Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
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
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
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
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
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
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 

Dernier (20)

Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
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)
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
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
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
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!
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 

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