Java Mailing List Archive

http://www.gg3721.com/

Home » user.groovy »

[groovy-user] methodMissing Trouble

James Hughes

2008-08-18

Replies: Find Java Web Hosting

Author LoginPost Reply
Hello,

I am trying to write an example of using methodMissing but am having trouble.

What I want to do is on a Person class I want to be able to call printProperty<property_name> so a call to new Person(title:"Test").printPropertyTitle() would result in Test being printed to the console. My current code ALWAYS calls method missing and I can't see what is wrong. Can anyone help?

class Person {
  String name
  String address
 
  def methodMissing(String f, args){
    println "METHOD MISSING"
    if(f.startsWith("printProperty")){
       def target = f-"printProperty"
       target = target[0].toLowerCase() + target[1..-1]
       if(Person.metaClass.hasProperty(this,target)){
          Person.metaClass."$f" = {
            println delegate."$target"
          }  
          println this."$target"      
       }else{
          println "NO SUCH PROPERTY"
       }    
    }
  }
}
def x = new Person(name:"akjsfsk")
x.printPropertyName()
x.printPropertyName()

James Hughes | Senior Software Engineer | Kainos | M: +353 (0)877 931 634 | j.hughes@(protected)

P Please consider the environment and do not print this mail unless necessary


--------------------------------------------------------------------
This e-mail is intended solely for the addressee and is strictly confidential; if you are not the addressee please destroy the message and all copies. Any opinion or information contained in this email or its attachments that does not relate to the business of Kainos
is personal to the sender and is not given by or endorsed by Kainos. Kainos is the trading name of Kainos Software Limited, registered in Northern Ireland under company number: NI19370, having its registered offices at: Kainos House, 4-6 Upper Crescent, Belfast, BT7 1NT,
Northern Ireland. Registered in the UK for VAT under number: 454598802 and registered in Ireland for VAT under number: 9950340E. This email has been scanned for all known viruses by MessageLabs but is not guaranteed to be virus free; further terms and conditions may be
found on our website - www.kainos.com


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email


©2008 gg3721.com - Jax Systems, LLC, U.S.A.