Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Groovy meta-object programming


May 14, 2021 Groovy


Table of contents


Meta-object programming or MOP can be used to dynamically call methods, and classes and methods can be created instantly.

So what does that mean? L et's consider a class called Student, which is an empty class with no member variables or methods. Suppose you have to call the following statement on this class.

Def myStudent = new Student() 
myStudent.Name = ”Joe”; 
myStudent.Display()

Now in meta-object programming, the above code works even if the class does not have the member variable Name or method Display().

How does this work? W ell, in order for this to work, one must implement the Groovy Interceptable interface hook to Groovy's execution process. The following are the available methods for this interface.

Public interface GroovyInterceptable { 
   Public object invokeMethod(String methodName, Object args) 
   Public object getproperty(String propertyName) 
   Public object setProperty(String propertyName, Object newValue) 
   Public MetaClass getMetaClass() 
   Public void setMetaClass(MetaClass metaClass) 
}

So in the interface description above, assuming you have to implement invokeMethod(), it will be called by each method, either existing or not existing.

The property is missing

So let's look at an example of how we implement meta-object programming for missing properties. The following keys should be noted in the following code.

  • Class Student does not define a member variable named Name or ID.

  • Class Student implements the GroovyInterceptable interface.

  • There is a parameter called dynamicProps that will be used to hold the value of the member variable created instantly.

  • Methods getproperty and setproperty have been implemented to get and set the value of the class's properties at runtime.

class Example {
   static void main(String[] args) {
      Student mst = new Student();
      mst.Name = "Joe";
      mst.ID = 1;
		
      println(mst.Name);
      println(mst.ID);
   }
}

class Student implements GroovyInterceptable { 
   protected dynamicProps=[:]
	
   void setProperty(String pName,val) {
      dynamicProps[pName] = val
   }
   
   def getProperty(String pName) {
      dynamicProps[pName]
   } 
} 

The output of the following code will be -

Joe 
1

The method is missing

So let's look at an example of how we implement meta-object programming for missing properties. The following keys should be noted in the following code -

  • Class students now implement the invokeMethod method, which is called regardless of whether the method exists.

class Example {
   static void main(String[] args) {
      Student mst = new Student();
      mst.Name = "Joe";
      mst.ID = 1;
		
      println(mst.Name);
      println(mst.ID);
      mst.AddMarks();
   } 
}
 
class Student implements GroovyInterceptable {
   protected dynamicProps = [:]  
    
   void setProperty(String pName, val) {
      dynamicProps[pName] = val
   } 
   
   def getProperty(String pName) {
      dynamicProps[pName]
   }
   
   def invokeMethod(String name, Object args) {
      return "called invokeMethod $name $args"
   }
}

The output of the following code is shown below. Note that even if method Display does not exist, there are no errors that are missing method exceptions.

Joe 
1 

Meta-class

This feature is relevant to the MetaClass implementation. I n the default implementation, you can access fields without calling their getters and setters. on.

class Example {
   static void main(String[] args) {
      Student mst = new Student();
      println mst.getName()
      mst.metaClass.setAttribute(mst, 'name', 'Mark')
      println mst.getName()
   } 
} 

class Student {
   private String name = "Joe";
	
   public String getName() {
      return this.name;
   } 
}

The output of the following code will be -

Joe 
Mark

Method is missing

Groovy supports the concept of methodMissing. ng. he following example shows how to use methodMissing.

class Example {
   static void main(String[] args) {
      Student mst = new Student();
      mst.Name = "Joe";
      mst.ID = 1;
		
      println(mst.Name);
      println(mst.ID);
      mst.AddMarks();
   } 
} 

class Student implements GroovyInterceptable {
   protected dynamicProps = [:]  
    
   void setProperty(String pName, val) {
      dynamicProps[pName] = val
   }
   
   def getProperty(String pName) {
      dynamicProps[pName]
   }
   
   def methodMissing(String name, def args) {         
      println "Missing method"
   }  
}

The output of the following code will be -

Joe 
1 
Missing method