[ https://issues.apache.org/jira/browse/GROOVY-8286?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17835500#comment-17835500 ]
Eric Milles edited comment on GROOVY-8286 at 10/5/24 7:16 PM: -------------------------------------------------------------- Groovy can access any field or method on an instance of the class that declares it. For example you have {{Person}} here as the declaring class of {{setName}} and the type of {{person}} variable. STC does emit an error for the method case but not for the field case since it has been in common use for so long. This becomes more of a problem when your method accepts a Person. Sending a subclass will fail a field access -but not- _and_ a method access. For example: {code:java} package foo; class Bar { String field = "boo"; String method() { return field; } } {code} {code:groovy} void test(foo.Bar bar) { print bar.method() // MissingMethodAccess print bar.@field // MissingFieldException print bar.field // MissingPropertyException } class Baz extends foo.Bar { } test(new Baz()) {code} was (Author: emilles): Groovy can access any field or method on an instance of the class that declares it. For example you have {{Person}} here as the declaring class of {{setName}} and the type of {{person}} variable. STC does emit an error for the method case but not for the field case since it has been in common use for so long. This becomes more of a problem when your method accepts a Person. Sending a subclass will fail a field access but not a method access -- exactly the reverse of the STC warnings. For example: {code:java} package foo; class Bar { String field = "boo"; String method() { return field; } } {code} {code:groovy} void test(foo.Bar bar) { print bar.method() // prints "boo" print bar.@field // MissingFieldException print bar.field // MissingPropertyException } class Baz extends foo.Bar { } test(new Baz()) {code} > Groovy does not respect package-private modifier of Java class > -------------------------------------------------------------- > > Key: GROOVY-8286 > URL: https://issues.apache.org/jira/browse/GROOVY-8286 > Project: Groovy > Issue Type: Bug > Affects Versions: 2.4.8 > Reporter: Angela Guardian > Priority: Major > > 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: > {code:java} > package org.example.people; > public class Person { > private String name; > void setName(String name) { > this.name = name; > } > public String getName() { > return this.name; > } > } > {code} > And I have a Groovy class {{Main}} in package {{org.example}}: > {code:java} > 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. > } > } > {code} > Is this standard? Using {{\@CompileStatic}}, though, throws an > {{IllegalAccessException}}. > -- This message was sent by Atlassian Jira (v8.20.10#820010)