Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-7444

FactoryBuilderSupport doesn't support builder methods when not top-level

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 2.3.7, 2.4.3
    • None
    • groovy-runtime
    • jvm 1.7, Mac OSX

    Description

      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>

      Attachments

        1. StaticBuilderClassBugTest.groovy
          3 kB
          Marc Hadfield

        Activity

          People

            Unassigned Unassigned
            mchadfield Marc Hadfield
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: