Apache Groovy 4 Adds Support for New Java Features

The Apache Software Foundation's (ASF) Groovy team recently announced the release of Groovy 4.0.0, the latest version of the open source, general-purpose programming language for the Java Virtual Machine (JVM).

This release, announced at the end of January, comes with a range of new features, from Switch Expressions to Sealed Types, Records, and Groovy-Integrated Query—approximately 800 new features, improvements, and bug fixes since the release of Groovy 3.0.0 in February 2020.

According to Paul King, principal software engineer at OCI and VP and chair of Groovy project management committee, more than 80 contributors from the Groovy community and ecosystem added to this release.

This release was designed with the latest version of the Java Development Kit (JDK) in mind. For example, support for the Java Platform Module System (JPMS) has been improved. The JPMS requires that classes in distinct modules have distinct package names. This requirement is known as the "split packaging requirement." The latest Groovy release comes with its own "modules" that weren’t historically structured according to this requirement, King explained.

This version also includes support for Switch Expressions, which evaluate to a single value and can be used in Statements. "Groovy has always had a very powerful switch statement," King wrote, "but there are times when a Switch Expression would be more convenient."

Groovy-Integrated Query (GINQ), is an incubating feature in this release. GINQ is designed to support querying collections in a SQL-like style involving things like lists, maps, and/or the user's own domain objects or those returned when processing (such as JSON, XML, and other structured data.) The Groovy Team has plans to further develop GINQ to add GQuery support for SQL databases, where an optimized SQL query is generated based on the GQuery expression, similar to Groovy’s DataSet functionality.

There's also new native support for Records (incubating) in this release, as well as Record-like classes (also known as "emulated records"). Records are immutable data classes that require only the type and name of fields. Record-like classes have all the features of native records, but don’t have the same information at the bytecode level and so won’t be recognized as records by a Java compiler in cross-language integration scenarios.

And there's a Java equivalent of GroovyShell in this release, which was designed to allow developers to work with snippets of Java code more easily.

Groovy is a Java-syntax-compatible, object-oriented language that has been around since the early 2000s, when it was submitted to the JCP as JSR 241. Version 1.0 was released in 2007, and version 2.0 wasn't released until 2012. Groovy 2.4 was released under the stewardship of Pivotal Software. In 2015, Groovy became a project at the ASF.

Groovy is both a static and dynamic language. It has features that are similar to Python, Ruby, and Smalltalk. It can be used as both a programming language and a scripting language. Because it compiles to the JVM, it interoperates seamlessly with other Java code libraries. Unlike Java, Groovy supports domain-specific languages (DSLs) and meta-programming.

Groovy 4.0.0 is available now; resources, convenience binaries, downloadable documentation, and an SDK bundle can be found here, King wrote in his announcement. "We recommend you verify your installation using the information on that page," he wrote. "Jars are also available within the major binary repositories." Groovy 5.0 is the next official major version of Groovy. The team expects to have an alpha releases of that version soon.

About the Author

John K. Waters is the editor in chief of a number of sites, with a focus on high-end development, AI and future tech. He's been writing about cutting-edge technologies and culture of Silicon Valley for more than two decades, and he's written more than a dozen books. He also co-scripted the documentary film Silicon Valley: A 100 Year Renaissance, which aired on PBS.  He can be reached at [email protected].