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 2022/08/19 17:10:33 UTC

[groovy] branch GROOVY-8283 updated (c2b19b0f9e -> 0d07a0c8f7)

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

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


 discard c2b19b0f9e GROOVY-8283: field hides getter of super class (not interface)
     new 0d07a0c8f7 GROOVY-8283: field hides getter of super class (not interface)

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (c2b19b0f9e)
            \
             N -- N -- N   refs/heads/GROOVY-8283 (0d07a0c8f7)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/main/java/groovy/lang/MetaClassImpl.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)


[groovy] 01/01: GROOVY-8283: field hides getter of super class (not interface)

Posted by em...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 0d07a0c8f7f5e929bc4301fea75dbd7e3142aaa2
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri Aug 19 11:54:51 2022 -0500

    GROOVY-8283: field hides getter of super class (not interface)
---
 src/main/java/groovy/lang/MetaClassImpl.java | 27 +++++++++++---
 src/test/groovy/bugs/Groovy8283.groovy       | 56 ++++++++++++++++++++++++++++
 2 files changed, 77 insertions(+), 6 deletions(-)

diff --git a/src/main/java/groovy/lang/MetaClassImpl.java b/src/main/java/groovy/lang/MetaClassImpl.java
index c32102989d..21fdacff01 100644
--- a/src/main/java/groovy/lang/MetaClassImpl.java
+++ b/src/main/java/groovy/lang/MetaClassImpl.java
@@ -1981,12 +1981,12 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
         //----------------------------------------------------------------------
         Tuple2<MetaMethod, MetaProperty> methodAndProperty = createMetaMethodAndMetaProperty(sender, sender, name, useSuper, isStatic);
         MetaMethod method = methodAndProperty.getV1();
+        MetaProperty mp = methodAndProperty.getV2();
 
-        if (method == null || isSpecialProperty(name)) {
+        if (method == null || isSpecialProperty(name) || isVisibleProperty(mp, method, sender)) {
             //------------------------------------------------------------------
             // public field
             //------------------------------------------------------------------
-            MetaProperty mp = methodAndProperty.getV2();
             if (mp != null && Modifier.isPublic(mp.getModifiers())) {
                 try {
                     return mp.getProperty(object);
@@ -2097,19 +2097,19 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
         //----------------------------------------------------------------------
         Tuple2<MetaMethod, MetaProperty> methodAndProperty = createMetaMethodAndMetaProperty(sender, theClass, name, useSuper, isStatic);
         MetaMethod method = methodAndProperty.getV1();
+        MetaProperty mp = methodAndProperty.getV2();
 
-        if (method == null || isSpecialProperty(name)) {
+        if (method == null || isSpecialProperty(name) || isVisibleProperty(mp, method, sender)) {
             //------------------------------------------------------------------
             // public field
             //------------------------------------------------------------------
-            MetaProperty mp = methodAndProperty.getV2();
             if (mp != null && Modifier.isPublic(mp.getModifiers())) {
                 return mp;
             }
 
-            //----------------------------------------------------------------------
+            //------------------------------------------------------------------
             // java.util.Map get method
-            //----------------------------------------------------------------------
+            //------------------------------------------------------------------
             if (isMap && !isStatic) {
                 return new MetaProperty(name, Object.class) {
                     @Override
@@ -2243,6 +2243,21 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
         return "class".equals(name) || (isMap && ("empty".equals(name) || "metaClass".equals(name)));
     }
 
+    private boolean isVisibleProperty(final MetaProperty field, final MetaMethod method, final Class<?> sender) {
+        if (!(field instanceof CachedField)) return false;
+        if (Modifier.isPrivate(field.getModifiers())) return false;
+        Class<?> owner = ((CachedField) field).getDeclaringClass();
+        // ensure access originates within the type hierarchy of the field owner
+        if (owner.equals(sender) || !owner.isAssignableFrom(sender)) return false;
+
+        if (!Modifier.isPublic(field.getModifiers())
+            && !Modifier.isProtected(field.getModifiers())
+            && !inSamePackage(owner, sender)) return false;
+
+        // GROOVY-8283: non-private field that hides class access method
+        return !owner.isAssignableFrom(method.getDeclaringClass().getTheClass()) && !method.getDeclaringClass().isInterface();
+    }
+
     private Tuple2<MetaMethod, MetaProperty> createMetaMethodAndMetaProperty(final Class senderForMP, final Class senderForCMG, final String name, final boolean useSuper, final boolean isStatic) {
         MetaMethod method = null;
         MetaProperty mp = getMetaProperty(senderForMP, name, useSuper, isStatic);
diff --git a/src/test/groovy/bugs/Groovy8283.groovy b/src/test/groovy/bugs/Groovy8283.groovy
new file mode 100644
index 0000000000..3446c9c42b
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy8283.groovy
@@ -0,0 +1,56 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package groovy.bugs
+
+import org.junit.Test
+
+import static groovy.test.GroovyAssert.assertScript
+
+final class Groovy8283 {
+    @Test
+    void testFieldPropertyShadowing() {
+        assertScript '''
+            class A {}
+            class B {}
+            class C {
+                protected A foo = new A()
+                A getFoo() { return foo }
+            }
+            class D extends C {
+                protected B foo = new B() // hides A#foo; should hide A#getFoo() in subclasses
+            }
+            class E extends D {
+                void test() {
+                    assert foo.class == B
+                    assert this.foo.class == B
+                    assert this.@foo.class == B
+                    assert this.getFoo().getClass() == A
+
+                    def that = new E()
+                    assert that.foo.class == B
+                    assert that.@foo.class == B
+                    assert that.getFoo().getClass() == A
+                }
+            }
+
+            new E().test()
+            assert new E().foo.class == A // not the field from this perspective
+        '''
+    }
+}