In the older (1.x.x) versions of Groovy you can add constructors using metaClass.constructor
Example.metaClass.constructor << { String arg0 -> new Example(arg0, "") }
Is there a way to register constructors using the new Groovy 2.0 extension modules?
This seems to work:
Define an extension class as normal for Groovy 2 and just add the constructors in a static initialiser
public class ExampleHelper {
static {
Example.metaClass.constructor << { String arg0 -> new Example(arg0, "") }
}
}
Not that I know of...
You could add a static factory method to the Example class ie:
class ExampleExtensionStatic {
public static Example newInstance( Example type, String arg0 ) {
new Example( arg0, '' )
}
}
Then (after adding a link to this class in the staticExtensionClasses
field of the org.codehaus.groovy.runtime.ExtensionModule
file), you could do:
Example.newInstance( 'arg0' )
This is something worth asking on the mailing list to see if constructors are worth adding to the Module Extension system.