GDSL Awesomeness – contributing methods with current class return types or parameters

24 / Sep / 2012 by Kushal Likhi 0 comments

Hi,

 

Sometimes we need to contribute dynamically using current target class type.

 

Lets take the example of .get() and .list() methods of Grails domain class.
The signature of each method is as follows:

  • public <DomainClassType> get(Long id) , Example: DemoClass.get(1) will return an object of “DemoClass”
  • public List<DomainClassType> list()

 

The GDSL Script for the above example will be as follows:

import com.intellij.patterns.PsiJavaPatterns
import com.intellij.patterns.PlatformPatterns

//get the context of all files, and enable path matching. Note: Don't change anything in context here.
def ctx = context(
        ctype: PsiJavaPatterns.psiClass().withName(PlatformPatterns.string().matches(/.*/))
)  

contributor(ctx) {
    def path = ""
    try {
        path = psiClass.containingFile.originalFile.virtualFile.path
    } catch (Exception e) {
    }

    //Check for Path pattern here
    if (path =~ ".*/*grails-app/domain/.*") {
    //Contribute to matching classes here.
        method(name: 'get', type: psiClass.qualifiedName, params: [id: Long])
        method(name: 'list', type: "List<${psiClass.qualifiedName}>")
    }
}

 

Read Further in the “GDSL AWESOMENESS” Series

  1. GDSL Awesomeness – Introduction to GDSL in IntelliJ Idea
  2. GDSL Awesomeness – Understanding Context And Contributors in Detail
  3. GDSL Awesomeness – Defining dynamic property in a class
  4. GDSL Awesomeness – Defining dynamic method in a class
  5. GDSL Awesomeness – Adding Hinting for missingMethod implementation
  6. GDSL Awesomeness – Setting a global context
  7. GDSL Awesomeness – Delegating Closure Calls
  8. GDSL Awesomeness – Defining methods in a closure passed to method of particular name
  9. GDSL Awesomeness – Defining Properties in a closure passed to method of particular name
  10. GDSL Awesomeness – Contributing to classes which match a path/name pattern
  11. GDSL Awesomeness – contributing methods with current class return types or parameters
  12. GDSL Awesomeness – AutoComplete Script for grails-Mail-Plugin “sendMail” closure
  13. GDSL Awesomeness – Getting Code of a method or Field in GDSL Script
  14. GDSL Awesomeness – Advanced Example showing domain properties implementation

 

 

Hope It Helped.
Kushal Likhi

FOUND THIS USEFUL? SHARE IT

Leave a comment -