views:

20

answers:

2

I'm writing a Groovy script (as part of a Grails plugin) and I want to get a list of properties for a GrailsDomainClass that a user of my plugin might define. I can do this using domainClass.properties (where domainClass is a GrailsDomainClass).

However, suppose a user has the grails domain class:

class Example {
  String name

    static constraints = {
    }

  def getSomeNonExistingProperty(){
    return "Not-a-real-property"
  }
}

In this case, domainClass.properties returns a list with both name and someNoneExistingProperty

I understand that this is because of Grails is generating a read-only property on-the-fly for use where someone has a getBlah() method. That's great, but in my script I want to perform some actions with the "real" properties only (or at least non read-only properties).

That is, I would like some way of distinguishing or identifying someNonExistingProperty as a read-only property, or, alternatively, as a property generated by Grails and not entered explicitly as a field in the domainClass by the user of my plugin.

I've looked at the GrailsDomainClassProperty Class and it has a range of methods providing information about the property. However, none of them appear to tell me whether a property is read-only or not, or to allow me to distinguish between a field defined in the domainClass and a field created on-the-fly by Grails as a result of a "getSomeNonExistingProperty()" method.

Am I missing something obvious here? Is there a way of getting a list of just the explicitly user-defined fields (eg name, in the above example)?

A: 

I believe transient properties are what you are trying to exclude

Aaron Saunders
Thanks - but if that's the case I'm still missing something, I think. If I have p, corresponding to the property generated from getSomeNonExistingProperty() method on-the-fly, then p.name, for example, returns the String "someNonExistingProperty", p.isPersistent() returns "true" and p.TRANSIENT returns the String "transients". These are the same values returned by the other "real" property (name), except that p.name = "name". What am I missing?
Glennn
A: 

I've run into this problem a few times, and instead of trying to work around it I typically just end up renaming my getX() method. It's probably the easiest option.

Edit: Alternatively, I wonder if you could use reflection to see which methods are defined on the class, and while iterating over your properties see if the property has an explicit getter defined, and omit it. I'm not very familiar with reflection when it comes to Groovy and Grails, especially with the dynamic methods, but it's a possible route of investigation.

Rob Hruska
Renaming getX() methods would work, except that this is a plugin, so it isn't (in general) my code the script is acting on - I don't want to insist that any person using the plugin would need to avoid getX() methods. The idea of using reflection is worth following up, thanks. I'll see if that helps - however I'm dubious as there will then be the reverse problem of not excluding "real" properties that, nevertheless, do have a getX() method associated with them.
Glennn