Skip to content

Commit

Permalink
GROOVY-8283: field hides setter of super class (not interface)
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Apr 28, 2024
1 parent c9b6ec2 commit 8324f8b
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/main/java/groovy/lang/MetaClassImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2747,7 +2747,7 @@ public void setProperty(final Class sender, final Object object, final String na
Object proxy = Proxy.newProxyInstance(
theClass.getClassLoader(),
new Class[]{method.getParameterTypes()[0].getTheClass()},
new ConvertedClosure((Closure) newValue, name));
new ConvertedClosure((Closure<?>) newValue, name));
arguments = new Object[]{proxy};
newValue = proxy;
} else {
Expand All @@ -2758,7 +2758,7 @@ public void setProperty(final Class sender, final Object object, final String na
//----------------------------------------------------------------------
// field
//----------------------------------------------------------------------
if (method == null && field != null) {
if (field != null && (method == null || isVisibleProperty(field, method, sender))) {
boolean mapInstance = (isMap && !isStatic);
if (field.isFinal()) {
if (mapInstance) { // GROOVY-8065
Expand Down Expand Up @@ -2806,9 +2806,9 @@ public void setProperty(final Class sender, final Object object, final String na
return;
}

//------------------------------------------------------------------
//----------------------------------------------------------------------
// java.util.Map put method
//------------------------------------------------------------------
//----------------------------------------------------------------------
if (isMap && !isStatic) {
((Map) object).put(name, newValue);
return;
Expand Down
72 changes: 72 additions & 0 deletions src/test/groovy/bugs/Groovy8283.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,76 @@ final class Groovy8283 {
assert new E().foo.class == A // not the field from this perspective
'''
}

@Test
void testWriteFieldPropertyShadowing() {
def shell = new GroovyShell()
shell.parse '''package p
class A {}
class B {}
class C {
boolean setter
protected A foo = new A()
A getFooA() { return this.@foo }
void setFoo(A a) { setter = true; this.@foo = a }
}
class D extends C {
protected B foo = new B() // hides A#foo; should hide A#setFoo in subclasses
B getFooB() { return this.@foo }
}
'''
assertScript shell, '''import p.*
class E extends D {
void test1() {
foo = null
assert !setter
assert fooA != null
assert fooB == null
}
void test2() {
this.foo = null
assert !setter
assert fooA != null
assert fooB == null
}
void test3() {
this.@foo = null
assert !setter
assert fooA != null
assert fooB == null
}
void test4() {
this.setFoo(null)
assert setter
assert fooA == null
assert fooB != null
}
void test5() {
def that = new E()
that.foo = null
assert !that.setter
assert that.fooA != null
assert that.fooB == null
that = new E()
that.@foo = null
assert !that.setter
assert that.fooA != null
assert that.fooB == null
that = new E()
that.setFoo(null)
assert that.setter
assert that.fooA == null
assert that.fooB != null
}
}
new E().test1()
new E().test2()
new E().test3()
new E().test4()
new E().test5()
'''
}
}

0 comments on commit 8324f8b

Please sign in to comment.