1 Example: Meta-Object Protocol 1.1 practice goals
Use the mop extension class.
1.2 Create a Groovy object and return all methods and property calls
Create a groovy class. When each property is accessed, it pretends to be called by the method and returns a fixed value.
Package Mop class Anymethodexecutor { The Getter method is ignored String value= "Lars"; Always returns 5, regardless of what property is passed in Object GetProperty (String property) { return 5; } void setProperty (String property,object o) { Ignore } def methodmissing (String Name,args) { def s = name.tolowercase (); if (!s.contains("Hello")) { return "This method is just fake"; } Else { return "Still a fake method but ' hello ' back to you."; } } } |
Use the groovy script below to test the method.
Package Mop class anymethodexecutortest { Static Main (args) { def test = new anymethodexecutor (); You can call any method like this below assert "This method is just fake" = = Test. Hall (); assert "This method is just fake" = = Test. Hallo (); assert "Still a fake method but ' hello ' back to you." ==test. HelloMethod (); assert "Still a fake method but ' hello ' back to you." ==test. Hellom (); The following basic property settings are ignored Test. Test = 5; Test. superdupercool = 100; All properties will return 5. assert test. Superdupercool = = 5; assert test.value==5; } } |
1.3 Exercise: Adding JSON output to the groovy class, a stupid method and a clever way
Create the Groovy class below.
package Mop.json import groovy.json.JsonBuilder   class Task { String summary; String description;     def methodmissing (String name,args) { If (name = = "ToJson") { Jsonbuilder B1 = new jsonbuilder ( this ) ; return b1.tostring (); } } |
Used to methodMissing represent toJson method calls. This implementation is a stupid way to hack into domain using the "frame" code.
The following script triggers the build of the JSON.
Package Mop.json class tasktest { Static Main (args) { def t = new Task (Summary: "mop", Description: "Learn all About Mop"); println (t.ToJson()); } } |
Output
{"Summary": "Mop", "description": "Learn all About Mop"} |
Smart way: Groovy allows you to create an MetaClass instance and automatically registers it in the specified class. This registration is based on the package name conversion.
// define an instance of Metaclass in a package, and groovy will register it in a class
Note: The package for this proxy class must be defined as above rule.
This example is: Groovy.runtime.metaclass. Package name, will be automatically registered to the task on this domain. Therefore, the task does not need to be inherited or implemented equal to the proxy class Association.
Package Groovy.runtime.metaclass.mop.json2 Import Groovy.json.JsonBuilder class Taskmetaclass extends delegatingmetaclass { Taskmetaclass (Metaclass Meta) { Super (meta); } @Override def InvokeMethod (Object object, String method, object[] args) { println (method); if (method = = "ToJson") { Jsonbuilder B1 = new Jsonbuilder (object); return b1.tostring (); } return Super. InvokeMethod (object, method, args); } } |
This will allow you to remove the Def methodmissing (String Name,args) {This method in domain.
Package Mop.json2 class Task { String Summary; String description; } |
Run this short test script again and verify that the work converted to JSON is valid.
Package Mop.json2 class tasktest { Static Main (args) { def t = new Task (Summary: "mop", Description: "Learn all About Mop"); println (t.ToJson()); } } |
Output
ToJson {"Summary": "Mop", "description": "Learn all About Mop"} |
22 Example: Meta-Object Protocol