groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Basurita <basuraparabar...@gmail.com>
Subject get/setProperty for POJOs
Date Wed, 11 Oct 2017 17:11:42 GMT
Hi, I've been away from the Groovy world for some years.
As of recently, I've been trying to add some behavior to Groovy scripts embedded in a large
Java codebase.
A few things that I "half-remembered" striked me as odd.
In this mail I want to ask about why Groovy does not honor certain methods for POJOs

My questions:
  * Why does Groovy NOT honor get/setProperty for pojos?
  * Why does it honor propertyMissing?

I'm sure there must be good/historical reasons for this incongruency, but I haven't been able
to google a good answer.
And I don't think the answer will be "oh! we've never thought of that!" :-)
If it were up to me, I would honor both or none...

And I'm not looking for "solutions" that add those methods through a metaClass blah blah
I just would like a good rationale behind the way Groovy "naturally" handles properties for
POJOs

thanks, bye!


For reference, I created a POJO and added the following:

// === a regular Java class: MyPojo.java
public class MyPojo {
  private int myProp= 88;
  private int myOtherProp= 44;

  public int getMyProp() {
    return myProp;
  }

  public Object getProperty(String name) {
    return 555;  // whatever
  }

  public Object propertyMissing(String name) {
    System.out.println("propertyMissing read "+name);
    return "hello";
  }
  public void propertyMissing(String name, Object value) {
    System.out.println("propertyMissing write "+name+"   val: "+value);
  }
}

// === and now from a groovy script

def p= new MyPojo()
assert p.myProp == 88       // called getMyProp()
assert p.myOtherProp == 44  // read directly

assert p.xxx == "hello"     // propertMissing(String) honored!
p.yyy = 2222                // prints  "propertyMissing write yyy   val:
222"


Mime
View raw message