alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (Groovy4006Bug.groovy)

This example Groovy source code file (Groovy4006Bug.groovy) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

inner, inner, myinnerclass4006v1, myinnerclass4006v1, myinnerclass4006v3, myinnerclass4006v3, myinnerclass4006v4, myouterclass4006v1, myouterclass4006v2, oc3, oc4, oc4new, runtimeexception, runtimeexception

The Groovy Groovy4006Bug.groovy source code

/*
 * Copyright 2003-2010 the original author or authors.
 *
 * Licensed 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

class Groovy4006Bug extends GroovyTestCase {
    void testOuterThisReferenceImplicitPassingToInnerClassConstructorNoArg() {
        try {
            assertScript """
                class MyOuterClass4006V1 {
                    def outerName = 'OC1' 
                    def foo() {
                        def ic = new MyInnerClass4006V1()
                        ic.bar()
                    }
                    class MyInnerClass4006V1 {
                        def innerName
                        MyInnerClass4006V1() {
                            this.innerName = 'IC1'
                        }
                        def bar() {
                            assert this.innerName == 'IC1'
                            assert this.outerName == 'OC1'
                            this.outerName = 'OC1New'
                            assert this.outerName == 'OC1New'
                            throw new RuntimeException('V1 - Inner class now successfully refers to implicitly passed outer this reference!')
                        }
                    }
                }
                def oc = new MyOuterClass4006V1()
                oc.foo()
            """
            fail('The script run should have failed with RuntimeException, coming from bar() of inner class')
        } catch (RuntimeException ex) {
            assert ex.message == 'V1 - Inner class now successfully refers to implicitly passed outer this reference!'
        }
    }

    void testOuterThisReferenceImplicitPassingToInnerClassConstructorWithArg() {
        try {
            assertScript """
                class MyOuterClass4006V2 {
                    def outerName = 'OC2' 
                    def foo() {
                        def ic = new MyInnerClass4006V1('IC2')
                        ic.bar()
                    }
                    class MyInnerClass4006V1 {
                        def innerName
                        MyInnerClass4006V1(innerName) {
                            this.innerName = innerName
                        }
                        def bar() {
                            assert this.innerName == 'IC2'
                            assert this.outerName == 'OC2'
                            this.outerName = 'OC2New'
                            assert this.outerName == 'OC2New'
                            throw new RuntimeException('V2 - Inner class now successfully refers to implicitly passed outer this reference!')
                        }
                    }
                }
                def oc = new MyOuterClass4006V2()
                oc.foo()
            """
            fail('The script run should have failed with RuntimeException, coming from bar() of inner class')
        } catch (RuntimeException ex) {
            assert ex.message == 'V2 - Inner class now successfully refers to implicitly passed outer this reference!'
        }
    }

    void testOuterThisReferenceImplicitPassingToInnerClassConstructorWithArgInAProp() {
        try {
            assertScript """
                class MyOuterClass4006V3 {
                    def outerName = 'OC3' 
                    def icField = new MyInnerClass4006V3('IC3');
                    def foo() {
                        icField.bar()
                    }
                    class MyInnerClass4006V3 {
                        def innerName
                        MyInnerClass4006V3(innerName) {
                            this.innerName = innerName
                        }
                        def bar() {
                            assert this.innerName == 'IC3'
                            assert this.outerName == 'OC3'
                            this.outerName = 'OC3New'
                            assert this.outerName == 'OC3New'
                            throw new RuntimeException('V3 - Inner class now successfully refers to implicitly passed outer this reference!')
                        }
                    }
                }
                def oc = new MyOuterClass4006V3()
                oc.foo()
            """
            fail('The script run should have failed with RuntimeException, coming from bar() of inner class')
        } catch (RuntimeException ex) {
            assert ex.message == 'V3 - Inner class now successfully refers to implicitly passed outer this reference!'
        }
    }

    void testOuterThisReferenceImplicitPassingToInnerClassConstructorWithArgInAField() {
        try {
            assertScript """
                class MyOuterClass4006V4 {
                    def outerName = 'OC4' 
                    private def icField = new MyInnerClass4006V4('IC4');
                    def foo() {
                        icField.bar()
                    }
                    class MyInnerClass4006V4 {
                        def innerName
                        MyInnerClass4006V4(innerName) {
                            this.innerName = innerName
                        }
                        def bar() {
                            assert this.innerName == 'IC4'
                            assert this.outerName == 'OC4'
                            this.outerName = 'OC4New'
                            assert this.outerName == 'OC4New'
                            throw new RuntimeException('V4 - Inner class now successfully refers to implicitly passed outer this reference!')
                        }
                    }
                }
                def oc = new MyOuterClass4006V4()
                oc.foo()
            """
            fail('The script run should have failed with RuntimeException, coming from bar() of inner class')
        } catch (RuntimeException ex) {
            assert ex.message == 'V4 - Inner class now successfully refers to implicitly passed outer this reference!'
        }
    }
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy Groovy4006Bug.groovy source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.