Groovy

Thursday February 13, 2020

Groovy 3 Highlights

Groovy 3 Highlights 

General Improvements

Groovy has both a dynamic nature (supporting code styles similar to Ruby and Python) as well as a static nature (supporting styles similar to Java, Kotlin and Scala). Groovy continues to improve both those natures - filling in any feature gaps. As just one example, Groovy has numerous facilities for better managing null values. You can use Groovy's null-safe navigation operator, piggy back on Java's Optional or provide a null-checking extension to the type checker. These are augmented in Groovy 3 with null-safe indexing for arrays, lists and maps and a new AST transformation @NullCheck for automatically instrumenting code with null checks.

In general, the language design borrows heavily from Java, so careful attention is paid to changes in Java and acted on accordingly if appropriate. A lot of work has been done getting Groovy ready for Java modules and for making it work well with JDK versions 9-15. Other work has dramatically improved the performance of bytecode generation which makes use of the JVMs invoke dynamic capabilities. Additional changes are already underway for further improvements in these areas in Groovy 4.

There are also many other performance improvements under the covers. More efficient type resolution occurs during compilation and more efficient byecode is generated for numerous scenarios. The addition of a Maven BOM allows more flexible usage of Groovy from other projects.

Groovy also has particular strengths for scripting, testing, writing Domain Specific Languages (DSLs) and in domains like financial calculations and data science. On-going work has been made to ensure those strengths are maintained. The accuracy used for high-precision numbers has been improved and is configurable. Much of the tooling such as Groovy Console and groovysh have also been improved.

Other key strengths of Groovy such as its runtime and compile-time meta-programming capabilities have also seen many minor enhancements. All in all, this release represents the culmination of several years of activity. Over 500 new features, improvements and bug fixes have been added since Groovy 2.5. Just a few highlights are discussed below.

Parrot parser

Groovy has a new parser. While mostly an internal change within Groovy, the good news for users is that the new parser is more flexible and will allow the language to more rapidly change should the need arise.

New syntax

The new parser gave us the opportunity to add some new syntax features:

  • !in and !instanceof operators
assert 45 !instanceof Date
assert 4 !in [1, 3, 5, 7]
  • Elvis assignment operator
def first = 'Jane'
def last = null
first ?= 'John'
last ?= 'Doe'
assert [first, last] == ['Jane', 'Doe']
  • Identity comparison operators
assert cat === copyCat  // operator shorthand for is method
assert cat !== lion     // negated operator shorthand
  • Safe indexing (for maps, lists and arrays)
println map?['someKey'] // return null if map is null instead of throwing NPE

Java compatibility

The Groovy syntax can be thought of as a superset of Java syntax. It's considered good style to use the enhancements that Groovy provides when appropriate, but Groovy's aim is to still support as much of the Java syntax as possible to allow easy migration from Java or easy switching for folks working with both Java and Groovy.

The flexibility provided by the new parser allowed several syntax compatibility holes to be closed including:

  • do/while loop
def count = 5
def factorial = 1
do {
    factorial *= count--
} while(count > 1)
assert factorial == 120
  • Enhanced classic Java-style for loop (see multi-assignment for-loop example; note the comma in the last clause of the for statement)
  • Multi-assignment in combination with for loop
def count = 3
println 'The next three months are:'
for (def (era, yr, mo) = new Date(); count--; yr = mo == 11 ? yr + 1 : yr, mo = mo == 11 ? 0 : mo + 1) {
    println "$yr/$mo"
}
  • Java-style array initialization (but you might prefer Groovy's literal list notation)
def primes = new int[] {2, 3, 5, 7, 11}
  • Lambda expressions (but you might often prefer Groovy's Closures which support trampoline/tail recursion, partial application/currying, memoization/auto caching)
(1..10).forEach(e -> { println e })
assert (1..10).stream()
              .filter(e -> e % 2 == 0)
              .map(e -> e * 2)
              .toList() == [4, 8, 12, 16, 20]
def add = (int x, int y) -> { def z = y; return x + z }
assert add(3, 4) == 7
    • Method references (but you might often prefer Groovy's Method pointers which are Closures with the previously mentioned benefits)
    assert ['1', '2', '3'] == Stream.of(1, 2, 3)
                                    .map(String::valueOf)
                                    .toList()
          • "var" reserved type (allows Java 10/11 features even when using JDK 8)
          var two = 2                                                      // Java 10
          IntFunction<Integer> twice = (final var x) -> x * two            // Java 11
          assert [1, 2, 3].collect{ twice.apply(it) } == [2, 4, 6]
          • ARM Try with resources (Java 7 and 9 variations work on JDK 8 - but you might prefer Groovy's internal iteration methods for resources)
          def file = new File('/path/to/file.ext')
          def reader = file.newReader()
          try(reader) {
              String line = null
              while (line = reader.readLine()) {
                  println line
              }
          }
          • Nested code blocks
          • Java-style non-static inner class instantiation
          • Interface default methods (but you might prefer Groovy's traits)
          interface Greetable {
              String target()
              default String salutation() {
                  'Greetings'
              }
              default String greet() {
                  "${salutation()}, ${target()}"
              }
          }

          Split package changes

          In preparation for Groovy's modular jars to be first class modules, several classes have moved packages. Some examples:

          groovy.util.XmlParser => groovy.xml.XmlParser
          groovy.util.XmlSlurper => groovy.xml.XmlSlurper
          groovy.util.GroovyTestCase => groovy.test.GroovyTestCase

          In most cases, both the old and new class are available in Groovy 3. But by Groovy 4, the old classes will be removed. See the release notes for a complete list of these changes. 

          DGM improvements

          Groovy adds many extension methods to existing Java classes. In Groovy 3, about 80 new such extension methods were added. We highlight just a few here:

          • average() on arrays and iterables
          assert 3 == [1, 2, 6].average()

          • takeBetween() on String, CharSequence and GString
          assert 'Groovy'.takeBetween( 'r', 'v' ) == 'oo'

          • shuffle() and shuffled() on arrays and iterables
          def orig = [1, 3, 5, 7]
          def mixed = orig.shuffled()
          assert mixed.size() == orig.size()
          assert mixed.toString() ==~ /\[(\d, ){3}\d\]/

          • collect{ } on Future
          Future<String> foobar = executor.submit{ "foobar" }
          Future<Integer> foobarSize = foobar.collect{ it.size() } // async
          assert foobarSize.get() == 6

          • minus() on LocalDate
          def xmas = LocalDate.of(2019, Month.DECEMBER, 25)
          def newYear = LocalDate.of(2020, Month.JANUARY, 1)
          assert newYear - xmas == 7 // a week apart
          

          Other Improvements

          Improved Annotation Support

          Recent version of Java allow annotations in more places (JSR308). Groovy now also supports such use cases. This is important for frameworks like Spock, Micronaut, Grails, Jqwik and others, and also opens up the possibility for additional AST transformations (a key meta-programming feature of Groovy).

          Groovydoc Enhancements

          In addition to Groovydoc supporting the new parser, you can now embed Groovydoc comments in various ways:

          • They can be made available within the AST for use by AST transformations and other tools.
          • Groovydoc comments starting with a special /**@ opening comment delimiter can also be embedded into the class file. This provides a capability in Groovy inspired by languages like Ruby which can embed documentation into the standard binary jar and is thus always available rather than relying on a separate javadoc jar.

          Getting Groovy

          The official source release are on the download page. Convenience binaries, downloadable documentation, an SDK bundle and pointers to various community artifacts can be found on that page along with information to allow you to verify your installation. You can use the zip installation on any platform with Java support, or consider using an installer for your platform or IDE.

          The Windows installer for the latest versions of Groovy 3 are available from bintray. (community artifact)

          For Linux users, the latest versions of Groovy 3 are also available in the Snap Store. (community artifact)

          For Eclipse users, the latest versions of the Groovy 3 groovy-eclipse-batch plugin are available from bintray. (community artifact)

          For Intellij users, the latest community editions of IDEA have Groovy 3 support. 

          Monday July 15, 2019

          Groovy 3.0.0-beta-2 Windows Installer Released (Community Release)

          The Windows installer for Groovy 3.0.0-beta-2 is now available from Bintray: https://bintray.com/groovy/Distributions/download_file?file_path=groovy-3.0.0-beta-2-installer.exe.

          I've again included a preview of an msi built with WiX, which I'm seeking feedback on: https://bintray.com/groovy/Distributions/download_file?file_path=groovy-3.0.0-beta-2+%28preview+installer%29.msi

          Be aware that you need to fully uninstall the NSIS based Groovy installation before installing with an MSI installer.

          Groovy 3.0.0-beta-2 Released

          The Apache Groovy team is pleased to announce version 3.0.0-beta-2 of Apache Groovy. Apache Groovy is a multi-faceted programming language for the JVM. Further details can be found at the https://groovy.apache.org website.

          This is a pre-release of a new version of Groovy. We greatly appreciate any feedback you can give us when using this version.

          This release includes 40 bug fixes/improvements as outlined in the changelog: https://issues.apache.org/jira/secure/ReleaseNote.jspa?projectId=12318123&version=12345498

          Sources, convenience binaries, downloadable documentation and an SDK bundle can be found at: https://groovy.apache.org/download.html We recommend you verify your installation using the information on that page.

          Jars are also available within the major binary repositories.

          We welcome your help and feedback and in particular want to thank everyone who contributed to this release.

          For more information on how to report problems, and to get involved, visit the project website at https://groovy.apache.org/

          Note: Apache Groovy 3.0.0-beta-2 was compiled with JDK8, so the illegal access warnings will come back if you use JDK9+. But don't worry, we will do another release in a week or two. Please verify the issues you reported first and give us feedback, which will help us improve the quality of next release.

          Best regards,

          The Apache Groovy team.

          Sunday May 12, 2019

          Groovy 2.5.7 and 3.0.0-beta-1 Windows Installers Released (Community Artifacts)

          The Windows installer for Groovy 2.5.7 (Community Artifact) is now available from Bintray: https://bintray.com/groovy/Distributions/Windows-Installer/groovy-2.5.7-installer.

          The Windows installer for Groovy 3.0.0-beta-1 (Community Artifact) is now available from Bintray: https://bintray.com/groovy/Distributions/download_file?file_path=groovy-3.0.0-beta-1-installer.exe.

          These are also the first releases where a preview of the Windows Installers is created with the WiX Toolset.  You are invited to try them out and provide any feedback you might have.  The intention is to eventually replace the current NSIS-based installer with this installer.  It is believed to be reasonably stable.  The maintainer of these installer has personally been using these instead of the NSIS based installer for a while now.  Here are the links to those installers:

          Be aware that you need to fully uninstall the NSIS based Groovy installation before installing with an MSI installer.

          Groovy 3.0.0-beta-1 Released

          Dear community,

          The Apache Groovy team is pleased to announce version 3.0.0-beta-1 of Apache Groovy.
          Apache Groovy is a multi-faceted programming language for the JVM.
          Further details can be found at the https://groovy.apache.org website.

          This is a pre-release of a new version of Groovy.
          We greatly appreciate any feedback you can give us when using this version.

          This release includes 109 bug fixes/improvements as outlined in the changelog:
          https://issues.apache.org/jira/secure/ReleaseNote.jspa?projectId=12318123&version=12344761

          Sources, convenience binaries, downloadable documentation and an SDK
          bundle can be found at: https://groovy.apache.org/download.html
          We recommend you verify your installation using the information on that page.

          Jars are also available within the major binary repositories.

          We welcome your help and feedback and in particular want
          to thank everyone who contributed to this release.

          For more information on how to report problems, and to get involved,
          visit the project website at https://groovy.apache.org/

          Best regards,

          The Apache Groovy team.

          Groovy 2.5.7 Released

          Dear community,

          The Apache Groovy team is pleased to announce version 2.5.7 of Apache Groovy.
          Apache Groovy is a multi-faceted programming language for the JVM.
          Further details can be found at the https://groovy.apache.org website.

          This release is a maintenance release of the GROOVY_2_5_X branch.
          It is strongly encouraged that all users using prior
          versions on this branch upgrade to this version.

          This release includes 56 bug fixes/improvements as outlined in the changelog:
          https://issues.apache.org/jira/secure/ReleaseNote.jspa?projectId=12318123&version=12344939

          Sources, convenience binaries, downloadable documentation and an SDK
          bundle can be found at: https://groovy.apache.org/download.html
          We recommend you verify your installation using the information on that page.

          Jars are also available within the major binary repositories.

          We welcome your help and feedback and in particular want
          to thank everyone who contributed to this release.

          For more information on how to report problems, and to get involved,
          visit the project website at https://groovy.apache.org/

          Best regards,

          The Apache Groovy team.

          Groovy 2.4.17 Released

          Dear community,

          The Apache Groovy team is pleased to announce version 2.4.17 of Apache Groovy.
          Apache Groovy is a multi-faceted programming language for the JVM.
          Further details can be found at the https://groovy.apache.org website.

          This release is a maintenance release of the GROOVY_2_4_X branch.
          It is strongly encouraged that all users using prior
          versions on this branch upgrade to this version.

          This release includes 5 bug fixes/improvements as outlined in the changelog:
          https://issues.apache.org/jira/secure/ReleaseNote.jspa?projectId=12318123&version=12345028

          Sources, convenience binaries, downloadable documentation and an SDK
          bundle can be found at: https://groovy.apache.org/download.html
          We recommend you verify your installation using the information on that page.

          Jars are also available within the major binary repositories.

          We welcome your help and feedback and in particular want
          to thank everyone who contributed to this release.

          For more information on how to report problems, and to get involved,
          visit the project website at https://groovy.apache.org/

          Best regards,

          The Apache Groovy team.

          Tuesday January 01, 2019

          Groovy 3.0.0-alpha-4 Windows Installer Released (Community Artifact)

          The Windows installer for Groovy 3.0.0-alpha-4 (Community Artifact) is available from the usual place: https://bintray.com/groovy/Distributions/download_file?file_path=groovy-3.0.0-alpha-4-installer.exe.

          Note this installer doesn't include Spock since there isn't a Spock release compatible with Groovy 3.0.

          Groovy 3.0.0-alpha-4 Released

          Dear community,

          The Apache Groovy team is pleased to announce version 3.0.0-alpha-4 of
          Apache Groovy. We expect this to be the last "alpha" release of Groovy
          3.0.0 as we shift our focus to releasing this next version of Groovy.

          2018 was an exciting year for Groovy with download numbers going over
          100M in 2018 for the first time. We also had 18 releases and over 30
          new contributors. Thanks to you all! We expect 2019 to be even more
          exciting as we bring to fruition the hard work (including hundreds of
          improvements and fixes) that have already gone into 3.0.0.

          Apache Groovy is a multi-faceted programming language for the JVM.
          Further details can be found at the http://groovy.apache.org website.

          This is a pre-release of a new version of Groovy.
          We greatly appreciate any feedback you can give us when using this version.

          This release includes 138 bug fixes/improvements as outlined in the changelog:
          https://issues.apache.org/jira/secure/ReleaseNote.jspa?projectId=12318123&version=12343541

          Sources, convenience binaries, downloadable documentation and an SDK
          bundle can be found at: http://www.groovy-lang.org/download.html
          We recommend you verify your installation using the information on that page.

          Jars are also available within the major binary repositories.

          We welcome your help and feedback and in particular want
          to thank everyone who contributed to this release.

          For more information on how to report problems, and to get involved,
          visit the project website at https://groovy.apache.org/

          Best regards,

          The Apache Groovy team.

          Monday December 24, 2018

          Groovy 2.5.5 Windows Installer Released (Community Artifact)

          The Windows installer for Groovy 2.5.5 (community artifact) is available at: https://bintray.com/groovy/Distributions/Windows-Installer/groovy-2.5.5-installer.

          Groovy 2.5.5 released

          Dear community,

          The Apache Groovy team is pleased to announce version 2.5.5 of Apache Groovy.
          Apache Groovy is a multi-faceted programming language for the JVM.
          Further details can be found at the http://groovy.apache.org website.

          This release is a maintenance release of the GROOVY_2_5_X branch.
          It is strongly encouraged that all users using prior versions on this branch upgrade to this version.

          This release includes 20 bug fixes/improvements as outlined in the changelog:
          https://issues.apache.org/jira/secure/ReleaseNote.jspa?projectId=12318123&version=12344435

          Sources, convenience binaries, downloadable documentation and an SDK bundle can be found at: http://www.groovy-lang.org/download.html
          We recommend you verify your installation using the information on that page.

          Jars are also available within the major binary repositories.

          We welcome your help and feedback and in particular want to thank everyone who contributed to this release.

          For more information on how to report problems, and to get involved, visit the project website at https://groovy.apache.org/

          Best regards,

          The Apache Groovy team.

          Tuesday December 18, 2018

          Groovy 2.4.16 Windows Installer Released (Community Artifact)

          The Windows installer for Groovy 2.4.16 (community artifact) is available from: https://bintray.com/groovy/Distributions/Windows-Installer/groovy-2.4.16-installer.

          Groovy 2.4.16 Released

          Dear community,

          The Apache Groovy team is pleased to announce version 2.4.16 of Apache Groovy.
          Apache Groovy is a multi-faceted programming language for the JVM.
          Further details can be found at the http://groovy.apache.org website.

          This release is a maintenance release of the GROOVY_2_4_X branch.
          While we recommend moving to the current 2.5.x releases (currently 2.5.4),
          for any users who can't upgrade to 2.5 and are using 2.4.x versions,
          we strongly recommend that you upgrade to this version.

          This release includes 18 bug fixes/improvements as outlined in the changelog:
          https://issues.apache.org/jira/secure/ReleaseNote.jspa?projectId=12318123&version=12342996

          Sources, convenience binaries, downloadable documentation and an SDK
          bundle can be found at: http://www.groovy-lang.org/download.html
          We recommend you verify your installation using the information on that page.

          Jars are also available within the major binary repositories.

          We welcome your help and feedback and in particular want
          to thank everyone who contributed to this release.

          For more information on how to report problems, and to get involved,
          visit the project website at https://groovy.apache.org/

          Best regards,

          The Apache Groovy team.


          Tuesday July 10, 2018

          [ANNOUNCE] Announcing CodeNarc 1.2

          groovy-logo.png

          The CodeNarc Team is proud to announce the release of version 1.2.

          CodeNarc is a static analysis tool for Groovy source code.

          Version 1.2 includes 5 new rules and several enhancements and bug fixes. See the full details in the release notes.

          New Rules

        • StaticFieldsBeforeInstanceFields rule (convention) - Enforce that all static fields are above all instance fields within a class.
        • StaticMethodsBeforeInstanceMethods rule (convention) - Enforce that all static methods within each visibility level (public, protected, private) are above all instance methods within that same visibility level.
        • PublicMethodsBeforeNonPublicMethods rule (convention) - Enforce that all public methods are above protected and private methods.
        • GrailsDomainStringPropertyMaxSize rule (grails) - String properties in Grails domain classes have to define maximum size otherwise the property is mapped to VARCHAR(255) causing runtime exceptions to occur.
        • NoJavaUtilDate rule (convention) - Do not use java.util.Date. Prefer the classes in the java.time.* packages. Checks for construction of new java.util.Date objects.

          Check out the project on GitHub!

          The Grails CodeNarc Plugin has been updated to version 1.2 as well.

        • Calendar

          Search

          Hot Blogs (today's hits)

          Tag Cloud

          Categories

          Feeds

          Links

          Navigation