Metaprogramming in Groovy allows developers to write more flexible and dynamic code by enabling runtime modification of classes, methods, and properties. With features like Categories, ExpandoMetaClass, dynamic method addition, and AST transformations, Groovy provides powerful tools for extending or altering behavior without modifying source code. This guide covers these key metaprogramming concepts.
1. Categories and ExpandoMetaClass
What is the purpose of using Categories in Groovy?
a) To change the behavior of existing methods
b) To modify the runtime behavior of classes globally
c) To create new classes dynamically
d) To extend the functionality of specific classes at runtime
What does ExpandoMetaClass allow you to do in Groovy?
a) Modify classes only during compile time
b) Add or modify methods and properties at runtime
c) Only add properties, not methods
d) Extend Java classes
Which of the following is true about ExpandoMetaClass in Groovy?
a) It is used for static method resolution
b) It enables adding methods and properties dynamically to a class
c) It replaces the need for closures
d) It only works with classes from the Groovy standard library
Which class is used to create categories in Groovy?
a) MetaClass
b) Category
c) ExpandoMetaClass
d) GroovyClass
What is a typical use case of Categories in Groovy?
a) Altering the behavior of a specific instance of a class
b) Changing a class permanently in all contexts
c) Adding new methods to Java classes
d) Modifying objects of any class dynamically
2. Adding Methods at Runtime
How do you add a method to a class at runtime in Groovy?
a) By using the addMethod method of the class
b) Using the ExpandoMetaClass API
c) Modifying the class directly in the source code
d) Using reflection to add a method
In which way can you remove a method added at runtime in Groovy?
a) Use removeMethod of the class
b) Use ExpandoMetaClass.removeMethod()
c) Delete the method in source code
d) Methods cannot be removed once added
What is an example of a method being added dynamically to a class using ExpandoMetaClass?
a) MyClass.metaClass.newMethod = { println 'Hello' }
b) MyClass.addMethod("newMethod")
c) ExpandoMetaClass.addMethodTo(MyClass, "newMethod")
d) MyClass.methods.add('newMethod')
What is a potential downside of adding methods dynamically at runtime?
a) It can make the code harder to maintain
b) It significantly improves performance
c) It is only available for Groovy classes
d) It is illegal in Groovy
What does the following Groovy code do? MyClass.metaClass.methodName = { println 'New Method' }
a) It defines a static method in MyClass
b) It adds a new method called methodName to MyClass
c) It modifies the constructor of MyClass
d) It removes the method methodName from MyClass
3. Property Missing and Method Missing
In Groovy, what is methodMissing used for?
a) To handle undefined methods dynamically
b) To define missing methods at compile time
c) To create missing properties automatically
d) To throw an error when a method is not found
What does propertyMissing allow in Groovy?
a) Handling undefined properties dynamically
b) Creating missing methods dynamically
c) Adding new methods at runtime
d) Changing the value of existing properties
Which method is called when an undefined method is invoked on an object in Groovy?
a) methodMissing
b) propertyMissing
c) missingMethod
d) onMethodMissing
Which of the following is true about the propertyMissing method in Groovy?
a) It allows handling undefined properties
b) It only works for static properties
c) It is used for handling class-level methods
d) It does not work for classes defined in Java
What is the primary advantage of using methodMissing and propertyMissing?
a) They allow dynamic behavior and flexibility for classes at runtime
b) They are faster than static methods
c) They can be used to write multithreaded code
d) They are primarily used for error handling
In Groovy, what happens when methodMissing is called?
a) The runtime engine will attempt to invoke the method normally
b) An exception is thrown, and the program halts
c) A custom implementation can be provided for the missing method
d) The method is automatically added to the class
How do you implement a custom propertyMissing method in Groovy?
a) By defining a method named propertyMissing in the class
b) By using the metaClass property
c) By invoking methodMissing within the class
d) By overriding the get method
How does Groovy handle undefined properties at runtime when propertyMissing is implemented?
a) It returns null by default
b) It calls the propertyMissing method
c) It throws an exception automatically
d) It adds the property dynamically
In the case of both methodMissing and propertyMissing, how does Groovy respond if they are not implemented?
a) It throws an exception
b) It handles the method or property like a normal Groovy feature
c) It attempts to look up the missing method or property in the superclass
d) It ignores the missing method/property silently
Which method would you override to customize the behavior when a missing property is accessed in Groovy?
a) propertyMissing
b) methodMissing
c) missingProperty
d) getProperty
4. AST Transformations
What does AST Transformation allow in Groovy?
a) Transforming source code during compile time
b) Adding runtime logic to a class
c) Modifying classes during their execution
d) Removing methods from a class
Which Groovy annotation is used to trigger an AST transformation?
a) @CompileStatic
b) @Transform
c) @Category
d) @MetaClass
How does AST transformation improve Groovy’s performance?
a) It allows compile-time optimizations and code generation
b) It increases memory consumption
c) It slows down the execution of the program
d) It provides runtime code modifications
Which of the following is an example of an AST transformation in Groovy?
a) @CompileStatic
b) @Category
c) @Autowired
d) @Lazy
What is the primary advantage of using AST transformations in Groovy?
a) They make code more dynamic at runtime
b) They reduce the complexity of debugging
c) They can optimize code and modify behavior during compilation
d) They make the code completely static
Which of the following transformations adds a default constructor to a class in Groovy?
a) @ToString
b) @TupleConstructor
c) @EqualsAndHashCode
d) @Canonical
Which Groovy AST transformation is used to automatically implement toString(), equals(), and hashCode() methods for a class?
a) @ToString
b) @Canonical
c) @TupleConstructor
d) @Slf4j
Can AST transformations be applied to existing classes in Groovy?
a) Yes, using Groovy’s @CompileStatic transformation
b) No, they can only be applied to new classes
c) Yes, by applying annotations at runtime
d) No, they can only be applied during code compilation
What is the role of the @CompileStatic AST transformation in Groovy?
a) It compiles the code statically, optimizing runtime performance
b) It transforms dynamic Groovy code into static Java code
c) It allows dynamic typing in Groovy
d) It adds dynamic features to Java classes
Which of the following would you use to apply multiple AST transformations to a single class in Groovy?
a) Use separate annotations for each transformation
b) Chain transformations in a single annotation
c) Only one transformation can be applied at a time
d) AST transformations cannot be combined
Answer Table
Qno
Answer (Option with Text)
1
b) To modify the runtime behavior of classes globally
2
b) Add or modify methods and properties at runtime
3
b) It enables adding methods and properties dynamically to a class
4
b) Category
5
a) Altering the behavior of a specific instance of a class
6
b) Using the ExpandoMetaClass API
7
b) Use ExpandoMetaClass.removeMethod()
8
a) MyClass.metaClass.newMethod = { println 'Hello' }
9
a) It can make the code harder to maintain
10
b) It adds a new method called methodName to MyClass
11
a) To handle undefined methods dynamically
12
a) Handling undefined properties dynamically
13
a) methodMissing
14
a) It allows handling undefined properties
15
a) They allow dynamic behavior and flexibility for classes at runtime
16
c) A custom implementation can be provided for the missing method
17
a) By defining a method named propertyMissing in the class
18
b) It calls the propertyMissing method
19
a) It throws an exception
20
a) propertyMissing
21
a) Transforming source code during compile time
22
a) @CompileStatic
23
a) It allows compile-time optimizations and code generation
24
a) @CompileStatic
25
c) They can optimize code and modify behavior during compilation
26
b) @TupleConstructor
27
b) @Canonical
28
a) Yes, using Groovy’s @CompileStatic transformation
29
a) It compiles the code statically, optimizing runtime performance
30
a) Use separate annotations for each transformation