Groovy - Using @PackageScope for package-private visibility

[Last Updated: Aug 11, 2020]

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

class Task {
    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)



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

Example Project

Dependencies and Technologies Used:

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

See Also