Groovy doesn't recognize the private access modifier. See this question. There is nothing private in Groovy.
As to how Groovy does it, Groovy is generating the classes for things you write in Groovy. It can write whatever access modifiers it wants in the class files. But you can also use Groovy to inspect the private parts of Java code. Groovy is calling setAccessible
on the constructor as AlexR's answer demonstrates.
The security manager can prevent this kind of thing. In groovysh, the Security manager is org.codehaus.groovy.tools.shell.util.NoExitSecurityManager
, which only checks permissions if its parent is nonnull. In groovysh its parent is null.
Groovy relies on having the 'suppressAccessChecks' permission set in order to function. The groovy.policy file for the core-groovy project has this notice:
/* Notes on the contents of this policy file:
*
* The following methods in groovy have privileged operations wrapping
* setAccessible. If these wrappers are not provided, most codebases below
* must have the following grant:
* permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
* MetaMethod.createMetaMethod
* MetaMethod.invoke(Object Object[])
* ReflectionMetaMethod.invoke(Object Object[])
* DefaultGoovyMethods.dump(Object)
*/
There is a page on Groovy's website of "things you can do but better leave undone", it lists the privacy issue here:
- Disregarding other objects' privacy
When accessing methods, fields, or properties of other classes, make
sure that you do not interfere with private or protected members.
Currently Groovy doesn't distinguish properly between public, private,
and protected members, so watch out yourself.
As Tim Yates says on the linked question, it is not clear if that is a defect or a feature. Given that it could tend to break existing code, it seems unlikely to me that it will get "fixed" any time soon, if ever.