Groovy calls deprecated getter method instead of public class field

I am using groovy to talk to a Java library. I am trying to get the value of a class field, and I am having trouble because of groovy’s implicit call to getters. I’m doing something like this:

println model.schemaComponent

and groovy is calling the getSchemaComponent method.

The problem I have is that the Java class has a public field named schemaComponent, and a getter method for that field (getSchemaComponent). Now, in the Java library, they have deprecated the getter method in favor of the public field, and the getter method always returns null.

Is there a way to somehow force groovy to get the value from the public field and not the getter method?

Thank you

Answer

Groovy has a direct field-access operator [email protected], so you can write your expression like this:

That said, any library that leaves an API call in place while trashing its semantics is one I would run away from fast.

Leave a Reply

Your email address will not be published. Required fields are marked *