Details
-
Type:
Bug
-
Status:
Closed
-
Priority:
Major
-
Resolution: Fixed
-
Affects Version/s: 1.1-rc-3
-
Fix Version/s: 1.5
-
Component/s: class generator
-
Labels:None
-
Number of attachments :
Description
The following code:
[code]
class DefaultNoArgCtor {
DefaultNoArgCtor(String s) {}
// DefaultNoArgCtor(int s) {} <== uncomment this line to get the BUG
}
def a = new DefaultNoArgCtor()
println a
[/code]
If you uncomment the 2nd constructor, then instead of getting an exception about not being able to find the ctor (or something similar)
what I am getting (trunk) is:
[trace]
java.lang.reflect.InvocationTargetException
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:585)
at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:101)
at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:130)
Caused by: java.lang.ClassCastException:
org.codehaus.groovy.reflection.CachedConstructor
at org.codehaus.groovy.runtime.metaclass.MethodSelectionException.appendMethods(MethodSelectionException.java:80)
at org.codehaus.groovy.runtime.metaclass.MethodSelectionException.getMessage(MethodSelectionException.java:59)
at java.lang.Throwable.getLocalizedMessage(Throwable.java:267)
at java.lang.Throwable.toString(Throwable.java:344)
at java.lang.String.valueOf(String.java:2615)
at java.lang.StringBuffer.append(StringBuffer.java:220)
at groovy.ui.GroovyMain.run(GroovyMain.java:319)
at groovy.ui.GroovyMain.process(GroovyMain.java:294)
at groovy.ui.GroovyMain.processArgs(GroovyMain.java:111)
at groovy.ui.GroovyMain.main(GroovyMain.java:92)
... 6 more
[/trace]
Activity
| Field | Original Value | New Value |
|---|---|---|
| Resolution | Fixed [ 1 ] | |
| Status | Open [ 1 ] | Resolved [ 5 ] |
| Status | Resolved [ 5 ] | Closed [ 6 ] |
Comment history (from mailing list)
Playing with constructors and metaprogramming, I have found out that
all POGOs do have the default constructor, even if they do not declare
one.
[code]
class NoDefaultCtor {
def field
NoDefaultCtor(param)
{ field= param }String toString() {
{field.class.name}return "toS: $
"
}
}
a = new NoDefaultCtor("first")
println a // => toS: java.lang.String
b = new NoDefaultCtor() // I wouldn't expect this to work
// println b => NPE I do expect this
[/code]
Something that people should be aware of. The implication on
metaprogamming: you cannot add a no-arg constructor through the
metaclass, as there is already one in place.