1. Class discovering the class |
Def S = "hello" Printlns. Class Printlns. getclass () String. constructors. Each {println it} Println "" String. Interfaces. Each {println it} Println "" |
2. Domain discovering the fields of A Class |
Def d = new date () Printlnd. Properties Println "" D. properties. Each {println it} Println "" D. Class. declaredfields. Each {println it} Println "" // Important object metaclass Printlndate. metaclass |
3. Check the existence of the domain checking for the existence of a field |
Class person { String firstname String lastname String tostring () {"$ {firstname }$ {lastname }"} Metapropertyhasproperty (string property ){ Return this. metaclass. hasproperty (this, property) } } Def person = new person () Def request = [firstname: "bill", lastname: "Gates"] Request. Each {name, value-> If (person. metaclass. hasproperty (person, name )){ // If (person. hasproperty (name )){ Person. setproperty (name, value) } } Println person |
4. Method discovering the methods of a class |
Def d = new date () D. Class. Methods. Each {println it} D. Class. Methods. Name Println "" // Evaluate run in a new shell D. Class. Methods. Each {method-> If (method. Name. startswith ("get ")){ Print "$ {method. name }:" Evaluate ("DD = new date (); println dd. $ {method. name }()") } } Println "" // Def binding = new binding () Binding. setvariable ("D", d) Defgs = new groovyshell (binding) D. Class. Methods. Each {method-> If (method. Name. startswith ("get ")){ Print "$ {method. name }:" GS. Evaluate ("println D. $ {method. name }()") } } Println "" // Gstring (most concise way to dynamically call a method on a class) D. Class. Methods. Each {method-> If (method. Name. startswith ("get ")){ Print "$ {method. name }:" Println D. "$ {method. name }"() } } Println "" |
5. Check the existence of methods for checking for the existence of a Method |
Class greeting { Defsayhello (){ Println "hello, stranger" } Defsayhello (string name ){ Println "Hello, $ {name }" } } Def G = new greeting () If (G. metaclass. respondsto (G, "sayhello", null )){ G. sayhello () } If (G. metaclass. respondsto (G, "sayhello", string )){ G. sayhello ("Jane ") } Println "Number of sayhello () Methods:" + G. metaclass. respondsto (G, "sayhello"). Size () G. metaclass. respondsto (G, "sayhello"). Each {M-> Println "$ {M. Name }$ {M. nativeparametertypes }" } |
6. Domain pointer creating a field pointer |
Def P = new person () P. Name = "Jane" Println P. Name // Field pointer, which can be used to operate privtate Printlnp. @ name |
7. Method pointer creating a method pointer |
Def list = [] Def insert = List. & add Insert "Java" Insert "groovy" Println list |
8. Calling methods that don't exist (invokemethod) |
Class person { String name Map relationships = [:] Object invokemethod (string what, object who ){ If (relationships. containskey (what )){ Who. Each {thisperson-> Relationships. Get (what). Add (thisperson) } } Else { Relationships. Put (what, who as List) } } } Defscott = new person (name: "Scott ") Scott. Married "Kim" Scott. Knows "Neal" Scott. workedwith "Brian" Scott. Knows "Venkat" Scott. workedwith "Jared" Scott. Knows "Ted", "Ben", "David" Printlnscott. Relationships |
9. Creating an expando |
Def E = new expando () E. Latitude = 70 E. longpolling = 30 Println E E. arewelost = {-> Return (E. longpolling! = 30) | (E. Latitude! = 70) } Printlne. arewelost () |
10. Adding Methods to a class dynamically (categories) |
Categories allow you to add new functionality to any class at runtime. Use (randomhelper ){ 10. Times {println 10. Rand ()} } Class randomhelper { Static int rand (integer self ){ Def r = new random () Return R. nextint (self. intvalue ()) } } Use (internetutils ){ Println "http: // localhost/". Get () Println "http://search.yahoo.com/search". Get ("P = groovy ") Defparams = [:] Params. n = "10" Params. VL = "lang_eng" Params. P = "groovy" Println "http://search.yahoo.com/search". Get (Params) } Class internetutils { Static string get (string self ){ Return self. tourl (). Text } Static string get (string self, string querystring ){ Defurl = self + "? "+ Querystring Return URL. Get () } Static string get (string self, map Params ){ Def list = [] Params. Each {k, V-> List <"$ k =" + urlencoder. encode (V) } Defurl = self + "? "+ List. Join ("&") Return URL. Get () } } |
11. Adding Methods to a class dynamically (expandometaclass) |
Integer. metaclass. Rand = {-> Def r = new random () Return R. nextint (delegate. intvalue ()) } 5. Times {println 10. Rand ()} String. metaclass. Get = {-> Return delegate. tourl (). Text } Println "http: // localhost/". Get () A category is perfect ifyou want to limit the scope of your new methods to a well-de defined Ned blockof code. an expandometaclass is better if you want to have your newmethods applied to all instances when SS the entire running application. If you want your new functionality to be easily shared by both Java andgroovy code, categories leave you with a plain old Java class with staticmethods. expandometaclasses are more closely tied to groovy, but theyare SIGNI than cantly more than T as well. |