Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-8286

Groovy does not respect package-private modifier of Java class

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Information Provided
    • 2.4.8
    • None
    • None
    • None

    Description

      Hi. I'm using Java (1.8) with Groovy (2.4.8) via GMavenPlus (although with the project I'm working on, we actually also programmatically compile Groovy code and add it to the class loader to mix them up). It seems however, in Groovy scripts/classes, package-private methods defined in a Java class can be called.

      For example, I have a Java class Person in package org.example.people, defined like so:

      package org.example.people;
      
      public class Person {
          private String name;
      
          void setName(String name) {
              this.name = name;
          }
      
          public String getName() {
              return this.name;
          }
      }
      

      And I have a Groovy class Main in package org.example:

      package org.example
      
      import groovy.transform.CompileStatic
      import org.example.people.Person
      
      class Main {
          static void main(String[] args) {
              Person person = new Person()
              person.setName('Bob') // Shouldn't be call-able since they belong to different packages.
              println person.getName() // Prints 'Bob' to the console.
          }
      }
      

      Is this standard? Using @CompileStatic, though, throws an IllegalAccessException.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              angela.guardian Angela Guardian
              Votes:
              1 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: