Skip to content

Commit

Permalink
GROOVY-8283: field hides getter of super class (not interface)
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Aug 19, 2022
1 parent 74acee4 commit 0d07a0c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/main/java/groovy/lang/MetaClassImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1981,12 +1981,12 @@ public Object getProperty(final Class sender, final Object object, final String
//----------------------------------------------------------------------
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);
Expand Down Expand Up @@ -2097,19 +2097,19 @@ public void setProperty(Object object, Object newValue) {
//----------------------------------------------------------------------
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
Expand Down Expand Up @@ -2243,6 +2243,21 @@ private boolean isSpecialProperty(final String name) {
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);
Expand Down
56 changes: 56 additions & 0 deletions src/test/groovy/bugs/Groovy8283.groovy
Original file line number Diff line number Diff line change
@@ -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
'''
}
}

0 comments on commit 0d07a0c

Please sign in to comment.