Re: [groovy-user] Class.newInstance() not failing for one (untyped) argument constructors
Am 02.01.2012 11:19, schrieb Wujek Srujek:
> Hi. I am trying to create an instance of a class that has a default
> constructor. I also would like that the newInstance method throws an
> exception when there is no default constructor. This doesn't happen
> always, see the following code:
>
> class Foo {
> def Foo(a) {
> println '>>> ' + a
> }
> }
>
> Foo.class.newInstance()
>
> it prints '>>> null', which is incorrect behavior in this case. If I add
> a type to the argument, like int or String, it fails correctly, _unless_
> the type is List or Map - in which case I get '>>> null' again.
that it fails for String is strange... that should not be the case actually
> I am not
> sure what causes this, maybe a method from DefaultGroovyMethods? This of
> course works no matter what the type is for constructors that take more
> than 1 argument.
>
> The questions are:
> 1. Is this expected behavior? This seems like a really nasty corner case.
> 2. How can I invoke the 'real' newInstance method, that fails if there
> is no default costructor?
there is a newInstance method in DGM overriding the reflection method,
yes. This is to mirror the method call behaviour - which also uses null
for single argument methods without a given argument. Though I have this
one on my list of things to remove in the new MOP, because I think it is
a quite nasty and confusing ability.
If you want to use the "real" reflection method, you will have to call a
Java method that does it for you I'm afraid - in the worst case it means
you have to write a small helper in java.
bye blackdrag
--
--
Jochen "blackdrag" Theodorou - Groovy Project Tech Lead
blog: http://blackdragsview.blogspot.com/
german groovy discussion newsgroup: de.comp.lang.misc
For Groovy programming sources visit http://groovy-lang.org
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email