Introduction to Groovy Scripting

IDT-GD-200 — Student Guide

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In this first chapter, we will set expectations for how the course will be delivered and the expectations to get the most out of this course. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Groovy extends the Java language rather than replacing it Groovy is actually written in Java as well. It was invented by James Strachan and was inspired by dynamic languages like Python and Ruby. There are other languages that aim to run on JVMs, but Groovy is unmatched for interoperability with Java. (Make sure the ladder page of features covered in Java is in this presentation) Apache Groovy is a powerful, optionally typed and dynamic language for the Java platform aimed at improving developer productivity. It accomplishes this by reducing the amount of boilerplate code required for common tasks, providing additional utility libraries, and allowing a more functional approach to Java. Its syntax is easy to understand if you are a beginning developer, and familiar if you are a Java developer. It’s so smoothly integrated with Java that existing Java source code can be compiled with the Groovy compiler, and third-party Java extensions just work. The rich ecosystem has existing projects for web development, multitasking, build and testing tools, code analysis, and GUI development. These partner projects are shown on the Groovy website, and we’ll come to that. There are many powerful features including things like closures that help clean up code and make it more functional. We look at Builders which allow you to create object graphs or mark up or JSON. You can compile Groovy code as well as run it as scripts. There’s compile time and runtime metaprogramming that we will dive into later. Domain Specific Languages are a way of targeting a specific class of problem with tailored syntax. The powerful scripting capabilities of Java make it really great for automating build processes, and for setting up automated tests. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

This is your go-to page. It contains links to all the other resources we will discuss in this course . For all the online resources, use the links from the home page rather than bookmark the page directly. The groovy documentation is excellent and should definitely be bookmarked to refer back to later in the course. You can find the groovy source code and see how some of the features that we’ll cover in the course are created. A good thing to do when using an open source project is to review the open issues that have been found. It will give you some of the limitations that have been encountered by other users and also will shed some light into how the community sees prioritization of issues. Reviewing these will also give you a sense of responsiveness and activity in the project. There is also a slack space that is dedicated to Groovy and its subprojects. It is fairly active and the community is really helpful. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Getting more information on Groovy is trivial. Just a quick google search will reveal many different resources like online tutorials, books, and the ultimate, stack overflow links. Groovy in Action gets my vote as being both the most comprehensive and practical guide to Groovy. As with any subject, there is a wealth of information available online. All the usual sources are there, and just waiting to be found by your favourite search engine. These three books are really great reference works. Experienced developers will tell you that almost all developer documentation tells you in painstaking detail how to do everything the language can do. These books tell you why you need to do these things. Books like these, written by people with intimate knowledge of the thinking behind the design of the language, will set you up for a successful career in the language. Groovy In Action is the undisputed definitive reference on the Groovy programming language, authored by core members of the development team. This book presents Groovy like no other can-from the inside out. With relevant examples, careful explanations of Groovy's key concepts and features, and insightful coverage of how to use Groovy in-production tasks, including building new applications, integration with existing code, and DSL development, this is the only book you'll need. Physically, it’s quite a large book. Updated for Groovy 2.4. Making Java Groovy is a practical handbook for developers who want to blend Groovy into their day-to-day work with Java. It starts by introducing the key differences between Java and Groovy—and how you can use them to your advantage. Then, it guides you step-by-step through realistic development challenges, from web applications to web services to desktop applications, and shows how Groovy makes them easier to put into production. I f you start getting into the groovy world, you're probably going to learn about Ken really quickly. He's a great instructor. He has an online video tutorials. Groovy brings you the best of both worlds: a flexible, highly productive, agile, dynamic language that runs on the rich framework of the Java Platform. Groovy preserves the Java semantics and extends the JDK to give you true dynamic language capabilities. Programming Groovy 2 will help you, the experienced Java developer, learn and take advantage of the latest version of this rich dynamic language. You’ll go from the basics of Groovy to the latest advances in the language, including options for type checking, tail-call and memoization optimizations, compile time metaprogramming, and fluent interfaces to create DSLs. Evaluate the books for yourself. Read the reviews, consider your own objectives, and choose the book or books that suit you best. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

How to Get the Most Out of This Course Instructor notes This is general advice and guidance, so feel free to add your own hints and tips. Like anything, you’ll get what you put into it back out of it. Student Notes This section is about the best ways to get everything you can out of this course. I know how exciting it can be to pick up a new course and want to learn a new subject. This is a pretty long course, there are over a hundred slides and over 14 hours duration. So there's a lot of information here to learn. Here are a couple of things that are going to help you be successful at this course. We have some exercises and quizzes in each section. After each exercise there is a review where your instructor and you will go through the exercise together. The exercises are carefully selected and designed to help you retain information. Please go through the exercises and if you don't understand something please ask. And the best thing to do there is ask on the forum because chances are somebody else is going to have the same question that you do. Your instructor will give you details of the forum that is relevant to this particular delivery of the course. So please speak up and we’ll make sure you get those answers as quick as possible. It is worth your time to browse other questions and answers too. Often, a question from a fellow student opens a point that had not occurred to others. The quizzes are designed as self-evaluation points, so you can check for yourself how you are going. So that's the exercises and quizzes. At the end of this course there is an additional resource section full of all the information that you are likely to need. It’s a list of books and links to articles, podcasts, newsletters everything that is available to you from the groovy community. Also if you're a blogger or you like to create screencast for YouTube I think another way to really learn a subject is as you're going through and you find a subject that you really want to try to get a better grasp on. Let's say you're really interested in closures and you want to learn more about them. You know when you learn something go out there and write a blog post about it or create a screencast about it. I can tell you from experience one thing that I've always found is when you want to tell somebody else about something you are probably a little bit harder on yourself and want to make sure you really understand that subject before you go tell somebody else about it. So those kind of resources have always helped me personally. Maybe they'll help you as well. We really want you to be successful at this course, instead of just passively watching me. We are going to go through some basics, then set up a full Groovy environment. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

The best way to begin learning a new language is to set up a development environment and begin experimenting with it. The objective of this chapter is to get you set up on your machine to start using Groovy. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Instructor Notes Student Notes Installing groovy for a development environment consists of ensuring you have a compatible java development kit (JDK), downloading the Groovy distribution, and setting a few variables on your system. To run groovy, you don’t need the full zip download, you only need to have the groovy jar file present in the classpath of the java runtime environment (JRE) that you are using. That is a difference for how you might want to deploy a Groovy app to a server versus set up a development environment. The Groovy distribution comes with the jar files as well as some tools that we’ll take a look at. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

You need to check that you have a JDK installed and verify the version of the JDK is compatible with the version of the Groovy distribution you are installing. To do this, you can open up a terminal window and run java --version and see what prints out. “How can I find out what JDK versus JRE is installed” You can run javac --version and that will tell you if the JDK is configured correctly. This would mean there is a previously configured JDK on your machine. It is a good idea to remove all installations of the Java Development kit before we start. Start the Settings application from the Windows Start menu, and search for “uninstall”. That will bring you to the page shown. Type Java into the search box and uninstall the listed applications. Set a GROOVY_HOME variable to point to the groovy install folder. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

If you need to install a JDK, you can use the Oracle JDK that is found here: Go to https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html You can also use OpenJDK which is an alternative to OracleJDK with less stringent licensing requirements. Here is OpenJDK - https://jdk.java.net/ To install the software, you can use an installer that you download or you can simply unzip the binaries and set up a JAVA_HOME variable to point to it. Once installed, you should be able to open up a new terminal and enter javac --version and get the version that you installed to print out. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

The Groovy Download Page Instructor notes Target time 1 min Student Notes This is the Groovy download page. In the top right area there is always a single button that will download the current stable version as a zip file. We just want to notice what the current stable release version number is. Then we scroll down as we see on the next slide… ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

If we scroll down the page, we come to the Windows installer for the current stable release. That is the one we want for this course. Double click on it, and let it run. Follow the instructor as they download and run the installer. Select the full install. The only thing you may want to change is the destination folder. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Groovysh is a REPL - Read Execute Print Loop - that is useful for developing scripts and testing syntax for a language. It is located in $GROOVY_HOME/bin This is a good starting point for small projects that you want to develop interactively. For example if I were writing a script to analyze the size of my files in a directory and print the top 5, I may start here. It is also a good tool to see quickly if the syntax you want to use will produce the results that you are expecting. Some developers find REPLs more useful than others though - if you are struggling through a one-liner, then REPL is probably the way to go. This is a useful tool for testing and prototyping code as you develop. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

groovyc is the groovy compiler. It is located in $GROOVY_HOME/bin. You will be using the groovy compiler non-stop in your development, but it is likely to be under the covers for you. The groovyc compiler generates java class files that are used by the JVM. An interesting fact about the groovy compiler is that the file extension doesn’t have to be .groovy - it can be anything you want it to be. It will, however, always generate a .class file. Because Groovy is an extension to the Java language, it has the same relationship to bytecode. You don’t have to compile your Groovy code into bytecode. If you want performance, you can run the compiler if you wish. Your Groovy files will be compiled to bytecode classes. The bytecode file will execute faster than the native Java file, but it will only work on the platform where it was compiled. There is no required naming structure for Groovy source files, but by convention they end with an extension of .groovy. Your instructor will demonstrate the use of the groovyc compiler. Notice the similarity of the name to javac, the Java compiler. A compiled Groovy class can be run directly from the Java virtual machine, as long as the groovy-all distribution is included in the classpath. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Groovy Console is launched via groovyConsole or groovyConsole.bat, both located in $GROOVY_HOME/bin Groovy Console is an interactive text editor with syntax highlighting. This is certainly useful for developing code if you don’t want to install an IDE or want to test out some syntax validation. You can create your script and execute it all in a single editor with the output printed in the bottom pane. It will allow you to author and execute your code in a single window. There are some other features in the groovy console beyond just the simple syntax highlighting and code execution. In the next slide we’ll see how to use the AST browser. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

One interesting feature of the Groovy Console is the AST (Abstract Syntax Tree) browser. It is accessed from Script/ Inspect AST or simply control T. Groovy Console can visualize the AST representing the currently edited script. This is particularly handy when you want to develop AST transformations or understand what ASTs that you are using are adding to the class. Th ese will be covered in much more depth later in the course, but since these are underpinnings of how Groovy works we are introducing the concept here. A list of useful compile-time transformations can be found at ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Intellij Idea is another great IDE from Jetbrains, the authors of Pycharm and other products. The free community edition is all you’ll need for this course. Your instructor will demonstrate how to create a new Groovy project and point to the Groovy installation folder. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

When Idea has finished the installation process, there are some options presented that are quite important. The recommended options are shown in the diagram. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Instructor notes Please refer to separate Word document. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Instructor notes Please refer to separate Word document. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Please refer to your lab guide Exercise 1: Three Ways to Hello World Task 1: Configure IntelliJ Idea Task 2: Hello World with Generic Tools Task 3: Hello Word with Groovy Console Task 4: Hello World with IntelliJ Idea

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Please refer to your lab guide

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Slide notes

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

We now have our development environment configured, lets get started exploring the Groovy language by seeing some of the basics. We will be showing our examples in IntelliJ and you’ll see some of the syntax highlighting and autocompletion in action during the presentation. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

If you have come from another language, importing pre-built code should be familiar to you. They are a way to leverage resources that have been developed someplace else. You don’t need to import classes that are in the same package (folder) as the class you are developing. Groovy imports many of the common libraries that you’ll utilize in your code by default. If you need to import classes from outside of the current package you can do so by using the fully qualified class name or by using a wildcard after a package to import all of the classes in that package. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Instructor notes Target time 2 min Have IntelliJ Idea already open in another window; it takes a while to load. Demonstrate the code completion for groovy in IntelliJ. Show the auto-resolve of the groovy.xml.MarkupBuilder in this. Student Notes When you are using code from other libraries, you can reference the class youre using by either the fully qualified name com.company.package.Classname. A more common way of doing this though is to simply import com.company.package.Classname or com.company.package.* and then just reference Classname in the code. The slide shows this import syntax but for a MarkupBuilder class. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Keywords are used by the compiler to let it know something about the context of the arguments around it. Using these keywords is restricted for naming variables/classes since it would confuse the compiler - if you must use a keyword or it makes sense to use one in the naming context, typically you would spell it differently. A common example is Class clazz = new Class() Because Groovy adds new features to Java, there are additional keywords that have to be taken into account. These links point you to both languages keywords - bear in mind that there are minor variations in keywords between versions. You don’t have to memorise them. Both Groovy Console and Idea will tell you pretty quickly if you try to create a user defined name that conflicts with the keyword list. As you type, you will see that keywords are highlighted in your program, so you know at once if the name you had in mind is not going to work You just need to know that the keywords exist, and set up a naming convention that will minimize clashes. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In Groovy, everything is an object, even the basic types like ints, chars, floats etc. If you don’t declare a type explicitly, then the type is adapted to whatever can store what you’ve assigned. By default, a number with a decimal point is a big decimal, not a double. We can force the use of double with the use of a suffix of d. That’s just one example of forcing a data type using a syntax, there is a link to show you other ways of declaring the data type on creation without explicitly assigning it to a class type. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Strings in Groovy are a special and important data type. There are actually 2 different string implementations that you can use. They are used so commonly that there are several ways to declare them and each way has its own features. Interpolation is important, you can use it to substitute variable values into a string. You can also use interpolation for executing an expression at the time that the string is written/evaluated. In this lesson we're going to look at working with strings in Groovy if we remember back to our discussion on data types. There are 17 total data types in Java. We have our eight primitive types or eight wrapper classes, and String. Any time we need to create a variable with more than one character in Java we use a String. A little recap here on how to create these different data types in Java. So when we want to create a character, we use the char data type we give our variable name and then in single quotes we place our character. It is an instance of java.lang.character. Now if we need more than one character, we use the data type String and we place our string inside double quotes. So this is a string. And now we print its class and this is going to be an instance of java.lang.String. So in Java that difference of single quotes versus double quotes matters. And as you'll see in Groovy it doesn't really matter until it matters. If that makes any sense. So let's just jump into Groovy here. We can still use characters if we want to in Groovy but by default everything is a string. This means that if we assign a literal character or a string to a variable and we don't give it a type we're just going to assume it's a string. Let’s use our def keyword where don't care what type this is. And I'm going to create a variable called c2 and then no matter whether in single or double quotes if we just place a character here and print this out. The value of c2.class is going to be an instance of java.lang.String. Snow we define str2 and we could place this in single quotes or double quotes and it doesn't really matter. This is going to be an instance of java.lang.String. We have two types of strings that we work with in Groovy; plain strings and G-strings. Plain old strings are instances of java.lang.String and G-strings or Groovy strings, as we sometimes refer to them, are instances of groovy.lang.String. Groovy strings are simply strings that allow a placeholder expression to be evaluated at runtime. You may have heard of this in other languages referred to as string interpolation. Here’s an example here from Java. If we add a variable called name of type String and we assign to it, then we had a message and we wanted to include our name in our message. We concatenate a literal, then concatenate the variable name , then concatenate another literal. So that's how we do that in Java. In Groovy we have a little bit cleaner solution here. In Groovy we can use our double quoted string and that becomes important. We can use the dollar sign curly bracket and then the variable name and that's all we need to do. So if we go print line message2 and run that you can see that we can use that to evaluate our expressions at runtime. Double quotes are important because that is the only time that we do the evaluation of our expressions. If we were to say message3 and do the same thing inside of single quotes and print that out all you're going to see is the actual literal of the dollar sign curly bracket name printed out. So in single quotes it's important to remember that we're just printing out what's there. In double quotes we will try to evaluate that expression at runtime. This doesn't hold true only for variables. This is an expression, so if we wanted to do something like message4 we can use expressions here, and now inside there we can just do some math. So this is a valid expression right. So now I can print out a message4 and you'll see that we're evaluating an actual expression in this case some mathematical expression inside of our double quoted string. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

We also have the ability to create multiline strings in Groovy. So in this case I want to create a large message and we can use single quotes or double quotes. We delimit the string with three single quotes or three double quotes and we can create a message that spans multiple lines. Now if we go ahead and print that out with println message, you'll see that that is our multi-line string. As you can imagine these single codes versus double quotes definitely matter. If we need to do a string interpolation or evaluate an expression in our multi-line message just as we do in a string or a single line message these double codes matter. If we need to evaluate that expression, we’ll want to use these three double quotes here to include something like 1 plus plus we need a dollar sign and curly brackets.. We still have one more little trick that we're going to look at and it's something called the dollar slashy. Say we had a variable called folder containing a Windows like folder name. C:\Groovy\foo\bar If we try to println folder, println is going to say that it's an unexpected character found. And that's because of the back slashes. Normally we must escape those characters. Now we could come through here and escape all of these if we wanted to and print that out and it would work fine. Sometimes you really don't want to have to do that. Instead, we can use something called the dollar slashy and what this allows us to do is write our strings out without having to worry about escaping them. So we start with the dollar slash (that's why it’s called the dollar slashy) and end with slash dollar. Anything in between is our actual string. And we don't have to escape anything in there. And so if we print this out now, we again get this but we didn't have to escape our slashes in our file name. This as you can imagine becomes very helpful when working with regular expressions. And we're going to look at that in the next lesson. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

All of the primitive types are represented by their object types in Groovy. This shows the mapping for both integral values and decimal values. This means that you can use object methods on the variable whether you create it with byte b or Byte b; you can use b.toString We also have the ability to create multiline strings in Groovy. So in this case I want to create a large message and we can use single quotes or double quotes. We delimit the string with three single quotes or three double quotes and we can create a message that spans multiple lines. Now if we go ahead and print that out with println message, you'll see that that is our multi-line string. As you can imagine these single codes versus double quotes definitely matter. If we need to do a string interpolation or evaluate an expression in our multi-line message just as we do in a string or a single line message these double codes matter. If we need to evaluate that expression, we’ll want to use these three double quotes here to include something like 1 plus plus we need a dollar sign and curly brackets.. We still have one more little trick that we're going to look at and it's something called the dollar slashy. Say we had a variable called folder containing a Windows like folder name. C:\Groovy\foo\bar If we try to println folder, println is going to say that it's an unexpected character found. And that's because of the back slashes. Normally we must escape those characters. Now we could come through here and escape all of these if we wanted to and print that out and it would work fine. Sometimes you really don't want to have to do that. Instead, we can use something called the dollar slashy and what this allows us to do is write our strings out without having to worry about escaping them. So we start with the dollar slash (that's why it’s called the dollar slashy) and end with slash dollar. Anything in between is our actual string. And we don't have to escape anything in there. And so if we print this out now, we again get this but we didn't have to escape our slashes in our file name. This as you can imagine becomes very helpful when working with regular expressions. And we're going to look at that in the next lesson. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Groovy supports many of the standard operators from other languages. Operator precedence is always important to know for a language. Usually this becomes important when you have a complex expression that isn’t evaluating the way you are expecting. If that is the case, reviewing the operator precedence is going to help you solve your bug. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

There are additional operators that are supported by groovy that are more specialized and will be covered later in the course. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Groovy truth, or the result of a condition, is implemented according to the objects that are being used in the comparison. The table shows how a condition will be evaluated if the object is of the type on the left. Remember that Groovy is an optionally typed language, so the same condition can have a different result based on the type of the argument. This is different from the way some languages handle truth, where the comparison of an object is essentially just whether the object has a reference or not. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

This one is a bit of a trick question. It declares a GString with a value of “Patrick”, but the println uses a String - and Strings dont support interpolation. That means that the output will simply print the characters without any substitution. How could we change this so that it printed Name is Patrick? Use double-quotes to print a GString instead. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

This adds 1 and assigns the result to i. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

We will dive deeper into these control structures later in the course, but for now we will have a brief look at what is true and iterating through a list. Zero as a number, and empty string and a null value are all treated as false. So the uninitialized variable nullString is treated in the same way as the empty string for the purposes of deciding whether it is true or not. This is an important point, and different from other languages you may have encountered. Iterating through a list is simple, as you can see from the screen. You can do it with the simple and obvious for loop, or the even simpler each method. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

We will dive deeper into these control structures later in the course, but for now we will have a brief look at what is true and iterating through a list. Zero as a number, and empty string and a null value are all treated as false. So the uninitialized variable nullString is treated in the same way as the empty string for the purposes of deciding whether it is true or not. This is an important point, and different from other languages you may have encountered. Iterating through a list is simple, as you can see from the screen. You can do it with the simple and obvious for loop, or the even simpler each method. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

We will dive deeper into these control structures later in the course, but for now we will have a brief look at what is true and iterating through a list. Zero as a number, and empty string and a null value are all treated as false. So the uninitialized variable nullString is treated in the same way as the empty string for the purposes of deciding whether it is true or not. This is an important point, and different from other languages you may have encountered. Iterating through a list is simple, as you can see from the screen. You can do it with the simple and obvious for loop, or the even simpler each method. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

We will dive deeper into these control structures later in the course, but for now we will have a brief look at what is true and iterating through a list. Zero as a number, and empty string and a null value are all treated as false. So the uninitialized variable nullString is treated in the same way as the empty string for the purposes of deciding whether it is true or not. This is an important point, and different from other languages you may have encountered. Iterating through a list is simple, as you can see from the screen. You can do it with the simple and obvious for loop, or the even simpler each method. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

We will dive deeper into these control structures later in the course, but for now we will have a brief look at what is true and iterating through a list. Zero as a number, and empty string and a null value are all treated as false. So the uninitialized variable nullString is treated in the same way as the empty string for the purposes of deciding whether it is true or not. This is an important point, and different from other languages you may have encountered. Iterating through a list is simple, as you can see from the screen. You can do it with the simple and obvious for loop, or the even simpler each method. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In this lecture we're going to take a quick look at exception handling. In Groovy, exception handling is pretty similar to Java. And we get some of the same logic so we can create a try catch finally block or just a try catch or even a try finally block. However there is a difference between Groovy and Java and it is a big one. In Groovy every exception is optional, and this goes for checked exceptions as well. And this means that they do not need to be part of the method signature. To explain that, here’s an example of what something may look like in Java. In Java we may have a method that is public void Foo and we're going to throw an exception like this: throw new Exception() If we do this in Java, this needs to be part of the method signature, so we need to say this does throw an exception. In Groovy we don't need to do that so we can just say foo and we can just say throw new exception(“Foo Exception”). So that right there is a big fundamental difference and one that many developers like. People dislike declaring exceptions as part of the method signature. This allows us much better exception handling in Groovy. So let's take a look at an example. We create a log and this will be an empty list. And I want to try something. I'm going to call foo but I want to check for an exception in case foo does throw an exception - which it’s going to. The method foo is going to throw an exception. You can always catch a generic exception or if this was throwing a very specific exception, maybe a null pointer exception, you can catch that as well. So in this case I know that it's just a generic exception so I'm just going to catch exception. I'm going to assign it to a variable called e so that we can use it. Now to add to the log list. I want to say you could call e.getMessage but we know that we can access that getter just by using the property notation here. And then we can use a finally block that is going to run basically at the end of this no matter what we do. We add a line to log and add the word Finally to it. Next, println log. Whenwe run that we see that our log list has caught the exception and added the e.message so the message for the exception is added to our log and then ran the finally block and performed any final logic that it needed to do. So that's just a quick example of us being able to do a try catch finally you can do try catch. Just try .. finally the same way that we can do it in Java. But again with that one big change that we do not have to have the exception declared in our method signature which is which is really nice. One final thing; Java 7 introduced what was known as a multi-catch syntax. We have a try block with come code in it, and we want to catch multiple exceptions so let's say we want to catch a FileNotFoundException and or we wanted to catch a NullPointerException we could do that and we're going to assign both of those to the variable e. And if we needed to figure out which one was actually caught, we could easily do so like this: println e.class.name println e.message So that's just another way of being able to handle multiple exceptions, and Groovy can handle this as well. Next is an exercise Introduction to Docker ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Here we see four types of comment in Groovy. The first type of comment is called the shebang It is only used in Groovy, not Java. It is understood on Unix systems. It gives the location of the Groovy environment, so that a file can be made executable and just called from the command line. It is ignored on Windows. The second and third are standard Java comment types. A pair of slashes appearing anywhere on a line tells Groovy to ignore everything on the line after that pair of slashes. The second type is a multiple comment, where Groovy ignores everything from a /* pair until it encounters */. The third type is a GroovyDoc comment that starts with /**. That can be read by documentation tools. It is used to document the purpose and usage of a class or method. There are tools out there that will then read these special comments and generate documentation for your code for you. Comments are good for adding context or clarity of code. They help any other developers understand what is going on in your code. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Instructor notes Target time 3 min A text file called assert.groovy is provided. Open it with groovyConsole and change the assertions to show how it works. *** Add the optional message to the assert! S tudent notes In Groovy, assertions are enabled by default. You must provide the assert keyword with an expression that normally evaluates to true. If the expression is true, nothing happens. If the expression is false, then just like in other languages an exception is generated. The big difference with Groovy is this. When the assertion fails, detailed information is included in the exception. You can use the Groovy Console to advantage in debugging just like the slide. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

What if the collection was empty? ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

What if the collection was empty? ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Please refer to your lab guide Exercise 1: Three Ways to Hello World Task 1: Configure IntelliJ Idea Task 2: Hello World with Generic Tools Task 3: Hello Word with Groovy Console Task 4: Hello World with IntelliJ Idea

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Please refer to your lab guide

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Slide notes

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In this section we look at simple data types in Groovy, and in the next we introduce complex data types. Also in this section we will discuss operator overloading. Strings in Groovy are everything you know from Java and other languages, and much more. As part of our discussion of strings, we will touch briefly on the world of regular expressions, a powerful, widely used and insufficiently understood way of searching and parsing all sorts of text strings. Java has the same simple data types We will look at some basic operators, and there’s a quiz at the end. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes In practice, a class name should be the same as the file that contains the class definition. Although in groovy this is not enforced, you should always do this unless you have a good reason not to. By default all classes and methods are public. By default all of a class’ properties are private, but Groovy will generate a setter method for the properties. If you specify a modifier, then the getter/setter methods are no longer generated by default. You can also declare multiple properties of the same type on the same line. Inside of the methods, you can also declare local variables to hold values. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

When you need data and behaviors structured according to the class for use in your project, you are instantiating an object. You can do this by using the keyword new - this means that you are going to set aside new resources to store the resulting object. You’ll notice when we used the new keyword we used the class name to tell it what blueprint it was. We also have the ability to set properties on the object at creation time through constructors. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Instructor notes Please refer to separate Word document. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes We mentioned that the by default all properties are private and that we will generate getters and setters for them. There are other modifiers that we will discuss here. Private means that only the class itself can access it. If you came from Java, you’ll know that protected means that anything that is in the same package or a subclass will be able to access it. This isn’t true in Groovy though - protected modifier functions broadly like the public modifier in Groovy. You can use @CompileStatic if you are really trying to enforce protected, but it wont work in all cases. Just know that in Groovy protected ~= public. I n this lecture we're going to start to dive into classes. So we've seen classes throughout this course so far and even in some of the exercise that we've worked on, but we really haven't had a chance to discuss them. And in this lecture that's all we're going to do. We're going to dive into them and start to break them down and explain and go through all of the different properties and concepts that you have here inside of classes. Classes in Groovy are almost identical to how they are in Java where we declare a class using the class keyword and then the class can contain things like constructors, fields, methods and initializers. We have declared a class person simply by saying class person { In Java you may have to declare an access modifier by prepending a modifier like private or public. The access modifier is what controls access to the class. By default, every class is public in Groovy, so we do not need to do that here. But you can change the access modifier if you need to for a particular class. Now we're going to talk about fields and local variables and create a couple. So let's start with some fields. Or you may refer to them as properties later on. But let's start with fields. So we've seen this before if we need to declare a property in a class. We give it a type or no type if you don't need to and you give it a name. We have a field called firstName declared as String firstName Save this and go up to script and go to inspect AST (control T on Windows or command T on the Mac), which gives us a display like the slide. The display shows us what this class looks like when it's created. By default, if we don't give it any access modifier on the field level, it's a private variable. And with that private variable, when we don't declare anything, Groovy creates getters and setters for us so if we have this firstName we get a getter getFirstName and a setter setFirstName. We’ll start to talk about more about properties when we get into Groovy beans later in the section. But for now, note that if you don't give an access modifier that’s what happens. Now what happens if we do give it an access modifier. We know that by default it's private. But what if we ourselves said private. If we were to look at the AST now, we would still see the private string firstName but you'll see there's no getter or setter in the AST, and that's because we’ve explicitly said that that field is private. So Groovy didn't generate anything for us. It's only when you don't declare an access modifier that it decides to do that. We'll get into more of that when we talk about Groovy beans. Now we can also declare multiple fields on the same line if we want. Both of our fields are going to be strings so we can say String firstName, lastName Now if we look at the AST, there is private String firstName private String lastName and we have generated getters and setters. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Instructor notes We mentioned that the by default all properties are private and that we will generate getters and setters for them. There are other modifiers that we will discuss here. Private means that only the class itself can access it. If you came from Java, you’ll know that protected means that anything that is in the same package or a subclass will be able to access it. This isn’t true in Groovy though - protected modifier functions broadly like the public modifier in Groovy. You can use @CompileStatic if you are really trying to enforce protected, but it wont work in all cases. Just know that in Groovy protected ~= public. Another modifier we will cover here is the static modifier. Static means that the value will not change in the class. So if you have a value like pi that you need to declare for a class, you may consider using a static modifier for the variable like this static int pi = 3.14 When we declare a variable, we can also not give it a type. We've talked about this before, the def keyword. We're going to create a field here with the name dob. But there is no type yet. And the type will get figured out later based on what we assign to it. But right now we don't want to assign a type to it. Again there are different access modifiers here too so we can say there is private which we have already seen, there is protected which can only be used in the same package, and there is public. So we can say protected String f1, f2, f3 We can create a private Date createdDate and we can initialize our fields right in line and say new Date(). That will be initialized to today's date. Now there's also two key words that we really haven't seen so far in this course. We have created a static field called welcomeMessage and we’ve assigned this a value of Hello. So we know that normally if we need to access a field in a class we need to create an instance first. So normally we would say Person p = new Person() p.welcomeMessage Normally we would have to do that to print this out but with a static field we can access it without creating an instance so we can say Person.welcomeMessage and if we print that out, we should have HELLO. This static variable can be changed. There are a lot of times where you don't want that to be changed. And the way that we do that is by using the final keyword so we would create a public static final which means it can't be changed. This is of type string. And normally this is what we call a constant. Other languages have the idea of a constant. Constants in Java and Groovy you'll usually see as all uppercase and that's how you know if you look at a field just by its name you'll know right away, that's a final static field that we cannot change, and that we can just access without creating an instance. So that's a little bit about fields. Now let’s talk about local variables. We have a method in our class, and inside our method here we have some variables. Now these variables are local to this method, whereas any field or any variables created inside the class but outside of methods are variables that are fields of that class or instances of that class. These have been declared inside a method therefore they are local variables to that method alone. So if we create an instance of person and say person.msg, Groovy is going to say I don't know what you're looking for because I can't find a field called msg in the person class and that's because it's local to this method. We can create a message in the method. We can also create firstName and initialize it to “Freddy”. Now firstName is not going to collide with firstName in our Person class because it is locally scoped to this method. So if we wanted to, inside the foo method we can say println “$msg $firstName: We're going to need an instance so we can say def person = new Person() println person.foo() And that prints Hello Freddy. So that's a little bit about fields and local variables. You're going to hear these referred to as properties sometimes. When we get into building Groovy beans, they are properties because a property is a field of a class and has getters and setters. This should start to make a little more sense as we go throughout this section. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Constructors are how you create new object instances. By default if you don’t declare a constructor, you get the no-args constructor. The no-args constructor will create an instance of your object with default values. If you declare a constructor, then you will not have a no-arg constructor created for you by default. Groovy adds another default constructor for us; the map constructor. This allows you to specify which properties should be set by using a map or property names to values. You should create constructors for common ways that you will create objects. It is also a way to control ‘required’ properties to construct an object. For instance, if you require a value for one of the properties to properly use a class, then you should not have a constructor that is no-args. Instead you should have a constructor that sets that property by default. In the last lecture we looked at creating our class and creating some fields and local variables. In his lecture we're going to start to build on that a little further and we'll look at constructors and methods inside our class. We have created our Person class again. And the first thing we're going to look at here is something called a constructor. When we create a new instance of a class, we say Person p = new Person() That new person is called an implicit zero arg constructor. That's how we get a new instance of a Person. In Java, we would need to create a constructor inside our class like the commented lines shown in the console. In Groovy, you have a little more power than that. Let our class have two private strings first and last. When we instantiate our class, we can use named parameters to supply values for these two fields. You'll notice that we don't have to create a constructor for every situation that we need to instantiate a class with. In other languages, you need a constructor for every combination of parameters that you decide to allow when users of your class create an instance of it. In Groovy all we have to do is to provide some named parameters. In the slide that is what we do with Person p = new Person(first: “Freddy”, last: “Nurk”) The because we have this line at the top of our file: @groovy.transform.tostring We can just say Println p and we can see that the new instance of Person has the first name and last name already filled in. So that's a little bit about the default constructor that we get. Now we can create our own constructors too. Let's say that we wanted to have a fancy constructor that would take the full name. Say we want to have a call like this: Person second = new Person(“James Dean”) We have to write a constructor that will understand that this one argument is the full name. And that's going to print line p. In our constructor we create a list of strings. That list holds both parts of the name. So we just take our full name and we're going to use a method in this String class called split which splits at the space and gives us a list called parts. In this case it's going to be two items “James” and “Dean” So we can say this that first is equal to parts[0]. And last is equal to parts[1]. if you are coming from Java then you're used to seeing something like this. If we run this, we get the same result as before. We have a person class that was instantiated, and then we took that one name, split it in two and assigned our first name the value of whatever the first index in that list is. So that's creating our own constructors. One note of caution: if you define a fancy constructor like this, you have to use it whenever you instantiate the class. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Methods are behaviors for a class. This is where you perform computations on the state of the object or you mutate the state of the object. You’ve seen in our scripting that just declaring variables or assigning them can be used - you can also declare new variables inside of methods and they are not accessible outside of that method. Methods also specify a return type. This is so that the caller of the method knows what to expect as a response. In Groovy, if you do not put a return statement, then the result of the last line is returned to the caller. If you use static methods, you do not have to have an instance of the class to call it. You can call it directly using the ClassName.method. Note though that static methods can only access static variables - this is how you are able to use them without a declared instance. Let's talk a little bit about methods. We have seen methods, we’ve created them in this course so far, but we haven't really talked about them. You may see this in Java you have something like public void Foo. And then inside of here we do stuff. It can take some variables too, String a, String b and when it’s finished it returns its result. So we're used to seeing something like that. Well in Groovy we know that by default our class and our methods are public by default, so we do not need the public keyword so we can just create something like String getFullName. And then again, we don't need the return keyword. If you remember the last line of this method is going to be returned so we can just concatenate the first and last names and that's how we get our full name. Now on top of that we don’t even need to define the return type. So if we wanted to, we could say def bar() and all this is saying is that we don't care about whatever we're returning from this method. It could be a number, could be a string. It could even be void if we wanted it to be. Just like fields we can also create static methods. So if we wanted to create a static method called doGoodWork and all this thing did was print line doing good work just like before. We don't need an instance for this; we can call this method on a Person class which is not an instance. And if we go ahead and print that out you'll see doing good work. So let's talk a little bit about method arguments as well. So we know that we can create a method called some method and we know that we could create an argument. So we may have an argument called numbers and we can do something with that argument. If we wanted to type this out and we knew that this was going to be a list we can assign it a type. If we don't assign it a type, it's almost like saying that we don't care what you pass into here, you could pass in a string you could pass in an integer you could pass in a list if you wanted to. But even with methods and method arguments, if we know what this is going to return, always try to type that out so we know we’re going to take a list of numbers here and we’re going to return a List. So that's a little bit about arguments. We can also assign a default values so if we know that we need a list and we know that there are chances that we may not get a list passed to us this is the default value of list. You know this is really helpful too when you're creating Booleans. In the example we have a Boolean canAccessAll, but you want the default for this to be false. And then as we covered earlier, there are cases where you won't know how many arguments you're going to pass to a particular method. So you can use something called the Varargs and that just allows us to not really care about what how many arguments we're going to get. So in this case if we were to call concat we can pass in one two we could pass in three or four if you needed to and that is the Vaargs and that just allows us to supply as many arguments as we need. So I think that's where we're going to leave it. We looked at creating constructors. We also have that implicit constructor that we can supply named parameters to, to initialize our class. We also look at methods, a little bit of the differences between Java and Groovy. We know that the by default, access modifiers are public. If we don't care about the return type or if even if it's going to be void we could use the def keyword. And again we have the ability to create static methods. We have the ability to supply arguments to our methods and type those out and supply defaults. So over the last few lessons we looked at creating a class, how to create fields and local variables, methods and constructors. These are the building blocks of your classes with that knowledge you have everything you need to start constructing classes. Let's move on to the next lecture and we'll start to talk about how we structure our programs ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Packages are hierarchical structures that hold classes. Think of a file system where you have a folder with subfolders. The folder would be a package, the subfolder would also be a package - and the subfolder package would contain the .path to the parent folder. The name of the package provides a namespace of sorts to tell you where a class is coming from. Your package structure should be something like com.companyname.department.project. etc. This will give your classes meaningful names and if you are designing libraries for use outside of this project, then it will give them an idea of where/why it was developed. When we started this course, we said we would be looking at a couple of different ways of going through some of these examples. We started using Groovy console for a lot of the lectures because it's really easy to fire up and there’s no need to create a project. You just write some code and see how it works. As we start to get into some more complicated things in this course, we're going to be using IntelliJ Idea. It’s a great IDE, but feel free to use something else, like Eclipse for example. So in this lecture we're going to talk about organizing our classes into packages. So we looked at how to create a class, and all the different things that go into a class. But as you can imagine as programs start to grow the number of classes are going to grow very quickly. We need a way to organize those because we don't want to have one folder with 150 different classes in it because it makes finding them very hard and it just isn't well organized. Just like Java, there's a way to organize these, and the way we do that is to use packages. A package is really nothing more than a directory and you'll see that as we jump into this demo. But just like in Java, in Groovy we must declare what the package is before we define the class and when no package is declared we use the default package. And this will make a little bit more sense as we proceed with this demo so let's go. So in IntelliJ, we're going to create a new Groovy project. We're using 1.8 and 2.5.8 at the time of this recording – image 1 on the slide. Our project name is Package Demo and click Finish – image 2 on the slide. Now that our project is created we can see that when we create a new project we have the source folder or src folder where we're going to put all of our source code. If we right click on the src folder, we have the opportunity to create a new package – image 3 on the slide. On Mac, command t will also work, as will control-t on Windows. So let’s assume we're building an MVC package, we will use this as an example because it makes sense. We're going to build out an MVC application slot in Spring Boot. In Spring Boot you have controllers, views, services and domain objects. Good practice is to put these into different packages. So we're going to start with a new package and we're going to start with the base package which is going to become IdfAcademy, and usually you use this as a reverse domain name lookup so you have com. and then whatever your domain name is – image 4 on the slide. With that as our base package we create a new application called Application in our base package because we really don't want any code in the default package, we want to have everything inside of a package somewhere. So just looking at a class we know what package it's a part of right away. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Please refer to separate quiz document ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Inheritance occurs when a class extends or implements another class. It is in this way that we can compose objects to create something more specific, but still meets our needs. The interfaces or abstract classes that you inherit from or extend provide a contract for what a user of the class can expect. It is also a way to ensure that the user of a class understands the intended purpose behind it. In groovy you also have traits that you can inherit - which in a way allows you to extend from more than one class. In the next few lectures we're going to start to dive into some of the key concepts in object oriented programming. And we're going to start to walk through them and take a look at a few examples and how they work but we're not going to dive too deep in them. In this section we're going to talk about inheritance. Inheritance works in Groovy pretty much the same way it does in Java. We won’t spend too long in discussions on when to use inheritance how much not to use it, when to favor other methods of extending classes, things of that nature. We just want to show you how inheritance works in Groovy, so on screen is the project we were working on last; that simple package demo. We deleted the three classes we added earlier. Let’s look at what inheritance is and where it comes in handy. So we've learned how to create a class. We know how to add fields or properties to it. We know how to add methods to it. But what we want to look at here is really kind of slimming down our code when we can. And the way that we do that is when there are objects that are similar to other objects, we can make use of inheritance by inheriting both fields and methods from other objects. So that may sound kind of funny at first but let's take a look at that example and maybe that will help it sink in a little. We created a new domain class and this domain class was called Phone. So we can start to think about what properties belong to this particular class. And we can think of a phone, the phone in your pocket. It may have a name. It may also have an OS so whether it's running iPhone or Android may also belong. They may have an app store so this could be the Google Play store or the Apple store. So those are some different properties of a phone that no matter what phone you have. They could all have in common some methods as well. And so there may be a way to power around this phone. There may be a way to power off. There may be a way to have it ring. We're keeping it generic here on purpose, but these are the properties and methods that make up a phone. Well now what if we wanted to create specific phones. Let's say we wanted to create an Android phone or an iPhone class. We can do so. We created a new class called IPhone. Now in this class there are a lot of shared properties with Phone like name, appStore and os. We wouldn't want to have to recreate those every single time we created something that was like Phone. So we're not going to do that here. What we're going to do is use inheritance and we're going to inherit all of the properties and methods of the phone class and the way we do that is by using the extends key word. And now we need to provide a class that we're extending from. That’s all we need for now. So we're going to extend from Phone and we're going to use the IPhone class. We created a Groovy script right in the base package and called it app.groovy. Yes. The statement is just Iphone myPhone = new IPHONE() The new IPhone class has no properties in our definition, but we can use named parameters when we construct the object because the IPhone class has all the properties and methods of Phone. So we can say IPhone myphone = new IPhone(name:'iPhone', appStore: 'Apple Store',os:'ios') When we start typing that statement into the IDE, we keep getting suggestions on what properties to use. So now we have this object and we can use println to see a ToString representation of our new iPhone. And again the iPhone class itself doesn't have any implementation of the name property, but we can use println to print it out. And now in this case let's take a look at another thing here. As we develop our little application, we can define things that are specific to the iPhone so maybe we have a property called os version maybe we have a method called home button press, because there's a home button iPhone, or maybe we have a way to call airplay. We may well develop specific properties and methods that belong just to our iPhone. But what if there was a certain situation where we needed a method that was in the Phone, And perhaps for whatever reason this powerOn method in the Phone class doesn't do exactly what we needed it to do. We want our own version of the powerOn method, but we want to change the behavior. What we can easily do is define our own method here, also called powerOn. What we're doing here is overriding the method in the phone class. That was in the base class and we're going to provide our own implementation of that particular method. You can see this is one of the great benefits of using an IDE. It flags this method s an override, and with one click it will take you to the method that is being overridden. So that's just a little bit about inheritance. It just helps clean up your code. It helps keep your classes clean. There is no need to repeat functionality. If they have something in common with another class that extends keyword allows you to extend the class and they're going to pool and inherit those properties and methods. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Inheritance occurs when a class extends or implements another class. It is in this way that we can compose objects to create something more specific, but still meets our needs. The interfaces or abstract classes that you inherit from or extend provide a contract for what a user of the class can expect. It is also a way to ensure that the user of a class understands the intended purpose behind it. In groovy you also have traits that you can inherit - which in a way allows you to extend from more than one class. In the next few lectures we're going to start to dive into some of the key concepts in object oriented programming. And we're going to start to walk through them and take a look at a few examples and how they work but we're not going to dive too deep in them. In this section we're going to talk about inheritance. Inheritance works in Groovy pretty much the same way it does in Java. We won’t spend too long in discussions on when to use inheritance how much not to use it, when to favor other methods of extending classes, things of that nature. We just want to show you how inheritance works in Groovy, so on screen is the project we were working on last; that simple package demo. We deleted the three classes we added earlier. Let’s look at what inheritance is and where it comes in handy. So we've learned how to create a class. We know how to add fields or properties to it. We know how to add methods to it. But what we want to look at here is really kind of slimming down our code when we can. And the way that we do that is when there are objects that are similar to other objects, we can make use of inheritance by inheriting both fields and methods from other objects. So that may sound kind of funny at first but let's take a look at that example and maybe that will help it sink in a little. We created a new domain class and this domain class was called Phone. So we can start to think about what properties belong to this particular class. And we can think of a phone, the phone in your pocket. It may have a name. It may also have an OS so whether it's running iPhone or Android may also belong. They may have an app store so this could be the Google Play store or the Apple store. So those are some different properties of a phone that no matter what phone you have. They could all have in common some methods as well. And so there may be a way to power around this phone. There may be a way to power off. There may be a way to have it ring. We're keeping it generic here on purpose, but these are the properties and methods that make up a phone. Well now what if we wanted to create specific phones. Let's say we wanted to create an Android phone or an iPhone class. We can do so. We created a new class called IPhone. Now in this class there are a lot of shared properties with Phone like name, appStore and os. We wouldn't want to have to recreate those every single time we created something that was like Phone. So we're not going to do that here. What we're going to do is use inheritance and we're going to inherit all of the properties and methods of the phone class and the way we do that is by using the extends key word. And now we need to provide a class that we're extending from. That’s all we need for now. So we're going to extend from Phone and we're going to use the IPhone class. We created a Groovy script right in the base package and called it app.groovy. Yes. The statement is just Iphone myPhone = new IPHONE() The new IPhone class has no properties in our definition, but we can use named parameters when we construct the object because the IPhone class has all the properties and methods of Phone. So we can say IPhone myphone = new IPhone(name:'iPhone', appStore: 'Apple Store',os:'ios') When we start typing that statement into the IDE, we keep getting suggestions on what properties to use. So now we have this object and we can use println to see a ToString representation of our new iPhone. And again the iPhone class itself doesn't have any implementation of the name property, but we can use println to print it out. And now in this case let's take a look at another thing here. As we develop our little application, we can define things that are specific to the iPhone so maybe we have a property called os version maybe we have a method called home button press, because there's a home button iPhone, or maybe we have a way to call airplay. We may well develop specific properties and methods that belong just to our iPhone. But what if there was a certain situation where we needed a method that was in the Phone, And perhaps for whatever reason this powerOn method in the Phone class doesn't do exactly what we needed it to do. We want our own version of the powerOn method, but we want to change the behavior. What we can easily do is define our own method here, also called powerOn. What we're doing here is overriding the method in the phone class. That was in the base class and we're going to provide our own implementation of that particular method. You can see this is one of the great benefits of using an IDE. It flags this method s an override, and with one click it will take you to the method that is being overridden. So that's just a little bit about inheritance. It just helps clean up your code. It helps keep your classes clean. There is no need to repeat functionality. If they have something in common with another class that extends keyword allows you to extend the class and they're going to pool and inherit those properties and methods. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In this lecture we're going to talk about interfaces. If you are coming from the Java world, a common style of programming is using Java interfaces. In this lecture we're not going to dive deep into when to use or the theory behind interfaces. Instead we will look into what interfaces are, and how we can use them in Groovy. An interface is like a class in some ways, and the best way to do this is to look at a demo. First a new package called Domain. We have a Person class, that has properties first and last, in the Domain package Then we create a new package called Service. In that MVC world let's say I have a service layer here and I have a Person domain object. To create a new Groovy Interface, we select the option to create a new Groovy class. In an IDE there is usually a way to create an interface separate from a class. In IntelliJ, there is. It is in the dropdown below the class name when you select create new class. The interface which we're going to look at now and trait which we're going to look at in the next lecture. So we're going to choose interface and a common naming structure for an interface is to use a capital I. So we used a capital I and PersonService so IPersonService and then Interface then click OK. So now you'll see the big difference here is instead of the keyword class there is the key word interface . An interface is a little bit different from a class in that it's just a contract. It's a group of abstract methods. And that's what that means is that we're going to define the name of the method and the return type, but we aren't going to define the method body. We’re not going to implement any logic. So let's say in a PersonService world in MVC structure there might be a reason to get a list of people. So we need a method to return a list of persons. So we need a method called findAll and we may need a method to return a person and we might call that find. So now the difference is you'll see we have no method body here. If we were to create our braces and try to do something here, we would see a bunch of red squiggly lines come up and a message that abstract methods must not have a body. So again in an interface we are defining the logic; we're defining the contract. And if you want to implement this interface then these are the methods that you must provide. And again this is just a kind of paradigm in Java using using interfaces and we'll get into it as we go through the course. So now we have this IPersonService and what we're saying is if you want to implement this interface then you must provide an implementation of the find method and the findAll method. This needs a class called PersonService in the package Service. So this is just a normal class but now in my PersonService I'm going to implement an interface. So if we start typing this out IPersonService, at this point this is implementing the IPersonService but we're getting squiggly lines so at this point. The IDE is complaining to us saying you're implementing an interface but there's two methods in that interface and you haven't implemented those methods. So now we could type those out exactly as they were in the interface, or we could make use of the IDE and we can just right click and go to generate. The IDE says here's all the methods that I found in the interface. Which ones do you want to implement. And by default it's going to select them all. Because that's what we need to do. It's also going to insert and override annotations so go ahead and click OK. And now you'll see that the error has gone away. At this point we can add the correct logic to each of the methods. We've implemented logic for all the methods that were in the interface. We have a script in the base package of this application, and it is going to create an instance of our Person service. So this is of type PersonService and we say PersonService personService = new PersonService() And to show that it is all there we can println one of the PersonService properties. And there's our person domain object. So again really what we're talking about if you're coming from the Java world you may be used to following the strict style of coding that really encourages using interfaces and what we're trying to get to here is that in Groovy you can do the same thing. There are other maybe opportunities to shy away from this and use dynamic programming and traits and get away from this. But if you want to use interfaces, you can just as you do in Java. So I think with that will close this one up and let's go ahead and move on to traits. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student Notes Typically these are used when high-level functionality will not change, but sub-level functionality may change. Think about a car. It accelerates and decelerates regardless of the engine that powers it. So accelerate could be implemented as rotateMotor() and rotateMotor be abstract since we know that a motor changes the speed of a car. We just dont know what motor it has or whether it uses gas or electricity. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In groovy you also have traits that you can inherit - which in a way allows you to extend from more than one class. I say that it is similar to extending because OK so we just finished up with interfaces and now it's time to look at a really cool feature in Groovy called traits and traits basically allow us to compose capabilities into our classes. Like interfaces we have something that we need to inherit. A trait is a capability that we want to do something but when we implement a trait we can actually Inherit the implementation of that trait. So if this starts to sound a little bit like Java 8 default methods then you're on the right track. The big difference here though is that a trait can contain state. As an aside, we will discuss Java 8 default methods briefly because they are similar to what we are looking at with interfaces. In Java 7 and earlier, an interface cannot have a method body. In Java 8, a new keyword was introduced, default . Now in Java 8 you can say this: public interface IPersonService () default public void doSomething() { System.out.prntln(“Doing something…”) } } This means that in Java 8, when you implement IPersonService, you may implement the doSomething method, but if you don’t then the implementation in the interface is used because it is the default. So that's a little bit about interfaces in Java its default methods. This is going to work similar in traits but we can also add state and a couple of other things to our traits that make them a little special. In the documentation, there's some great information on traits. Now we're going to go over a few of these things. Probably not every little thing though so you may want to read through this documentation when you get a chance. Traits are a structural construct of the language which allow us to have composition of behaviors, runtime implementation of interfaces, overriding specific behavior and a few other things. They are the same as interfaces, but with the addition of two things; default implementation of methods (like Java 8) and state. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes Here we are showing in IntelliJ a programmatic example of how this works. This is the example right out of that documentation just in case you go back and read through it you'll be pretty familiar with what's going on. When we create a new Groovy class remember that we can change the type. Here we are creating a trait called FlyingAbility. In the left hand red rectangle the name is FlyingAbility and it's a trait so click OK. It looks like a class; it has a package declaration, it has some keyword and then a name. The only difference is instead of class or interface we're using the keyword trait. So now that we have our trait here. You remember in the interface we had to define an abstract method. Well here we can define an implementation so we can define a method called Fly and it is going to return a string. In the bird class and we implement the FlyingAbility, that's our trait, we've now implemented that method fly without having to override it. There's no there is no contract now that says we must implement that particular method. Now if we create a bird in app.groovy, we have to import that bird. Now we can reference the new bird and assert that the fly method is equal to the string. If we run this, we should see nothing in the output which is good. That means that the assert was true, and it was. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes We can go on and create more so let's create another trait and we're going to call this SpeakingAbility. And again this is a trait. We have a method speak that returns a string.. So now the bird class can implement not only FlyingAbility but also SpeakingAbility. Back in the app we can assert b.speak() equal to “I’m speaking”. When we run that again, no assertion fails we are good to go. So that’s traits at a high level. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes Back to the FlyingAbility trait, we can add an abstract method So let's go and say abstract foo. Now we can’t define a body here because we've said that this is going to be an abstract method. If we save this and go back into our Bird class now you’re going to see that it's complaining because it has not implemented the method that we have defined as abstract so we can say implement method and get the opportunity to implement foo. We can also do private methods inside our trait. When we went into our Bird class, there was no mention of the bar method, because it is private and we’re notallowed to use it. There is no contract with this class and no need for us to implement it. We can create public or private fields so we can say public string, private string and we can start to give our traits state so you can begin to see just how powerful traits can be and why they're used in so many places. I'm a big fan of the Grail's framework and Grail's uses them heavily. In fact they went through a huge rewrite in version 3.0 to make more to make heavy use of traits just because it makes things so much easier. One thing to note; although it is legal to create public properties in traits, the documentation notes that it is not considered good practice. If you are interested, look up the diamond problem and Groovy. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Everything in Groovy is an object - even Groovy Scripts. You can see that the simple one-line script has been wrapped into a class with some other stuff in it. The main part of this is the run() method - which is primarily where our code will be. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Groovy provides many annotations that we can use to simplify our life writing code. We have already seen the ToString annotation in a previous exercise. Here we look at the effect of just one annotation; Immutable. By adding a single line preceding our class definition, we make our class immutable. If you look at the AST transformation at the Output stage (see the dropdown at the top), we can see how much work Groovy does for us. Immutable classes have to be defined as final, and there are many other rules that apply to the definition of properties and methods. Groovy implements all these rules, in the correct syntax, instantly and automatically. The appropriate constructor is also generated, along with a hash code method, and getters and setters for all our properties. You could do all this yourself, but why would you? ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Because we have annotated the Person class with Immutable, when Groovy built the class it has made it impossible to change a Person object after instantiation. In the slide we have written a program that attempts to update the first name property, and an exception is thrown. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Instructor Notes We are going to create a class that represents a single tweet. This is an exercise both about code and starting to think about what goes into building a class. There is no right or wrong answer here so don't be afraid to build your class how you see fit. I will go over in the review what I was thinking about when I built mine but again my answer is not the right one. .

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Please refer to your lab booklet

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Slide notes

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Operator overloading allows you to provide an implementation for the operators we discussed in the previous chapters. This comes in handy when it makes sense that you would be able to add two types together or when using different logic operators makes sense for a different type. Using the operator instead of calling a method named something else will also make your code more readable and easily maintained. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Operator overloading allows you to provide an implementation for the operators we discussed in the previous chapters. This comes in handy when it makes sense that you would be able to add two types together or when using different logic operators makes sense for a different type. Using the operator instead of calling a method named something else will also make your code more readable and easily maintained. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

This just reiterates that the basic types in Groovy are objects and that you can call the methods on those types as well. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

What about being able to override that plus method for objects that we create? We're going to start with a pretty simple example here to give you an idea of what is possible. Let's say we started out with a class called Account. This account is not going to have much going on but we will have a big decimal and we'll call that a balance. That is, we have a balance in our account. So now let's say we created an account called savings and we set a new Account and our balance in our savings account is one hundred dollars. Then we create another account called checking and in our checking account we have a balance of $500. Say we want to ask “give me the balance of my savings plus my checking account”. If we go ahead and run that there is going to be an exception thrown and that error is going to be “No signature of method account.plus” What we're trying to do here is add one account to another, but we have no way of handling this. Well we know that from our previous example that in the number class there is simply a method called plus that knows how to add two numbers together. What we can do here is create our own plus method that knows how to add two accounts together. So let's do that. We're going to return an account, so we're going to have a method called plus that accepts another account as an argument and all that's going to do is going to return a new account. Remember the return statement is optional and we do not need it here. The last line of this method is an explicit return. Now we will have a new account and we're going to take a balance of this account. We create a toString method to write out a nice line displaying the account balance, just for simplicity. The first object is the caller, and the second object is the parameter other. So in this case savings plus checking so savings is going to say: “I want to take my balance and I want to go ahead and add the other balance” We know in this case that these are numerical values so just simply calling the addition operator will work out just fine. Our method is going to return a new account that has a balance of the balance of this object plus the other balance. If we run this now, we should see that the account balance is 600 and that is because that is returning a new account object and it's taking the new account and then calling plus on it. The println function calls our toString method to display the balance of the savings, the checking, and the sum of the two balances. This is a really cool case of operator overloading. We have seen that we can take objects like the numbers that we looked at in the previous example and call plus on the two numbers. Then when we call plus on two strings, the string class overloads the plus method to concatenate two strings. This gets even cooler when we can overload those operators for our own custom classes. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In previous sections we looked at the different Java and Groovy data types. We saw some of the differences when we moved to Groovy and we briefly want to expand on those when it comes to numbers. If you haven't used Java before don't worry about it but if you have, there are some important differences with Groovy when it comes to working with numbers. Just to recap if we come in here and create a variable with the value 10 and print out its class, we know that from the previous section that is going to be an instance of Java.lang.Integer. We also know that if we come in and create a decimal say 10.5 and print its class it’s going to be an instance of big decimal. And just to recap those it's important to know that by default those are the different types of classes Groovy is going to use for those types of numbers. So next I want to talk about converting data types. Many programming languages support the conversion of a value into another of a different data type. This kind of conversion can be implicit or explicit. Implicit conversions which are also called coercion is automatically done for us. Explicit conversions are also known as casting. So let's look at the example of that. So here we have a particular variable I'm going to call myFloat and I want to assign it a value of 10.5. Now we know by default that is going to be a big decimal. But in our case, we want that to be a float so we can actually tell the language here that that is going to be a float.. And so if we print that out now it’s an an instance of java.lang.Float because we explicitly told the compiler that even though you want to default this to a decimal we want it to be a float. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Coercion is forcing one type into another type. You need these if the standard type for the operation wont fit your needs for some reason. A typical use case for this is with the precision of floating point numbers. Another common place for coercion is when performing operations on Lists or Maps. We’ll go over those when we cover those types though. Implicit coercion where the type is inferred from the result of an operation, and explicit coercion or casting. An example is when two floats are added, the result is a double! Not a float. This is very important and the table we covered illustrates the resultant types from operations. Implicit coercion actually happens behind the scenes. Now we're going to look at some coercion that happens when we have different data types in our operations. We will look at some rules when we're doing things like addition subtraction multiplication and division. The first rule we look at is: If either operand is a float or a double, the result is always going to be a double.. This is very different from Java. In Java, if only floats are involved the result is a float. But no matter what if a float or double is involved in Groovy the result is a double. So here in this first case we have a float and we have a double. We have a result that divides our double by our float. We print out the result and then we print the class. And so the result comes out the 2.0 and the type is an instance of java.lang.Double. Here we have two floats and we do the same thing. And if we print that out that again is the instance of java.lang.Double. So again in Java that would be a float. Here in Groovy that is a double. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

If there is a big decimal involved, the rules are consistent, but may not be what you expect. As we saw earlier, if either operand is a float or double the result is a double. That includes a float and a big decimal. If there is no float or double, and either operand is a big decimal, the result will be a big decimal. There’s no image for this, but integer addition subtraction and multiplication rules are the same as in most languages. If either operand is a big integer the result is going to be a big integer. If it’s a long and an integer the result will be an integer. You don’t need to memorize these rules, they are available in chart form with a simple search, and you will get used to them quickly and easily. Integer division is a special case that we’ll deal with on the next slide… ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

If we use double precision floating point numbers for arithmetic, sometimes we can encounter small errors as in the first example. In the second example we see the same operation in decimal arithmetic, with an expected result. Now we see why decimal arithmetic is generally preferred in Groovy. Integer division in Groovy is different from Java and most other languages. In java and other languages int 15 divided by int 2 will give int 7. As you can see in the slide, in Groovy the result is BigDecimal 7.5! That is a correct, but possibly unexpected result. In order to achieve the same result as other languages, Groovy provides the intdiv method. This you should memorize. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

The Groovy Development Kit provides many helpful methods. Here we will discuss a few of them. First a few for converting between strings and numbers. The toInteger method will convert a string or a decimal fraction to an integer. It will throw an exception if there are non-numeric characters in the string, which is where isNumber method is useful. More methods coming up… ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

The DGK provides some very nice iteration methods. Here we write the number 20 and we're going to look at this thing called times and this allows us to go ahead and loop that many times. We want to say 20 times I want you to perform this operation. So in this case we’re just going to print the dash in single quotes and if we go ahead and run that you’ll see that print out. We can also do something like this. We can say I want you to go from one up to the number 10. This is a little teaser type introduction to closures; we’ll get into them later in the course. Here we can say print line., and you can see we go from one up to 10 including 10, and then every time we just print out that number. Now we can also do it the other way and say downto 1, and if we run that we get the opposite of the upto. So we have times, upto, downto and finally we have step ,which allows us to step through a number range with a given precision. n this case we're going to start from zero to the number one but we want to step each time, incrementing by 0.1. And now if we go ahead and run that you can see that we are stepping from 0 to 1 but we're incrementing By 0.1 every time and printing out that number. So when working with these numbers, especially for coming from Java, it can feel a little bit funny or wrong at times to be calling methods on these numbers, but you just have to remember that these numbers are actually objects that we're working with. And once you start to remember that all of this makes a lot more sense. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Slide notes

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Please refer to your lab booklet

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Slide notes

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In this section we are looking at complex data types, known as Collections. The methods and properties Groovy gives you to work with collections are really powerful and easy to use. One of the best features of the language, in fact. First, we look at Ranges. Ranges are available in Groovy and other languages, but not in Java. We look at Lists, both array lists and inline lists. We look at Maps as well. Collections are so useful that you will find them everywhere and use them every day you are programming in Groovy. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Regular expressions are a staple of coding and scripting. They are a way to express patterns of characters that can be matched within text. They come in many different flavors and range anywhere from very simple - for example \d to match a digit to very complex like matching an email address according to the RFC. http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html This section will explain some of the basics of regex and how they are integrated directly into the Groovy language. Groovy even supports special operators as shortcuts to Regular Expressions. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Instructor notes Target time 2 min Student Notes Using these tools will give you a guided approach to learning regex in a bit more depth. The regex101 site is great to use during development of a regex and I use that often. It is very intuitive and you can paste the text you want to match directly into the tool. This course will cover some of the basics to be used in examples though. The page shown here is a free resource that has been stable for several years now. Its address is https://www.regular-expressions.info/reference.html It's a regular expression quick reference and it contains almost all the characters you can use to build out a pattern. And in the demo we'll look at actually building these patterns. When you're building these patterns you need to know what characters do what. Some characters match exactly the same character in the target, some characters stand for a class of characters, for instance a lower-case d can mean any digit., and an asterisk can mean any character. By combining the characters and symbols you build up a pattern that does what you want. You need to understand how to work this, but you don't need to memorize it. You will find that you don’t use regular expressions very often, but when you do, they provide a powerful solution to a problem that would be very difficult to solve in another way. If you need to solve a common task with regular expressions, you’ll find that someone has already solved it and kindly placed the solution on the web for you. If your problem is unusual, then there are online test sites that allow you to paste in your sample text and your regex. That way you can develop a pattern that will reliably do what you want before you incorporate it into your code. Here are two sites that are available at the time of writing: https://www.regextester.com/94608 http://myregexp.com/ ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

This slide shows some simple search patterns. For example ABC means that we are looking for an A followed directly by B followed directly by a C anywhere in the text that we are searching. The simplest regex can be a sequence of characters while the more complex patterns can sometimes be really difficult to understand. The slide shows three patterns with their meaning. We talked about ABC which is literal text that we're looking to match anywhere in a string. The second pattern is a b followed any one of the following characters a, e, I, o or u which is then followed by a t. So you see that matches things like bat bot bit bet and but. Then we start to get a little more complicated. The second pattern looks for an opening and closing pair of tags in a HTML document. The specific tag being searched for here is TAG. And finally you could see if we get a little bit more complicated below where we're looking for any email address in a block of text that is being scanned. So while that one can look a little complex that's actually a little bit easier. The best way to deal with regular expressions is to look up a regular expression reference document, make your best attempt, then test it using any one of the free online regex testers. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In Groovy, there are Pattern and Matcher classes. The Pattern class represents a regular expression pattern and the Matcher operates the pattern on inputs. Find Operator is great when you need to find the pattern across an input and return instances of those. Note that it returns a Matcher; in a matcher you use find() and group() to move the cursor through matches and return the match itself. Match operator is great when you just need to verify that input is a certain pattern. It returns a boolean value based on whether it matches or not. Java has had support for regular expressions or regex, as they are often referred to, since 1.4. Regular expressions are a representation of a search pattern used to scan and match for text. Groovy uses Java’s regular expression API.. but it also adds three operators to make working with regex a little bit easier. And those operators are the find operator which is the equal sign tilde. If you don't know where tilde is, it's usually above the tab key on your keyboard. You can use this to directly to build a java.util.regex.Matcher instance. The find operator is used to find something in a block of text. Then there is the match operator which is two equal signs and a tilde. The match operator is a slight variation of the find operator that does not return a matcher but a boolean and requires a strict match of the input stream. And finally the pattern operator defines a pattern for us. So we use the tilde and some type of pattern which is a string and it just allows us to easily create a pattern instance. These will make more sense when we get into some of the demos. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Here the instructor will demonstrate how to use this stuff. We’ll start off by giving just a simple java pattern and how you would create it just so we can see and contrast how we do it in Groovy. So the first thing is to import the java.util.regex package and then we would need to create a pattern. We use Pattern pattern = Pattern.compile() to do that. If our pattern was something simple like ABC there would be no problem. If we want a backslash in the pattern we have a problem. We need to escape that backslash with a second backslash to get one backslash in the pattern. This first double backslash is for Java’s benefit. If you look at the regex reference, you see that \b means something, so if we want to search for a single backslash, we need to escape both our backslashes so we wind up with four backslashes. This could quickly become unmanageable. You can see from our very simple example that it's already complex. You can imagine when you get into some of these more complex patterns, how difficult those could be to read. That's a little bit of Java background for you. Now if we jump into Groovy, what does Groovy look like. We looked in another tutorial on how to construct strings in Groovy. We know that we when we construct strings we can use single quotes or we can use double Quotes. We can use Groovy strings to contain a variable here. We could use a multi-line string for example. We also know that Groovy offers slashy string. What they do is they use the forward slash as a delimiter and they can become particularly useful when we are defining regular expressions and patterns as there is no need to escape or our backslashes. How this works is we can use a slashy string and that way we don't need quotes or anything. What we do is we start this with a forward slash. And in this case, we can now say backslash B. And that means that we no longer have to escape that backslash in there. And so this is what's known as a slashy string. And there's going to be situations where most of the time this is going to work great but there can be situations where this won't work. So let's say we have a URL and you start to type this out and every hit another forward slash. Well we can go ahead and escape that with another forward slash and you like. And now that kind of ends there. There is something called a dollar slashy which helps us out here. So if we put in a dollar this dollar slash means this is the start of our expression. And then what we can do is we can end it with a slash dollar sign and that will end our expression. But what we can do is we can start to use these as patterns so if we say pattern is equal to and remember one of our operators was just that tilde. This replaces the need to do all of this. You can simply say tilde and build an expression. And now if we println pattern class you can see that we now have an instance of java.util.regex.pattern. You can see how easy it is to create not only a pattern but to build them out using the slashy string expressions. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

The first data type that we're going to look at is called a range and a range is simply a list of items that we're going to move through. Say we have a list of numbers like 1 to 10 or a list of letters like A to Z. Those are lists of things that we need to move through. We basically move through them by calling next starting from the first value. In the first example we start with one and we keep calling next successively until we reach the end which would be 10. We also can use a reverse range and in that instance, we start on the top value and call previous on it until we get to the end. So we would move from 10 to 1. In this section we're going to talk all about ranges. We're going to talk about how to use and create them. But before we do, here’s a quick example of something we probably have all written at some point. So whether you're coming from Java or another programming language you've probably done this at some point. It’s the good old for loop. We begin with a for loop to loop through the numbers 1 to 10 and print out each number. In the example we start with integer x is equal to 1., and while this is less than or equal to 10, increment x and each time we're going to print out the value of x. So nothing fancy there but I'm sure it's something we've all done at some point. Then there’s the case where we want to start from the number 10 and move backwards and print out that number. So in that case we might start with the integer Y is equal to 10 and loop while Y is greater than or equal to 1. Again, pretty basic. We also may have used this in the past to move through an array. So we're going to talk about lists in the next lecture but just a quick introduction we're going to create a list of letters, so we say letters is equal to a b and c. So now in the past we may have written a for loop to say for integer is equal to zero. All arrays in Java are zero based. We call the size method to give us the length of the array. Now we just print out the letters in the array. Again, this is probably something we've all done at some point but it's not really the most efficient way to do it. I mean that's a lot of ceremonious code there just to accomplish some very simple tasks as far as moving through items or in what we're going to call it now, a range, so let's take a look at specifying a range and how we can go ahead and create it and some different things that we can do with it. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Let's take a look at creating a range and using it. Here we create a new range of numbers and the way that we create a range is by using the double-dot operator between the left or the from value and the right or the to value. So in this case we're going to say I want to range from 1 to 10. And so the double dots in-between indicate that this is a range. So now what I can do is print this out and if I print line r as a list it will print out all the values in that range. And if we also look at the println r.class.name you can see that it is an instance of groovy.lang.IntRange. The class range itself is a Java interface that extends the Comparable class. And so the actual implementation is an integer in our case because we are using integers in our range. So the interface itself has some pretty cool methods in it. It has a from and a to value so we can take a look at that and then we also inherit from List, Iterable and Collection. So we get all of these methods here as well. In order to have a range you need to know what the from value is. You can always just call the from value and if we print that out that's going to be one. If we print out the to value and that's going to be 10. If we look down here at our list that was printed out here from our range, we have all of the values from 1 to 10 including 10. So what happens in an instance such as our For loop when we wanted to loop from one to x is less than 10, not less than or equal to, less than 10. We can do that too in a range by creating what's called a half exclusive range. And the way that we do that is we simply use the double-dot in between but then we use the less than symbol. So now if we go ahead and run this, you'll see that our range includes everything less than 10. So that's just another way that you can create a range. Let's talk about some things we can run through to test this stuff out. So we're going to go back to using our search here and we're going to say zero to 10. So now if we remember if we look back at the documentation, we inherit from the class List here. And one of those methods we can check is contains and contains will let us know if that list contains that element. So now we can do something like 0..10 contains zero. And if that runs well then nothing shows up so we can kind of assume that that worked. We can also say assert 0 to 10 contains 10 and if we run that, that should work as well. So now when I make this false so let's say zero to 10 contains negative 1. What this has just seen is that the range of zero to 10 does not contain the number negative one. So that's just another good check. And again just playing around with code like this it's good work through some statements and see if you understand exactly how something's working. Here we are playing around with ranges and doing some research statements. Let's check the upper bound, Assert zero to 10 contains 11. That should be false as well. So then we can write some tests to kind of make sure that our assertions are working right. If nothing happens, our assertions are true and our understanding is correct. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

So now we know a little bit about ranges. So let's take a look at some other things that we can create ranges for. So what I'm going to do here is let's create a date called today. We will say that it is a new date. And then I want to create a date called one week away is equal to say today plus seven. And we can thank the wonderful GDK for that one. We can set up a range from today to a week from today. You can see that we have today's date at the recording time. To create a range from that, is just like creating a range that we did before so we would say range is from today to one week away and print. You can see that the GDK was smart enough to figure out that from today to one week away is a range of days. We have a from value, and we have to value, and really all we are doing is calling next on that. So when it when it sees today it calls next and it figures out that this is a date so obviously next would be tomorrow’s date. So we have a range of dates there. We can also do this for strings if we wanted to create a range of letters we would just say Range listOfLetters = a..j That would give us a list of letters.. So again this isn't just for numbers we can use it for all sorts of things which ends up being really useful. We can even create ranges for custom classes which is going to be one of the exercises that we do in this chapter, so we'll look forward to that. We’ll explain that when we get into that exercise. If you haven't noticed as we're going through this, ranges are objects. That's why we're able to call methods on them. That is an important note. Ranges are pretty cool. They're easy to create. There's a bunch of different things we can do with them. We suggest looking through the documentation and looking at all the available methods that we have and just play around with them a lot. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Instructor notes Target time 5 min Student Notes Now we're going to cover another complex data type called a list. One of the best features of Groovy that really pulls developers into the language is just how easy it is to work with collections. Groovy uses comma separated values surrounded by a square brackets to create a list. Under the hood, this is simply a java.util.ArrayList by default.. This is important. Groovy doesn't create any new collection classes. It simply makes working with the existing ones a little bit easier. So let's take a look at how to create a list. So we're going to come into the Groovy console here. We're going to type nums equals and we're just going to use the square brackets. So right off the bat this is a valid expression and it just creates a nums list with no values in it. You can just create and initialize it to nothing. But most of the time we'll probably want to put some values in it so we can do that on the same expression. We just put some numbers in here some duplicates and that should be enough numbers.. So now we have a bunch of numbers here, we’ll print out the numbers and what type of class this is. We can take a look at that. So you see and that by default we use a java.util.ArrayList. So if you're familiar with the Java API at all, an ArrayList is just a concrete implementation of the List class. So at first we didn't really care what the type was but knowing that this is an ArrayList we could type this as an array list and that would be fine, but because it is an implementation of the List interface we could also type this as a List so you may see it declared different ways and any of those would be OK. An ArrayList is just one implementation of the list interface. There are other concrete implementations such as linked list. What if we wanted to use another implementation instead of an array, there are certain situations where maybe you would. We can do that easily by using the as keyword, shown in the third example. And if we go ahead and run that then you'll see now that type is java.util.LinkedList. So that can definitely come in handy as well. For now, leave this as the default array list and we're going to take a look through the API look at some methods for working with our lists. If we open the Groovy documentation page, we can see all the methods that are available to us. What we’ll do is go through some of these, not every single one, but go through some of them and just look at how we can use them to manipulate our lists. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

So back to the Groovy console the first thing to do is learn how we can add items, remove specific items, or clear them out. So let's start with add. How can we add new items to our nums list. Well we can do it a couple of different ways. The first way we can do it is we can push something to it. So we want to push the value ninety nine. Let's go ahead and print that out. Note: The behaviour of push and pop was changed between Groovy 2.4 and Groovy 2.5. In versions prior to Groovy 2.5, the item was added to or removed from the right-hand or high index end of the list. Since Groovy 2.5, the item is inserted at or deleted from the beginning of the list, and all the original items have their index value changed by 1. This was done to align with Java. A new method, removeLast, was implemented to provide the previous behaviour of the pop method. The moral of the story is, do not use push and pop without considering this change. Use add, removeLast and putAt for most use cases. The putAt method replaces an item at a specific (zero-based) index. So let's go ahead and run that. And we see that 77 is there. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Another way that we can modify a list is to simply access a specific index. So we’re going to access that zero based index and I want to change that to say 777. So if I run that now we can see that this overrode that specific index. Another way that we can do this is we can use some overload operators. And for that your instructor may choose to display the documentation here. So Plus works because this is basically all an overloaded operator we can use the plus symbol and it allows us to add elements to our list. But it's important to read just documentation because you could see that this says this operation will always create a new object for the result leaving the original list unharmed. So let's go back here and take a look at this. Let’s take our original one nums and let's add some more using the plus operator, and let's print that up. The new list is created but our original list is never harmed. So that's pointing out that you can use that plus operator. You can also use += which includes the assignment back into the original list. It's good to create a new list by adding a new element but there's also another operator overload and that's for the left shift operator. So if we want to say nums left shift then 999. Now if we go ahead and print that out that is just another way of adding an item to our list so those were some helpful methods for adding items to our list. But how about removing items. Well there's a few methods that can help us out here as well. We have discussed the pop method which allows us to remove the first item from a list. There is a removeAt method that takes an index. So if we want to remove a specific element based on its index we can do that as well. And then we have the minus method which, like the plus method, is basically an operator overload that allows us to use the minus symbol and it's important to note that like the plus operator, it creates a new list based on the first operand minus whatever we're taken away. The operands are not changed. With the minus method, items are removed based on their contents, not their index. So that's important to note. On the Groovy Console you can see the result of nums – 3 is to remove both elements that contained a 3. So that's a little bit about how to remove items. Let's talk about how to access items. To get a specific item at an index we can easily do so we can say println nums and then we're going to use the square bracket notation to supply an index so we can say the index of four. So let's run this and that is going to be 4 So we can use that notation. There's also a getAt method and we can also do some pretty cool methods, that are all in the documentation. But there are certain cases where we where we may not want to use the bracket notation, because one of the getAt method overloads actually takes a range. We learned about ranges in the last lecture. We can supply a range here to say give me every element from 0 to 3. as println nums[0..3] So what this should print out is [0, 1, 2, 3.] And now we want to look at how we can kind of clear these things out. There's a clear method. that we can call, just nums.clear() or we can just assign an empty list like nums = [] So either of those would work if we wanted to go ahead and clear our list. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Now we’ll cover some of the methods that we may want to work with. One of the things you'll probably want to do is be able to loop through a list. We can use the for in loop so we want to say for (x in nums) { println x } Then every element in the list is assigned to x in turn, and the loop runs until all the elements of the list have been processed. This will work fine, but there's some really great methods available that have to do with closures, and we’ll save those until we get to understand closures a little better. So while you can use this ,you probably won't as much, once you learn a little bit about some of the power that closures give us. In the documentation there’s another method called flatten() to flatten a list. If we have a bunch of nested lists inside a list then it will give us the ability to flatten it. Let’s take a look at how that works. In our list, we’ll add a few new items. We know that the left shift operator will allow us to add some items. Here we are adding new lists to a list and that is fine. Your list can contain multiple objects not just numbers so it can contain another list if it wanted to. So if we printed that out now, we see that we have a list with some lists inside of it. So if we wanted to, we could call flatten on this and it will flatten that list out and make it one big large list for us. So that's a pretty cool method. Another thing we can do is we notice that we have some more duplicate elements in here where we could use Println nums.unique() So if we print that out, we can see that it only prints out the unique elements. So even to go a little bit further out, and we talked about this earlier, what we can do is besides using the default implementation of a list called an ArrayList if we're starting to work with lists like this where we want to have no duplicates we can use the unsorted set from Java as well. In the Groovy console we have set up a list with a bunch of unordered numbers. And we make sure we have some doubles in there. That should be good. Now we create this as a set. So now if we print these out you will see that this doesn't contain any duplicates even though we typed 3 and 8 a bunch of times, 2’s in here a couple of times, there are no duplicates in a set. If we wanted to have these numbers sorted when they were stored, we could do that as well. We can store the list as SortedList,  and then not only do we get no duplicates, but it has nicely ordered those for us as well. So those are a bunch of different methods for working with lists in Groovy. Again, just look through the API. You're never going to remember all of these methods. It’s worth making a habit of looking through the API often. Look in there, read the documentation. The documentation is great if you're not sure how to do something. If you look through that that list class, there's all those methods that give us the ability to manipulate these lists. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

The last data type that we're going to look at and this section is called a map, also referred to as dictionaries in other languages. It's basically a way to store key value pairs. So we have a key that represents what the data is. And we have a value which is the actual value of the data. So with that we're just going to dive right in and go through a couple of examples here. In Groovy console we’re going to create a map kind of pretty similar to the way we defined lists. def map is equal to use of square brackets but a little bit different here. There’s a colon and that's going to represent a map. Remember that [] is a list without a colon, and [:] with the colon is now map. And that's how you can tell the two data types apart. The println statement gets the class name and we're going to come back to that in a second because that's important. For right now the default implementation here is a linked hash map, java.util.LinkedHashMap. So just as in a list we know that if we wanted to give this a type, we could say this is a LinkedHashMap. If we wanted to just say Map that's fine as well. So that's that. Next, we’re going to create a map with some properties in it. I'm going to create a new one called person. And what we're going to do is add some properties, so we have a key here. This is the key name which is first and then we have a colon and then we have a value. So the value of first name is Terry and we separate each key value pair in this map with a comma. So now we're going to come in and say last is Splonge. Email is t_splonge@gmail.com and then we're going to close this off. Now if we println person you can see we have our map here and it has our keys and our values are in this nice little map for us. So one thing that we can do is we can access different keys in there but their key names so if we wanted to say println person.first that will access the key in that particular map so the key is first and it's getting its value for us. So println person.first just prints out the value Terry. This is a little bit different from what we're used to. You know in other objects person dot is really just calling a getter. So if we had a class called person and we used person.first we would really expect underneath the hood to just be calling a method called getfirst. Well it's a little bit different when it comes to maps when you use the dot operator we're actually accessing the property itself. And so that's why at line 2 and 4 on the Groovy console, we couldn’t just say Println myMap.class.name because if we run that we’re going to get an error that says cannot get property name on a null object. So what that's doing instead of calling the getter getClass it's looking for a property in that map called class. And if a property does not exist in a map it's always going to return null for you. You can really call anything you can call foo, and that's going to be null as well. So that's why if we ever want to know what that type is, we always need to call this actual getter here getClass() and getName() And that should work out fine. So that's how we can access a specific property. How about adding something new. So let's say we wanted to add something to a person for example we wanted to add Twitter and we can say it as person.twitter = "@TerryTheGreat" So now we have our original person map and what we've done is we've just added a new key value pair to it and it's now been assigned to that particular map. So that's that. Now we would like to do something more. Let's create a new map called Twitter with user name of "@TerryTheGreat” and an email address of “t_splonge@gmail.com”. OK so this is our new map called Twitter. Now by default every key that we use is of type string so we don't have to put this in quotes because by default it's going to be a string. Now where this becomes a little tricky is when we started getting into other things so say we want to call this key ‘embedded blank’. You could try and run it without the quotes and you would get an error. And that's because that's just not a valid. So what we need to do is explicitly tell it that this is a string that we want to use as a key for this particular key value pair. So if we just put the key name in quotes and then run this, everything looks OK. Now what if we wanted to do something like this. What if we wanted to have a variable for a key. So we have an email key and we're just going to call it e-mail. This variable will contain the e-mail address. So that's our key variable we want to use that in this particular example. If we want to evaluate this as a variable we need to put this in some parentheses here. So if we run this now we'll get the desired outcome. So that's just a little bit about key data types and how we can use variables as keys. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Looking through the documentation on the map API there is a bunch of useful methods in there that we can use. So let's say that we wanted to find out in the person map how big it was like how many key value pairs do we have. We can run that and we see we get four. By default maps are not sorted in any fashion. We can have the ability to create a different type of map that is sorted, but that is a little bit out of scope at this particular time. But for just creating a map there is no sorting to the keys. If we wanted to sort it all we could we can call person.sort and if we print that out now, you'll see that the keys are sorted alphabetically. So that's just a little bit about a few methods. If you look through the API there's a lot of useful methods and there are a lot of them have to do with closures. And we're going to get into that in the next section. But before we do that let’s look at looping through a map. One thing that we can do to loop through that particular person map is we can say: for (entry in person ) Then we just println each entry. So this is going to print the key equals value pair for each one. Now say we wanted to get a little bit a little get a little bit more fine grained with this and we wanted to like print out the key value pair. We wanted to get a key or we wanted to get a value for each. We could do something like key in. There's a method in the map class called keyset so we can say for (key in person.keySet() ) which will give us a list of keys so we can loop through that and then print the key and its value. So those two things are helpful. But what's really going to be helpful are these two methods called each and each with index. But these take some closures. And then there's also some methods like collect and find are we're going to revisit these as soon as we get into closures. We will introduce closures before we start to dive into this. But we'll talk about closures and then have another lecture that we come back to the collections like maps and lists and look at a lot of the methods that take closures so they make a little bit more sense than they would right now. We're going to move into closures next section and everything's really going to start coming together. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Slide notes

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Please refer to your lab booklet

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Slide notes

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In this section we are looking at Closures. They are a core feature of the language, and they’ve been around since the very beginning. A closure works like a method, but it is an anonymous block of code that can be passed as a parameter in a method call. Once you understand closures, the APIs that we use for dealing with complex data types make a lot more sense. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Closures are a really big part of groovy. Using closures, you can reduce the number of lines of code you’re writing and make more elegant solutions. In Groovy, closures are also objects, which means you can pass them around to other parts of the code and they can also store state. In this section we're going to finally dive into closures. You weren't going to get very far in Groovy before you started running into closures. They're everywhere and a really big part of the language. In fact they're probably the key feature that has made Groovy as popular as it is today. That is why this section is extremely important to understand and I hope you will take the time to understand what we go over here. So what is a closure. A closure is just like a method except it is a first class citizen of the language. When I have a method in a class, we give it a name. It takes some arguments and performs some actions. A closure will do the same thing. But unlike that method a closure is an object and can be used or passed around your programs. So what are closures used for. Well they're used for a bunch of things. Most notably iterators. So we are going to look at those in this section and we've probably already seen them as we've gone throughout this course, but closures give us a nice way to iterate over lists maps and other objects. Callbacks. We no longer have a need for anonymous inner classes like we did in Java; we can just create callbacks. We have higher order functions; functions that are calling other functions. Specialized control structure. You can use closures to create your own control structures. Builders, which are a really big thing in Groovy that we are going to cover in this course. Closures give you a great way to build out some different structures like HTML or XML. Resource allocation. Often, we forget to close resources such as when opening and closing a file. Closures give us a nice API to open files, read them, and then not have to worry about closing them, because it's something that we forget to do a lot. Threads. There is a static method on a thread class for starting, and it takes a closure and it just makes creating new threads and performing actions very simple. We are excited to show you that later in this course as well. We have a whole section based on building Domain-Specific Languages and closures are a huge reason why building your own DSLs is really fun in Groovy. And finally just fluent interfaces; being able to take a lot of verbose, noise polluted code and clean that up and get down to some really concise easy to understand code. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In the introduction to this section we started to look at what closures are. In this lecture we dive into some basics of how to create closures and use them. Here in Groovy console we’re going to create a closure. We're going to say def c is equal to a closure, which is just a block of code inside of the curly braces. We println the class name but that may not help us much if c is an instance of closure. So you see the class name is not going to really help you much. But what the important thing to understand here is that it is an instance of the class closure. So there is a class defined in Groovy called closures. Now let's go and create a new method. We're going to call it sayHello and we're going to have a block of code here and inside of this bock of code is the method body. This is an anonymous function. It's a method but it has no name. Just like a method it can take arguments and perform a function - perform some kind of programming. We can do something here in the method body if you will. All we will do is println Hello. Now we can call this just like we do a method so let's say sayHello() and if we run this you see that we printed the line Hello. Now his is like a function that can take arguments so if we want to take arguments, we can pass them as parameters into this block of code. In the next example we have an argument named Name and we separate the closure arguments from the closure body using this arrow syntax here. So the name is the argument. Anything after that dash arrow is now the closure body. So now we can say sayHello2(‘Fred’) and pass a name into our closure. And that works as we expect. So now you're probably asking yourself well how's that different from any other method. Well as we start to get into this, you'll see that a method can't be passed around. It's not an object. Closures are objects that we can then pass around our programs. So that's where the power really begins to come in. Just start to know though that this is really that block of code. It acts like an anonymous function. So let's do something else here we're going to make a list now. We've seen this before. We got some numbers here. So now if we look on the right at the Groovy documentation, we see that there's an each method and you'll see that the each method takes a closure. That closure that is a block of code that we want to do something with. In the Groovy Console we can say nums.each. So each takes a closure and inside the closure we can do something so we can println. Now we would think that here we would want to print each of the values in our list. So how do we do that. We need we need access to that element. By default, if there are no arguments passed into a closure, there is a special reserved keyword called it . And what that does is, it is the current element that we're working with. So if we go ahead and run that you'll see each of those printed out. Now if we don't want to call it it because it doesn't really do a lot for us here in this case, if we wanted an actual name for it we could just give it a name, so we're going to say element and just like we did before we are going to declare that as an argument and then use the arrow to separate the argument from the method body. If we run sayHello2(‘Fred’) now, it's going to work as expected. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In the introduction to this section we started to look at what closures are. In this lecture we dive into some basics of how to create closures and use them. Here in Groovy console we’re going to create a closure. We're going to say def c is equal to a closure, which is just a block of code inside of the curly braces. We println the class name but that may not help us much if c is an instance of closure. So you see the class name is not going to really help you much. But what the important thing to understand here is that it is an instance of the class closure. So there is a class defined in Groovy called closures. Now let's go and create a new method. We're going to call it sayHello and we're going to have a block of code here and inside of this bock of code is the method body. This is an anonymous function. It's a method but it has no name. Just like a method it can take arguments and perform a function - perform some kind of programming. We can do something here in the method body if you will. All we will do is println Hello. Now we can call this just like we do a method so let's say sayHello() and if we run this you see that we printed the line Hello. Now his is like a function that can take arguments so if we want to take arguments, we can pass them as parameters into this block of code. In the next example we have an argument named Name and we separate the closure arguments from the closure body using this arrow syntax here. So the name is the argument. Anything after that dash arrow is now the closure body. So now we can say sayHello2(‘Fred’) and pass a name into our closure. And that works as we expect. So now you're probably asking yourself well how's that different from any other method. Well as we start to get into this, you'll see that a method can't be passed around. It's not an object. Closures are objects that we can then pass around our programs. So that's where the power really begins to come in. Just start to know though that this is really that block of code. It acts like an anonymous function. So let's do something else here we're going to make a list now. We've seen this before. We got some numbers here. So now if we look on the right at the Groovy documentation, we see that there's an each method and you'll see that the each method takes a closure. That closure that is a block of code that we want to do something with. In the Groovy Console we can say nums.each. So each takes a closure and inside the closure we can do something so we can println. Now we would think that here we would want to print each of the values in our list. So how do we do that. We need we need access to that element. By default, if there are no arguments passed into a closure, there is a special reserved keyword called it . And what that does is, it is the current element that we're working with. So if we go ahead and run that you'll see each of those printed out. Now if we don't want to call it it because it doesn't really do a lot for us here in this case, if we wanted an actual name for it we could just give it a name, so we're going to say element and just like we did before we are going to declare that as an argument and then use the arrow to separate the argument from the method body. If we run sayHello2(‘Fred’) now, it's going to work as expected. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In the introduction to this section we started to look at what closures are. In this lecture we dive into some basics of how to create closures and use them. Here in Groovy console we’re going to create a closure. We're going to say def c is equal to a closure, which is just a block of code inside of the curly braces. We println the class name but that may not help us much if c is an instance of closure. So you see the class name is not going to really help you much. But what the important thing to understand here is that it is an instance of the class closure. So there is a class defined in Groovy called closures. Now let's go and create a new method. We're going to call it sayHello and we're going to have a block of code here and inside of this bock of code is the method body. This is an anonymous function. It's a method but it has no name. Just like a method it can take arguments and perform a function - perform some kind of programming. We can do something here in the method body if you will. All we will do is println Hello. Now we can call this just like we do a method so let's say sayHello() and if we run this you see that we printed the line Hello. Now his is like a function that can take arguments so if we want to take arguments, we can pass them as parameters into this block of code. In the next example we have an argument named Name and we separate the closure arguments from the closure body using this arrow syntax here. So the name is the argument. Anything after that dash arrow is now the closure body. So now we can say sayHello2(‘Fred’) and pass a name into our closure. And that works as we expect. So now you're probably asking yourself well how's that different from any other method. Well as we start to get into this, you'll see that a method can't be passed around. It's not an object. Closures are objects that we can then pass around our programs. So that's where the power really begins to come in. Just start to know though that this is really that block of code. It acts like an anonymous function. So let's do something else here we're going to make a list now. We've seen this before. We got some numbers here. So now if we look on the right at the Groovy documentation, we see that there's an each method and you'll see that the each method takes a closure. That closure that is a block of code that we want to do something with. In the Groovy Console we can say nums.each. So each takes a closure and inside the closure we can do something so we can println. Now we would think that here we would want to print each of the values in our list. So how do we do that. We need we need access to that element. By default, if there are no arguments passed into a closure, there is a special reserved keyword called it . And what that does is, it is the current element that we're working with. So if we go ahead and run that you'll see each of those printed out. Now if we don't want to call it it because it doesn't really do a lot for us here in this case, if we wanted an actual name for it we could just give it a name, so we're going to say element and just like we did before we are going to declare that as an argument and then use the arrow to separate the argument from the method body. If we run sayHello2(‘Fred’) now, it's going to work as expected. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In the introduction to this section we started to look at what closures are. In this lecture we dive into some basics of how to create closures and use them. Here in Groovy console we’re going to create a closure. We're going to say def c is equal to a closure, which is just a block of code inside of the curly braces. We println the class name but that may not help us much if c is an instance of closure. So you see the class name is not going to really help you much. But what the important thing to understand here is that it is an instance of the class closure. So there is a class defined in Groovy called closures. Now let's go and create a new method. We're going to call it sayHello and we're going to have a block of code here and inside of this bock of code is the method body. This is an anonymous function. It's a method but it has no name. Just like a method it can take arguments and perform a function - perform some kind of programming. We can do something here in the method body if you will. All we will do is println Hello. Now we can call this just like we do a method so let's say sayHello() and if we run this you see that we printed the line Hello. Now his is like a function that can take arguments so if we want to take arguments, we can pass them as parameters into this block of code. In the next example we have an argument named Name and we separate the closure arguments from the closure body using this arrow syntax here. So the name is the argument. Anything after that dash arrow is now the closure body. So now we can say sayHello2(‘Fred’) and pass a name into our closure. And that works as we expect. So now you're probably asking yourself well how's that different from any other method. Well as we start to get into this, you'll see that a method can't be passed around. It's not an object. Closures are objects that we can then pass around our programs. So that's where the power really begins to come in. Just start to know though that this is really that block of code. It acts like an anonymous function. So let's do something else here we're going to make a list now. We've seen this before. We got some numbers here. So now if we look on the right at the Groovy documentation, we see that there's an each method and you'll see that the each method takes a closure. That closure that is a block of code that we want to do something with. In the Groovy Console we can say nums.each. So each takes a closure and inside the closure we can do something so we can println. Now we would think that here we would want to print each of the values in our list. So how do we do that. We need we need access to that element. By default, if there are no arguments passed into a closure, there is a special reserved keyword called it . And what that does is, it is the current element that we're working with. So if we go ahead and run that you'll see each of those printed out. Now if we don't want to call it it because it doesn't really do a lot for us here in this case, if we wanted an actual name for it we could just give it a name, so we're going to say element and just like we did before we are going to declare that as an argument and then use the arrow to separate the argument from the method body. If we run sayHello2(‘Fred’) now, it's going to work as expected. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Slide notes

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Please refer to your lab booklet

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Slide notes

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In this section we are looking at Object Oriented Programming. We will learn how to write classes, methods, properties, constructors, all the fun stuff. We will look at local variables, and how to organize your classes to make your programs readable, understandable, reliable and efficient. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In the introduction to this section we started to look at what closures are. In this lecture we dive into some basics of how to create closures and use them. Here in Groovy console we’re going to create a closure. We're going to say def c is equal to a closure, which is just a block of code inside of the curly braces. We println the class name but that may not help us much if c is an instance of closure. So you see the class name is not going to really help you much. But what the important thing to understand here is that it is an instance of the class closure. So there is a class defined in Groovy called closures. Now let's go and create a new method. We're going to call it sayHello and we're going to have a block of code here and inside of this bock of code is the method body. This is an anonymous function. It's a method but it has no name. Just like a method it can take arguments and perform a function - perform some kind of programming. We can do something here in the method body if you will. All we will do is println Hello. Now we can call this just like we do a method so let's say sayHello() and if we run this you see that we printed the line Hello. Now his is like a function that can take arguments so if we want to take arguments, we can pass them as parameters into this block of code. In the next example we have an argument named Name and we separate the closure arguments from the closure body using this arrow syntax here. So the name is the argument. Anything after that dash arrow is now the closure body. So now we can say sayHello2(‘Fred’) and pass a name into our closure. And that works as we expect. So now you're probably asking yourself well how's that different from any other method. Well as we start to get into this, you'll see that a method can't be passed around. It's not an object. Closures are objects that we can then pass around our programs. So that's where the power really begins to come in. Just start to know though that this is really that block of code. It acts like an anonymous function. So let's do something else here we're going to make a list now. We've seen this before. We got some numbers here. So now if we look on the right at the Groovy documentation, we see that there's an each method and you'll see that the each method takes a closure. That closure that is a block of code that we want to do something with. In the Groovy Console we can say nums.each. So each takes a closure and inside the closure we can do something so we can println. Now we would think that here we would want to print each of the values in our list. So how do we do that. We need we need access to that element. By default, if there are no arguments passed into a closure, there is a special reserved keyword called it . And what that does is, it is the current element that we're working with. So if we go ahead and run that you'll see each of those printed out. Now if we don't want to call it it because it doesn't really do a lot for us here in this case, if we wanted an actual name for it we could just give it a name, so we're going to say element and just like we did before we are going to declare that as an argument and then use the arrow to separate the argument from the method body. If we run sayHello2(‘Fred’) now, it's going to work as expected. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In the introduction to this section we started to look at what closures are. In this lecture we dive into some basics of how to create closures and use them. Here in Groovy console we’re going to create a closure. We're going to say def c is equal to a closure, which is just a block of code inside of the curly braces. We println the class name but that may not help us much if c is an instance of closure. So you see the class name is not going to really help you much. But what the important thing to understand here is that it is an instance of the class closure. So there is a class defined in Groovy called closures. Now let's go and create a new method. We're going to call it sayHello and we're going to have a block of code here and inside of this bock of code is the method body. This is an anonymous function. It's a method but it has no name. Just like a method it can take arguments and perform a function - perform some kind of programming. We can do something here in the method body if you will. All we will do is println Hello. Now we can call this just like we do a method so let's say sayHello() and if we run this you see that we printed the line Hello. Now his is like a function that can take arguments so if we want to take arguments, we can pass them as parameters into this block of code. In the next example we have an argument named Name and we separate the closure arguments from the closure body using this arrow syntax here. So the name is the argument. Anything after that dash arrow is now the closure body. So now we can say sayHello2(‘Fred’) and pass a name into our closure. And that works as we expect. So now you're probably asking yourself well how's that different from any other method. Well as we start to get into this, you'll see that a method can't be passed around. It's not an object. Closures are objects that we can then pass around our programs. So that's where the power really begins to come in. Just start to know though that this is really that block of code. It acts like an anonymous function. So let's do something else here we're going to make a list now. We've seen this before. We got some numbers here. So now if we look on the right at the Groovy documentation, we see that there's an each method and you'll see that the each method takes a closure. That closure that is a block of code that we want to do something with. In the Groovy Console we can say nums.each. So each takes a closure and inside the closure we can do something so we can println. Now we would think that here we would want to print each of the values in our list. So how do we do that. We need we need access to that element. By default, if there are no arguments passed into a closure, there is a special reserved keyword called it . And what that does is, it is the current element that we're working with. So if we go ahead and run that you'll see each of those printed out. Now if we don't want to call it it because it doesn't really do a lot for us here in this case, if we wanted an actual name for it we could just give it a name, so we're going to say element and just like we did before we are going to declare that as an argument and then use the arrow to separate the argument from the method body. If we run sayHello2(‘Fred’) now, it's going to work as expected. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In the introduction to this section we started to look at what closures are. In this lecture we dive into some basics of how to create closures and use them. Here in Groovy console we’re going to create a closure. We're going to say def c is equal to a closure, which is just a block of code inside of the curly braces. We println the class name but that may not help us much if c is an instance of closure. So you see the class name is not going to really help you much. But what the important thing to understand here is that it is an instance of the class closure. So there is a class defined in Groovy called closures. Now let's go and create a new method. We're going to call it sayHello and we're going to have a block of code here and inside of this bock of code is the method body. This is an anonymous function. It's a method but it has no name. Just like a method it can take arguments and perform a function - perform some kind of programming. We can do something here in the method body if you will. All we will do is println Hello. Now we can call this just like we do a method so let's say sayHello() and if we run this you see that we printed the line Hello. Now his is like a function that can take arguments so if we want to take arguments, we can pass them as parameters into this block of code. In the next example we have an argument named Name and we separate the closure arguments from the closure body using this arrow syntax here. So the name is the argument. Anything after that dash arrow is now the closure body. So now we can say sayHello2(‘Fred’) and pass a name into our closure. And that works as we expect. So now you're probably asking yourself well how's that different from any other method. Well as we start to get into this, you'll see that a method can't be passed around. It's not an object. Closures are objects that we can then pass around our programs. So that's where the power really begins to come in. Just start to know though that this is really that block of code. It acts like an anonymous function. So let's do something else here we're going to make a list now. We've seen this before. We got some numbers here. So now if we look on the right at the Groovy documentation, we see that there's an each method and you'll see that the each method takes a closure. That closure that is a block of code that we want to do something with. In the Groovy Console we can say nums.each. So each takes a closure and inside the closure we can do something so we can println. Now we would think that here we would want to print each of the values in our list. So how do we do that. We need we need access to that element. By default, if there are no arguments passed into a closure, there is a special reserved keyword called it . And what that does is, it is the current element that we're working with. So if we go ahead and run that you'll see each of those printed out. Now if we don't want to call it it because it doesn't really do a lot for us here in this case, if we wanted an actual name for it we could just give it a name, so we're going to say element and just like we did before we are going to declare that as an argument and then use the arrow to separate the argument from the method body. If we run sayHello2(‘Fred’) now, it's going to work as expected. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In the introduction to this section we started to look at what closures are. In this lecture we dive into some basics of how to create closures and use them. Here in Groovy console we’re going to create a closure. We're going to say def c is equal to a closure, which is just a block of code inside of the curly braces. We println the class name but that may not help us much if c is an instance of closure. So you see the class name is not going to really help you much. But what the important thing to understand here is that it is an instance of the class closure. So there is a class defined in Groovy called closures. Now let's go and create a new method. We're going to call it sayHello and we're going to have a block of code here and inside of this bock of code is the method body. This is an anonymous function. It's a method but it has no name. Just like a method it can take arguments and perform a function - perform some kind of programming. We can do something here in the method body if you will. All we will do is println Hello. Now we can call this just like we do a method so let's say sayHello() and if we run this you see that we printed the line Hello. Now his is like a function that can take arguments so if we want to take arguments, we can pass them as parameters into this block of code. In the next example we have an argument named Name and we separate the closure arguments from the closure body using this arrow syntax here. So the name is the argument. Anything after that dash arrow is now the closure body. So now we can say sayHello2(‘Fred’) and pass a name into our closure. And that works as we expect. So now you're probably asking yourself well how's that different from any other method. Well as we start to get into this, you'll see that a method can't be passed around. It's not an object. Closures are objects that we can then pass around our programs. So that's where the power really begins to come in. Just start to know though that this is really that block of code. It acts like an anonymous function. So let's do something else here we're going to make a list now. We've seen this before. We got some numbers here. So now if we look on the right at the Groovy documentation, we see that there's an each method and you'll see that the each method takes a closure. That closure that is a block of code that we want to do something with. In the Groovy Console we can say nums.each. So each takes a closure and inside the closure we can do something so we can println. Now we would think that here we would want to print each of the values in our list. So how do we do that. We need we need access to that element. By default, if there are no arguments passed into a closure, there is a special reserved keyword called it . And what that does is, it is the current element that we're working with. So if we go ahead and run that you'll see each of those printed out. Now if we don't want to call it it because it doesn't really do a lot for us here in this case, if we wanted an actual name for it we could just give it a name, so we're going to say element and just like we did before we are going to declare that as an argument and then use the arrow to separate the argument from the method body. If we run sayHello2(‘Fred’) now, it's going to work as expected. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In the introduction to this section we learned a little bit about what builders are and how they can help us. Now we will look at some of the different builders and look at some examples of how they work.. Once you see them in action, you'll get an idea of how useful they can be to us. So the first one that we're looking at is the MarkupBuilder. And we're going to specifically use this one for generating XML. The Groovy documentation page is shown on the screen. Get to this page by selecting Documentation, then under the API heading select the API documentation. Then select the groovy.xml package, and the MarkupBuilder class. Here you can see the different constructors that we have and the different methods that we have available to us. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes The screen shows IntelliJ Idea in our project. We are just using a simple script to illustrate this builder. When we create an instance of MarkupBuilder, the IDE imports the groovy.xml package. It's all we need to do is create a new instance of this and we are ready to generate some XML here. We're going to start off with a root node and to do so we're just going to give it a name, we call this sports and then we're going pass in a closure. Let’s review what is going on here So we have this builder and we’re calling a method on it called sports. Remember a couple of things here. We know that when we pass a closure to a method, and the closure is the last argument we can omit the parentheses and just have a closure surrounded by braces. Another thing that's going on here is if you were to look at the documentation of MarkupBuilder and any classes that it extends from, you're not going to find a method called sports in there and we know that from the OOP section of this course that what's going on here is probably some type of methodMissing or invokeMethod functionality going on behind the scenes. So no method called sports as defined. So some kind of missing method processing catches that and knows what to do with it. In the case of MarketBuilder, it's going to create a document structure for us. Next, we want to build individual nodes here, so we name it sport and give it an attribute. So an attribute inside of that method is just a map so we're passing in maps for attributes and we are passing in another closure. Now we add an element and a name ‘baseball’ and repeat it for some more sport names. When we run the script, you can see the output in the output section on the lower left of the screen.. Notice we have sports as the root node and then everything underneath it starts building out. Beneath that we have the sport nodes with the names we gave in the script. This is so very easy to use. If you've used Java before, you will know that it is very verbose and difficult to build out XML. And in some other languages it's easier than Java but it's still not simple. To me this is very simple we can use code to build out our XML structure here. So this is really cool. When we created our instance of MarkupBuilder, the default is to just print the result when we run the script. If we were to go back and look at the constructors, one of the constructor overloads is to take a writer. So if we wanted to, we could write to a file an output stream, a stream etc.. So that's pretty helpful. Also there's some great methods in here, two of which we’ll look at just now. The method setOmitEmptyAttributes allows empty attributes to be removed from the generated markup and setOmitNullAttributes allows null attributes to be removed from the generated markup. So there are some helpful methods in there. Take a look through the through the API and the documentation and see what's available to you. But this is just an example of the MarkupBuilder and how incredibly easy it is to use and generate XML for you. Now this is a MarkupBuilder and it is used to generate XML and HTML because they're both markup languages. So we'll look at how we can kind of create some HTML out in a further lesson. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Now that we have seen an example of how to use the MarkupBuilder, here are some hints and tips about learning more about how to get more information.. This information originally came from Kenneth Kousen, President, Kousen IT, Inc. who is an amazing presenter, speaker and blogger who does a bunch of YouTube videos. He’s also on the groovy podcast. The issue is that the Groovy documentation for builder is a little bit light. So if we go to the API and we look for MarkupBuilder it just says that it is a helper class for creating XML or HTML markup. There is a quick one example and a bunch of methods and really nothing else. So the outside of doing the normal basic stuff you're probably going to come across questions like how do I do this or how do I do that. And the documentation is a little lacking in that area. An easy way to look at other examples of this is to go into the Groovy source code on GitHub and look at some of the unit tests. Here we see another advantage of using open source projects. The developers of Groovy are very good about writing tests for all of their different test cases. The GitHub site for Apache Groovy is at https://github.com/apache/groovy . Under subprojects we find groovy.xml. We're going to go into the source into the test in the Groovy in the groovy.xml and now we have all these different tests here. Currently the URL is https://github.com/apache/groovy/tree/master/subprojects/groovy-xml/src/test/groovy/groovy/xml but remember that this may change in the future. You may need to navigate through the web site using the user interface provided there. So we're going to look at MarkupBuilder test and this is a test for testing a MarkupBuilder. So there's some set up information going on here. And then there's a variety of tests. You could learn a lot by copying some of these tests into say IntelliJ Idea or Groovy Console . Then you could play with it and see what's going on. This is a great way to learn not only the basic stuff but when you start to have questions about doing something a little bit different from the normal basic example. There is a test for markup up with colons in namespaces so you may have an instance where you have colons in there and you want to make sure that that works as well. There is another to test mark up with special characters. Maybe you want to use double quotes so on that builder you can set a property called double quotes so call a setter and set double quotes equal to true. So then we get into testing no escaping of attributes so maybe you wanted to put a link, and in the links case you would not want to escape the attributes. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

So when we started using the MarkupBuilder for generating XML we mentioned this can be used to generate XML and HTML. So while it's in the XML package in the documentation, it still clearly states that it's for creating markup. And so we can use that MarkupBuilder to also create some HTML. So let's take a look at how that works with a quick example here. On the left of screen we have a script called HTML.groovy and this is going to look pretty familiar to start off with. First we create a new MarkupBuilder called builder. And the root node for this HTML document is going to be HTML.. Since we're going to use HTML then we can start to build out the different sections that construct a HTML document. First create a title so ‘All about Groovy’. Then a description ‘ This is an about me page ’. Maybe we have some keywords ‘ Groovy, Java, GitHub ’ So now we have the head portion of our HTML document. Then we might like to start construct a body. So inside of the body of an HTML document one thing that we could have is an H1 tag and that H1 tag would say ‘ About language ’ then maybe we have a paragraph tag and we have some text ‘ This is a bunch of text about Groovy ’. So that's that and then maybe we have a section and inside of that section we have another heading and Then a table of available courses starting with a row containing th ‘id’ and th ‘name’. If we run this part, we're going to get the output just like we did using the XML builder for XML and now we're just using that MarkupBuilder to construct an HTML document. Besides using code to generate code line by line, we can define a List of courses, and we can use code to generate markup by looping through the list. We have the list called courses at the top of the code, with each list element being a map of name-value pairs. We have an id and a name, with a value for each. Now in the code below, we can use our old friend courses.each. And now that's going to take a closure and in the closure we can create a row for each course and inside the first td we would have the course id, and then inside of the second td we would put the course name. So that's pretty cool we're using code inside our builder here to generate markup. As you can see, this is a really easy way to generate markup. So if you remember back when we talked about the MarkupBuilder we said that it does take arguments and one of those arguments for the constructor is a writer. We can write this out to a file so let's create a FileWriter called writer. We will point this to an HTML directory and a file called about.html. When we run that it will create that writer and create an HTML file for us. So we have about.html. The code is in the middle of the screen. On the right is how it looks in a browser. So that's pretty cool we get a format in here too which is great. So that's just another way to use the MarkupBuilder. And again we can create a file or you can create other types of writers and pass those into your builder and that's pretty cool. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In the previous lectures we looked at the MarkupBuilder which we used to generate both HTML and XML. Now in this lesson we're going to take a look at a new builder called the JsonBuilder which we will use to generate JSON so the syntax is going to be pretty similar to the MarkupBuilder, but this is a completely different builder. Most of the builders use the same underlying structure, so most of the syntax is going to look pretty familiar. But we're generating a totally different type of data here. Left of screen is a script called json.Groovy and we create a new JsonBuilder called builder. The builder JsonBuilder is located in the groovy.json package. The root of this JSON is called books. Again we're passing in a closure as our last is our first and only argument no parenthesis, we can create a book. This book may have a title, an ISBN, and an author, with subfields first name, last name and Twitter handle. And then finally a structure for related books; what other books has he written or what other books have you read. So we'll say he has the "The 4 hour body“ and "The 4 hour Chef“. So that’s the basic structure of what our data is going to look like. Now the great thing about builders is we may know what JSON looks like, but we don't know the ins and outs of writing JSON; we don't understand every little nook and cranny of it. We don't have to worry about it. We're building a familiar structure here that we're used to building. You know this is a book and pass a closure and inside of that we have different properties. So we're just building on a book here. Now we can create design because we're using our builder. We can just print the output of toString, and we can see that we have all the fields. If we were to go to the documentation, we would see that there are a number of useful methods there, and one of the most useful is toPrettyString(). We can run that, and print the formatted output to the console. We can also output it to a file simply by creating a file and writing the .toPrettyString() output to the file. And now you see we get this really nice structured data that you can send to somebody or pop this into a tool that you use to send JSON payload somewhere else. It's just a nice convenient method to go and print this out in a format that we're used to looking at. So let's go ahead and run this again. It's going to print it out. And in the HTML folder we were using before there is this new file books.json that we can work with. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In this lesson we're going to take a look at the object graph builder. Now this is one of the most useful of Groovy’s builders in certain situations. So you'll find this builder in the Groovy.util package. So http://groovy-lang.org/api.html, the package groovy.util, then the class ObjectClassBuilder. It's a builder for creating object graphs. Each node defines the class to be created and the property on its parent if any at the same time. The best idea is to take a look through the documentation for yourself. Look at all of the different methods available to you. We're just going to take a look at a pretty simple example here and we’ll create some classes and then we'll look at how we might have built this graph out in Java. And then look at how we can use this builder to really simplify our lives. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes This is an example of the ObjectGraphBuilder in action. On the left of screen we have the class structure that needs to be populated. In the center of screen we have the Java way of populating the classes, commented out. In the lower portion of the right-hand screenshot, we have the classes being populated by the builder. Now we will go through the steps required to get to this state. First, we create a class called Book and inside a Book we have a title. Then a string called summary. The Book is divided into sections, so we have a list of Section called sections, initialized to an empty list. Below that we create the Section class and inside a Section we have a title and have a list of Chapter called chapters initialized to another empty list. And finally going to have a class called Chapter and this is just going to have a title. So a Book is going to be made up of Chapters and some Sections. So we have Section 1 and in Section 1 there's three different Chapters. And so this is a typical scenario when it comes to building out graphs. Now we look at how we might have done this job in Java. We would have a method called public Book createBook() and this is going to return a Book. So this is our job. What we might have done in the past is we'd start with a new Book. A Book b is equal to a new Book we say. This is Java style so we then call the setters to set the title and the summary. Next we have Section s equals a new Section, and call the setter to set its title. And then what we create a Chapter and call setters on each of its properties to set the values. Next we repeat the process to create a second chapter. Now we have a book. We have a section and then we have some chapters. We would have to build logic to deal with varying numbers of Sections and Chapters, then when we had it all built we would return a book. All we want to do is create a new book and add some information to it. You can see this is very verbose and just a whole lot of typing for something as trivial as just creating a new book. Now to use the ObjectGraphBuilder. And this is really going to help us out here. So again the main class is ObjectGraphBuilder in Groovy.util. So let's go ahead and create it. We create a new ObjectGraphBuilder called builder is equal to new ObjectGraphBuilder(). One note if you're running this in the Groovy console you do need a classloader in here; look in the documentation you'll see how you can easily do that. But here in IntelliJ we won't need it. And now we just call builder.book and that sets our root node. So just like we did when we were creating mark up or JSON, we need a root node. In this case it's going to be our Book class. So in our book class we set the title. So the title is going to be “Groovy in action”. Then the summary is going to be this long thing. We just set those two properties inside the closure. This is where we're going to start to build out our graph. So any call now is really calling a class constructor to create a new instance of that class. Book has a list of Section. So the build comes down to just the list of assignment statements that are simple and obvious to maintainers who might come after us. So that's all there is to it. We use the builder to call a method. Obviously, the book method doesn't exist in that builder, so somewhere in the missing method processing method it's going to use some metaprogramming to then figure out that it needs to call a constructor and instantiate a new book object. So it does that. And then the book we are creating sections and on Section recreating chapter. We have our book we have our title on our summary then we have our list of sections. This is just a much easier way to build on an object graph using a builder syntax instead of Java style syntax. Now Groovy does make some of this syntax go away and make it a little bit easier to build out that graph. But when you start getting into build bigger graphs like this it's definitely easier just to use a builder and and build this out. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In this section we've looked at a few examples of the different builders that are included in the Groovy programming language such as the MarkupBuilder and the JsonBuilder. Now we don't have the time to go through every single builder that exists but here is where you can find out more information about the available builders in Groovy. If you go to the API documentation, you will find not only the ones we have looed at but also there are builders called StreamingMarkupBuilder and StreamingJsonBuilder. If you're not quite sure what that is, you can always jump into the API and do a quick search for its StreamingJsonBuilder. Under Domain Specific Languages in the documentation, there is a concise list of all existing builders, as well as all the information you need to build your own builder. There is a SwingBuilder for creating GUI systems. So if you've ever built a GUI in Swing, Groovy gives you a nice builder to build those out. There's other builders in here too. You can also write your own. This lecture is just to give you the link to the documentation. Now you know that you aren’t limited to just the JsonBuilder and the MarkupBuilder. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

We looking at a lot of AST Transformations in this section. Now I want you to go through the documentation and find one that we didn't look at and see if you can get it to work on your own.

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Please refer to your lab booklet

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Slide notes

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In this section we are looking at REST services and how to work with them. REST is not the only way of providing web services, but SOAP is mainly for legacy systems. Because time is limited in this course, we will concentrate on REST and leave it to you to find out about how to implement SOAP services in Groovy. With REST we will look at XML and JSON, how we build them with Groovy builders, and also how we consume them. We will look at HTTP Get and POST, what they mean and how we use them. We will talk about HTTP status codes and content negotiation. There is nothing built into the language for calling a REST endpoint, but there is a great open source project out there that has become one of the standards for using REST. We will look at how to bring that project into our project., and how we use it. It is really easy, just calling different methods. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Grapes is a dependency manager for Groovy. First find the feature that you want to use. In our case we want to use a method from the Text part of Apache Commons. In the red circle we see the three values we need to give Grapes so it will go and fetch the module we want. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

This little script can be given to any organization that is running Groovy, whether or not they have downloaded the module from Apache Commons that the script uses. The Grapes dependency manager will silently go out and fetch the module and install it before running the script. The first time the script is run there is a pause while all that goes on in the background, thereafter it runs normally. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

As we start to work with Web services one of the things that we need to be able to do is to read and write data. In this lesson we're going to cover looking at reading and writing XML data. Just to get started we're going to have a little quick recap of what we did in the builders section to create XML. We have our normal markup builder here we have our builder building out a very trivial example that has sports as the root node and then inside of there we have a sport for each sport. So let's go ahead and actually do a couple of things here let's say builder.double quotes equals true. And create a file writer to write a file to the data folder and we’ll call it sports.xml. Nothing fancy going on here. We have a root node sports and then we have each element in there has a sport with an ID and a name for the sport. So that's a little recap on how we can create some XML. But let's talk about reading it or getting data out of that somehow. So just like writing this is something that was very hard to do in Java and even in other languages it’s not pretty. There are no easy ways to do this. Groovy again makes this very simple to do. We want the third element in this XML list and we want its name. Look at examples in Java in how you have to everything that you need to do just simply get that. It's very verbose. Thankfully there's Groovy. And this is really easy to do in Groovy. We can use something called the XmlSlurper. A funny name. So XmlSlurper has a bunch of different methods on it. So the class XmlSlurper will parse XML into a document tree that may be traversed similar to XPath expressions. And so if we look in here there are a couple different constructors. The empty constructor, which is the one that we're using now, creates a non-validating and namespace-aware XmlSlurper which does not allow DOCTYPE declarations in documents. . There are certain situations where you would want to validate the XML or have namespace awareness which you can do with a different instructor. And then there are many different methods. We're not going to go over all these but you can jump through the documentation and learn what the different capabilities of XmlSlurper are. So you can do things like ignore whitespace and then we have all these overrides for parse. So we say we want to parse a file we could pass in a file. Maybe you wanted an actual input stream or a string and then pass thoseso a helper method to parse the given text somehow. We will pass in our file we just wrote. Now we want to parse sports.xml because that's the XML that we had. So now once we get an instance of this, we can just traverse down the tree. We have a new XMLSlurper, we call parse on it, and that returns a parsed file. And this time I'm just going to parse and we actually want to parse out a file. We’ll point it to data/sports.xml, that's the file that we wrote before. And again we can go ahead and print line sports. And that's actually probably just going to bring back to us a list of sports. Because that's the node, when we call to string on it it's just going to print those sport names out. Something else that might come up is the ability to traverse down and find a specific example within that structure. So in this case I have sports I have a list of sports and let's say I wanted the third item in this list. Again in something else like Java, that would be very difficult to do. But here we can just start to traverse down. I want a sport and there's a list of three. And considering zero is the first element in that list, we're going to want the third one which is index 2, and we want the name. We can just run that and we should just get the name hockey printed out and there it is. That's just the tip of the iceberg. But you know that's really what you need to get started for reading and writing XML. And when we get into web service,s especially dealing with older systems who aren't using JSON, reading and writing XML is pretty necessary and thankfully very trivial in Groovy as compared to Java. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

When working with web services it's important to understand how to read and write different types of data. In this lesson we're going to look at reading and writing some data. So we looked at how to create some data back in the Builders section, but we'll run through just a quick recap of how that works here. Then we'll get into how to read that data. Just as there was an XmlSlurper when reading XML data there's a corresponding JsonSlurper in the groovy.json package. This is used for the same thing; taking text whether it's just a string of text or from a file and parsing that out. We can traverse that data and move through it to find specific examples of data or find a specific node in there. So here's documentation for the JsonSlurper. You can run through and read what it's about and then look at all the different methods that are in here. You'll see other overloaded methods for parse so we can pass in a reader, an input stream, we can pass in a file. And then we can also parse URLs. Then there's also a method that takes a string of text just like there was in the XmlSlurper example. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes So let's run back into a project here and this should look pretty familiar. This is the book data that we built in the Builders section. So we have JsonBuilder builder we build out our data using the builder syntax so we have a root element of books and then we have a currentBook and a nextBook with some information inside of them. Then we create a new file data/books.json and write it out using the builder.toPrettyString method. When we run that, inside the data folder we have our books.json. So that's just a recap of how we can create JSON data. Let's look at how we can read it. We are reading the file we just wrote. So I have books as the toot node. Here's the actual JSON data. The first thing to do is to create a JsonSlurper: JsonSlurper slurper = new JsonSlurper() And now let's work on reading that file we created. So again it's going to be pretty much the same. Now we have our slurper let's set json equal to slurper.parse. We're going to pass in our file so we're going to see a new File object pointing to the file we just created. So now we have a file. We have that same structure that we had before, we have that map of its data so we can easily traverse down through it. So we can say print line the current book by just listing the node hierarchy to traverse: println json.books.currentBook.title When we run this, we're getting an instance of slurper parsing out the file that we wrote before and we're just using this to grab specific elements out of that JSON file. So here we have the title. Here we have the author with all the attributes in it. So that's really all there is to it; just like XML this is very easy to do. Reading and writing data in Groovy especially XML and JSON data is extremely easy and especially as we start to work with more web services these days, creating services working with REST clients. It's just so nice to have something work this easy and you know if you look at this code you could probably figure out what's going on right away. There is no hunting through API docs to figure out what the heck's going on here. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In this lecture we're going to talk about each HTTP verbs to take advantage of consuming or creating APIs it is important to understand what HTTP verbs are, and what they do. We use HTTP verbs all the time and we don't even realize it. If we open up a browser and type in an address and then hit Enter, when a page loads we’re using the HTTP verb GET. If we go to a contact page, we're using GET again but if we fill out a form and click submit, most likely we're using the HTTP verb POST. So what are these verbs and what can they do. Well in this graphic here we have a we have some methods and paths and what they do. So let's talk through them. In the first one the HYYP method is a GET. So if we were to visit a resource that is located in the path /posts that would direct the application to get all of the posts in that directory. If we were to use the HTTP GET on the path/post/1 that would say to the application server that it needs to get all of the posts that have an ID of 1, so that getting one specific post. If we use the HTTP method POST on the path /posts and provided a JSON body that contains a post, then we would create a new post. If we use the method PUT on that same one that we used before on the posts/1 then we would update the post that has an ID of 1 and again we would pass in a JSON object as the body. Finally if we went to posts/1 with the method DELETE we would delete that existing post. So you can see how this starts to really be helpful because basically have two different paths here. We have /posts and then depending on the method that we send we would perform two different actions. Now if we didn't do that you might have a path of say /getposts or /list posts and then you would have a path of /createPosts and so on, so you'd have these URIs all over the place. Whereas this makes it very easy to look at and see what's going on and know that based on the verb that we pass it's going to do different things. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes So just to recap. In one scenario we have a browser that makes a request to a server using GET or POST and that is going to return a response format in HTML or other resources so in the case of the Web site we return a document page using some server side code that return our HTML pages. We can also return images or style sheets or things like that. In the scenario that we're talking about today, which is REST, we have a client, and that client could be a tool like curl, or it could be a server to server communication. Another service could be requesting this resource and makes a call to our application server. It’s going to use one of the following request methods and then we're going to return either JSON or XML. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Other important topic in the world of building and consuming web API eyes are HTTP status codes in the last lecture we looked at different HTTP request methods like GET and POST when we make a request using one of those methods we don't want to be left in limbo as to the result of our request. HTTP status codes help us to understand what the status of our request is. They are broken down into the following groups. We have 100 to 200 is basically information status codes. The 200 range is a success status. 300 usually involves some type of redirection or telling us that something has been moved. 400s are usually saved for some type of error such as a 404 which is the infamous page not found. 500s are usually left for server errors. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes So let's break down some of the more common status code you'll see in each of these groups. 200 is an OK, and it basically says that the request has succeeded. 201 lets us know that something was created. 204 says that there is no content. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes The 300 codes: There’s 301 which says moved permanently. 304 says that we can give you back what you're requesting, but nothing has changed so 304, not modified. 307 is a temporary redirect. In this case the request should be repeated with another URI however future requests can still go to this one. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes The 400 codes: 400 is a bad request. The request could not be understood by the server due to malformed syntax. The client cannot read should not repeat this request without changing something in it. 401 and 403 are both around authorization. 401 Is an unauthorized response and should be used for missing or bad authentication. 403 forbidden response should be used afterwards when the user is authenticated but isn't authorized to perform the requested operation on a given resource. And finally 404 is a not found. We couldn't find the requested resource. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes And finally five hundreds. One you'll see a lot is 500 internal server error. The server encountered an unexpected condition which prevented it from fulfilling a request. 502 is a bad gateway. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student Notes We’re here on the Groovy-lang web site and if in Chrome if you go into developer tools, and select the network tab and refresh the browser here we're going to see some useful information. For each network request that has occurred while the Network tab is open, we have the request name, its status code and some information about the request. Here's the request name. There are some redirects (307) and a sucess status (200). Above that is a chart of page response times and more information than we have time to discuss today. So just a quick little way can open up a web page and a look at the statuses that are being returned for a given resource. Chrome will only show requests that happened while the network tab was open. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Wikipedia definition: “Content negotiation refers to mechanisms defined as a part of HTTP that make it possible to serve different versions of a document (or more generally, representations of a resource) at the same URI, so that user agents can specify which version fits their capabilities the best. One classical use of this mechanism is to serve an image in GIF or PNG format, so that a browser that cannot display PNG images (e.g. MS Internet Explorer 4) will be served the GIF version.” Now what this means is in a scenario where we have a client (and a client can be anything; a tool that we're using to make this request or the server to server communication) a client that makes a request to a server and says send some information along. So the tool says hey server I'm sending you JSON and I would like JSON back and then the server says hey no problem I can do that. There may be other instances where your sending JSON but maybe you need XML back because that’s what your application requires; some XML response. You can do that too, and you can mix and match those. It doesn't have to be the same back and forth. There are a couple of ways that we can do this. One of the most common ways to do it is by using an Accept header. That’s just the header that's sent along and a request that says hey I would like JSON back. The Restful API defines URL patterns for content negotiation. The client may use specific extension in resource URIs. For example, a client can ask for details using: http://rest.api.com/v1/employees/20423.xml http://rest.api.com/v1/employees/20423.json In above case, the first request URI will return a XML response where the second request URI will return a JSON response. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Everything that we've done in this section has led to this lesson. We started off by talking about how to process and create XML and JSON and that was our introduction into the types of data that we're going to work with when we're talking to Web services. Then we took a little primer into the world of REST what are things like verbs, what are the different status codes, what is content negotiation. So this was a basic intro to REST. We definitely recommend reading up a little bit more on it. Now that we have that basic info, let's figure out how we're going to talk to these different services. So we start off with just a simple get. This is not a REST call. This is just something nifty you can do in Groovy. We know that if we punch in just a web address in a browser that we're going to get back a bunch of text and that is a GET request and the response to that's going to be 200 hopefully. Well what you don't know is you can do this really easily, like just about everything else, in Groovy. We can just print line an address here let's say groovy-Lang.org. Now we can use a toURL method that's on a string class, and from that now that we have a user object, we can call the text getter. If we run this, we're going to see down here in our console a heap of HTML. That’s the Groovy home page in text form. So that's just a nice neat little nifty trick to grab the text of a URL. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes Now we’ll take a look at an API that we can use to work with and it's a pretty funny one. This is the internet Chuck Norris database and really just a nice API you can call GET on, You can pass in values and you get some funny Chuck Norris jokes. It's open and importantly for our purposes, you don't have to authenticate. It's obviously a little more complicated when you start authenticating against an API before you can use it. This one is wide open so we can just go ahead and call it. In the red rectangle is where the jokes are at: the URL http://api.icndb.com/jokes/random So we're going to play with this. You can also pass some different parameters if you wanted to. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes So the first thing that we need to talk about is, while we don't have a built-in native REST client in Groovy, there is a good one written in Groovy and it's called the http-builder. We're going to use@ grab for this. You type the @Grab annotation into the IDE, then alt + enter, then you get the opportunity to grab the artefacts. The string base is the base URL for the content that we want to access, so in our case it is http://api.icndb.com. Next we create a new RESTClient called chuck. The IDE will add the appropriate import statement for us. You always want to provide the base to the constructor and then we'll call the individual operations. We need to set the content type for the client to use in its content negotiation, so when we type in chuck.contentType = ContentType. JSON The IDE will insert another import statement for us. So we're just setting our Content-Type to JSON now. Next we call a get method. To retrieve one random joke the URL suffix is '/jokes/random’. We're going to call that without any parameters and that should give us back one of the Chuck Norris jokes. So path is '/jokes/random’. And in this point we're not going to pass any parameters. Here we define a closure and we're going to get back a response and some JSON. In the closure we ask for the HTTP status code, and just print out the toString representation of the JSON. The response code is 200, which you remember is success. And then we get back some JSON converted over to a map for us to work with. And then in the value it shows this is the API bringing back like categories just the number. The ID of the joke and then this is the actual joke. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes So let's build upon this. In most REST based services you're probably need to either supply some parameters or an ID or something like that. So in our case we can send some parameters; a first name and a last name and that will allow us to customize the joke. So let's go out and do that. So let's set up a map of programs. First name we'll say Groovy, last name we’ll say Greenlantern and now here on our Get method we have a path and we can also pass query: params. As you see since we're passing in some params this hA come out with a new joke: “When Groovy Greenlantern is web surfing websites get the message "Warning: Internet Explorer has deemed this user to be malicious or dangerous. Proceed?&quot” If you do have any problems let your instructor know. Sometimes you when you when you call grab, it doesn't refresh these dependencies right away, but there is a progress message at the bottom of the console pane. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Because there are no right answers here, there is no exercise review for this one.

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Please refer to your lab booklet

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Slide notes

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

GDK stands for Groovy Development Kit. This section is put together to show you the kind of things in the GDK that are available to us. One of the best things about Groovy is that it makes complex things very simple, and this is intended as a way of bringing these things together. There is a section in the Groovy documentation the is devoted to the GDK and some of the things in that section are what we are going to be talking about. When we are working with the GDK, one of the areas is working with files and input and output. So we will learn how to work with a file system to create files, manipulate, create and delete directories. We will look at database programming with Groovy, connecting to a database do we can do some SQL methods whether it’s creating some tables or just reading data. We are going to look at templates, using email templates as an example. Finally we’ll look at dates. Dates are really easy in Groovy because of the assistance that the API gives us. We’ll have a quiz and an exercise too. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Apache Groovy Lecture 116 When you start writing scripts or even full-blown applications, one of the things you're eventually going to need to know how to do is to work with the file system. Java gives us the ability to do so with the Java that IO in Java and IO packages. One thing that I always found annoying with Java is that we as programmers are left with the error prone task of cleaning up and closing resources. In general there are just a lot of pain points when working with files and IO in Java. And of course Groovy comes along to make this easier. The JDK provides us with several methods that let us focus on application logic and not low-level operations. To get to this page, go to the documentation at Groovy-lang.org, select the GDK documentation, select the package java.io and select the File class. The JDK is everything that Groovy has added to the API for us. So if we look in here there's a bunch of append methods which are really nice if you just want append text onto a file. There's ways to delete a directory, create temporary directory, and loop over each file and each directory and you can also do that recursively if you need to and there are a lot of really great methods to make working with files and directories a lot easier. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes So at this point we will look at a demo and we'll start looking at all of the nice features of working with files and directories. The screen shows IntelliJ Idea, with the package com.idfacademy.gdk, in the script gdk.Groovy . This just a script and we have written some code to demonstrate the File class in the Groovy.io package in the Groovy Development Kit. First, we create a new file. We said def file = new File( 'dummy.txt’ ) then on the next line we wrote some text to the file with file.write("This is some text\n") Below that in a comment, we have shown an alternative way of doing this: new File('dummy.txt').text = '...’) It's just a file with some text in it. One important thing if you're new to Java and/ or Groovy: the File class refers to both files and directories so that can be confusing at first and that's why the GDK adds a lot of those helper methods like eachDirectory or eachDirectoryRecursive. So when you're talking about a new File you could be talking about a file or a directory. To append to that file so we can easily do that by invoking the append method to add some more text. Next, we have some code to read those lines. The line List lines = file.readLines() reads each line of the file into a separate element of the list called lines. We don’t have to open and close the file, we just have to define a variable to hold the file handle, then invoke some methods in the File class. We can now do lines.each and just print out each one. The next thing is to list all the files in a directory. We have a String that represents the directory that holds all the source code we are using in this course. We create a new File instance that takes this string as its path parameter to the constructor. Then we can use one of the methods that the GDK provides in the Files class, eachFileRecurse(). This method takes a closure. The closure will receive a parameter, we use the name dfile. The method recursively loops over every file that we have in that directory. One of the methods that we have in the File class is a check to see if it is a file. We use the isFile method to make sure that we're not printing out other directories that are in this directory as well. We are using eachFileRecurse instead of eachFile to not only check this directory but also to check directories below it. So this directory has some Groovy files and some text files. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes If we open a new File instance we can point it to the current directory by just giving it the dot that says ‘this directory’. So you see that we had a bunch of files in the structure but it only printed out the Groovy ones and that's because we filter those out. Now there are ways to do filters on files but we'll get into that maybe later. Right now we just wanted to cover some of the basics with files. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes So at this point let's move into directories. So we're here in a new file called dirs.Groovy . The path string is the directory that contains the source for this course. We have created a list called dirFiles as an empty list. We say new File and point it to the path directory. And then the eachFile method loops through each file in a directory. So if it's a directory we print its name. If it’s a file, we add its name to the dirFiles list. After we get to the end of the directory, we print our the dirFiles list. Instead of doing that extra check, we can just get the entire subfolder there and if we wanted just the name, we could say subfolder dot name. We can also go ahead and check attributes on things like directories so say we have a directory here there are some things that we can check on it and one of those is the directory size. So if we print that out it is going to be in bytes, so if you wanted to figure out megabytes you have to do some basic arithmetic there to figure that out. And then we can do things here like create new directories. We can say new File again everything's a file we can say dummy and just use the mkdir method. That's going to make our directory. If we wanted to create multiple directories, we could say something like mkdirs and that will make multiple directories for us. You can see the new directory structure in the red rectangle. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes Now one more thing and that's input and output. So we print a prompt “Please enter your favorite sports:” Now we can use something called System, the method in will read from the console input. This is a helper method to create a new reader for a stream and then it passes it into a closure. So in Java this is a little bit more complex to do, but here we just we just add a reader and we’re away Then read the line from which the user enters the input and when we're done . We ran it and typed in the L A Lakers, and printed it out. So we took our input from the system we read it and we assigned it to a variable and then we put it to our standard print line there. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Instructor notes Target time 14 min Working with databases in Java using JDBC can be extremely frustrating at times. Most of the time we just want to create a connection to a database and then either manipulate data or even query it. Trying to accomplish something as trivial as that in Java requires a lot of complicated and verbose code. In comes Groovy with what is often described as Groovy SQL, a better JDBC. In this lesson we're going to learn how to connect to a database, create a schema, insert some data, and query against it. The screen shows the Groovy API and the groovy.sql package. There's a lot of stuff in here for you to look at but mainly today we're going to focus on the SQL class. The SQL class provides a lot of functionality to allow you connect to a database, query on certain data and many other things.. And as you see here in the documentation, “ A facade over Java's normal JDBC APIs providing greatly simplified resource management and result set handling ”. It's taken that JDBC that is frustrating to work with at times, and made it much easier with a bunch of helpful methods. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes The screen shows a simple Gradle project and its dependencies. Inside this Gradle project we are going to connect to a MySQL database that we have running on our localHost and to do so we have brought in the MySQL connector. One little tip here if you need to bring in dependencies in IntelliJ, use command/N (Control/N on Windows) and that brings up a context menu, and one of the entries a add Maven artifact dependency. You can bring this up, and search for the dependency you need. We have included the compiled dependency for mySQL and the connector so that we can connect to a mySQL database. We have set up a database called Twitter that has no tables. That's going to be the basis for our little demo application that we're going to walk through today together. . ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes So we're here in a simple script called twitter.groovy and we have some basic stuff set up for us and we're going to start to build on that. We have Strings to hold a username and password If you look back in the API documentation in the SQL class, there is a static method newInstance() that allows us to create a new instance of a connection to our database. In our case we need to pass a connection string, a username, a password and a driver class name of com.mysql.jdbc.Driver. So in our case we are going to connect to a SQL database. Our MySQL connection string is ‘jdbc:mysql://localhost:3386/twitter’. Initially, we had a println statement after the newInstance method to confirm that we have reached the next statement. Before we really do anything, it is always good practice to make sure that we can connect to the database. There’s no point in writing a huge script if we don't have our connection set up correctly so do this first and then you can start to build on it. Once the newInstance method has completed, we have a valid MySQL connection object called sql, and we can call the execute() method on it to execute a string containing SQL statements. The first step is to make the script re-runnable by deleting our table if it exists. The next step is to create a schema. This is where those three quotes we discussed in the early part of this course come in handy for inserting multiple lines. Now we can make it easy to go ahead and create some SQL statements. We create a table called users with three columns; id, username and bio. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes If we open up my SQL workbench now and refresh tables we should see our users table now has our id, username and bio. Groovy SQL has done its job. It created our schema for us. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student Notes Now to create some data. We're using the execute again, we're executing a SQL statement here and in this case we're inserting into users the id, username and bio. So our id of 1 (you could easily make this a generated value, we didn't in this case), we have a username of ‘therealdanvega’, and here is the bio. The screen shows a second method of doing the same thing. We created a map called twitterUser with three name/ value pairs; def twitterUser = [id:2,username:’foo’, bio:’foo’] To load the database table from the map we're using those three double quotes because in a single code scenario we can't use the G-strings, the Groovy strings. In this case where we're using are double quotes, we can refer to our map. Now we have two different scenarios where we are creating some data. So again this drop table if exist is going to check to make sure that's there. So in this case it's dropping it recreating it inserting some data and we are complete. We've connected to our database we've created a schema we created some data for it. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes Now at this point we'd probably want to be able to query that data. The screen shows methods defined in the Sql class of the groovy.sql package. There's a lot of different methods in this class. Don’t be overwhelmed by it. You just start to figure out things that you're going to need as you need them. One of the methods that is overloaded here is something called rows() and this returns a list of Groovy read results, which is really a map. There are many overloads, and you can pass in several different parameter types. We can just send some SQL, and if we wanted to send offset and maxRows and we could do some pagination as well. On several overloads we can have a closure. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes So once the result set comes back then let's do something with it. The screen shows two alternative methods of retrieving data. We can call rows and get a list of data back and work with that. We could do def here, if we know what it's going to be, it’s good practice to type it out so we know that we’re getting a list of GroovyRowResult back. So let's call this rows and we're just going to say Sql.rows and we're going to pass in some SQL., “Select * from users” and that’s all. Now that we have our variable rows, and we're going to just print that out. We would see a list with three maps of data. There is something else we can do though. In the Sql class there is a method eachRow. So let's just say that we wanted to pass in “select * from users” and then pass it a closure and now we can say give it a name of row. So now we're basically taking this select statement and then for each row that's returned, we can print line the username as “Tweet @${row.username}” So with our two rows of data in there we're going to get @therealdanvega and tweet@foo. So that's just a little bit of information about working with databases and Groovy. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In this lesson I want to talk about working with dates in the Groovy programming language. Working with dates and Java has always been a real pain point for developers. With some of the additions to the API in Java 8 it got a little bit easier but it's still way easier to do in Groovy. In this lesson we will run through a demo and show off how easy it is to work with dates and do some different things like adding and subtracting or looping through dates. So let's just get started right away with how to create a new date. So the one thing we can do is we have a Date object and we can say Date today = new Date. If we print line today it gives us a formatted date. This is the default format that it prints out in. So a new date is just instantiating a new Date object with the default value of the current date and time. But what if I wanted to create a new date on a specific date so let's say we wanted to say Date bDay = new Date(’08/21/1978’) Now we're instantiating a new date object; we're passing in a string that represents a date and it’s going to use that and pass that underneath the hood to do what it needs to do to create a new date. So now what if we had that format and we wanted to display it in a different format. We can say println bDay.format('M-d-Y') These format strings are clearly documented in the Groovy doc. One of the things that the GDK does is it adds a lot of new methods to the API here that that really help us out. We want to display the month the day then the year. In the execution pane at the bottom of the screen, we can see the birthday as it normally is; this is the default. Then we can see the birthday in the way that we formatted it out with the month, the day of the month, and then this is the four digit year. Let's talk about adding and subtracting now. Let's create a new date we'll call it oneWeekFromToday. If you look through the GDK and look at all of the different methods that Groovy adds, you'll see our minus and plus. So we can add a number of days to this date and it returns a new date. So let's think about that. We have today already. We should be able to just call plus and pass a number of days so that we can do. We can also say oneWeekAgo is equal to today minus seven. We can print line oneWeekFromToday and print line oneWeekAgo. Now we have some easy ways to come up with dates based on simple addition and subtraction. So you see oneWeekFromToday is the 19 th and oneWeekAgo is the 5 th . So the other thing to note here is that remember these are methods. The method plus and the method minus are overloaded here so we don't need to call the method. We can use the operator overload of + or - and just specify the number. We can call those methods, or we can treat it just like we do a regular number. So that's pretty cool. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes That's an easy way to and add and subtract days to a specific day and then if you go back to the API there are methods called upTo and downTo. And all this allows us to start from oneWeekFromToday and go down to today. The method is going to accept a closure so we can do anything we want with each date. At this point I'm just going to println it because we're in a closure with no specific parameter. Now it's going to it's going to take the date one week from today and then step back all the way down to today. In the execution pane we can see the dates decreasing down to today. And here’s another way to iterate over some dates. If we want to loop over some dates we can create a range. On line 11 we create a range called two weeks and we'll start with one week ago and remember from treating ranges, a rang is just one value on the left and one value on the right separated by two dots. So if we go to dots now and we say one week from today we now have a range so we have a range of two weeks here one week ago and one week from today should give us two weeks. We can now treat the range as a List, and use the each method to feed each line in turn to our closure. In the same way as the downTo method, we can print line it. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes Another thing we can do is use next and previous. We start with a date called newYear abd we will set the date to Jan 1 2000. Now we can print line newYear.next(), and that gives us January 2. Now we can also find what the previous day was. When we print newYear.previous(), as expected it gives us December 31, 1999. So those are the next and previous methods. And then finally one thing to look at. One thing that it's really gets confusing when you first start working with dates is that when they have to get inserted into the database, the API comes with a really nice method called toTimeStamp. Given the java.util.date, this is always helpful for returning a java.sql.timestamp object that you can then use to insert into a database row. We can print line rightNow.toTime Stamp() and you'll see it's in a different format. The SQL format needs to be different and so that creates the format that SQL's looking for when we need to insert that into a database. Again it's just real easy to work with dates in Groovy and are many different methods that you can discover by looking through the GDK. You know how much easier it is working with data objects, so we encourage you to look through the documentation and explore and find out what’s available to you. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Because there are no right answers here, there is no exercise review for this one.

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Slide notes

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Slide notes

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In this section we are looking at Runtime Metaprogramming. Metaprogramming is a programming where a program modifies a program. It can be done at compile time, so that functionality is added while the program is being built. It can be done at runtime, that is the program is running, and it is changed on the fly. We will look at runtime metaprogramming here and move on to compile time metaprogramming in the next section. We will look at the Meta Object Protocol (MOP) and how it can be controlled to change the behavior of classes and methods. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In this lecture we’re discussing dynamic programming. More specifically in this section we're going to be focusing in on runtime metaprogramming and then the next section we are focusing on compile time metaprogramming. So you get to hear the word meta a lot in the section, in the next two sections for that matter. Meta, and metaprogramming in particular, is basically the writing of computer programs that write or manipulate other programs. And even in some cases themselves. So in this lecture we're just going to dive into a little bit of the basics of metaprogramming in Groovy and what makes this all possible. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes So the first thing we're going to talk about is something called the meta object protocol or the MOP. And really the MOP is just a collection of rules of how a request for a method call is handled by the Groovy runtime system and how to control the intermediate layer. So this is directly out of the Groovy in Action Second edition book. Whenever Groovy calls a method it doesn't just say all right what class is that class foo. Let me call the method bar on that class. It doesn't do that. Instead it asks this intermediate layer What should I do here. And based on a bunch of rules in a decision tree, based on what type of object you're working with, that's how it knows what to end up calling. That intermediate layer is called the MOP. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes And so here we're looking at a graphic and basically on the left we have what happens when we call from Groovy on the right is when we call from Java. We know that we can call from Java to Groovy and vice versa. But there they take a little bit different path so let's start on the Java side over here on the right. If we compile a Java file class and we call it from Java it calls it directly. It calls right into that class and it looks for a method. Subsequently if we create a class in Groovy and call it from Java, we're calling directly into that class. Now when we call from Groovy ,there's a slightly different path taken. In Groovy, when the caller calls a Groovy method in the target of a Groovy class file we pass through this MOP. And then this MOP has a bunch of rules on what we should do next. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

When we're talking about calls from Groovy, we could really be dealing with three different types of objects. The first one is a Plain Old Java Object (POJO); we have a regular Java object whose class can be written in Java or any other language for the JVM for that matter. So we have a Java object there. Then we have two others that are written in Groovy. We have a Plain Old Groovy Object (POGO) which is a groovy object whose class is written in Groovy. It extends the java.lang.Object and implements Groovy.lang.GroovyObject interface by default. Then we also have something called the Groovy Interceptor and a Groovy Interceptor is just an object that implements the GroovyInterceptable interface and has a method interception capability. So these are the three different types of objects that Groovy is going to be working with and based on which one it has, it's going to take a different route. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes Looking at the decision tree here, we can get an idea of what the MOP is doing. Right away if it's a Java object it's simple; calls the Java object and its target method and moves on. This decision tree does not apply. But when it's a groovy object it goes through the decision tree that looks like this. So first off does a class implement GroovyInterceptable? Yes call its invokeMethod(). No, does the method exist in its MetaClass or this class itself? We'll get to MetaClasses later in this section. But MetaClass is just way to add methods to a particular class. So is it there? Yes, call that original method. Now does the property exist in the MetaClass or the class? I don't see a property in there. In this area we start to get into ways to manipulate the MOP and we can start to customize and add some hook methods into the MOP that allow us to influence that call. So one of those is methodMissing. So does this class have a methodMissing. Yes it does, go ahead and call that. No, does it have an invokeMethod? If it does go ahead and call that. Finally if we got all the way down that tree and we couldn't find anything to do then throw a MissingMethodException. So that is a little bit about how the dynamic nature of a method calling in Groovy works. If it's a little confusing right now that's perfectly fine. This will all start to make sense as we go through this section and the next. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In the last lecture we started to look at what the MOP is or the meta object protocol. And we looked at how it influences what happens when we call a method in Groovy. So now we're going to start to dive in a little bit further. We know that it doesn't just directly call a method; that it looks down it's decision tree and tries to figure out what it needs to do next. Well now we're going to look into customizing that MOP with different hooks so Groovy gives us the ability within that decision tree to declare things to catch these different scenarios. So we're going to do is look at just a normal class and how every class in Groovy implements groovyObject and what methods are available in there. One of those methods is the invokeMethod. So we'll take a look at that and what that actually does for us. Look at get properties set property and property missing for dealing with hooking into different math property calls. And finally we'll look at method missing which allows us to hook into as it states any missing methods that are called. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes So let's create a normal Groovy class called Employee, and we will just compile the skeleton that was generated when we created it. Now we look in the out folder at the generated class.. So you can see public class Employee implements GroovyObject so we know that all classes in Groovy implement this GroovyObject. Let's take a look at this and see what what's actually under the hood here. So you see that this interface is implemented by all Groovy objects specially any for using Groovy objects when in the Java world. So we have these different methods here. So there is invokeMethod, getProperty, setProperty, getMetaClass and setMetaClass. The last two are going to discuss in the next section. The rest of them we are going to deal with here. Let's go into our invokeMethod. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes So this invokeMethod that we saw in that Employee class is available in any class, because all classes implement GroovyObject. So let’s look at how this all works so we can define our own invokeMethod here. We have a new Groovy class file called InvokeMethodDemo.Groovy. Inside that we have a class InvokeDemo that contains a method called test. All this does is return “method exists”. So we can assert that InvokeDemo.test() is equal to“method exists”. As you would expect if we were to run this everything runs fine and we get exactly what we were looking for. What happens in a scenario where we call something that doesn't exist? Let’s call undefinedMethod, which doesn’t exist. That would fail because we don't have a method on this class called undefinedMethod. We have a way to handle this, because that invokeMethod method is available in the class. We can implement that. The invokeMethod is going to take a String name and an object of arguments. All we will do is return a string to say we have been called. Now we can safely assert that when an undefined method is called the result will be the string from our invokeMethod. That should work out just fine. And again now that works so now, we have we have a way to handle a particular method that was not defined in our class and that's by using that invokeMethod. So to recap. When we invoke an existing method in our InvokeDemo class, the invokeMethod method is not called. If we call a method in our Invoke Demo class that does not exist, our local implementation of the invokeMethod is called. We are able to do this because we are implementing that method that was in the GroovyObject interface. So that's invoke method. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes Let's take a look at getProperty setProperty and property missing, starting with getProperty. Every read access to a property can be intercepted by overriding the getProperty method of the object. Again we're implementing that GroovyObject interface so we can override that getProperty method. We start by create a class called PropertyDemo and let's create a few properties, prop1, prop2 and so on. We have some default values for these properties up to prop3 and that should be good enough. Then we instantiate the class below and invoke the three getters. When we run that, we didn’t see anything you wouldn't expect. We have some default values here we call the getter, we know that there is a getter underneath the hood here. What we can do now is implement that method called getProperty and we can intercept that. So now what we're doing is basically overriding getter. So all we're doing here is printing out the get property was called prop one prop to prop 3. Because we have intercepted the get, we need to include the get function in our override. That is the reason for the line return MetaClass.getProperty( this , name) Note that the parameter list to MetaClass.getProperty is different from the getProperty override we have written. The MetaClass getProperty takes an object (this) and a string (name). And we're just going to call this. Now if we were to run this we should see our string printed and the value returned, which is what we want. So what happens if we call a property that doesn't exist, for example called Prop4. In our method we could print a message or return some other value OK. As the code is written, there is a message output on every call. If the property exists, it is returned.. The last one, prop4 was not found. So we print out a message and return a string. So that's one way of kind of manipulating the getter of a property and doing something with it. There's a slightly easier of a way of doing this. And that's something called the property missing. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes Here we are looking at propertyMissing. Groovy supports the concept of propertyMissing for intercepting otherwise failing property resolution. So in this case we've created a new instance of Foo and we're calling a property on it called bar which we don't have. We can define this propertyMissing method and it will catch that. So this is a little bit easier than the other one, but the getProperty method is very powerful for a lot of things. The important thing to remember is that you are overwriting the getters for those properties. So whatever you're doing to manipulate those properties you still need to return something. So that's a little bit about get and property missing. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes And then there of course is setProperty as well. So we've overridden the get but you can also intercept write access to properties by overriding setProperty. So again setProperty is a method that is in the interface GroovyObject that we're implementing here. So we have setProperty, it takes a string name and an object value, and you are referencing the actual field by using direct field access like we saw in a demo earlier. And so in this case you're just overriding. Here we're creating a new instance of Pogo and we're setting our property to ‘a’ but that property comes out to be ‘Overridden’. And that's because no matter what we've set that value to, that setter always calls our setProperty method that sets it to ‘overridden’. So that's a little bit about get property missing and set property. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes So finally we're going to look at missing method. This is really one of the cool hooks that you get into the method dispatch that really makes this a lot of fun. Groovy supports the concept of methodMissing. This method differs from invokeMethod in that it is only invoked in the case of a failing method dispatch when no method can be found for the given name and or given arguments. So whereas invokeMethod can be called for any method that we invoke whether it exists or doesn't exist, methodMissing is only called when we can't find a method. So let's take a look at that. We have a new Groovy script called MissingMethodDemo.groovy. We include an overriding method called methodMissing, snd this is going to take a string of name and an object of arguments called args. It will test for the missing method name and then print out the missing method name and the arguments it was called with. We create an instance of MyEmployee and invoke a method that does not exist. If we run this and you can imagine what is going to happen. There is no method called someMethod but our methodMissing method will catch that particular method because it's not defined, and it'll take the name of the method that was called and the arguments that were passed to it. So then it says method missing called and here's the name of the method and then it gives us what the arguments were as well. So now we get the available arguments; in this case we just passed it in some numbers. We don't we don't always have to catch every single method. One thing that we can do is call someOtherMethod. This will trigger our check in the missingMethod method. So if the name is not someMethod, which in the second case it won't be, we throw the MissingMethodException and then rethrow the name and args. So now we don't have to always write this catch all for any method that anyone declares all of the time. We could easily set up a scenario where we're only looking for certain methods. And in this case, anything else, we want to go and throw our MissingMethodException. So if we go ahead and run this the first one should call and work. Second one should throw MissingMethodException so could not find constructor and MissingMethodException. So that's all there is to that. That method override is really just a great way to hook into that MOP. And if we were to look back at that graphic that we showed in slide 6, one of those things in that decision tree was is there a methodMissing method defined in that class. Oh there is. Well go ahead and call that because that should be able to handle the method we're trying to call that I can't find so far. So again a lot of these methods, and the GroovyObject that defines some of these that we implement, these are all ways to hook into the MOP and customize what happens when a particular method or property is caught. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In a previous lecture we looked at how to hook into the MOP and take advantage of scenarios when we can't find properties or we can’t find methods and by using methods like methodMissing, propertyMissing we're able to do some dynamic programming there. In this lecture we look at how that works, We go through decision tree in the MOP every time a property or a method is called and then we figure out what to do with it. That works because every single class has an associated MetaClass with it. The MetaClass contains all of the methods or properties that your class has within it and where the dynamic nature of Groovy really comes into play is that you can add methods and properties to that MetaClass. You aren't adding anything to the concrete class you're adding it to this layer that's above your class. We'll look at a couple of different examples here. First, here’s a little quick debugging just to show you that this is not magic. We have the skeleton of a class that we instantiate below it. Then we put in a println statement so that we can put a break point here. So a break point here in IntelliJ is going to allow us to stop the execution of the program right here. And then we can use the debugger to evaluate what's going on inside of our program. So instead of running it like we normally do, select Debug module name. When it reaches the break point the debugger will stop and allow us to inspect things. And so if we look at this and look at the variable dev on the bottom right, you can see that it has a property on it called MetaClass. So we could say dev.metaClass because that's going to call the getMetaClass and that's another one of those methods that's in the GroovyObject interface that we have access to. So if we want to get the MetaClass we can. Now a MetaClass has a bunch of information on it and it does a lot of really cool things. And this is what we will focus on in this particular lecture. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes I mean let's look at something else. So in Groovy there is this class called Expando. And if we create a new instance of Expando and if we look at the source for this, you'll see that this class represents a dynamically expandable bean. So now that we know a little bit more about beans we know that beans are simply classes with properties and you can see if you start look through this class you start to understand how this stuff works because it's not just magic. You know there’s getProperty There's setProperty there's invokeMethod… There's a lot of methods in here that make this magic come to life. And what magic is that. Well the Expando class again is a dynamically expandable bean. So what that means is we can add fields and add methods to this expanded class that don’t exist in it. So let's do that on the next slide. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes So we wanted to say that name is equal to Fred and you write code. And we provide a closure as a method here that takes no arguments and it prints “name loves to write code”. When we call e.write code as a method that should print line Fred loves to write code. So this Expando class allows us to add properties and methods to it. Now the one thing that we can say here is, these things are added on a per instance basis. So if we create another Expando e2 equal to new Expando and try to call writeCode on this, it’s is going to fail and tell me that my e2 class has no method called writeCode and that's because we added it to this instance of the Expando that we instantiated as e. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes Now we know what an Expando is. Well our MetaClass is actually and Expando, so it acts like that. Knowing that, we can add fields and methods to our MetaClass which is what everything is called through. We recall every single class has an associated MetaClass with it. And when a method call or property call goes through, it runs through that MetaClass first. We're going to do this on this is a per instance basis first. We can’t just say dev.name because there is no property called name in the Developer2 class. Similarly, there is no method called writeCode. So what we're doing here is we're trying to do the same kind of principle on our Developer class that we did on the Expando. We're trying to add a new name and a new method called writeCode where we can add it to our class itself. Our class is not an Expando class but the MetaClass that is associated with our class is. So we can do is simply add these to the MetaClass and remember calling dev.MetaClasses really like calling the getter so it’s calling getMetaClass and then dot is setting that particular property or method on it. So now if we run this it works. Now we're actually calling the method directly on the class instance. So what happens is when a call goes into the class the instance of then to write code we can see that that method is not in this class but because the call is going through the Associated MetaClass, the MetaClass knows how to handle that method and then that method is called. So that's pretty cool. Again this is a per instance basis if we created a new instance of developer this would not work. Now you can start to do this on an every instance basis. And this is always where the the line from Spiderman comes into thought. You know “with great power comes great responsibility”. So in instances like this you've got to be really careful what you’re messing with. We know there is a String class. Well we can do the same thing with a String class that we can with our class. Every class has a MetaClass associated with it. So now we can add methods this way. If we wanted to add a method called shout and in this method, we're going to take whatever the string is and uppercase it. So now we can do is we can print and print. Let's just say “hello all” and we're going to call a method called shout. This is an instance of a String, and every instance of String has a MetaClass associated with it. But now we're attaching this to all instances of String. So before this is called it looks and it's MetaClass it's MetaClass has a method called shout which uppercase it. So now if we go ahead and run this we should see the words HELLO ALL. in upper case. So again this is for every instance. So that's manipulating the MetaClass for every instance. Now again that is a scenario where you want to be pretty careful. You don't want to be just willy nilly adding methods to String class because that becomes pretty dangerous. Not only that but there is no documentation for that if you're on a larger team and you're writing code and you're adding methods to say the String class, that can be pretty confusing for coworkers to look and go well what the what the heck is going on there. This stuff comes in really handy, but it also can be really dangerous. So you've got to be careful with where you're just adding stuff to. So that's the MetaClass and now we know that that there is the layer above the class that gets called and that's how we can start to dynamically add methods and properties to our classes. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes Using the MetaClass to add dynamic capabilities to our programs is usually the way to go. Sometimes though, we might prefer an alternative method that is a little bit more convenient and not so application-wide. In this lesson we look at this problem and show how categories help us solve it. There are also a few built in categories into the language and we will look at a demo of one of them today. We have a new file called catdemo and we're going to look at the code from the MetaClass example that we used from the last lesson. There was the scenario where we wanted to add a new method called shout. So we did it to the String class and we created a new method called shout and all that is going to do is take no arguments and it's going to uppercase that particular string so then we can do a println “hello world”.shout() and if we run that it should print out HELLO WORLD in all uppercase. So now a lot of the times this is going to work great. But you can see a problem here. We're adding a method to the String class which is the String class which we're going to use throughout our program. Right. And you know in a scenario where we're working especially on teams and or even like an open source project people may not know that that method exists unless there is a well-defined API. So just sporadically adding methods to the String class is probably not a great idea. What we can do is create something called a category class and it allows us to confine and only add methods to a particular block of code instead of application wide. Let's look at how we can do this. The first thing to do is create a new class. We’ll create a new Groovy class called StringCategory. Now it doesn't need to end in Category but that's the convention. You'll see some other classes in the API that are have Category on their names. At this point we want to create a static method on this class that do something. In our case we're going to just use our shout method. It's going to return a string. We're going to create a static method. It's going to return a string. We're going to call this shout. This is going to take a string as an argument and all it's going to do is return the string in uppercase. Now we have our method that we can use, and the way that we use a category class is this. So the way that we use a category class in Groovy is with the use keyword. We use a particular category class in this case. You can use more than one if you want to. In our case we're just going to use one, so we start typing in the string category and then we’re going to have a block here. In this block, anything in that string category class is like a native function call on this block. We can say print the line “hello world” and just call shout like this: println “hello world”.shout() This is going to work exactly as it did before. But, you can probably tell, with one big caveat. It's only available within this use block. Now if we try and just copy that line and paste it outside the block, it is going to throw an error. So we're going to see a no signature of method String shout. Especially on something like the String class, it's a much better approach because it keeps it confined and other developers can know that there is a category class they can use as they need to within that particular block of code. That's how you create a category class that's how you use the category class. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes Now one more thing; there are some category classes built into the language. You’ll find them easily by going to the documentation and search for categories. One of the category classes is the TimeCategory. We're going to look at that now. We’re have a new script and we will just call it Time. Later in this course there’s a dates demo that's really going to show you how easy it is to work with dates in Groovy as compared to Java. But there isn't a lot of extra help for time stuff. Being able to represent time in different formats is where the TimeCategory really comes in. It lets us do some really cool things. We say use (TimeCategory) so the IDE will import that. And now we could do some cool stuff. One minute from now is 1.minute.from.now So I could say one minute from now and really all that's happening there is we're just calling different methods that the TimeCategory class is making available to us. So if you look at the TimeCategory class, you'll see the category classes are the same whether you write them or someone else, that they just provide the static methods You have like things like minus, getDaylightSavings, that kind of stuff. There's just a bunch of methods in there. Look at the docs look at the source code if you want. We can do some other things like Ten hours ago: println 10 .hours.ago Three months ago: def someDate = new Date() println someDate - 3 .months we could do let's say day That's just how to use a category. But again when working with dynamic runtime metaprogramming it's important to know what you're doing when you're adding stuff to something like the String class and in certain situations like that maybe it's better to use a category and kind of know that you're confining it to that particular block of code instead of just application wide. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In this lecture we're going to have a quick review of the methodMissing method that allows us to catch any methods that are not declared and then we're also going to look at a bit of a performance boost with a pattern called the Intercept/ Cache/ Invoke pattern. We have a class called Developer and just a recap we remember that if we want to catch any method, we can create a method called methodMissing that's going to take an argument of name and args. Let's go ahead and print a line to say (name) method was called: Println “$name method was called…” Right. All right so now let's create a new instance of developer: Developer dan = new Developer() Then we call a method: dan.writeGroovy() and if we go ahead and run this what we're going to see is that because Groovy is not a method that exists in this class, methodMissing is going to catch that and print out the line “writeGroovy method was called”. So far so good. We've already done this. But now let's start to build on this. Let's say that we had a list of languages and we'll create an empty list by default and then down below we populate the list: dan.languages << “Groovy” dan.languages << “Java” Remember from the MetaClass that we can access its methods. So we print out how many methods are in the Metaclass: println dan.metaClass.methods.size() When we run this now, we see that there are 17 methods. Now we invoke our missing method three times in succession: dan.writeGroovy() dan.writeGroovy() dan.writeGroovy() and print out the method count again: println dan.metaClass.methods.size() If we run this, every time method missing is called, we are printing out the statement whatever method was called. In our case, our methods are always going to start with “write” as in write language like writeJava writeGroovy etc, so we test for that: if ( name.startsWith(“write”) ) { Now we want to get the language so we do that by splitting on the name. So we're just going to split on that and take the second member of the list: String language = name.split(“write”)[1] If our list of languages that contains language, then we're in the right ballpark. We don't want to just allow anybody to write anything. So if I wrote HTML out then that's not in my list of languages. So that wouldn't that wouldn't get us in here. Now we're going to write an implementation: If (languages.contains(language) ) { def impl = { Object[] -> println “I like to write code in $language” } And then we get the MetaClass. Remember when we started talking about MetaClass that every single method call goes through its MetaClass first. So it's looking on the MetaClass to find out if there is a method called write whatever the language is. We call the MetaClass and add a new method to there. So in this case we're adding a method called the name, and we're going to set it to the implementation above: getMetaClass().”$name” = impl When we call that method, we will print that line “I like to write in [language]”. Finally we return the implementation with the args: return impl($args) And we should be good to go. So now what we're looking at. Remember when methodMissing is called the print line statement is called to name the method. So here's what I'd expect. We have our two new languages. We have a print line so the print line is going to basically say 17 then writeGroovy is called three times. What happens is methodMissing should only get called once because the first time through we’re calling methodMissing because writeGroovy does not exist. But on the first time through we find that we create the implementation of writeGroovy and we add that to the MetaClass. Now the next time through that method exists in our MetaClass and methodMissing is never going to get called. So again this is just a little bit of a performance boost because you don't want to go through all of the hooks to find out where this is and eventually you get to nothing missing and then perform some logic the next time through. This is already on our MetaClass and therefore we don't need to get into all of this find it. Our MetaClass should now have one more method. The writeGroovy method was called three times. The first time we get the message from methodMissing saying it was called, and the line “I like to write code in Groovy” and that's because the first time we do it we stick the implementation on the metaclass. Then on the next two calls we only get the “I like to write code…’ message, so you could see methodMissing is only called once which means it's calling writeGroovy on the MetaClass and now we have an 18th method on our MetaClass.. And then the method writeJava was called. We now have a 19th method on our MetaClass and we we didn't have to call the methodMissing the second time through. So this is what's called the intercept/ cache/ invoke pattern. And again it's just a performance. This was invented by Graeme Rocher who is the lead developer on the Grails project. So just a big shout out to him for this nice performance boost here. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Instructor notes We are going to create a class that represents a single tweet. This is an exercise both about code and starting to think about what goes into building a class. There is no right or wrong answer here so don't be afraid to build your class how you see fit. I will go over in the review what I was thinking about when I built mine but again my answer is not the right one.

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Please refer to your lab booklet

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Slide notes

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In this section we are looking at Compile Time Metaprogramming. The main type of compile time metaprogramming we will discuss is AST Transformations. We will not discuss how to write your own AST Transformations. That would be a bit too much to include in this course, so we will concentrate on the typical AST transformations that you will use in your daily programming life and see how much time and effort they can save you. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

This is the Compile Time Metaprogramming page from the Groovy-lang website. The link to this page is on the documentation page under Getting Started. We will look at examples of a lot of the basic AST transformations that you're going to want to be using. Once you start to see it, then it'll start to make sense that we can stick an annotation and customize it onto a class or on to some methods, and when the compiler runs it creates code for us that is there in the compiled class. We can see the compiled classes and so it makes our life a lot easier without having to write some of this boilerplate boring code. We're going to look at ToString, EqualsAndHashCode, Sortable and Immutable, a bunch of really cool transformations They save us a lot of time. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In this lesson we're going to look at the ToString AST transformation. To look up the documentation, go to groovy-lang.org, select Documentation well down on the left of the page, then select “GroovyDoc documentation of the Groovy APIs” under the API heading, then under Packages select groovy.transform. The ToString documentation is linked to below Annotation Types. We can write these ToString methods ourselves or we can generate them from the IDE, but if something changes in that class then we always need to update the method. It makes it a lot cleaner if we just add the ToString annotation to our class: @groovy.transform.ToString Or import groovy.transform.* … @ToString Once you add that, Groovy is going to create a ToString method that includes all the properties of the class. Obviously, that's not always the ideal scenario. So there are options available. They are listed under Optional Element Summary in the documentation. You can include the options in the AST transformation so that you can customize what you want it to look like. For example there are excludes. Maybe there is a particular field that should not be shown when we print the class. You can choose to ignore null values. You can also choose whether or not to include the names of properties when displaying property values. We recommend you take the time to look through the documentation on all the different attributes you can include. But let's look at an example. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes Here we have a Groovy class called person. We have some properties; a first name, a last name and an e-mail. You could write this toString method yourself or you can use your IDE to generate one for you. We have a demo application called ToStringDemo. It has Person p as a new Person, and some values for our properties. We can write our own toString method, or we can have the IDE generate one for us, shown commented out on the slide. The problem with this is, if we change fields or add fields then we always have to update this particular method and it's just a pain to have to maintain. A better solution is to add a ToString annotation to our class. If we start typing ToString, we'll see that it's in the groovy.transform package. When we hit enter the IDE imports that for us. Now we have our person Albert Winkle and a gmail account. We can customize it if this doesn't work for us. We can ignore nulls if we don't want nulls to show up. One other thing we can do is excludes. Now certain times we may not want particular fields to show up. In this case if we wanted to exclude the email property our annotation would be @ToString(includeNames = true, excludes=(‘email’) ) That's the ToString. It is an AST transformation that we can just tack onto a class. We get that generated automatically every build. If we were to look at the actual bytecode, in the Person class you would see a toString method there. That's not something we want to have to write and maintain every single time we change a field in our class. So the @ToString annotation is really helpful. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In this lesson we're going to take a look at the EqualsAndHashCode AST transformation. So if you're new to the Java and Groovy world don't worry about that. We're going to look at an example of what EqualsAndHashCode is used for. But really this again just like all of the other AST transformations we're going to look at. It just cleans up our code and separates that actual logic into a much cleaner solution. Just like the ToString annotation, here's some documentation on how to get going with it. There are some options that we can set on the transformation one of which is excludes, to exclude a particular property from the calculation of whether or not an object is equal. S ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes We will use that person class that we used in the ToString demo and we'll go through a scenario here on why we need EqualsAndHashCode. In our demo we're going to create two instances of Person, p1 and p2 so that you can see that by the example here that they all have the same properties, so they have a first name a last name and an email and they're all exactly the same. But when we run this it's going to tell us that that assert fails. Groovy says they're not equal because the application has no idea what you mean by equal. Its only reference is by an instance, and those two instances are different because it doesn't know how to compare them. What we do in Java and Groovy here is we create an EqualsAndHashCode method that does know if two objects are equal. One problem with creating these is we can go out and create it ourselves. But again, just like toString, if any properties change we're going to have to regenerate this or rewrite this and creating these from scratch or even copying and pasting some old code is very error prone. So in come in here and obviously we can generate EqualsAndHashCode so we can say next I want to include all three fields. And we end up with an equals method and a hashCode method. Now those two those would work and our equals assertion would work. But there is a better way. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes If we type EqualsAndHashCode the IDE is going to import that transformation for us. All we've done is add that; we run this, and our program runs just fine. Now that our class knows how to perform equality, it knows that the instances may be different but they have the same properties and that's how we're defining whether or not these two instances are equal to each other. The same person can have more than one email address, so we might want want to exclude the email from the equality test. We can do that in the same way as we add excludes to the ToString. So that's how we use EqualsAndHashCode. Again that just helps with equality in comparing two objects. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In this lesson we're going to take a look at the TupleConstructor AST transformation. This is going to be a real short one but it's really handy as well. Groovy provides us with a nice way to instantiate objects. So in this first instance you can see we're creating a new customer and we're using named parameters even though we didn't set up a constructor that takes all of these named parameters. Groovy gives us that ability. There are certain times when we have a small object, or maybe we're creating this object from Java where we want to just pass in the implicit parameters and we don't want to have to sit there and name them out. In the red rectangle we have a TuplePerson class. It has a ToString on it so we can print it out. We also added the new TupleConstructor annotation. In the demo script we created two objects, one in the Person class we have seen before, and one in the TuplePerson class that has the TupleConstructor annotation. In the TuplePerson we don’t need to name the parameters, we just listed them and the IDE put in the names for us. Just like all the other transformations that we've looked at there are some different attributes that you can tack on to this TupleConstructor to customize it to your needs. So take a look at the documentation. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

The next AST transformation that we're going to look at is Canonical. This is just a combination of the previous three annotations that we looked at; ToString, EqualsAndHashcode and the TupleConstructor. These three are so commonly used and they’re so commonly used together that Groovy decided to create a new annotation called Canonical. In the red rectangle we have a CanonicalPerson class with the only annotation being @Canonical. In the app we create two instances of CanonicalPerson with unnamed parameters. The IDE names them for us using the TupleConstructor capability. We can print p1, using the ToString capability. Then we can safely assert that the two instances are equal, using the EqualsAndHashcode capability. Then we print a line to confirm that nothing happened on the assertion line. Canonical is very commonly used; you're going to find yourself using it a lot. One word of caution: If you need to customize just one, say you wanted to exclude a property from ToString, then you need to add the ToString to that particular class and then pass in the particular attributes that you need. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

The next AST transformation that we're going to look at is Singleton. Singleton is a way to make a class can follow a certain standard. In the Java world, if a class is a Singleton, we can only have one instance of this class in existence. So the way you get around it in Java is you usually create a static field that holds the instance in a private constructor so you can never create an instance of that class. You can only call its getInstance method; a static method to create an instance of that class. Something we can easily do in Groovy is add the Singleton AST transformation. And now when we create an instance we're going to get an error as you see in the bottom left of the screenshot. We don't want to be able to create an instance of it; there's only going to be one in existence. If you want an instance of the dbConnection then you need to get it through its getInstance method. If you look at the generated class code on the right of the screenshot, you'll see that there is a public static final dbConnection instance. If you want to get an instance you call the getInstance method and then there’s a private constructor. If you try to publicly create a new instance of this, you're going to get that runtime error. We put a try catch around the normal way of creating an instance so you can see the exception message that is generated. After the catch block is the correct method of instantiating a singleton and a print of the toString output for the class. So again @Singleton just allows us to follow the singleton pattern and make sure only one instance of that class exists. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

The next AST transformation we're going to look at is Sortable. Sortable is a very useful transformation and it makes sorting objects or classes very easy. So normally if you want to write your own class in Java you need to do a few things to make sure that it can be sortable. You need to implement a comparable interface. Then you need to have a method in there called compareTo which is based on the properties that you want to compare to. So there is some work involved to make this sort of all but in our case in Groovy we don't need to write that underlying logic. And then any time properties change, we don’t have to update or maintain that logic; we can simply add the @Sortable annotation to our class. So let's take a look at an example here. We have a SortablePerson class. Its only properties are first and last. We have the @Canonical annotation, so we can create them easily and print instances at will. We have five instances, four with the last name Gulf and one with the last name Anderson. We made a List of these persons and call toSorted on the List. There is no point in calling toSorted unless the list elements can be compared for sorting. Adding the @Sortable annotation does just that. If we run at this point, with no conditions on the @Sortable annotation, the elements will be sorted by first name then last name. If we would like to sort in the more usual way, by last name, and by first name when the last name is the same. The option can be set on the @Sortable annotation to include fields in a particular order by adding the includes option with a list of property names expressed as strings. If you look at the output section of the screenshot you can see that the instances are now correctly sorted. So, @Sortable is a nice transformation that we can add to our classes to easily make them sortable. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

The next AST transformation that we're going to look at is Immutable. So immutable means that once we create an instance of something that we cannot change its state. To make a class immutable in Java we follow a set of conventions like; The class is automatically made final. All the properties must have private final backing fields with getters and no setters. There are more conditions, you can go to the documentation to read through what makes a class immutable. But Groovy makes it very easy not to have to follow each one of these conventions ; we just add this transformation to our class and Groovy will do this for us. Here we have our PersonImmutable class with properties first and last. We have Canonical on it and we can add @Immutable. Be careful we want to make sure we're using the Groovy.transform one. In the demo script we create an instance of PersonImmutable and try to assign a new value to the first property. When we run that we get the exception shown on the screen. It says: “Cannot set readonly property: first for class: com.idfacademy.metaprograming.PersonImmutable”. In fact if we open the PersonImmutable.class file in the out directory we will see that there are no setters for that particular field. So Immutable is something you could do on your own but with Groovy, adding a single annotation to our class means that we now have an immutable class. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In this lesson we're going to take a look the AST transformation called TypeChecked. One of the biggest complaints with dynamic languages is the lack of compile time type checking and because of the dynamic nature of Groovy we don't get a lot of that either. But we can, with this particular AST transformation. We have a new method called getFullName that returns the value of whatever firstName is and whatever lastName is. If we mistype lastName as lastname, that will still compile, but it won’t produce the expected result. Because even though the compiler can't find that particular field lastname, we don't know that that's necessarily a problem. We know that Groovy has runtime metaprogramming capabilities that would allow us to add lastname whenever we wanted to. So even though it's not, right at this point, lastname could eventually be a valid value. We can ask Groovy type check this, and we can add this annotation at the class level which means everything in this class is going to be type checked, or we can add it at the level of individual methods or properties. As soon as we add the annotation, the field in error turns red and we get an error message that you see in the red rectangle. So there would be a problem there if we were to try to compile it. So while Groovy is a dynamic language, a lot of people don't know that it does have static type capabilities. So between the @TypeChecked and the Compile Static annotations we can really treat this in certain situations as a static language if we need to or as a static type language if we need to. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In a previous lesson we looked at the @TypeChecked AST transformation. In this lesson we're going cover @CompileStatic. The two go together; CompileStatic will let the Groovy compiler use compile time checks in the same style as Java, then perform static compilation thus bypassing the Groovy Memory Object Protocol or the MOP. So we looked at the MOP before and this may kind of sound like @TypeChecked and it kind of is. There is a major difference though and that is the MOP. …”thus bypassing the Groovy Memory Object Protocol…” So @TypeChecked keeps methods going through the MOP while @CompileStatic is generating method calls similar to Java’s byte code. This really means that their semantics are different, but it also means that you can still apply metaprogramming on top of @TypeCheck code as long as the method calls can be resolved at compile time. So that's really the difference: @TypeChecked is going to allow you to still do some dynamic programming while @CompileStatic really is closer to Java's generated bytecode. So we're going to look at a quick example. So we have a few methods here and we can go ahead and annotate the class with compile static or individual methods. In this case if we annotate the class with @CompileStatic, every property and method goes through the type checking here. So this is really at a class level scenario. If we wanted this to go through for almost everything in this class except one particular method, you can turn that off for one method or even a couple of methods if you wanted to. So we can use at compile static and then inside of that, TypeCheckingMode like @CompileStatic (TypeCheckingMode. SKIP ) Both @TypeChecked and @CompileStatic give you a little more flexibility when you need to change the way things work in in Groovy to match Java. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

In this lesson we're going to take a look at a pretty cool transformation called Builder. Until this point, all the transformations that we've looked at have been in the groovy.transform package. The builders stuff is in another package called groovy.transform.builder. Let's go into that package and you'll see one annotation type for builder and then some other classes here that are different types of strategies to use with this builder so we can see looking at the documentation here the @Builder AST transformation is used to help write classes that can be created using something called fluent API calls. Basically the builder allows you a new way to construct objects and it can really help out when you're creating objects of objects as well. So we'll see that there's some different strategies in here. We're only going to look at the default strategy today but at the end of this I'll give you a way that you can kind of use other strategies if you want to jump into the documentation and look at it. So just a quick note that Groovy provides some built-in mechanisms for creating objects. They are included in this page. The syntax for building out some of our objects is very appealing, so we're going to take a look at it and then really it's up to you to find out if you find any use for it. So again we're in the app builder notation and then documentation and then you can see there are different strategies here. When you don't pass in a strategy to the app builder annotation, it's going to use the default strategy. So this is a first place that you're going to want to look. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Student notes Here we have a class called BuilderDeveloper with some properties; firstName, lastName, middleInitial, email, hireDate and a List called languages. We need to imort groovy.transform and groovy.transform.builder. There are two annotations, @Builder and also @ToString just so we can see what happens when we create our object. So that is everything that we need to do to our class. We have an @Builder annotation. We're not providing a different strategy so we can just leave that off, and Groovy will use the default strategy. Now in the Default.groovy script, we use the builder to instantiate an object. Most of the time when we create a new instance of an object like this, we would say BuilderDeveloper dev = new BuilderDeveloper(… and then pass in some named arguments or call setters on our object. Here what we're doing is we're calling a method called build(). We split the statement into separate lines; .builder() and now we can call setters on our build object so it looks like for every property it looks like we're calling the setter, then finally when we're done creating all of our properties, we're just going to call the build() method. So now at this point we should have an object. We can check that with a println statement. So we could see just another easy way to instantiate an object here using the app builder notation, and just a way to create some fluent APIs. Sometimes when people are coming to Groovy, they don't really understand that an object like our BuilderDeveloper object here has the ability to take named parameters because they don't see a constructor, and then if somebody wrote this, right away they would kind of understand that we’re just doing setters on our developer object to build it out. So again it's really up to you to find if you have use for this. ‹#›

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Instructor notes We looking at a lot of AST Transformations in this section. Now I want you to go through the documentation and find one that we didn't look at and see if you can get it to work on your own.

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Please refer to your lab booklet

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

Slide notes

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A

© 2026 Identitrain, Inc. All Rights Reserved.IDT-GD-200 rev A