-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GROOVY-8283: field hides getter or setter of super class (not interface)
- Loading branch information
1 parent
65506f4
commit ec44e38
Showing
2 changed files
with
161 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
* 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 testReadFieldPropertyShadowing() { | ||
def shell = new GroovyShell() | ||
shell.parse '''package p | ||
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 | ||
} | ||
''' | ||
assertScript shell, '''import p.* | ||
class E extends D { | ||
void test() { | ||
assert foo.class == B | ||
assert this.foo.class == B | ||
assert [email protected] == B | ||
assert this.getFoo().getClass() == A | ||
def that = new E() | ||
assert that.foo.class == B | ||
assert [email protected] == B | ||
assert that.getFoo().getClass() == A | ||
} | ||
} | ||
new E().test() | ||
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() | ||
''' | ||
} | ||
} |