[
https://issues.apache.org/jira/browse/GROOVY-7209?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Daniel Sun reassigned GROOVY-7209:
----------------------------------
Fix Version/s: 7.0.0-alpha-1
Assignee: Daniel Sun
> Groovy metaClass fails to override method when the target method is within
> constructor
> --------------------------------------------------------------------------------------
>
> Key: GROOVY-7209
> URL: https://issues.apache.org/jira/browse/GROOVY-7209
> Project: Groovy
> Issue Type: Bug
> Components: groovy-jdk
> Affects Versions: 2.3.3
> Reporter: Yue Zhang
> Assignee: Daniel Sun
> Priority: Major
> Fix For: 7.0.0-alpha-1
>
> Attachments: Hello.groovy, HelloTest.groovy
>
>
> It seems that Groovy won't check the metaclass first if the target method is
> within constructor:
> I just tried to write this simple code to test overriding methods using
> metaClass.
> The code is here:
> {code}
> class Hello {
> public Hello()
> {
> Foo()
> }
> public void Foo()
> {
> println "old"
> }
> }
> {code}
> It has a Foo() method which simply prints "old" and it was called by the
> constructor.
> Here's the test code:
> {code}
> class HelloTest {
> @Test
> public void test() {
> boolean methodFooWasCalled = false
> Hello.metaClass.Foo = {-> println "new"
> methodFooWasCalled = true
> }
> Hello hello = new Hello()
> assertTrue methodFooWasCalled == true
> }
> }
> {code}
> I was expecting that the output should be "new" since Foo() has been
> overridden. But it still printed "old". Does anyone know why it fails? Thanks
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)