tags:

views:

63

answers:

1

Hi,

In the Groovy console, the following code executes without error:

class F {
  private def getFoo() {"foo"}
  private def barValue = "bar"
}

def f = new F()
assert f.barValue == "bar"
assert f.properties.containsKey("foo")

This implies that:

  • One can access private members of classes outside the class
  • A class' properties are derived from the private (and public) getter/setter methods

It seems to me that both of these are extremely severe language bugs. Although I really like Groovy, I find it incredible that a language that has been around for a reasonably long time, hasn't implemented something as fundamental as the concept of privacy correctly.

Are these actually bugs or am I missing something?

Thanks, Don

+1  A: 

It is intentional, sadly. This changed with 1.5. Unfortunately, in order to support their mechanism for closures, and their meta-object protocol, they have to have access to private members.

This article explains a little more thoroughly: http://www.benjaminbooth.com/tableorbooth/2008/07/groovy-15-no-private-for-you.html

Benjamin Cox