Groovy and Grails Groovy Language Fundamentals & Metaprogramming 2 — Questions and Answers
Question 1: What is Groovy's Meta-Object Protocol (MOP) primarily used for?
- Defining database schemas at runtime
- Intercepting and modifying method calls and property access dynamically (Correct answer)
- Compiling Groovy scripts to native bytecode
- Managing dependency injection containers
Correct answer: Intercepting and modifying method calls and property access dynamically
The MOP allows Groovy programs to intercept, synthesize, and delegate method calls and property access at runtime.
Question 2: Which Groovy interface must a class implement to intercept calls to methods that do not exist on it?
- GroovyCallable
- GroovyInterceptable (Correct answer)
- DynamicObject
- MetaMethod
Correct answer: GroovyInterceptable
Implementing `GroovyInterceptable` causes all method calls on an object to go through `invokeMethod`, even existing ones.
Question 3: In Groovy metaprogramming, what does adding a method to `ExpandoMetaClass` achieve?
- It permanently modifies the JVM bytecode of the target class
- It adds the method to all instances of that class at runtime without modifying source code (Correct answer)
- It creates a subclass with the additional method
- It generates a Groovy AST transformation
Correct answer: It adds the method to all instances of that class at runtime without modifying source code
`ExpandoMetaClass` lets you add methods to existing classes at runtime, visible to all instances, without touching source code.
Question 4: What is the purpose of the `methodMissing` hook in Groovy?
- To define abstract methods that must be overridden in subclasses
- To catch calls to methods that don't exist and handle them dynamically (Correct answer)
- To mark deprecated methods that should be avoided
- To declare optional interface methods with default implementations
Correct answer: To catch calls to methods that don't exist and handle them dynamically
`methodMissing` is invoked when a called method is not found, enabling dynamic method generation or delegation.
Question 5: Which Groovy feature allows you to add methods to an existing class temporarily within a defined scope using a helper class?
- Traits
- Mixins
- Categories (Correct answer)
- Delegates
Correct answer: Categories
Groovy Categories allow you to add methods to existing classes within a `use` block, scoping the additions to that context.
Question 6: What does the `@Delegate` AST transformation in Groovy do?
- Marks a method to be called asynchronously
- Generates delegation methods so the annotated field's methods become part of the enclosing class's API (Correct answer)
- Declares the class as a singleton managed by Grails
- Enables compile-time null-safety checks on the annotated field
Correct answer: Generates delegation methods so the annotated field's methods become part of the enclosing class's API
`@Delegate` automatically generates forwarding methods for all public methods of the annotated field, implementing the delegation pattern.
Question 7: In Groovy, what is a Trait primarily used for?
- Defining immutable value objects
- Providing reusable method implementations that can be mixed into multiple classes (Correct answer)
- Annotating classes for Spring component scanning
- Specifying database table mappings for GORM
Correct answer: Providing reusable method implementations that can be mixed into multiple classes
Traits in Groovy allow you to define reusable sets of methods (with implementations) and fields that classes can incorporate via `implements`.
What is Groovy's Meta-Object Protocol (MOP) primarily used for?