 Okay, so hello everyone. This is my first time in Singapore and I'm very happy to be here with you I'm Guillaume Laforge. I work for Google, but I've been working on the Apache Groovy project for the past 13 years now A little bit less lately, but I've been one of the key Members of the project. I am also the PMC. That's the project management committee I'm the chair of the PMC at the Apache Foundation because the The Groovy project moved to the Apache Foundation. When was that a year and a half ago? I think or before Yeah, something like this. Oh, no two years now. I forget so in my Day job. I'm a developer advocate for Google Cloud platform So I speak about various things like I don't know Google App Engine and machine learning APIs. That's what that's what I'm gonna cover Advoxed days Singapore, but tonight I'm gonna speak about Groovy and in particular what's new in Groovy What's new talk? Kind of expects people to have some knowledge of Groovy, but if you don't that's okay Are you all Java developers? Everyone who's all who's already got some experience with Groovy. Oh, yeah, nice and I know at PayPal as well. There were there's some places. There are some places where Groovy is being used If I recall correctly But I don't remember the details. All right so In Groovy we have What we call AST transformations. That's code transformations which allow you to Implement existing design patterns with just the use of an annotation or create some boilerplate code with Such annotations. So for example, we have So we you know, it looks just like normal Java annotations we have annotations for Generating a two-string method or equals a NAS code or At immutable which creates an immutable class with everything that's needed the Like making the fields private Like creating proper equals a NAS code, etc. All the things that are required to make something immutable So that you cannot change the values of its attributes, etc And we also have a code transformation called canonical, which is a bit like immutable But it's we could have called it immutable in a way So it's adding things like two-string methods equals an as code methods various constructors for Depending on the the properties of that class. So it's going to add a person String name into each constructor, but also a person string name constructor and a default constructor And yeah, and you can see for example when I call the two-string method down there. This is the the the two-string that is generated instead of the usual Java to string and Here I'm using the include names parameter, which actually adds the the name of the properties in the two-string So so this is This canonical transformation, but the thing is that we Changed how it was implemented because it's combining different code transformations Before it was kind of a hack of how it combined things together, but there's a mechanism called meta annotations in groovy, which allows bundling, you know somehow Code transformations or annotations together What's interesting as well with meta annotations is that it's a compile time feature So for example, you can create a meta annotation that groups different annotations together. So let's say you have a What example could I take like a at business something annotation that could combine at data object plus at Various annotations that are grouped together and you could just simplify that with just one annotation your own business annotation And that's that's pretty handy. So that's the mechanism that we used to combine at two-string at equal to NASCAR etc together and Canonical now uses that mechanism and In particular this include names Parameter that is passed to that annotation Is actually delegated to the under underlying two-string Cut transformation. So it does the delegation of the the properties Not the properties the the parameters of the annotations So this you don't need to read that carefully, that's the the documentation that we have The there's what we call an annotation collector which collects the various annotations together And there are there's also more control on the kind of how do you delegate to include names who for example? You could have two annotations with The same parameter potentially So there's even more control on how you combine annotations or come to cut transformations together So these are the the five modes which are available So we have Like in at Canonical at immutable cut transformations. We also have bundle within those transformations We have tuple constructor which creates New person first constructor and Here what's new is the fact that we have added some pre-post Hooks or events so that you can do some additional checks So since the constructors are actually generated by the group compiler directly if you want to add some stuff in the constructor instead of Reimplementing the constructor yourself Because it would be the one Used another one generated. So if there's one that you provide It's the one that's used we don't generate one, but then if you want to do some additional checks for example Making sure that the first name is always our case or ensuring that you actually pass a non-null or non Empty first name you can add such checks. So these are new things The tuple constructor which generates the constructor usually generates Those four constructors. So here you have three properties first last and age So there's this first constructor which takes all the properties There's another one which takes just the First parameters one which takes just the first name and there's one a default constructor Which takes nothing but sometimes you want to be to have more control and you can pass Defaults equals true to just generate the one which takes all the parameters. So so that you're sure that Someone calls that constructor with all the parameters. Well people could always, you know, pass null values, obviously But at least you're sure that someone will pass a value for each of the properties And also perhaps for those who are no groovy a little bit less So there's no public keyword on class So it's not public class because by default classes are public in groovy When you see string first last or int age, these are properties. So it's just like Java beans There's actually a private field that is created plus a getter and a setter So it's just a shortcut for having a get first set first and a private string first field That's how it works So it's a little refinement And similarly to tuple constructor so we have what we call the the map constructor Which is the ability to pass? instead instead of for passing just the values directly did connect Regina to the the three First There's no. Oh, yeah author is below which adds the the book name So what we call the the map constructor is the the ability to actually give a name on reuse actually the name of the properties to be able to recognize the various Elements of the the various parameters of the constructor So this is the the map constructor And we're also adding the same mechanism with pre and post events so if you want to do some additional manipulations and those What do you see here? something I forgot to say is that the value of this prey Annotation value it's actually a closure a groovy closure. So it's code that's going to be Integrated into the implementation of your constructor done by the groovy compiler So groovy Initially at least was created as a dynamic language So things like the method dispatch decided which method should be called Was done at runtime, but we also added several years ago Static type checker and static compiler So it's possible to generate pretty much the same bytecode as Java C the Java C compiler does Which means that groovy is just as fast as Java if you use the the static compilation feature But the thing is there are still things like Cut transformations here auto clone which adds clone cloning capabilities to your class Here there's a there's a typo you notice that so I wanted to exclude from the the cloning operation I wanted to exclude the surname But I did a typo and it's sir instead of I instead of you and before this error would have been codes only at runtime and You wouldn't you know or so I don't remember if we generated a runtime exception or Since the property wasn't found it wouldn't Do anything about surname I forget what it was doing, but at least the behavior was wrong anyway So what we do now is that if you also make typos like these in the parameters of the annotation The compiler actually throw the compile time error Unlike what we were doing before So here yeah, you'd get In the so I don't know what's the exception we throw Yeah, it's a compilation exception And then it's the error message says error during auto clone processing excludes property surname doesn't exist I Mean I mentioned at immutable for creating immutable classes The thing with immutable was that it wouldn't it wouldn't work on the hierarchy of classes So it it was you could only put at immutable on Top-level class a class just extending object basically So for example here, I have a person class with a unequals an escort method doesn't matter much But then I want to have an athlete which extends person before that wasn't allowed But now it's possible to do so. So here I'm also customizing things like What I do in the constructor If equals an escort should call the super equals an escort from the parent class the person class and then Yeah, it's just a bunch of asserts checking that it does what We expect it to do like so we include the super properties and not just the ones defined here so we include sport but name as well and So in two string when we in the customization we do so we also include the super properties So that there's both sport and name that appear here So now at immutable works across class hierarchies as well With Java 8 oh by the way who's using Java 8? Are there still people stuck on Java 7 or before or be yes, um, okay, unfortunately, I'm sorry for you Well, we have time for Java 9 it's not even out yet, but well, it's another kind of warms so we we also changed some of the features of groovy to also support some of the the recent features or aspects of Java 8 for example immutable In order for a class to be immutable all the Fields all the properties of those classes have to be to also support this immutability aspect So there's only sudden Classes or you know like numbers like strings etc. Which can be considered immutable But then we also added things like optional In in terms of immutability because it's also immutable Yes, and we the compiler also checks so it's not just the fact of using optional that the the How do we call the the class on which that you wrap in the in an optional? I don't know if it's got a particular name We also check that date here is or isn't Immutable and in this case a date is a mutable object So you're going to get a compilation exception because date is mutable and we want something that's Some new transformations, so I really start this All the new the novelties with all the other cut transformations And I'll move or later on to some other aspects like the the Java 8 features that are coming up There's a new transformation For you know those times where you have to extend an existing class With or an interface or an abstract an abstract class that has got plenty of methods Which are abstract and sometimes really just need like one method to be implemented But not all of them. So you still have to implement them and just put void or empty bodies inside So here Auto-implement actually So that's kind of the the code that's generated here So automatically it implements all the abstract methods get at all closed size, which are part of the abstract list and Closable here, so it provides a default Implementation for those methods so you don't have to write this boilerplate code yourself and then if you have a particular need for Customizing the get method or something like that just implement the get method, but you don't have to implement the rest So it can be handy also for you know testing purpose if you want some Kind of mock objects or such and you just need a particular method to be implemented You can have the implementation of the other methods that you don't care about to be implemented for you You can also customize things like the exceptions which are thrown Well Here you saw that there were no exceptions thrown But you can say okay I want auto-implement, but I want the body of those methods which are implemented to throw an exception in this case You can say okay I want an exception to be thrown IO exception or particular exception with a message or The last one you can pass directly a bit of code in inside a closure here. That's going to be What's going to be used inside the the implementation to throw a custom exception or things like that? There are as part of the the groovy distribution there are a few Command line tools that you can use. There's the groovy console the groovy console. That's This tool here, so it's a kind of you know, just a little code editor So you can you know run code and have syntax highlighting and so on so it's a mini a text editor So it's not really a you know an ID and we also have I can run it here Bish will so have a oh One is not isn't it working Groovy Hmm, that's strange. Perhaps. I'm not Yeah, I don't know why I'll figure out so Gourish, it's our Ripple our shell that you can run within within a within a shell and There's a useful feature in groovy, which is the grab It's a code transformation actually as well so it's at grab and you define a dependency that you need in your groovy script or class and You can put at grab on things like imports but for example when you use a shell you enter line by line and There was no way to define a nano-tation or you'd have to put everything on a single line or something like this So there was really no way of adding Dependencies within your shell the your groovy shell So there's a special command that you can use within groovy shell to say, okay I want to add the guava dependency in my shell so that can so that later on I can import some of the classes from guava so you can Quickly try some code in in the shell that way I forgot to put that back in plain screen So I continue on the ST transformation You can add delegate that's for doing delegation on Properties of the class so usually you put at delegate on So add delegate string name, but you couldn't put delegate on getters but here for example So somehow it's a way to Yeah, just just do the the delegations on getters if you want to do some particular treatment in the in the getter So that when I do the the upper case method, which is going to call the two string method of The underlying property that it's delegated to but since we put the add delegate on the getter It's gonna actually return the reverse name instead of And then you called to uppercase Here but it's gonna get the name in reverse order instead of in the normal order Yeah, this one not very interesting for those who are using Jack's B for mashaling. There's a small shortcut So instead of I forget what it is you have to Yeah, you usually have to do Jack's B context create marshaller create on marshaller. So there's a shortcut if you don't want to Do each time create marshaller, but in the case you want to reuse the the marshaller all the time It's better to call create marshaller But otherwise if it's just a one-off thing you can just do Jack's B marshall instead of create marshall then marshall So it's a little shortcut alright We have a class So that's something that existed before but I'm showing what we had before and the new thing in the next slide We have a common line interface builder for creating when you create commands Which takes arguments that you want to run on your In your shell so you could define things like So here you'd call that I don't know if I have a bubble showing how you call it Yeah, no, I don't have I should have added that so you could call a yeah groovy Dash dash dash audience dash dash help etc So that's helpful for creating common line tools So this is the old way of doing things, but we also added an Annotation driven approach so that you can just use an interface to say okay. I want to have a dash H or there dash a to What's nice is that it's a Strongly typed in the sense that you have a an object Whose class is implementing the greater interface here Which contains the the parameters which are passed on the command line. So it's it's nice. Oh Yeah, and I've got this example. So if you if you pass those This argument on the common line You'll be able to call options dot audience to actually get What was passed as a flag on the command line? Tap yeah, so there's a new method So there's a with method in groovy which allows you to chain calls on On the current instance. So when I do new person I've got an instance of person and everything that I'm gonna do inside that with block of code It's like doing person dot name equals guillom person dot age equals 39, etc But you don't have to repeat person dot person dot, etc And also the thing was that if you wanted to chain Something else like calling the two string on that person you had to return the current objects within the block of code here So you always had to have return it which is the current instance We're working on so that you could basically this closure this method call with which takes a closure would return the actual person being used here and that way Two string would work on that instance. We created but it was a bit cumbersome to have to use return it and write return It explicitly and you could forget potentially so we also have a Tap method which is just like with but we which always returns the this instance on which we're we're working on So we were really, you know tapping through or through it to to have a peek at that current Object we're working on Well, I'll pass on that one some additional miscellaneous methods Then some something scary jazen The jazen generator So the fact that groovy is dynamic long wage Although, you know, I already told you that you can use static compilation but the fact that it's a dynamic long wage you can Do some interesting and clever stuff how you can parse some jazen content. So if you have a Well, I don't really have jazen content handy, but I could do a import groovy jazen So, let's say you have some jazen equals a One two three for example and you you you're going to parse that new jazen slurper the jazen slurper that slurps This parse text jazen is it correct? What am I doing oh Yeah, double quote now, what do you say a double quote for one? Oh, yeah, that must be it Okay, thank you Well spotted so when you do I'm gonna put that in a data structure Let's say so what's nice the fact that it's a dynamic long wage I don't really know what this data which is return here Is about your in it's actually a map of list or a list of map or that kind of stuff And what's nice the fact it's a dynamic language is although there's no a property or get a Getter or anything like this you can do Let's say something more explicit Yeah, you can do author and print that so there's no You're really just going through the the graph of object and you can do data author I see if you had if you were working with java beans basically We have this nice jazen support already in both for creating jazen as well as parsing jazen But the the jazen Generator when you create the the output Your jazen content there wasn't much control on this so it was Things were done in a default way, so there was no control Instead now we added a jazen generator which allows you to have more control on how you're gonna Marshall for example this person class into Jezans for example you can say okay. I want to exclude news if they are dates I want a special format you can exclude fields, etc So you can customize the the the jazen that you create and also There's also the possibility of adding some converters for certain types or if there's a key so if it's Just for the URL of whose key in the jazen object or in in the the in the Pojo in the java beans So it might be only the One particular URL, but not all of them so you can say okay If it's for a particular key for favorite URL, then you do something all the way otherwise. It's the normal Normal serialization that happens so you can do that Up and the optional key, yeah So yeah, you have even more control Also something I could mention is that the we Benchmarked the the jazen supports with Jackson and we are usually faster than Jackson for both Parsing and serialization But then if you really want more control Jackson suddenly goes much further than our jazen generator So we spoke about AST transformations and some of the things I'd like to say a few words about a new feature which is AST macros So to create AST or code transformations you had to know the API of the AST that means Abstract syntax tree when the compiler Compiles your code. It's going to generate some kind of tree of nodes of a class node a method node a property node, etc code nodes, etc But then it's a bit cumbersome to create code transformations So we wanted to simply simplify things a bit. So here What my example is doing it's going to add a Method I forget what my example is doing So we want to add a new method to a class that would be annotated with the add method and annotation So before to implement that for transformation we had to extend We still have to but I'll say something There's a new feature as well, which simplifies this even further So for example, if you wanted to create a new method that returned 42 all the time you had to know so this example is a bit It's quite simple, but so we are going to add a get message that returns 42 get message method that returns 42 all the time So you have to know that The body of the get message method has to have a return statement within which there's a constant expression Etc. So you really have to know the API of how the nodes are The AST nodes are built together. There's a new thing Which is this so it's not much shorter, but for a more complex example, it can be much much shorter So there's this new macro shortcut, which takes a closure and which is going to analyze This code there to create the return statement which contains the constant expression, etc And then you it's going to return the return statement here. So we can just pass the body Simple The code it should be I think there's a mistake in my slide because the code that's the code So instead of simplest code that should be code or here it should be simplest code. I have to fix that So again, just pass that to this method node that is added to the class So in this example, as I said, it's not much shorter, but for more complex Method bodies That's much much easier because you really write the code that you want to be added in the body of that new method So there are different Yeah, different variants I can skip that Let's have a look at a slightly more complex example. Let's say that you want to add an MD5 method for each of the of the properties So let's see So for for example for all the fields that you have Let's say I've got a You know a name property So I have get name, but then I want to add get name MD5 So for each properties of a class, I'm going to add get some feel MD5 and I just have to Add so the the annotation I'm creating it's MD5 and I just need to add that I should have I should show the example first perhaps Because yeah, so I don't show you the the example But yeah, so it's really just about decorating so all the all the fields of the class Fields of type string you put at MD5 on the string on the on the type of that property And then automatically we're going to add a get the name of the field MD5 method. So how do I do that? With this Macro feature it's also possible to pass because the code is not always static, but it depends on the surrounding context There's a way to do variable substitution So here for each of the fields that I'm going to find of type string. I want to actually Pass the The actual Instance of that field so that that so that I can do the MD5 operation so return macro macro is doing this This job and then here instead of doing new You know new method call new Constant expression etc. So imagine a big big structure of representing the nodes of the code It's gonna somehow pass this code and generate the AST nodes and then you can also have some surrounding context in Influence what's going to do what we're going to do inside the the body so it's a yeah It's a bit complicated to explain but for those who are Implementing their own code transformation. It's going to simplify their lives, but it's yeah far from trivial What else yeah, sometimes So there's a macro Class here, which allows you to create because earlier you saw that I created a method node that I added to the class etc But if you want to create Expressions like a method node you couldn't do that you couldn't just generate the code So if you want to generate things like a class or methods directly There's this macro class concept so that you can retrieve the method nodes or the class nodes And then you can hear in my example. I can add the new method nodes to my class. Oh five minutes I'm really too slow on on this So Yeah, so I had some examples like this, let's say you want to create a lock So you want users to use that in their code But really you want things to be implemented that way then this is how you can do it with this new macro feature I'll skip these ones and I'll show you this so in There's a new parser a new grammar that's created In groovy to add support for Java 8 features or new operators, etc Because we had a very old parser We wanted to add new features, but the the parser wasn't very capable So we there's a nickname for that parser which is a parrot its name because it's Mimicking what the old parser does so all the existing Java source groovy source code Still compiles and parse normally, but we add new features as well So for example groovy didn't support the old do-while loop. So we added it back nothing special fancy there or Arranged initializers it was also another difference with Java. They weren't supported So now we're we're adding those things back We also have those new operators like in JavaScript for identity comparison. So three equals or exclamation markable equal And also these ones which I quite like I've never liked how in Java you had to do not Something instance off because the none is always external to that expression. So we created two new Two new operators there, so it's a Quite exclamation mark instance of an exclamation mark in So it's part of the new syntax Safe indexing that's when you want to look at what's inside a list or a key of a map But if the list is null or map is null It's going to return No directly instead of throwing an old pointer exception. So that's what we call safe indexing The so this one was from Java 7 the try with resource notation since we had all their mechanisms to handle resources We never get to Implement that but we are fixing that But there's an even groovier way of doing things than compared to Java Yeah So say a little bit louder We compile to Real Java, yeah, real Java So Here we had to really re-implement the the parser we were using on plur2 when the latest versions on plur4 So what we wanted to it was very complicated to evolve the language and add new operators, etc So we really completely rewrote the grammar, but still supporting the very exact same syntax, right? And well, we actually started with the Java 8 Grammar in in the first place and re-add it back the groovy features. No, no, no, no, no, yeah So it's really our own our own stuff Trying with resources who will also support the Java 8 lambda notations except that we have some additional tweaks like for example the The the return keyword is implicit although if you used curly braces here In Java, you would have to explicitly use the return keyword But some nice thing there is that we support default arguments just like in groovy closures So it's something where we go a little bit beyond Java 8 lambdas We also support method references. So when you have the double columns there so static methods instance methods and also Instance references or I don't know how we call them. So we will support. Well, just just like Java 8 basically We also support the method reference on constructors as well With this oops with this notation and it works also for things like arrays Yeah Yeah What's special there? Yeah, constructor references. Oh, yeah, this one the Elvis operator Which allows you to do something like this. So if name is not No Empty etc. We're gonna this expression just this part. It's gonna return name. Otherwise, it's gonna return a default value So it's called the Elvis operator because it looks like the Elvis smiley few We we never managed to find a Good name for that. So we we called it Elvis But then if you wanted to do an assignment you had to do name equals and name blah blah blah Use the Elvis operator. So we decided to create a shortcut notation for this Assignment and that we call the Elvis assignment. Obviously. So you can just do Question mark equals and that's very it's totally equivalent to to that of course So that's pretty handy and in all the languages like Ruby or JavaScript There are not in JavaScript, but at least in Ruby. There's there was an equivalent operator for doing that And I'll finish with that things like the the java doc comments before they were not kept in the AST so for code transformations, you couldn't access what was part of the Of the java doc comments. So we added that you get access to that and there's also a special annotation if you want to get if you want the guru compiler to actually store the content of the The the java doc in in the in the class file so that it's available via Reflections about fire reflection you can get the java doc as well if it's annotated inside with a computer And that's it. So sorry for rushing the the end So it's been a long road in the sense that Groovy was first announced in 2003 and I've been involved in that project since 2003 So it's been a long road it's project very Very active with the lively community and just Just the day before I didn't did some stats on things like download numbers and even just for the first four months of the year We we have already had More than 12 million downloads of Groovy across the the various version just since the beginning of the year And last year for the whole year. We had 23 I think 23 million downloads, so it's a pretty significant number and it's really used in tons and tons of various contexts, but as always Speaking about open source. We always welcome contributions. So if ever you want to Get involved in in this project don't hesitate To to give a hand and help us And that's it. Thanks a lot for your attention Can we have a few questions or one or two questions? Yes? I was sure I was sure I would get that question So what's so so cutlin is a quite nice language I I kind of feel at ease with Kotlin because there's a pretty heavy inspiration Taken from Groovy for for example the closure. That's the closure notation of Groovy It's got the it default parameter. Well, that there are many Short cuts or notation aspects which are heavily inspired by by Groovy in Kotlin And the other big inspiration is also it's kind of a mix of Scala plus Groovy basically Except that decided to only go with static Compilation and not with a dynamic aspect. So for things like our nice json support or XML support, etc. Which take advantage of dynamic aspects of the language So you don't have such things for example in Kotlin But otherwise, no, it's a totally totally fine language. I guess with the The fact that Google my own company decided to go with Kotlin for adding Kotlin as a new options for Android developers. I'm pretty sure we are going to see a big adoption for Kotlin pretty soon but Interestingly when the Android team decided to use Gradle the Gradle build tool which is using Groovy for its build DSL its build syntax We also saw a nice although it was kind of indirect We also saw a nice increase in Groovy popularity. Thanks to thanks to that. So I'm sure Kotlin We will see a nice big Will it be Just for Android development will it spread outside for the server side? Is that right? I don't know. We'll see but It's a it's a nice language as well. Yeah Any other question one more. Yes So we are So it's been available for a little while but not in a released version So we've encouraged people tons of times Please test with the new pasta test with the new pasta so that we can show that it worked Of course, we also parse our own source code all the Groovy code in the Groovy code base We'll so parse it and it works out of the box will so parse The the Grails web framework, I forget what the other projects are I think Gradle as well plus some some other projects Right back probably. Yeah, so we ensure that at least some of the key Groovy users open source projects using Groovy also work well with the new pasta So we are highly confident that it's going to parse every pretty much every 999% of a Groovy code in the wild We can do that But yeah, we are pretty confident so the We even went as far as checking that the so it's not just about the syntax But also the the AST nodes the the structure that is actually parsed because in terms of Even if it's the same bytecode which is generated There are sometimes different ways of representing the code in the form of an abstract syntax tree So we not only check that the syntax is the same but also that the structure of the code Generated by the compiler before it creates the bytecode is also actually the same. Yeah All right, thanks