Showing posts with label invokeMethod. Show all posts
Showing posts with label invokeMethod. Show all posts

Saturday, August 5, 2017

Grails Groovy: Execute Code transparent before and after any method is invoked | Override Method Implementation | Override Method Body

Grails Groovy: Execute Code transparent before and after any method is invoked | Override Method Implementation | Override Method Body.
Note:-
  • Make sure the returned value from the method call is returned from the closure as well
  • Might be a expensive of a class has number of methods with heavy implementation
  • If selective execution is required, then check for the method name and then intercept the call


class Dummy {
    def method1() { 
        System.out.println "In method 1"
    }

    def method2(String str) { 
        System.out.println "In method 2"
    }
    
    static def method3(int a, int b) {
        System.out.println "In static method 3" 
    }    
}

Dummy.metaClass.invokeMethod = {String name, List args ->
   def result = null

   System.out.println("Do something before $name is called with args $args")

   try {
       result = delegate.metaClass.getMetaMethod(name, args).invoke(delegate, args)
   }
   catch(Exception e) {
        System.out.println "Handling exception for method $name"
   }

   System.out.println("Do something after $name was called with args $args")

   return result
}

Dummy.metaClass.'static'.invokeMethod = {String name, List args ->
   def result = null

   System.out.println("Do something before static method $name is called with args $args")

   try {
       result = delegate.metaClass.getMetaMethod(name, args).invoke(delegate, args)
   }
   catch(Exception e) {
        System.out.println "Handling exception for method $name"
   }

   System.out.println("Do something after static method $name was called with args $args")

   return result
}

def dummy = new Dummy()
dummy.method1()
dummy.method2('Test')
Dummy.method3(1, 2)

Thursday, October 3, 2013

Grails Groovy dynamically invokeMethod with empty/single/multi parameters

You need to first get bean object of your service class.
Create a service like below and get your required service by calling:


serviceFinderService.getServiceByName("serviceName");

 If you have a service named "StudentService.groovy" then replace "serviceName" with "Student" or "student".


package com.pritom.services

import org.codehaus.groovy.grails.web.context.ServletContextHolder
import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes

class ServiceFinderService {
    private static HashMap beanList = new HashMap();
    private def grailsApplication;
    private static def ctx;

    def serviceMethod() {

    }

    public def getServiceByName(String bean) throws Exception {
        if(!ctx) {
            ctx = ServletContextHolder.servletContext.getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT)
        }
        bean += "Service";
        bean = bean[0].toLowerCase() + bean.substring(1);
        if(beanList.containsKey(bean)) {
            return beanList.get(bean);
        }
        beanList.put(bean, ctx.getBean(bean));
        if(beanList.containsKey(bean)) {
            return beanList.get(bean);
        }
        throw new Exception("Invalid service");
    }
}


Now check and invoke the method if a specific method existing by such way:


def studentService = ServiceFinderService.getServiceByName("Student");

if(studentService.metaClass.respondsTo(studentService, "methodName", [Integer, String, Boolean] as Object[]) {
    def result = studentService.invokeMethod("methodName", [1, "Pritom K Mondal", true] as Object[]);
}

/* If your method is static then call it by: */
if(studentService.metaClass.respondsTo(studentService, "methodName", [Integer, String, Boolean] as Object[]) {
    def result = studentService.metaClass.getTheClass().invokeMethod("methodName", [1, "Pritom K Mondal", true] as Object[]);
}