mercredi 4 mai 2016

How do I get a java.reflect.Field value for a property inherited from a Groovy trait?

I'm using Groovy 2.4.3 inside of a Grails web application. I have the following Trait/Class hierarchy declared in it:

trait GuidData {
    String Guid
}

class Enrollment implements GuidData {
    String email
}

When I execute the following code I get the expected results:

Enrollment enrollment = new Enrollment(email: "joe@tester.com", guid: "XXXX")
assert enrollment.properties == ['email':'joe@tester.com', 'guid':'XXXX']
assert enrollment.getProperty("guid") == "XXXX")
Enrollment.getDeclaredField("email") == private java.lang.String Enrollment.email

But when I execute the method:

Enrollment.getDeclaredField("email")

I get a NoSuchFieldException. If I execute a "getDeclaredMethod()" for the "getEmail" method, I correctly get a java.reflect.Method instance back.

So ... it appears that Groovy properties defined in a trait show as Groovy properties and can be referenced like a property defined within the parent class, but they do NOT reflect as a field with getter/setters in the standard Groovy property pattern. In other words, if the property shows up in the instance call to the getProperties(), I would expect the reflection call to get the Field definition to work as well.





Aucun commentaire:

Enregistrer un commentaire