Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
None
-
None
-
None
Description
If groovy supports overloaded constructors, it really needs to support one constructor invoking another peer constructor. Maybe this is supported already, but trying to use "this(...)" fails. Here is my simple example:
class A {
A()
A {
println "entered ${i}"
}
static void main(args)
{ aa = new A() }}
Running gives this error:
Caught: groovy.lang.MissingMethodException: No signature of method A.doCall() is applicable for argument types: (java.lang.Integer) values: [23]
groovy.lang.MissingMethodException: No signature of method A.doCall() is applicable for argument types: (java.lang.Integer) values: [23]
at groovy.lang.MetaClass.invokeMethod(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.codehaus.groovy.runtime.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:63)
at groovy.lang.MetaClass.doMethodInvoke(Unknown Source)
at groovy.lang.MetaClass.invokeMethod(Unknown Source)
at org.codehaus.groovy.runtime.Invoker.invokeMethod(Unknown Source)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:106)
at A.invokeMethod(A.groovy)
at org.codehaus.groovy.runtime.Invoker.invokeMethod(Unknown Source)
at org.codehaus.groovy.runtime.InvokerHelper.invokeClosure(InvokerHelper.java:141)
at A.<init>(A.groovy:3)
etc...
My workaround is to create another method for each constructor to call, as shown below:
class A2 {
A2()
A2
{ init(i) } init {
println "entered ${i}"
}
static void main(args)
{ aa = new A2() }}
But this is undesirably noisey.
Another workaround is to have default parms in ctors, but this is not yet supported (JIRA issue has already been created regarding this).
Attachments
Issue Links
- relates to
-
GROOVY-435 Can't have private constructors
- Closed