InstantiationException - Java library calling newInstance method on Scala class

413 views
Skip to first unread message

RanUser

unread,
Sep 13, 2012, 12:15:04 AM9/13/12
to scala-user
I'm greatful for any direction on this one!

- Scala 2.9.2

I'm using the Cassandra Astyanax client library, which is throwing an
InstantiationException here:

private T createContents(Class<T> clazz) throws
InstantiationException, IllegalAccessException {
return clazz.newInstance();
}

The relevant exception portion:

...
at java.lang.reflect.Method.invoke(Method.java:601)
Caused by: java.lang.InstantiationException: com.ranuser.TestClient
$TestCompositeColumn
at java.lang.Class.newInstance0(Class.java:357)
at java.lang.Class.newInstance(Class.java:325)
at
com.netflix.astyanax.serializers.AnnotatedCompositeSerializer.createContents(AnnotatedCompositeSerializer.java:
167)

--

Astyanax requires a class with a zero arg constructor, and possibly a
multi argument class constructor (from what I've read):

class TestCompositeColumn(idIn: Long, key1In: String, key2In: String)
extends Ordered[TestCompositeColumn] {
@Component(ordinal = 0) var id: Long = idIn
@Component(ordinal = 1) var key1: String = key1In
@Component(ordinal = 2) var key2: String = key2In

def this() = this(0, null, null)
//equals, hashCode, compare all implemented
}

I also defined an companion object to try to simulate Java static
newInstance method (newInstance method is never reached, and I'm sure
I'm doing it wrong):

object TestCompositeColumn{
def newInstance() = new TestCompositeColumn
}



Alex Cruise

unread,
Sep 13, 2012, 1:07:20 PM9/13/12
to RanUser, scala-user
On Wed, Sep 12, 2012 at 9:15 PM, RanUser <ranu...@gmail.com> wrote:
> Astyanax requires a class with a zero arg constructor, and possibly a
> multi argument class constructor (from what I've read):
>
> class TestCompositeColumn(idIn: Long, key1In: String, key2In: String)
> extends Ordered[TestCompositeColumn] {
> @Component(ordinal = 0) var id: Long = idIn
> @Component(ordinal = 1) var key1: String = key1In
> @Component(ordinal = 2) var key2: String = key2In
>
> def this() = this(0, null, null)
> //equals, hashCode, compare all implemented
> }

It may help to add the @field meta-annotation to make sure the
annotations get applied to the fields, instead of (or in addition to?)
the getters. The syntax is a bit ugly, but this is how you do it:

import annotation.target.field

class Foo {
@(AnnotationWithoutParams @field)
@(AnnotationWithParams @field)(param=value)
guid: String
}

> I also defined an companion object to try to simulate Java static
> newInstance method (newInstance method is never reached, and I'm sure
> I'm doing it wrong):

This doesn't work in Java; newInstance is a non-static method of a
Class, not a static method of a class. :)

-0xe1a

RanUser

unread,
Sep 13, 2012, 4:47:47 PM9/13/12
to scala...@googlegroups.com, RanUser
I tried the annotation tip but still have the same exception.  I believe annotations are working because I can insert into the DB.
 
The problem continues to be the call to clazz.newInstance() . I also tried defining def newInstance() = new TestCompositeColumn() in the TestCompositeColumn class unsuccessfully as well.
 
Any other ideas how to deal with this clazz.newInstance() call made from the java library to my class?
 
Thanks!

Alex Cruise

unread,
Sep 13, 2012, 5:05:54 PM9/13/12
to RanUser, scala...@googlegroups.com
On Thu, Sep 13, 2012 at 1:47 PM, RanUser <ranu...@gmail.com> wrote:
> I tried the annotation tip but still have the same exception.  I believe
> annotations are working because I can insert into the DB.

Oh OK!  

Caused by: java.lang.InstantiationException: com.ranuser.TestClient$TestCompositeColumn

This is a clue though!  Inner classes in Scala have a hidden constructor parameter that's probably not being supplied here.  Try moving TestCompositeColumn to the top-level.  It doesn't have to be in its own file, just make it a peer of TestClient instead of a child.

> The problem continues to be the call to clazz.newInstance() . I also tried
> defining def newInstance() = new TestCompositeColumn() in the
> TestCompositeColumn class unsuccessfully as well.

Yeah, you're not going to get anywhere trying to override/define/redefine newInstance.  This document, and the surrounding Java reflection tutorial, will hopefully shed some light. :)


-0xe1a

RanUser

unread,
Sep 16, 2012, 1:03:04 AM9/16/12
to scala...@googlegroups.com, RanUser
Another set of eyes does wonders :)
 
The exception was due to TestCompositeColumn being an inner class as you suggested.
 
Thanks so much!
Reply all
Reply to author
Forward
0 new messages