Default constructor

149 views
Skip to first unread message

Jakob Odersky

unread,
Jul 20, 2011, 1:10:44 PM7/20/11
to squ...@googlegroups.com
Hi,

I recently ran into a problem when trying to run a select statement on a table.

java.lang.ExceptionInInitializerError
...
Caused by: java.lang.RuntimeException: invalid constructor choice public <class representing a row in the table>
...

After doing some searching on the internet I figured that the error came from my class having no default constructor but many parameters of type option. After adding the default constructor everything worked fine.

Am I correct that the default constructor is only used for reflection purposes, since squeryl cannot otherwise find out an option's type?
If so, how come the constructor has to be public or protected? (I tried private but got the same error as above)

Another strange behaviour I noticed was that on some tables taking options, there was no need for the default constructor.

Could someone be so kind and enlighten me to what purpose and when the default constructor has to be used?

I'm using squeryl version 9.4 with scala 2.8.1.final.

thanks,
--Jakob

David Whittaker

unread,
Jul 20, 2011, 1:52:37 PM7/20/11
to squ...@googlegroups.com
Hi Jakob,

Any chance you could send along the full stack of the exception?

0.9.4 final contains partial support for Option type deduction without no-arg constructors.  The two caveats I believe are that 1) It doesn't work well with Scala 2.9, but that doesn't seem to be an issue here and 2) It doesn't support Option[Enumeration] (or Enumeration at all for that matter).  Could that be what differentiates the classes in your project that do need no-arg constructors from those that do not?

As for why the constructor needs to be public/protected.... it stems from the default behavior of CGLIB if I remember right.  I actually put together a patch a while back to allow for private constructors but never tested in extensively or merged it in since I've been more interested on removing the no-arg constructor requirement all together.

Just so you know, I feel pretty confident at this point in saying that 0.9.5 will remove the no-arg constructor requirement completely for all versions of Scala.  I've been a bit busy with other things but the 0.9.5-SNAPSHOT releases should reflect that soon.

-Dave

Jakob Odersky

unread,
Jul 21, 2011, 7:40:53 AM7/21/11
to squ...@googlegroups.com
sure, here's the stack trace (it's quite long):

java.lang.ExceptionInInitializerError
    at com.foo.integration.Tables$$anonfun$selectAll$1.apply$mcV$sp(TableTest.scala:72)
    at com.foo.integration.Tables$$anonfun$selectAll$1.apply(TableTest.scala:71)
    at com.foo.integration.Tables$$anonfun$selectAll$1.apply(TableTest.scala:71)
    at org.squeryl.dsl.QueryDsl$class._using(QueryDsl.scala:44)
    at org.squeryl.dsl.QueryDsl$class.using(QueryDsl.scala:36)
    at org.squeryl.PrimitiveTypeMode$.using(PrimitiveTypeMode.scala:40)
    at com.foo.integration.Tables.selectAll(TableTest.scala:71)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:71)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:199)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:62)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.RuntimeException: invalid constructor choice public com.foo.dao.tables.CreditCardPaymentEntry(java.sql.Date,java.lang.String,scala.Option,scala.Option,scala.Option,scala.Option,scala.Option,scala.Option,scala.Option,scala.Option,scala.Option,scala.Option,scala.Option,scala.Option)
    at org.squeryl.internals.PosoMetaData.liftedTree1$1(PosoMetaData.scala:58)
    at org.squeryl.internals.PosoMetaData.<init>(PosoMetaData.scala:53)
    at org.squeryl.View.<init>(View.scala:58)
    at org.squeryl.Table.<init>(Table.scala:27)
    at org.squeryl.Schema$class.table(Schema.scala:338)
    at com.foo.dao.tables.package$.table(tables.scala:16)
    at com.foo.dao.tables.package$.<init>(tables.scala:25)
    at com.foo.dao.tables.package$.<clinit>(tables.scala)
    ... 38 more
Caused by: java.lang.IllegalArgumentException: argument type mismatch
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.squeryl.internals.PosoMetaData.liftedTree1$1(PosoMetaData.scala:54)
    ... 45 more

I haven't had much time to check out the differences between the classes that needed a no-arg constructor and the ones that didin't, but at a first glance it appears that all classes requiring the no-arg constructor had more than one non-option type parameter.
Hope that this helps,

--jakob

Maxime Lévesque

unread,
Jul 21, 2011, 8:19:39 AM7/21/11
to squ...@googlegroups.com

If you could post the definition of the offending class, it would help us much.

Thanks

ML

2011/7/21 Jakob Odersky <jode...@gmail.com>
Reply all
Reply to author
Forward
0 new messages