Trying to make a method in groovy protected
:
package com.foo
class Foo {
protected def getSomething(){
}
}
This doesn't work since groovy by default makes pretty much everything visible, so I tried using @PackageScope
package com.foo
import groovy.transform.PackageScope
@PacakgeScope
class Foo {
def getSomething(){
}
}
This sort of works, but only if the caller uses @CompileStatic
...
package com.bar
class Bar {
@CompileStatic
static void main(args){
def f = new Foo()
println f.getSomething()
}
The above throws IllegalAccessError
, that's nice, but without @CompileStatic
, no error is generated; not so nice. I can't force users to compile statically, so is there any alternative to enforce protected
methods?
From Groovy Documentation
Protected in Groovy has the same meaning as protected in Java, i.e. you can have friends in the same package and derived classes can also see protected members.
Ok, if protected
has the same meaning in Groovy but isn't enforced as such, doesn't that erode its meaning? Maybe I'm missing something,