dromodel
dromodel

Reputation: 10193

How do I disable generation of Groovy accessors?

Groovy Beans are great but I'm just curious if it's possible to declare a class member private and not generate accessors for it easily? The http://groovy.codehaus.org/Groovy+Beans>Groovy Beans page doesn't cover this topic. The only thing that I can think of would be to define the accessors and make them private.

Upvotes: 6

Views: 422

Answers (1)

ataylor
ataylor

Reputation: 66059

Groovy won't add accessors if the member is declared with an access modifier: private, protected or public. If you don't want accessors, just add whichever modifier is appropriate. Here's an example that illustrates this:

class Test1 { private int blat }
println Test1.metaClass.getMethods()*.name.findAll { it.endsWith("Blat") }
class Test2 { protected int blat }
println Test2.metaClass.getMethods()*.name.findAll { it.endsWith("Blat") }
class Test3 { public int blat }
println Test3.metaClass.getMethods()*.name.findAll { it.endsWith("Blat") }
class Test4 { int blat }
println Test4.metaClass.getMethods()*.name.findAll { it.endsWith("Blat") }

Prints:

[]
[]
[]
[getBlat, setBlat]

Upvotes: 10

Related Questions