Showing posts with label metaClass. Show all posts
Showing posts with label metaClass. 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)

Monday, September 30, 2013

Grails: how to get/set a meta-constraint on domain class property such as belongsTo or other variables

In the scaffolding templates there is a property domainClass from type org.codehaus.groovy.grails.commons.DefaultGrailsDomainClass. These object has the property constrainedProperties. To have access to any properties such in this example: 'readonly' you have to do this:

Your domain class:


class Contact {
   static belongsTo = [subscription: Subscription]

   static constraints = {
       subscription(nullable: false, attributes: [readonly: true])  
   }

   String description
}


The DefaultGrailsDomainClass has a constructor with a attribute from type Class maybe you can do this:


def domainClass = new DefaultGrailsDomainClass(Contact.class)
def ro = domainClass.constrainedProperties.subscription.attributes.readonly



def domainClass = new DefaultGrailsDomainClass(it.metaClass.getTheClass())
def constrainedProperties = domainClass.constrainedProperties;
if(constrainedProperties.containsKey("subscription")) {
    ConstrainedProperty v = (ConstrainedProperty) constrainedProperties.get("subscription");
    /**
     * Now check
     */
    if(v.isNullable()) {
        println "Nullable";
    }
    if(v.isBlank()) {
        println "Blank";
    }
    /**
     * There are may other constraints
     * http://grails.org/doc/latest/api/org/codehaus/groovy/grails/validation/ConstrainedProperty.html 
     * Java/Grails Class ConstrainedProperty
     */ 
 }