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 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.
+8
A:
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]
ataylor
2010-07-22 18:24:45
That's pretty cool. I had just assumed that public members had their accessors generated automatically but not private or protected.
Blacktiger
2010-07-23 14:01:08
Yeah, in fact it seems like "public" SHOULD make accessors but I'll have to research more.
Vinny
2010-08-02 16:29:54