GDSL Awesomeness – Defining dynamic method in a class
We can define dynamic methods in a class as follows:
Suppose we have injected a method name .clone([boolean] clearTime) in Date which clones the date object and returns the cloned one. This method takes an boolean argument argument too.
We can do it as follows:
(See inline comments for description)
[java]
//First we need to define a context
def dateContext = context(ctype: "java.util.Date") //ctype: takes the target class full name.
//Now we need to contribute the method in the class
contribute(dateContext) {
//name: Name of the method
//type: The return type of the method
//params: Map of Arguments passed.
method name: ‘clone’, type: ‘java.util.Date’, params: [clearTime: Boolean]
}
[/java]
Read Further in the “GDSL AWESOMENESS” Series
- GDSL Awesomeness – Introduction to GDSL in IntelliJ Idea
- GDSL Awesomeness – Understanding Context And Contributors in Detail
- GDSL Awesomeness – Defining dynamic property in a class
- GDSL Awesomeness – Defining dynamic method in a class
- GDSL Awesomeness – Adding Hinting for missingMethod implementation
- GDSL Awesomeness – Setting a global context
- GDSL Awesomeness – Delegating Closure Calls
- GDSL Awesomeness – Defining methods in a closure passed to method of particular name
- GDSL Awesomeness – Defining Properties in a closure passed to method of particular name
- GDSL Awesomeness – Contributing to classes which match a path/name pattern
- GDSL Awesomeness – contributing methods with current class return types or parameters
- GDSL Awesomeness – AutoComplete Script for grails-Mail-Plugin “sendMail” closure
- GDSL Awesomeness – Getting Code of a method or Field in GDSL Script
- GDSL Awesomeness – Advanced Example showing domain properties implementation
Hope it helps
Kushal Likhi
Wow! This can be one particular of the most helpful blogs We’ve ever arrive across on this subject. Actually Wonderful. I am also an expert in this topic therefore I can understand your effort. łuparki