Table of contents
1.
Introduction
2.
What are Grails and Groovy?
3.
Grails Static Type Checking and Compilation
3.1.
The GrailsCompileStatic Annotation
3.2.
The GrailsTypeChecked Annotation
4.
Frequently Asked Questions
4.1.
What is Grails?
4.2.
Is Groovy a programming language?
4.3.
What is type Checking?
4.4.
What is Static Type Checking?
4.5.
What is Dynamic Type Checking?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Grails-Static Type Checking And Compilation

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hey Ninja! Welcome to another article on Grails. Have you wondered how web applications are developed? What are the various frameworks and libraries used for web development? Great, then you are at the right place. This article is based on Grails, a Java framework for web development. 

This article will specifically discuss static type checking and compilation in Grails.

What are Grails and Groovy?

Many recent Java web frameworks are sophisticated and do not adhere to the Don't Repeat Yourself (DRY) principles.

Dynamic frameworks like Rails and Django paved the door for a more current approach to online applications. Grails expand on these ideas and significantly decrease the difficulty of developing web applications on the Java platform. 

Grails is an open-source web development framework that uses the Groovy programming language (which is, in return, based on the Java programming language). It is intended to be a high-productivity and highly effective framework by conforming to the "code by convention" idea.

Groovy is an open-source, object-oriented programming language for the Java platform that is Java syntax compliant. It is a dynamic and static language with features similar to Python, Ruby, and Smalltalk. 

Grails Static Type Checking and Compilation

Grails Static Type Checking and Compilation

Type Checking is also one of the important topics of programming. The technique of validating and enforcing type restrictions in values is known as type checking. A compiler must always ensure that the source program adheres to the source language's syntactic and semantic standards and the language's type rules.

Groovy is a dynamic language that employs a dynamic dispatch mechanism by default to carry out method calls and property access. This dynamic dispatch mechanism gives the language a lot of flexibility and strength. It is possible to dynamically add methods to classes at runtime and to dynamically replace existing methods at runtime.

These are crucial features that give the language a lot of strength. However, there may be times and occasions when you wish to disable dynamic dispatch to work in a more static manner. But don't worry because, with Groovy, we also get the option to do this. 

To instruct the Groovy compiler that a certain class should be built statically, use the groovy.transform.CompileStatic annotation, as seen below.

import groovy.transform.CompileStatic
 
@CompileStatic
class Ninja {
// This class is going to be statically compiled because we used @CompileStatic.
    
    def NinjaMethod() {
        String message = "This is the Ninja Method";
        println(message);
    }
}


One disadvantage of using CompileStatic is that you lose access to the power and flexibility provided by dynamic dispatch. In Grails you would be unable to execute a GORM(Grails object relationship mapping) dynamic finder from a class marked with CompileStatic since the compiler would be unable to validate that the dynamic finder method existed because it does not exist at compile time. GORM is the implementation of mapping the classes to the database and creating of tables. The GORM dynamic finder as the name suggests works dynamically, so it may cause a problem if the class is specified using the CompileStatic annotation.

This problem is solved by using the grails.compiler.GrailsCompileStatic annotation. You can use Groovy's static compilation features without giving up the accessibility to dynamic dispatch for Grails-specific features like dynamic finders with the help of grails.compiler.GrailsCompileStatic. By using the GrailsCompileStatic annotation in place of CompileStatic annotation, you can use the features of Grails and also take advantage of dynamic dispatch. GrailsCompileStatic acts similarly to CompileStatic, but is aware of particular Grails capabilities and offers dynamic access to such functionalities.

The GrailsCompileStatic Annotation

The GrailsCompileStatic Annotation

The GrailsCompileStatic annotation can be applied to a class or its methods. If you use the GrailsCompileStatic annotation on the class, all the class methods will be statically compiled. 

import grails.compiler.GrailsCompileStatic

@GrailsCompileStatic
class Ninja {

    // All the methods will be statically compiled.
    def NinjaMethod() {
        String message = "This is the Ninja Method";
        println(message);
    }

    def Coding Ninjas StudioMethod() {
        String message = "This is the Code Studio Method";
        println(message);
    }
}


If you do not want the entire class to statically compile and want only some specific methods, then this is also possible. You can specify the GrailsCompileStatic annotation for the methods you want to compile statically. Rest all the methods will be compiled dynamically. 

import grails.compiler.GrailsCompileStatic

Class Ninja {

    // Only NinjaMethod will be statically compiled.
    @GrailsCompileStatic
    def NinjaMethod() {
        String message = "This is the Ninja Method";
        println(message);
    }

    // Coding Ninjas StudioMethod will be dynamically compiled because we didn’t use the @GrailsCompileStatic
    def Coding Ninjas StudioMethod() {
        String message = "This is the Code Studio Method";
        println(message);
    }
}

 

It is possible to designate a class with GrailsCompileStatic and omit certain methods by marking them with GrailsCompileStatic and indicating that type checking for that method should be skipped, as demonstrated below.

import grails.compiler.GrailsCompileStatic
import groovy.transform.TypeCheckingMode

@GrailsCompileStatic
class Ninja {

    // NinjaMethod will be statically compiled
    def NinjaMethod() {
        String message = "This is the Ninja Method";
        println(message);
    }

    // Coding Ninjas StudioMethod will be dynamically compiled
    @GrailsCompileStatic(TypeCheckingMode.SKIP)
    def Coding Ninjas StudioMethod() {
        String message = "This is the Code Studio Method";
        println(message);
    }
}


Except for the Grails-specific interactions that cannot be statically generated, the code tagged with GrailsCompileStatic will be statically compiled. In domain classes, this includes executing dynamic finders and DSL(Domain Specific Language) code in configuration blocks like constraints and mapping closures.

When determining whether or not to statically compile code, be careful. There are advantages to static compilation, but to reap those advantages, you must forego the power and flexibility of dynamic dispatch. For example, if code is statically generated, it cannot benefit from runtime metaprogramming advances given by plugins.

The GrailsTypeChecked Annotation

The GrailsTypeChecked Annotation

The grails.compiler.GrailsTypeChecked annotation is similar to the GrailsCompileStatic annotation in that it allows static type checking but not a static compilation. 

import grails.compiler.GrailsTypeChecked

@GrailsTypeChecked
class Ninja {

    // This class's code will be checked for statically typed 
    // but dispatched dynamically at runtime.

    def NinjaMethod() {
        String message = "This is the Ninja Method";
        println(message);
    }

    def Coding Ninjas StudioMethod() {
        String message = "This is the Code Studio Method";
        println(message);
    }
}

Frequently Asked Questions

What is Grails?

Grails is an open-source web development framework that uses the Groovy programming language (which is, in return, based on the Java programming language). It is intended to be a high-productivity and highly effective framework by conforming to the "code by convention" idea.

Is Groovy a programming language?

Yes, Groovy is an open-source, object-oriented programming language for the Java platform that is Java syntax compliant. It is a dynamic and static language with features akin to Python, Ruby, and Smalltalk. 

What is type Checking?

The technique of validating and enforcing type restrictions in values is known as type checking. A compiler must always ensure that the source program adheres to the source language's syntactic and semantic standards, as well as the language's type rules.

What is Static Type Checking?

If the type of a variable/method is known at compile time rather than runtime, the language is statically typed. C, C++, C#, and Java are examples of statically typed languages.

What is Dynamic Type Checking?

Dynamic type checking is the technique of confirming a program's type at runtime. Groovy, JavaScript, PHP, Python, and Ruby are examples of such languages.

Conclusion

In this article, we learned about Grails-Static Type Checking And Compilation. We saw how you could specify what methods are to be statically and what methods are to be dynamically compiled. 

We encourage you to check out our other articles to learn more about Grails. 

I hope you have gained a better understanding of these topics now! Are you planning to ace the interviews with reputed product-based companies like AmazonGoogleMicrosoft, and more? 

Attempt our Online Mock Test Series on Coding Ninjas Studio now!

Happy Coding!

Live masterclass