Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I disable generation of Groovy accessors?

Tags:

groovy

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.

like image 236
dromodel Avatar asked Jul 22 '10 16:07

dromodel


1 Answers

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]
like image 129
ataylor Avatar answered Sep 28 '22 06:09

ataylor