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

Groovy example source code file (Groovy3424Bug.groovy)

This example Groovy source code file (Groovy3424Bug.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

class, groovy3424bug, groovy3424classproxymetaclass, groovy3424classproxymetaclass, groovy3424foo, groovy3424foo, groovytestcase, metaclass, metaclass, metaclassregistry, object, object, proxymetaclass, string

The Groovy Groovy3424Bug.groovy source code

package groovy.bugs

class Groovy3424Bug extends GroovyTestCase {

    MetaClassRegistry registry
    MetaClass originalMetaClass

    void setUp() {
        registry = GroovySystem.metaClassRegistry
        originalMetaClass = registry.getMetaClass(Groovy3424Foo)
    }

    void tearDown() {
        registry.setMetaClass(Groovy3424Foo, originalMetaClass)
    }

    void testCallSiteShouldBeUpdatedAfterProxyMetaClassIsSet() {
        def newFoo = { -> new Groovy3424Foo() }

        assert new Groovy3424Foo() instanceof Groovy3424Foo
        assert newFoo() instanceof Groovy3424Foo

        MetaClass mc = new Groovy3424ClassProxyMetaClass(registry, Groovy3424Foo, originalMetaClass)
        registry.setMetaClass(Groovy3424Foo, mc)

        assert 'constructor' == new Groovy3424Foo()
        assert 'constructor' == newFoo()
    }

    void testExpandoCallSiteShouldBeUpdatedAfterProxyMetaClassIsSet() {
        Groovy3424Foo.metaClass.constructor << { String test -> 'foo' }

        def newFoo = { -> new Groovy3424Foo('test') }

        assert 'foo' == new Groovy3424Foo('test')
        assert 'foo' == newFoo()

        MetaClass mc = new Groovy3424ClassProxyMetaClass(registry, Groovy3424Foo, originalMetaClass)
        registry.setMetaClass(Groovy3424Foo, mc)

        assert 'constructor' == new Groovy3424Foo('test')
        assert 'constructor' == newFoo()
    }

    void testCallSiteShouldBeUpdatedAfterOriginalMetaClassIsRestored() {
        def newFoo = { -> new Groovy3424Foo() }

        MetaClass mc = new Groovy3424ClassProxyMetaClass(registry, Groovy3424Foo, originalMetaClass)
        registry.setMetaClass(Groovy3424Foo, mc)

        assert 'constructor' == new Groovy3424Foo()
        assert 'constructor' == newFoo()

        registry.setMetaClass(Groovy3424Foo, originalMetaClass)

        assert new Groovy3424Foo() instanceof Groovy3424Foo
        assert newFoo() instanceof Groovy3424Foo
    }

}

class Groovy3424Foo {}

class Groovy3424ClassProxyMetaClass extends ProxyMetaClass {
    Groovy3424ClassProxyMetaClass(MetaClassRegistry metaClassRegistry, Class aClass, MetaClass adaptee) {
        super(metaClassRegistry, aClass, adaptee)
    }
    public Object invokeConstructor(final Object[] arguments) {
        'constructor'
    }
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy Groovy3424Bug.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.