Close

Groovy - Using @PackageScope for package-private visibility

[Updated: Feb 23, 2019, Created: Feb 23, 2019]

In Groovy, to declare a method or class with package-private visibility, we need to use @PackageScope annotation.

import groovy.transform.PackageScope
import java.lang.reflect.Modifier

@PackageScope
class Task {
    @PackageScope
    void run() {
    }
}

def modifiers = Task.class.modifiers
printf "'%s'%n", Modifier.toString(modifiers)

modifiers = Task.class.getDeclaredMethod("run").modifiers
printf "'%s'%n", Modifier.toString(modifiers)

Output

''
''

Per java standard empty modifier means no explicit modifier i.e. package-private


Example Project

Dependencies and Technologies Used:

  • Groovy 2.5.6
  • JDK 11
@PackageScope Example Select All Download
  • groovy-package-scope
    • src

See Also