This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_4_0_X by this push:
new 24242d4478 GROOVY-11358: STC: cannot access private field of abstract
class outside
24242d4478 is described below
commit 24242d4478db307b4f795aa4ef420847b46b316d
Author: Eric Milles <[email protected]>
AuthorDate: Wed Apr 10 09:05:54 2024 -0500
GROOVY-11358: STC: cannot access private field of abstract class outside
---
.../transform/stc/StaticTypeCheckingVisitor.java | 2 +-
.../stc/FieldsAndPropertiesSTCTest.groovy | 27 ++++++++++++++++++++++
2 files changed, 28 insertions(+), 1 deletion(-)
diff --git
a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
index bc461f4cb4..20d3b885a2 100644
---
a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++
b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -1825,7 +1825,7 @@ public class StaticTypeCheckingVisitor extends
ClassCodeVisitorSupport {
if (visitor != null) visitor.visitField(field);
checkOrMarkPrivateAccess(expressionToStoreOn, field, lhsOfAssignment);
boolean superField =
isSuperExpression(expressionToStoreOn.getObjectExpression());
- boolean accessible = ( !superField &&
receiver.equals(field.getDeclaringClass()) ) // GROOVY-7300
+ boolean accessible = (!superField &&
receiver.equals(field.getDeclaringClass()) &&
!field.getDeclaringClass().isAbstract()) // GROOVY-7300, GROOVY-11358
||
hasAccessToMember(typeCheckingContext.getEnclosingClassNode(),
field.getDeclaringClass(), field.getModifiers());
if (!accessible) {
diff --git a/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy
b/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy
index 64f72f40dd..fd1aafc48a 100644
--- a/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy
+++ b/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy
@@ -969,6 +969,33 @@ class FieldsAndPropertiesSTCTest extends
StaticTypeCheckingTestCase {
'''
}
+ // GROOVY-11358
+ void testPrivateFieldAccessOfAbstract() {
+ shouldFailWithMessages '''
+ abstract class A {
+ private int f
+ }
+ void test(A a) {
+ int i = a.@f // MissingFieldException
+ int j = a.f // MissingPropertyException
+ }
+ test(new A() {})
+ ''',
+ 'Cannot access field: f of class: A', 'No such property: f for class:
A'
+
+ shouldFailWithMessages '''
+ abstract class A {
+ private int f
+ }
+ void test(A a) {
+ a.@f = 1 // MissingFieldException
+ a.f = 2 // MissingPropertyException
+ }
+ test(new A() {})
+ ''',
+ 'Cannot access field: f of class: A', 'No such property: f for class:
A'
+ }
+
// GROOVY-5737
void testGeneratedFieldAccessInClosure() {
assertScript '''