You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2020/10/23 19:02:42 UTC

[groovy] 01/01: GROOVY-9791: SC: not dynamic property for protected field from diff pack

This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch GROOVY-9791
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit b0f80d0a256b5ee96d4acd829dae6a7b717fa734
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri Oct 23 14:00:23 2020 -0500

    GROOVY-9791: SC: not dynamic property for protected field from diff pack
---
 .../classgen/asm/sc/StaticTypesCallSiteWriter.java | 24 ++++++++--------------
 .../sc/FieldsAndPropertiesStaticCompileTest.groovy |  9 ++++----
 2 files changed, 13 insertions(+), 20 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java b/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java
index 9a99948..b0a3a6b 100644
--- a/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java
+++ b/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java
@@ -597,32 +597,26 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter {
     }
 
     /**
-     * Direct access is allowed from the declaring class of the field and sometimes from inner and peer types.
-     *
      * @return {@code true} if GETFIELD or GETSTATIC is safe for given field and receiver
      */
     private static boolean isDirectAccessAllowed(final FieldNode field, final ClassNode receiver) {
-        // first, direct access from anywhere for public fields
+        // a public field is accessible from anywhere
         if (field.isPublic()) return true;
 
-        ClassNode declaringType = field.getDeclaringClass().redirect(), receiverType = receiver.redirect();
+        ClassNode declaringType = field.getDeclaringClass().redirect();
 
-        // next, direct access from within the declaring class
-        if (receiverType.equals(declaringType)) return true;
+        // any field is accessible from the declaring class
+        if (receiver.equals(declaringType)) return true;
 
+        // a private field isn't accessible beyond the declaring class
         if (field.isPrivate()) return false;
 
-        // next, direct access from within the declaring package
+        // a protected field is accessible from any subclass of the declaring class
+        if (field.isProtected() && receiver.isDerivedFrom(declaringType)) return true;
+
+        // a protected or package-private field is accessible from the declaring package
         if (Objects.equals(receiver.getPackageName(), declaringType.getPackageName())) return true;
 
-        // last, inner class access to outer class fields
-        receiverType = receiverType.getOuterClass();
-        while (receiverType != null) {
-            if (receiverType.equals(declaringType)) {
-                return true;
-            }
-            receiverType = receiverType.getOuterClass();
-        }
         return false;
     }
 
diff --git a/src/test/org/codehaus/groovy/classgen/asm/sc/FieldsAndPropertiesStaticCompileTest.groovy b/src/test/org/codehaus/groovy/classgen/asm/sc/FieldsAndPropertiesStaticCompileTest.groovy
index 396a958..2fffda8 100644
--- a/src/test/org/codehaus/groovy/classgen/asm/sc/FieldsAndPropertiesStaticCompileTest.groovy
+++ b/src/test/org/codehaus/groovy/classgen/asm/sc/FieldsAndPropertiesStaticCompileTest.groovy
@@ -18,7 +18,6 @@
  */
 package org.codehaus.groovy.classgen.asm.sc
 
-import groovy.test.NotYetImplemented
 import groovy.transform.stc.FieldsAndPropertiesSTCTest
 
 final class FieldsAndPropertiesStaticCompileTest extends FieldsAndPropertiesSTCTest implements StaticCompilationTestSupport {
@@ -263,7 +262,7 @@ final class FieldsAndPropertiesStaticCompileTest extends FieldsAndPropertiesSTCT
         }
     }
 
-    @NotYetImplemented
+    // GROOVY-9791
     void testReadFieldFromSuperClass2() {
         assertScript '''
             package p
@@ -281,11 +280,11 @@ final class FieldsAndPropertiesStaticCompileTest extends FieldsAndPropertiesSTCT
             assert new B().m() == 0
         '''
         def b = astTrees['B'][1]
-        assert  b.contains('GETFIELD A.x')
+        assert  b.contains('GETFIELD p/A.x')
         assert !b.contains('INVOKEINTERFACE groovy/lang/GroovyObject.getProperty')
     }
 
-    @NotYetImplemented
+    // GROOVY-9791
     void testReadFieldFromSuperClass3() {
         assertScript '''
             package p
@@ -303,7 +302,7 @@ final class FieldsAndPropertiesStaticCompileTest extends FieldsAndPropertiesSTCT
             assert B.m() == 0
         '''
         def b = astTrees['B'][1]
-        assert  b.contains('GETFIELD A.x')
+        assert  b.contains('GETSTATIC p/A.x')
         assert !b.contains('INVOKESTATIC org/codehaus/groovy/runtime/ScriptBytecodeAdapter.getGroovyObjectProperty')
     }