This page is no longer maintained — Please continue to the home page at www.scala-lang.org

instantiate Object by reflection

3 replies
fbesl
Joined: 2011-10-21,
User offline. Last seen 1 year 2 weeks ago.

Hello everybody,

inspired by combinator parsing I try to instantiate objects by
reflection.
After parsing the following method gets a classname and a list of
arguments

def makeInstance(clname: String, arglist: List[AnyRef]): AnyRef = {
val argtypes = arglist.map(_.getClass).toSeq
val args = arglist.toSeq
Class.forName(clname).getConstructor(argtypes:
_*).newInstance(args: _*)
}

The Java getConstructor-Call wants a vararg of Type Class... which
seems to be
ok.
The Java newInstance-Call wants a vararg of Type Object... which
causes the
following errormessage

|Grammatik.scala:66: error: type mismatch;
| found : ?0 where type ?0
| required: AnyRef
|Note that implicit conversions are not applicable because they are
ambiguous:
| both method any2Ensuring in object Predef of type [A](x:
A)Ensuring[A]
| and method any2ArrowAssoc in object Predef of type [A](x:
A)ArrowAssoc[A]
| are possible conversion functions from ?0 to AnyRef
| Class.forName(clname).getConstructor(argtypes:
_*).newInstance(args: _*)
| ^

Especially the second line is a mystery to me.

What's wrong in my approach?

Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: instantiate Object by reflection
Note sure but try calling arglist.toArray instead of .toSeq.

On Fri, Oct 21, 2011 at 5:21 AM, fbesl <franz [dot] beslmeisl [at] googlemail [dot] com> wrote:
Hello everybody,

inspired by combinator parsing I try to instantiate objects by
reflection.
After parsing the following method gets a classname and a list of
arguments

 def makeInstance(clname: String, arglist: List[AnyRef]): AnyRef = {
   val argtypes = arglist.map(_.getClass).toSeq
   val args = arglist.toSeq
   Class.forName(clname).getConstructor(argtypes:
_*).newInstance(args: _*)
 }

The Java getConstructor-Call wants a vararg of Type Class... which
seems to be
ok.
The Java newInstance-Call wants a vararg of Type Object... which
causes the
following errormessage

|Grammatik.scala:66: error: type mismatch;
| found   : ?0 where type ?0
| required: AnyRef
|Note that implicit conversions are not applicable because they are
ambiguous:
| both method any2Ensuring in object Predef of type [A](x:
A)Ensuring[A]
| and method any2ArrowAssoc in object Predef of type [A](x:
A)ArrowAssoc[A]
| are possible conversion functions from ?0 to AnyRef
|    Class.forName(clname).getConstructor(argtypes:
_*).newInstance(args: _*)
|                                                                  ^

Especially the second line is a mystery to me.

What's wrong in my approach?

Tim P
Joined: 2011-07-28,
User offline. Last seen 1 year 4 weeks ago.
Reflection

Hi
I want to use reflection to generate case classes from XML.
Any recommendations for a reflection tool for the job?
Can't use 2.10 nightly builds and new API because of coordination with
other libraries.

Tim

Donald McLean
Joined: 2009-11-11,
User offline. Last seen 2 years 48 weeks ago.
Re: Reflection

What's wrong with Class.forName? It has the virtue of simplicity, even
if it isn't necessarily the prettiest call in the standard library.

Donald

On Sun, Jan 1, 2012 at 9:10 AM, Tim P wrote:
> Hi
> I want to use reflection to generate case classes from XML.
> Any recommendations for a reflection tool for the job?
> Can't use 2.10 nightly builds and new API because of coordination with
> other libraries.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland