Groovy’s metaprogramming capabilities provide powerful tools for dynamically altering and extending the behavior of classes and objects at runtime. This set of 30 multiple-choice questions (MCQs) covers advanced topics such as writing custom AST transformations, leveraging ExpandoMetaClass, debugging metaprogramming code, and building framework features. Mastering these concepts can enhance your Groovy skills and open the door to creating more flexible and efficient applications.
@CompileStatic in Groovy’s AST transformations?
ExpandoMetaClass keywordMetaClass on a classExpandoMetaClass.enableExpandoMetaClass()@Expando annotationMetaClass methods in Groovy?
ExpandoMetaClass.removeMethod()ExpandoMetaClass.disableExpandoMetaClass()MetaClass.removeMethod()addProperty()MetaClass.addDynamicProperty()@Profile annotation@Log annotationMetaClass when debugging metaprogramming code?
invokeMethod()logMethod()traceMethod()debugMethod()MetaClass to intercept method callsExpandoMetaClass to add methods at runtime| Qno | Answer |
|---|---|
| 1 | a) Abstract Syntax Tree |
| 2 | a) By creating a class that implements ASTTransformation |
| 3 | a) ASTTransformation |
| 4 | b) @ASTTransformation |
| 5 | b) The class’s bytecode at runtime |
| 6 | b) By modifying the abstract syntax tree during the compile phase |
| 7 | a) To add static type checking to the code |
| 8 | b) By rewriting the source code |
| 9 | b) Generating boilerplate code like getters and setters |
| 10 | a) @ASTTransformation |
| 11 | a) To extend existing classes dynamically |
| 12 | c) By invoking ExpandoMetaClass.enableExpandoMetaClass() |
| 13 | a) Add new methods to an existing class at runtime |
| 14 | b) It overrides existing methods dynamically |
| 15 | b) Using ExpandoMetaClass.disableExpandoMetaClass() |
| 16 | a) It allows mocking and stubbing of methods for unit testing |
| 17 | a) Spock framework |
| 18 | c) By assigning a value to the property name |
| 19 | b) It allows for dynamic extension of classes |
| 20 | b) The dynamic nature of method resolution |
| 21 | b) By integrating with a third-party profiler like VisualVM |
| 22 | a) TraceMetaClass |
| 23 | b) By adding logging methods through ExpandoMetaClass |
| 24 | b) It tracks method invocations and properties |
| 25 | a) invokeMethod() |
| 26 | c) By minimizing method interception |
| 27 | d) All of the above |
| 28 | d) All of the above |
| 29 | c) AST transformations |
| 30 | d) All of the above |