Java Magazine, Mar/Apr 2018
ORACLE COM JAVAMAGAZINE MARCH APRIL 2018 69 jvm languages Life isnt usually that simple but I hope this gives you an idea how much easier it will be to work with macros Its not a capability that is likely to change what Groovy developers do every day but it will make the creation of AST transformations easier Groovy already includes a wealth of useful AST transformations triggered by annotations such as @ Canonical @ Delegate @ Immutable @ InheritConstructors @ Slf4j @ TypeChecked and @ CompileStatic The new macro capability will make it much easier to create many more Traits Another major addition to Java 8 was the ability to define default and static methods inside interfaces While the Parrot parser will support them as well at least in some form Groovy already has something that does this and more traits Groovy traits are used like interfaces but their methods can contain implementations just like Javas default methods Traits can also have state however which Java interfaces cannot The Groovy documentation contains several simple examples along with the rules and restrictions on their usage but here is an example from the Grails framework Grails 3 is based on Spring Boot and it provides a powerful object relational mapping API called GORM that works with both relational and NoSQL databases The testing framework has recently been refactored to use traits Here is the beginning of a test of a Grails controller The controller receives HTTP requests by mapping a URL to a controller operation known as an action The declaration of a controller test which is auto generated by Grails when you define a controller looks like this class QuestControllerSpec extends Specification implements ControllerUnitTest QuestController DomainUnitTest Quest Both ControllerUnitTest and DomainUnitTest are traits and are implemented by the class the same way interfaces are The source code for ControllerUnitTest for example starts this way
You must have JavaScript enabled to view digital editions.