Details
-
Bug
-
Status: Open
-
Major
-
Resolution: Unresolved
-
2.3.7, 2.4.3
-
None
-
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
}
/**
- 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
}
/**
- 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>