SlideShare a Scribd company logo
1 of 87
[object Object]
http://hamletdarcy.blogspot.com ,[object Object]
[object Object],[object Object],[object Object]
[object Object],[object Object]
I shot an elephant in my pajamas.
Subject: I Verb: shot Direct Object: an elephant Indirect Object: in my pajamas
I shot an elephant in my pajamas. How he got in my pajamas,  I'll never know.
Subject: I Verb: shot an elephant in my pajamas Participle Phrase
We invited the strippers, JFK and Stalin
We invited the strippers, JFK and Stalin
Subject: We Verb: invited Participle: JFK strippers Stalin
We invited the strippers, JFK and Stalin
We invited the strippers, JFK and Stalin
Subject: We Verb: invited Participle Phrase: the strippers JFK Stalin
[object Object]
[object Object]
[object Object]
public class   Person { private  String name;  public void  setName(String name) {  this .name = name;} public  String getNameName() {  return   name ;  } public   static   void  main(String[] args) { Person p =  new  Person();   p.setName( “Hamlet” );  System. out .println(p);  } }
[object Object]
[object Object],[object Object]
public class Person { private String firstName;  private String lastName;  void setFirstName(String fName) { this.firstName = fName;  } public String getFirstName() { return firstName;  } public void setLastName(String lName) { this.lastName = lName;  } public String getLastName() { return firstName;  } } import lombok.Getter; import lombok.Setter; public class Person { @Getter  @Setter  private String firstName;  @Getter  @Setter  private String lastname;  } ,[object Object]
@Getter / @Setter @ToString @EqualsAndHashCode @NoArgsConstructor @RequiredArgsConstructor  @AllArgsConstructor @Data @Cleanup @Synchronized @SneakyThrows @Log @Delegate val ,[object Object]
Generates Java Boilerplate Compile Time Only –  For Eclipse and javac –  IDEA & NetBeans too Removable with delombok –  Javadoc –  GWT Read the fine print –  Know what is generated –  Slows compilation times? ,[object Object]
[object Object],Annotation Processor Javac Handler Eclipse Handler ,[object Object]
 
 
 
 
// javac private   void  generatePropertyChangeSupportField(JavacNode node) { if  (fieldAlreadyExists( PROPERTY_SUPPORT_FIELD_NAME , node))  return ; JCExpression exp = chainDots(node.getTreeMaker(), node,  "this" ); JCVariableDecl fieldDecl = newField() .ofType(PropertyChangeSupport. class ) .withName( PROPERTY_SUPPORT_FIELD_NAME ) .withModifiers( PRIVATE  |  FINAL ) .withArgs(exp) .buildWith(node); injectField(node, fieldDecl); } // ECJ private   void  generatePropertyChangeSupportField(EclipseNode node) { if  (fieldAlreadyExists( PROPERTY_SUPPORT_FIELD_NAME , node))  return ; Expression exp = referenceForThis(node.get()); FieldDeclaration fieldDecl = newField() .ofType(PropertyChangeSupport. class ) .withName( PROPERTY_SUPPORT_FIELD_NAME ) .withModifiers( PRIVATE  |  FINAL ) .withArgs(exp) .buildWith(node); injectField(node, fieldDecl); } Alex Ruiz – Custom AST  Transformations with Project Lombok http://www.ibm.com/developerworks/java/library/j-lombok/
[object Object],[object Object]
class Event { String title } class Event { String title public void getTitle() { title } public String setTitle(String t) { this.title = t  } } ,[object Object]
class Event { @Delegate Date when } class Event implements Comparable, Clonable { Date when boolean after(Date when) { this.when.after(when) } boolean before(Date when)  { this.when.before(when) } Object clone() { this.when.clone() }  Int compareTo(Date anotherDate)  { this.when.compareTo(otherDate)  } Int getDate()  { this.when.date } Int getDay()  { this.when.day } Int getHours()  { this.when.hours } Int getMinutes()  { this.when.minutes } Int getMonth()  { this.when.month } Int getSeconds()  { this.when.seconds } long getTime()  { this.when.time } Int getTimezoneOffset() {  this.when.timezoneOffset }  Int getYear()  { this.when.year } void setDate(int date) { this.when.date = date }  void setHours(int hours)  { this.when.hours = hours } void setMinutes(int minutes)  { this.when.minutes = minutes } void setMonth(int month)  { this.when.month = month } void setSeconds(int seconds) { this.when.seconds = seconds }  void setTime(long time) { this.when.time = time }  void setYear(int year)  { this.when.year = year } String toGMTString()  { this.when.toGMTString() } String toLocaleString() {  this.when.toLocaleString() }  } ,[object Object]
class Event { @Lazy ArrayList speakers } class Event { ArrayList speakers  def getSpeakers() { if (speakers != null) { return speakers } else { synchronized(this) { if (speakers == null) { speakers = [] }  return speakers } } } } ,[object Object]
@Immutable  class Event { String title } ,[object Object],–  Properties must be @Immutable or effectively immutable –  Properties are private –  Mutatators throw ReadOnlyPropertyException –  Map constructor created –  Tuple constructor created –  Equals(), hashCode() & toString() created –  Dates, Clonables, & arrays are defensively copied on way in & out (but not deeply cloned) –  Collections & Maps are wrapped in Immutable variants –  Non-immutable fields force an error –  Special handling for Date, Color, etc –  Many generated methods configurable ,[object Object]
[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Declarative Concurrency ,[object Object],Easier Cloning and Externalizing ,[object Object],Safer Scripting ,[object Object],[object Object],[object Object],[object Object]
[object Object],Local AST Transformations ,[object Object]
 
 
 
 
class  Event {   @Delegate Date when } @GroovyASTTransformationClass( "org.pkg.DelegateTransform" ) public  @ interface  Delegate {   ... } @GroovyASTTransformation(phase =  CANONICALIZATION ) public class   DelegateTransform   implements  ASTTransformation {   public void  visit(ASTNode[] nodes, SourceUnit source) {   ...   } } ,[object Object]
[object Object],[object Object]
[object Object],import   org.codehaus.groovy.ast.* import   org.codehaus.groovy.ast.stmt.* import   org.codehaus.groovy.ast.expr.* def   ast =  new  ReturnStatement( new  ConstructorCallExpression( ClassHelper. make (Date),   ArgumentListExpression. EMPTY_ARGUMENTS   ) ) assert  ast  instanceof  ReturnStatement ,[object Object]
[object Object],def  ast =  new  AstBuilder().buildFromSpec { returnStatement { constructorCall(Date) { argumentList {} } } } assert  ast[0]  instanceof  ReturnStatement ,[object Object]
[object Object],def  ast =  new  AstBuilder().buildFromString( 'new Date()' ) assert  assert ast[0]  instanceof  BlockStatement assert  ast[0].statements[0]  instanceof  ReturnStatement ,[object Object]
[object Object],def  ast =  new  AstBuilder().buildFromCode { new  Date() } assert  ast[0].statements[0]  instanceof  ReturnStatement ,[object Object]
[object Object],class  MainExample { @Main public   void  greet() { println  "Hello from greet()!" } } $ groovy MainExample.groovy  Hello from greet()! ,[object Object]
MethodNode makeMainMethod(MethodNode source) { def  className = source.declaringClass.name def  methodName = source.name def  ast =  new  AstBuilder().buildFromString( INSTRUCTION_SELECTION ,  false ,  """ package $source.declaringClass.packageName class $source.declaringClass.nameWithoutPackage { public static void main(String[] args) { new $className().$methodName() } } """ ) ast[ 1 ].methods.find { it.name ==  'main'  } } ,[object Object]
[object Object],[object Object]
How it Works Compiles Java to AST Analyzes AST Nodes Rewrites with Strings
(x != null && x instanceof SomeClass) @Override public void  visitBinaryExpression(PsiBinaryExpression exp) { if  (exp.token ==  '==' ) { if  (exp.left instanceof BinaryExpression ... ) { if  (exp.right instanceof BinaryExpression ... ) {   registerError(exp,  '...' ) } } } }
(x != null && x instanceof SomeClass) @Override public void visitBinaryExpression(PsiBinaryExpression expression) { super.visitBinaryExpression(expression); if (!expression.getOperationTokenType().equals(JavaTokenType.ANDAND)) { return; } PsiExpression lOperand = expression.getLOperand(); PsiExpression rOperand = expression.getROperand(); if (!isBinaryExpressionAndInstanceOfCheck(lOperand, rOperand) && !isBinaryExpressionAndInstanceOfCheck(rOperand, lOperand)) { return; } PsiBinaryExpression binaryExpression = getInnerBinaryExpression(expression); if (!binaryExpression.getOperationTokenType().equals(JavaTokenType.NE)) { return; } boolean leftIsNullCheck = isNullCheck(binaryExpression.getLOperand(), binaryExpression.getROperand()); boolean rightIsNullCheck = isNullCheck(binaryExpression.getROperand(), binaryExpression.getLOperand()); if (!leftIsNullCheck && !rightIsNullCheck) return; PsiReferenceExpression theReference = getReferenceFromNotNullCheck(binaryExpression); PsiExpression instanceOfOperand = getInstanceOfOperand(expression); if (!(instanceOfOperand instanceof PsiReferenceExpression)) { return; } String referenceNameA = (theReference).getReferenceName(); String referenceNameB = ((PsiReferenceExpression)instanceOfOperand).getReferenceName(); if (referenceNameA == null || referenceNameB == null) { return; } if (!referenceNameA.equals(referenceNameB)) { return; } registerError(expression); }
Rewrite: (x instanceof SomeClass) PsiElement andand = descriptor.getPsiElement(); if  (andand  instanceof  PsiBinaryExpression) { PsiExpression lhs = ((PsiBinaryExpression)andand).getLOperand(); PsiExpression rhs = ((PsiBinaryExpression)andand).getROperand(); if  (lhs  instanceof  PsiInstanceOfExpression) { replaceExpression((PsiBinaryExpression)andand, lhs.getText()); }  else if  (rhs  instanceof  PsiInstanceOfExpression) { replaceExpression((PsiBinaryExpression)andand, rhs.getText()); } }
Rewrite: (x instanceof SomeClass) when  (descriptor.psiElement) { is  PsiBinaryExpression @ ( val  lhs  is  PsiInstanceOfExpression, *) => replaceExpression(descriptor.psiElement, lhs.text)  is  PsiBinaryExpression @ (*,  val  rhs  is  PsiInstanceOfExpression) => replaceExpression(descriptor.psiElement, rhs.text)  } Kotlin's “Decomposer patterns”
How it Works Compiles Java to AST Analyzes AST Nodes Rewrites with Strings
[object Object],[object Object]
–  Dead Code –  Defects like Gstring as Map Key, Duplicate Map Key –  Return path analysis (null returns) –  Type inference (and improving) –  Concurrency Problems (busy wait, etc) –  Poor Testing Practices –  Un-Groovy code …  and 240+ more rules
How it works CodeNarc Rule:  Ban new java.util.Random() calls @Override void  visitConstructorCallExpression(   ConstructorCallExpression call) { if  (AstUtil.classNodeImplementsType(call. type , Random)) {   addViolation(call,  'Using Random is insecure...' )   }   super .visitConstructorCallExpression(call) }
How it works
How it works
How it works
[object Object],[object Object],[object Object],[object Object]
[object Object],From Terrence Parr's  “Language Implementation Patterns” ,[object Object],[object Object]
Embedded Languages def  s =  new  ArithmeticShell() assert 2 == s.evaluate( ' 1+1 ' ) assert 1.0 == s.evaluate( 'cos(2*PI)' ) public interface   GroovyCodeVisitor {   void  visitBlockStatement(BlockStatement statement);   void  visitForLoop(ForStatement forLoop);   void  visitWhileLoop(WhileStatement loop);   void  visitDoWhileLoop(DoWhileStatement loop);   ... }
[object Object],//FinallyStatement//ReturnStatement //SynchronizedStatement/Block[1][count(*) = 0] //AllocationExpression/ClassOrInterfaceType   [contains(@Image,'ThreadGroup')] | //PrimarySuffix   [contains(@Image, 'getThreadGroup')] ,[object Object],[object Object]
How it Works Compiles Groovy to AST Analyzes AST Nodes –  Not well typed –  Not many tokens –  unreliable with other AST Transforms
[object Object],[object Object],[object Object],[object Object]
[object Object],def   "Does simple math work?" () {   expect:   def  s =  new  ArithmeticShell()   s.evaluate(input) == output   where:   input  | output   '1 + 1'   | 2   'cos(2*PI)'   | 1.0 } ,[object Object],[object Object]
[object Object],Transform.jar | - transforms   | - global   | - CompiledAtASTTransformation.class   | - CompiledAtASTTransformation$_visit_closure1.class   | - ... | META-INF   | - services   | - org.codehaus.groovy.transform.ASTTransformation   | - org.codehaus.groovy.source.Extensions ,[object Object],[object Object]
[object Object],[object Object]
Ruby FizzBuzz 1.upto(100) do |n| print "Fizz" if a = ((n % 3) == 0) print "Buzz" if b = ((n % 5) == 0)  print n unless (a || b) print "" end
Mirah FizzBuzz 1.upto(100) do |n| print "Fizz" if a = ((n % 3) == 0) print "Buzz" if b = ((n % 5) == 0)  print n unless (a || b) print "" end
Mirah: Pure JVM Class Output public class Fizz-buzz { public static void main(String[] argv) { ... do { n = __xform_tmp_4; ... if (n % 15 == 0) System.out.println(&quot;FizzBuzz&quot;); else if (n % 5 == 0) System.out.println(&quot;Buzz&quot;); else if (n % 3 == 0) System.out.println(&quot;Fizz&quot;); else System.out.println(n); ... } while (__xform_tmp_4 <= __xform_tmp_5); } }
Mirah: .java File Output if (((n % 15) == 0)) { PrintStream temp$10 = System.out; temp$10.println(&quot;FizzBuzz&quot;); } else { if (((n % 5) == 0)) { PrintStream temp$11 = System.out; temp$11.println(&quot;Buzz&quot;); } else { if (((n % 3) == 0)) { PrintStream temp$12 = System.out; temp$12.println(&quot;Fizz&quot;); } else { PrintStream temp$13 = System.out; temp$13.println(n); } } }
What it Means –  Fast –  Lightweight –  Uses JDK –  Runs on Android? –  Runs on GWT?
How it Works
How it Works
 
Mirah Macros macro def eachChar(value, &block) quote {  `value`.toCharArray.each do | my_char | `block.body` end  } end eachChar('laat de leeeuw niet ...') do | my_char | puts my_char end
Mirah Macros class Person  make_attr :firstName, :string  end  p = Person.new p.firstName = 'hamlet' puts p.firstName
Mirah Macros macro def make_attr(name, type)  attribute_name = name.string_value() quote { def `name`  @`name`  end  def `&quot;#{attribute_name}_set&quot;`(value:`type`)  @`name` = value end  } end
[object Object],[object Object]
Difficult to find call-sites
Still Difficult to generate AST Macros are more general ,[object Object]
Easily quote ASTs into source

More Related Content

What's hot

Develop your next app with kotlin @ AndroidMakersFr 2017
Develop your next app with kotlin @ AndroidMakersFr 2017Develop your next app with kotlin @ AndroidMakersFr 2017
Develop your next app with kotlin @ AndroidMakersFr 2017Arnaud Giuliani
 
Introduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demoIntroduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demoMuhammad Abdullah
 
The Kotlin Programming Language
The Kotlin Programming LanguageThe Kotlin Programming Language
The Kotlin Programming Languageintelliyole
 
Lambdas and Streams Master Class Part 2
Lambdas and Streams Master Class Part 2Lambdas and Streams Master Class Part 2
Lambdas and Streams Master Class Part 2José Paumard
 
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)Phil Calçado
 
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)Pavlo Baron
 
Kotlin hands on - MorningTech ekito 2017
Kotlin hands on - MorningTech ekito 2017Kotlin hands on - MorningTech ekito 2017
Kotlin hands on - MorningTech ekito 2017Arnaud Giuliani
 
(ThoughtWorks Away Day 2009) one or two things you may not know about typesys...
(ThoughtWorks Away Day 2009) one or two things you may not know about typesys...(ThoughtWorks Away Day 2009) one or two things you may not know about typesys...
(ThoughtWorks Away Day 2009) one or two things you may not know about typesys...Phil Calçado
 
JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6Solution4Future
 
Kotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developersKotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developersBartosz Kosarzycki
 
Lambda and Stream Master class - part 1
Lambda and Stream Master class - part 1Lambda and Stream Master class - part 1
Lambda and Stream Master class - part 1José Paumard
 
Rust Workshop - NITC FOSSMEET 2017
Rust Workshop - NITC FOSSMEET 2017 Rust Workshop - NITC FOSSMEET 2017
Rust Workshop - NITC FOSSMEET 2017 pramode_ce
 
Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesGanesh Samarthyam
 
Kotlin in action
Kotlin in actionKotlin in action
Kotlin in actionCiro Rizzo
 
Clojure for Java developers - Stockholm
Clojure for Java developers - StockholmClojure for Java developers - Stockholm
Clojure for Java developers - StockholmJan Kronquist
 
Lambda Chops - Recipes for Simpler, More Expressive Code
Lambda Chops - Recipes for Simpler, More Expressive CodeLambda Chops - Recipes for Simpler, More Expressive Code
Lambda Chops - Recipes for Simpler, More Expressive CodeIan Robertson
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojureAbbas Raza
 

What's hot (20)

Develop your next app with kotlin @ AndroidMakersFr 2017
Develop your next app with kotlin @ AndroidMakersFr 2017Develop your next app with kotlin @ AndroidMakersFr 2017
Develop your next app with kotlin @ AndroidMakersFr 2017
 
Introduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demoIntroduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demo
 
Java Full Throttle
Java Full ThrottleJava Full Throttle
Java Full Throttle
 
The Kotlin Programming Language
The Kotlin Programming LanguageThe Kotlin Programming Language
The Kotlin Programming Language
 
Lambdas and Streams Master Class Part 2
Lambdas and Streams Master Class Part 2Lambdas and Streams Master Class Part 2
Lambdas and Streams Master Class Part 2
 
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
 
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)
 
Kotlin hands on - MorningTech ekito 2017
Kotlin hands on - MorningTech ekito 2017Kotlin hands on - MorningTech ekito 2017
Kotlin hands on - MorningTech ekito 2017
 
(ThoughtWorks Away Day 2009) one or two things you may not know about typesys...
(ThoughtWorks Away Day 2009) one or two things you may not know about typesys...(ThoughtWorks Away Day 2009) one or two things you may not know about typesys...
(ThoughtWorks Away Day 2009) one or two things you may not know about typesys...
 
JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6
 
Kotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developersKotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developers
 
Lambda and Stream Master class - part 1
Lambda and Stream Master class - part 1Lambda and Stream Master class - part 1
Lambda and Stream Master class - part 1
 
Rust Workshop - NITC FOSSMEET 2017
Rust Workshop - NITC FOSSMEET 2017 Rust Workshop - NITC FOSSMEET 2017
Rust Workshop - NITC FOSSMEET 2017
 
Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java Bytecodes
 
Kotlin
KotlinKotlin
Kotlin
 
Kotlin in action
Kotlin in actionKotlin in action
Kotlin in action
 
Clojure for Java developers - Stockholm
Clojure for Java developers - StockholmClojure for Java developers - Stockholm
Clojure for Java developers - Stockholm
 
Introduction to kotlin
Introduction to kotlinIntroduction to kotlin
Introduction to kotlin
 
Lambda Chops - Recipes for Simpler, More Expressive Code
Lambda Chops - Recipes for Simpler, More Expressive CodeLambda Chops - Recipes for Simpler, More Expressive Code
Lambda Chops - Recipes for Simpler, More Expressive Code
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
 

Similar to Code Generation JVM

#살아있다 #자프링외길12년차 #코프링2개월생존기
#살아있다 #자프링외길12년차 #코프링2개월생존기#살아있다 #자프링외길12년차 #코프링2개월생존기
#살아있다 #자프링외길12년차 #코프링2개월생존기Arawn Park
 
The Future of JVM Languages
The Future of JVM Languages The Future of JVM Languages
The Future of JVM Languages VictorSzoltysek
 
Naïveté vs. Experience
Naïveté vs. ExperienceNaïveté vs. Experience
Naïveté vs. ExperienceMike Fogus
 
Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalMichael Stal
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecLoïc Descotte
 
2007 09 10 Fzi Training Groovy Grails V Ws
2007 09 10 Fzi Training Groovy Grails V Ws2007 09 10 Fzi Training Groovy Grails V Ws
2007 09 10 Fzi Training Groovy Grails V Wsloffenauer
 
Patterns for JVM languages JokerConf
Patterns for JVM languages JokerConfPatterns for JVM languages JokerConf
Patterns for JVM languages JokerConfJaroslaw Palka
 
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018Codemotion
 
jRuby: The best of both worlds
jRuby: The best of both worldsjRuby: The best of both worlds
jRuby: The best of both worldsChristopher Spring
 
Scala uma poderosa linguagem para a jvm
Scala   uma poderosa linguagem para a jvmScala   uma poderosa linguagem para a jvm
Scala uma poderosa linguagem para a jvmIsaias Barroso
 
Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)Cody Engel
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Jesper Kamstrup Linnet
 
JavaScript Survival Guide
JavaScript Survival GuideJavaScript Survival Guide
JavaScript Survival GuideGiordano Scalzo
 
Kotlin @ Coupang Backend 2017
Kotlin @ Coupang Backend 2017Kotlin @ Coupang Backend 2017
Kotlin @ Coupang Backend 2017Sunghyouk Bae
 

Similar to Code Generation JVM (20)

#살아있다 #자프링외길12년차 #코프링2개월생존기
#살아있다 #자프링외길12년차 #코프링2개월생존기#살아있다 #자프링외길12년차 #코프링2개월생존기
#살아있다 #자프링외길12년차 #코프링2개월생존기
 
The Future of JVM Languages
The Future of JVM Languages The Future of JVM Languages
The Future of JVM Languages
 
Naïveté vs. Experience
Naïveté vs. ExperienceNaïveté vs. Experience
Naïveté vs. Experience
 
Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation Stal
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
 
What's New In C# 7
What's New In C# 7What's New In C# 7
What's New In C# 7
 
2007 09 10 Fzi Training Groovy Grails V Ws
2007 09 10 Fzi Training Groovy Grails V Ws2007 09 10 Fzi Training Groovy Grails V Ws
2007 09 10 Fzi Training Groovy Grails V Ws
 
Patterns for JVM languages JokerConf
Patterns for JVM languages JokerConfPatterns for JVM languages JokerConf
Patterns for JVM languages JokerConf
 
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
 
jRuby: The best of both worlds
jRuby: The best of both worldsjRuby: The best of both worlds
jRuby: The best of both worlds
 
Scala - en bedre Java?
Scala - en bedre Java?Scala - en bedre Java?
Scala - en bedre Java?
 
Scala - brief intro
Scala - brief introScala - brief intro
Scala - brief intro
 
Scala uma poderosa linguagem para a jvm
Scala   uma poderosa linguagem para a jvmScala   uma poderosa linguagem para a jvm
Scala uma poderosa linguagem para a jvm
 
Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)Privet Kotlin (Windy City DevFest)
Privet Kotlin (Windy City DevFest)
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?
 
JavaScript Survival Guide
JavaScript Survival GuideJavaScript Survival Guide
JavaScript Survival Guide
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
OO in JavaScript
OO in JavaScriptOO in JavaScript
OO in JavaScript
 
ES6 is Nigh
ES6 is NighES6 is Nigh
ES6 is Nigh
 
Kotlin @ Coupang Backend 2017
Kotlin @ Coupang Backend 2017Kotlin @ Coupang Backend 2017
Kotlin @ Coupang Backend 2017
 

More from HamletDRC

Static Analysis and AST Transformations
Static Analysis and AST TransformationsStatic Analysis and AST Transformations
Static Analysis and AST TransformationsHamletDRC
 
Static Analysis in IDEA
Static Analysis in IDEAStatic Analysis in IDEA
Static Analysis in IDEAHamletDRC
 
10 Years of Groovy
10 Years of Groovy10 Years of Groovy
10 Years of GroovyHamletDRC
 
Java Boilerplate Busters
Java Boilerplate BustersJava Boilerplate Busters
Java Boilerplate BustersHamletDRC
 
New Ideas for Old Code - Greach
New Ideas for Old Code - GreachNew Ideas for Old Code - Greach
New Ideas for Old Code - GreachHamletDRC
 
Java Boilerplate Busters
Java Boilerplate BustersJava Boilerplate Busters
Java Boilerplate BustersHamletDRC
 

More from HamletDRC (6)

Static Analysis and AST Transformations
Static Analysis and AST TransformationsStatic Analysis and AST Transformations
Static Analysis and AST Transformations
 
Static Analysis in IDEA
Static Analysis in IDEAStatic Analysis in IDEA
Static Analysis in IDEA
 
10 Years of Groovy
10 Years of Groovy10 Years of Groovy
10 Years of Groovy
 
Java Boilerplate Busters
Java Boilerplate BustersJava Boilerplate Busters
Java Boilerplate Busters
 
New Ideas for Old Code - Greach
New Ideas for Old Code - GreachNew Ideas for Old Code - Greach
New Ideas for Old Code - Greach
 
Java Boilerplate Busters
Java Boilerplate BustersJava Boilerplate Busters
Java Boilerplate Busters
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
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
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
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
 
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
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
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
 
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
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
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
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 

Recently uploaded (20)

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
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
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
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
 
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
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
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...
 
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
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
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
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 

Code Generation JVM

  • 1.
  • 2.
  • 3.
  • 4.
  • 5. I shot an elephant in my pajamas.
  • 6. Subject: I Verb: shot Direct Object: an elephant Indirect Object: in my pajamas
  • 7. I shot an elephant in my pajamas. How he got in my pajamas, I'll never know.
  • 8. Subject: I Verb: shot an elephant in my pajamas Participle Phrase
  • 9. We invited the strippers, JFK and Stalin
  • 10. We invited the strippers, JFK and Stalin
  • 11. Subject: We Verb: invited Participle: JFK strippers Stalin
  • 12. We invited the strippers, JFK and Stalin
  • 13. We invited the strippers, JFK and Stalin
  • 14. Subject: We Verb: invited Participle Phrase: the strippers JFK Stalin
  • 15.
  • 16.
  • 17.
  • 18. public class Person { private String name; public void setName(String name) { this .name = name;} public String getNameName() { return name ; } public static void main(String[] args) { Person p = new Person(); p.setName( “Hamlet” ); System. out .println(p); } }
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.  
  • 26.  
  • 27.  
  • 28.  
  • 29. // javac private void generatePropertyChangeSupportField(JavacNode node) { if (fieldAlreadyExists( PROPERTY_SUPPORT_FIELD_NAME , node)) return ; JCExpression exp = chainDots(node.getTreeMaker(), node, &quot;this&quot; ); JCVariableDecl fieldDecl = newField() .ofType(PropertyChangeSupport. class ) .withName( PROPERTY_SUPPORT_FIELD_NAME ) .withModifiers( PRIVATE | FINAL ) .withArgs(exp) .buildWith(node); injectField(node, fieldDecl); } // ECJ private void generatePropertyChangeSupportField(EclipseNode node) { if (fieldAlreadyExists( PROPERTY_SUPPORT_FIELD_NAME , node)) return ; Expression exp = referenceForThis(node.get()); FieldDeclaration fieldDecl = newField() .ofType(PropertyChangeSupport. class ) .withName( PROPERTY_SUPPORT_FIELD_NAME ) .withModifiers( PRIVATE | FINAL ) .withArgs(exp) .buildWith(node); injectField(node, fieldDecl); } Alex Ruiz – Custom AST Transformations with Project Lombok http://www.ibm.com/developerworks/java/library/j-lombok/
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.  
  • 40.  
  • 41.  
  • 42.  
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52. How it Works Compiles Java to AST Analyzes AST Nodes Rewrites with Strings
  • 53. (x != null && x instanceof SomeClass) @Override public void visitBinaryExpression(PsiBinaryExpression exp) { if (exp.token == '==' ) { if (exp.left instanceof BinaryExpression ... ) { if (exp.right instanceof BinaryExpression ... ) { registerError(exp, '...' ) } } } }
  • 54. (x != null && x instanceof SomeClass) @Override public void visitBinaryExpression(PsiBinaryExpression expression) { super.visitBinaryExpression(expression); if (!expression.getOperationTokenType().equals(JavaTokenType.ANDAND)) { return; } PsiExpression lOperand = expression.getLOperand(); PsiExpression rOperand = expression.getROperand(); if (!isBinaryExpressionAndInstanceOfCheck(lOperand, rOperand) && !isBinaryExpressionAndInstanceOfCheck(rOperand, lOperand)) { return; } PsiBinaryExpression binaryExpression = getInnerBinaryExpression(expression); if (!binaryExpression.getOperationTokenType().equals(JavaTokenType.NE)) { return; } boolean leftIsNullCheck = isNullCheck(binaryExpression.getLOperand(), binaryExpression.getROperand()); boolean rightIsNullCheck = isNullCheck(binaryExpression.getROperand(), binaryExpression.getLOperand()); if (!leftIsNullCheck && !rightIsNullCheck) return; PsiReferenceExpression theReference = getReferenceFromNotNullCheck(binaryExpression); PsiExpression instanceOfOperand = getInstanceOfOperand(expression); if (!(instanceOfOperand instanceof PsiReferenceExpression)) { return; } String referenceNameA = (theReference).getReferenceName(); String referenceNameB = ((PsiReferenceExpression)instanceOfOperand).getReferenceName(); if (referenceNameA == null || referenceNameB == null) { return; } if (!referenceNameA.equals(referenceNameB)) { return; } registerError(expression); }
  • 55. Rewrite: (x instanceof SomeClass) PsiElement andand = descriptor.getPsiElement(); if (andand instanceof PsiBinaryExpression) { PsiExpression lhs = ((PsiBinaryExpression)andand).getLOperand(); PsiExpression rhs = ((PsiBinaryExpression)andand).getROperand(); if (lhs instanceof PsiInstanceOfExpression) { replaceExpression((PsiBinaryExpression)andand, lhs.getText()); } else if (rhs instanceof PsiInstanceOfExpression) { replaceExpression((PsiBinaryExpression)andand, rhs.getText()); } }
  • 56. Rewrite: (x instanceof SomeClass) when (descriptor.psiElement) { is PsiBinaryExpression @ ( val lhs is PsiInstanceOfExpression, *) => replaceExpression(descriptor.psiElement, lhs.text) is PsiBinaryExpression @ (*, val rhs is PsiInstanceOfExpression) => replaceExpression(descriptor.psiElement, rhs.text) } Kotlin's “Decomposer patterns”
  • 57. How it Works Compiles Java to AST Analyzes AST Nodes Rewrites with Strings
  • 58.
  • 59. – Dead Code – Defects like Gstring as Map Key, Duplicate Map Key – Return path analysis (null returns) – Type inference (and improving) – Concurrency Problems (busy wait, etc) – Poor Testing Practices – Un-Groovy code … and 240+ more rules
  • 60. How it works CodeNarc Rule: Ban new java.util.Random() calls @Override void visitConstructorCallExpression( ConstructorCallExpression call) { if (AstUtil.classNodeImplementsType(call. type , Random)) { addViolation(call, 'Using Random is insecure...' ) } super .visitConstructorCallExpression(call) }
  • 64.
  • 65.
  • 66. Embedded Languages def s = new ArithmeticShell() assert 2 == s.evaluate( ' 1+1 ' ) assert 1.0 == s.evaluate( 'cos(2*PI)' ) public interface GroovyCodeVisitor { void visitBlockStatement(BlockStatement statement); void visitForLoop(ForStatement forLoop); void visitWhileLoop(WhileStatement loop); void visitDoWhileLoop(DoWhileStatement loop); ... }
  • 67.
  • 68. How it Works Compiles Groovy to AST Analyzes AST Nodes – Not well typed – Not many tokens – unreliable with other AST Transforms
  • 69.
  • 70.
  • 71.
  • 72.
  • 73. Ruby FizzBuzz 1.upto(100) do |n| print &quot;Fizz&quot; if a = ((n % 3) == 0) print &quot;Buzz&quot; if b = ((n % 5) == 0) print n unless (a || b) print &quot;&quot; end
  • 74. Mirah FizzBuzz 1.upto(100) do |n| print &quot;Fizz&quot; if a = ((n % 3) == 0) print &quot;Buzz&quot; if b = ((n % 5) == 0) print n unless (a || b) print &quot;&quot; end
  • 75. Mirah: Pure JVM Class Output public class Fizz-buzz { public static void main(String[] argv) { ... do { n = __xform_tmp_4; ... if (n % 15 == 0) System.out.println(&quot;FizzBuzz&quot;); else if (n % 5 == 0) System.out.println(&quot;Buzz&quot;); else if (n % 3 == 0) System.out.println(&quot;Fizz&quot;); else System.out.println(n); ... } while (__xform_tmp_4 <= __xform_tmp_5); } }
  • 76. Mirah: .java File Output if (((n % 15) == 0)) { PrintStream temp$10 = System.out; temp$10.println(&quot;FizzBuzz&quot;); } else { if (((n % 5) == 0)) { PrintStream temp$11 = System.out; temp$11.println(&quot;Buzz&quot;); } else { if (((n % 3) == 0)) { PrintStream temp$12 = System.out; temp$12.println(&quot;Fizz&quot;); } else { PrintStream temp$13 = System.out; temp$13.println(n); } } }
  • 77. What it Means – Fast – Lightweight – Uses JDK – Runs on Android? – Runs on GWT?
  • 80.  
  • 81. Mirah Macros macro def eachChar(value, &block) quote { `value`.toCharArray.each do | my_char | `block.body` end } end eachChar('laat de leeeuw niet ...') do | my_char | puts my_char end
  • 82. Mirah Macros class Person make_attr :firstName, :string end p = Person.new p.firstName = 'hamlet' puts p.firstName
  • 83. Mirah Macros macro def make_attr(name, type) attribute_name = name.string_value() quote { def `name` @`name` end def `&quot;#{attribute_name}_set&quot;`(value:`type`) @`name` = value end } end
  • 84.
  • 85. Difficult to find call-sites
  • 86.
  • 87. Easily quote ASTs into source
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95. – Groovy Wiki and Mailing List is amazingly helpful
  • 96. – Use your creativity and patience
  • 97. – http://hamletdarcy.blogspot.com & @HamletDRC Groovy, Grails, Griffon, and Agile Consulting [email_address] or [email_address]