Marc Hadfield created GROOVY-7444:
-------------------------------------

             Summary:  FactoryBuilderSupport doesn't support builder methods 
when not top-level
                 Key: GROOVY-7444
                 URL: https://issues.apache.org/jira/browse/GROOVY-7444
             Project: Groovy
          Issue Type: Bug
          Components: groovy-runtime
    Affects Versions: 2.4.3, 2.3.7
         Environment: jvm 1.7, Mac OSX
            Reporter: Marc Hadfield


 FactoryBuilderSupport class  must be a top level class to gain dynamic builder 
methods, otherwise it throws method not found exceptions.

<code>

package groovy.staticbuilderbug

import groovy.util.FactoryBuilderSupport;

import java.util.Map;

/**
 * top level class
 */
class A {}


class AFactory extends AbstractFactory {
        
        public Object newInstance(FactoryBuilderSupport builder,
                Object name, Object value, Map attributes
        ) throws InstantiationException, IllegalAccessException {
                return attributes != null ? new A(attributes) : new A()
        }
        
}

/**
 * Top level builder
 */
class BuilderOK extends FactoryBuilderSupport {
        
        
        public BuilderOK(boolean init = true) {
                super(init);
        }
        
        def registerObjectFactories() {
                registerFactory("A", new AFactory())
                registerFactory("B", new StaticBuilderClassBugTest.BFactory())
        }
        
}


class StaticBuilderClassBugTest {

        /**
         * static inner class
         */
        static class B {}
        
        /**
         * static inner factory
         */
        static class BFactory extends AbstractFactory { 
                
                public Object newInstance(FactoryBuilderSupport builder,
                        Object name, Object value, Map attributes
                ) throws InstantiationException, IllegalAccessException {
                        return attributes != null ? new B(attributes) : new B()
                }
        }
        
        /**
         * static inner builder
         */
        static class BuilderBroken1 extends FactoryBuilderSupport {
                
                public BuilderBroken1(boolean init = true) {
                        super(init);
                }
                
                def registerObjectFactories() {
                        registerFactory("A", new AFactory())
                        registerFactory("B", new BFactory())
                }
                
        }
        
        
        class BuilderBroken2 extends FactoryBuilderSupport {
                
                public BuilderBroken2(boolean init = true) {
                        super(init)
                }
                
                def registerObjectFactories() {
                        registerFactory("A", new AFactory())
                        registerFactory("B", new BFactory())
                }
        }
        
        void test() {

                def builderOK = new BuilderOK()
                
                def a = builderOK.A {}
                def b = builderOK.B {}
                assert a instanceof A
                assert b instanceof B
                
                try {
                        def builderBroken1 = new BuilderBroken1()
                        def a1 = builderBroken1.A {}
                        def b1 = builderBroken1.B {}
                        assert a1 instanceof A
                        assert b1 instanceof B
                } catch(Exception e) {
                        println "${BuilderBroken1.class.simpleName} failed: 
${e.localizedMessage}";
                }               
                
                try {
                        def builderBroken2 = new BuilderBroken2()
                        def a2 = builderBroken2.A {}
                        def b2 = builderBroken2.B {}
                        assert a2 instanceof A
                        assert b2 instanceof B
                } catch(Exception e) {
                        println "${BuilderBroken2.class.simpleName} failed: 
${e.localizedMessage}";
                }
                
                                
        }
        
        static main(args) {
        
                //test method has access to BuilderBroken2
                new StaticBuilderClassBugTest().test()
                
        }

}

</code>



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to