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

scala and reflection

6 replies
francisco treacy
Joined: 2009-02-13,
User offline. Last seen 42 years 45 weeks ago.

hi all,

coming from java-land i am familiar with java reflection.

i now have a scala application that needs to map some json into a
domain model. i already got json pairs into their typed scala
counterparts through parser combinators, and now need to stick them
into my model.

before proceeding with java reflection calls, i thought i might ask if
scala provides a more elegant way of achieving the same thing - i.e.
is package scala.reflect of any help in this usecase (i have found
sparse documentation on this subject). is it possible to do so
avoiding reflection? (i don't think so - but just double checking)

thanks,

francisco

Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: scala and reflection
I don't think there is anything special yet. scala.reflect supplies annotations to generate stuff that's useful for JavaBeans.Just remember that you may need to cast AnyVals to AnyRefs (asInstanceOf[AnyRef[) to pass them where java.lang.Object is needed.

On Mon, Mar 16, 2009 at 12:04 PM, francisco treacy <francisco.treacy@gmail.com> wrote:
hi all,

coming from java-land i am familiar with java reflection.

i now have a scala application that needs to map some json into a
domain model. i already got json pairs into their typed scala
counterparts through parser combinators, and now need to stick them
into my model.

before proceeding with java reflection calls, i thought i might ask if
scala provides a more elegant way of achieving the same thing - i.e.
is package scala.reflect of any help in this usecase (i have found
sparse documentation on this subject). is it possible to do so
avoiding reflection? (i don't think so - but just double checking)

thanks,

francisco

Jorge Ortiz
Joined: 2008-12-16,
User offline. Last seen 29 weeks 4 days ago.
Re: scala and reflection
You probably want to use pattern matching rather than reflection for this.

--j

On Mon, Mar 16, 2009 at 9:04 AM, francisco treacy <francisco.treacy@gmail.com> wrote:
hi all,

coming from java-land i am familiar with java reflection.

i now have a scala application that needs to map some json into a
domain model. i already got json pairs into their typed scala
counterparts through parser combinators, and now need to stick them
into my model.

before proceeding with java reflection calls, i thought i might ask if
scala provides a more elegant way of achieving the same thing - i.e.
is package scala.reflect of any help in this usecase (i have found
sparse documentation on this subject). is it possible to do so
avoiding reflection? (i don't think so - but just double checking)

thanks,

francisco

francisco treacy
Joined: 2009-02-13,
User offline. Last seen 42 years 45 weeks ago.
Re: scala and reflection

naftoli, thanks - yes i was casting everything to AnyRef.

jorge, i'm afraid i don't exactly get what you suggest.

would you go something like:

string match {
case "address" => instance.address_=(value)
...
}

instead of setter.invoke(instance, value) ?

i can't see how to turn a bunch of json values (in a generic fashion)
into its model representation in scala without recurring to
reflection. would you mind developing your thought a bit?

thanks,

francisco

On Mon, Mar 16, 2009 at 10:11 PM, Jorge Ortiz wrote:
> You probably want to use pattern matching rather than reflection for this.
>
> --j
>
> On Mon, Mar 16, 2009 at 9:04 AM, francisco treacy
> wrote:
>>
>> hi all,
>>
>> coming from java-land i am familiar with java reflection.
>>
>> i now have a scala application that needs to map some json into a
>> domain model. i already got json pairs into their typed scala
>> counterparts through parser combinators, and now need to stick them
>> into my model.
>>
>> before proceeding with java reflection calls, i thought i might ask if
>> scala provides a more elegant way of achieving the same thing - i.e.
>> is package scala.reflect of any help in this usecase (i have found
>> sparse documentation on this subject). is it possible to do so
>> avoiding reflection? (i don't think so - but just double checking)
>>
>> thanks,
>>
>> francisco
>
>

Jorge Ortiz
Joined: 2008-12-16,
User offline. Last seen 29 weeks 4 days ago.
Re: scala and reflection
Show me what your json model looks like (how is your data being represented) and I'll show you what I mean.

--j

On Tue, Mar 17, 2009 at 12:21 PM, francisco treacy <francisco.treacy@gmail.com> wrote:
naftoli, thanks - yes i was casting everything to AnyRef.

jorge, i'm afraid i don't exactly get what you suggest.

would you go something like:

string match {
 case "address" => instance.address_=(value)
 ...
}

instead of  setter.invoke(instance, value) ?

i can't see how to turn a bunch of json values (in a generic fashion)
into its model representation in scala without recurring to
reflection. would you mind developing your thought a bit?

thanks,

francisco


On Mon, Mar 16, 2009 at 10:11 PM, Jorge Ortiz <jorge.ortiz@gmail.com> wrote:
> You probably want to use pattern matching rather than reflection for this.
>
> --j
>
> On Mon, Mar 16, 2009 at 9:04 AM, francisco treacy
> <francisco.treacy@gmail.com> wrote:
>>
>> hi all,
>>
>> coming from java-land i am familiar with java reflection.
>>
>> i now have a scala application that needs to map some json into a
>> domain model. i already got json pairs into their typed scala
>> counterparts through parser combinators, and now need to stick them
>> into my model.
>>
>> before proceeding with java reflection calls, i thought i might ask if
>> scala provides a more elegant way of achieving the same thing - i.e.
>> is package scala.reflect of any help in this usecase (i have found
>> sparse documentation on this subject). is it possible to do so
>> avoiding reflection? (i don't think so - but just double checking)
>>
>> thanks,
>>
>> francisco
>
>

francisco treacy
Joined: 2009-02-13,
User offline. Last seen 42 years 45 weeks ago.
Re: scala and reflection

i actually don't have a "json model" , the parser is filling in my
entities. i'm pasting an example entity here:

class Comment extends Identifiable {

var id: String = _
var author: String = _
var url: String = _
var message: String = _

}

... along with the JSONParser i use. (warning: scala rookie experiment
ahead) => http://paste.pocoo.org/show/108384/

thanks,

francisco

On Tue, Mar 17, 2009 at 8:54 PM, Jorge Ortiz wrote:
> Show me what your json model looks like (how is your data being represented)
> and I'll show you what I mean.
>
> --j
>
> On Tue, Mar 17, 2009 at 12:21 PM, francisco treacy
> wrote:
>>
>> naftoli, thanks - yes i was casting everything to AnyRef.
>>
>> jorge, i'm afraid i don't exactly get what you suggest.
>>
>> would you go something like:
>>
>> string match {
>>  case "address" => instance.address_=(value)
>>  ...
>> }
>>
>> instead of  setter.invoke(instance, value) ?
>>
>> i can't see how to turn a bunch of json values (in a generic fashion)
>> into its model representation in scala without recurring to
>> reflection. would you mind developing your thought a bit?
>>
>> thanks,
>>
>> francisco
>>
>>
>> On Mon, Mar 16, 2009 at 10:11 PM, Jorge Ortiz
>> wrote:
>> > You probably want to use pattern matching rather than reflection for
>> > this.
>> >
>> > --j
>> >
>> > On Mon, Mar 16, 2009 at 9:04 AM, francisco treacy
>> > wrote:
>> >>
>> >> hi all,
>> >>
>> >> coming from java-land i am familiar with java reflection.
>> >>
>> >> i now have a scala application that needs to map some json into a
>> >> domain model. i already got json pairs into their typed scala
>> >> counterparts through parser combinators, and now need to stick them
>> >> into my model.
>> >>
>> >> before proceeding with java reflection calls, i thought i might ask if
>> >> scala provides a more elegant way of achieving the same thing - i.e.
>> >> is package scala.reflect of any help in this usecase (i have found
>> >> sparse documentation on this subject). is it possible to do so
>> >> avoiding reflection? (i don't think so - but just double checking)
>> >>
>> >> thanks,
>> >>
>> >> francisco
>> >
>> >
>
>

francisco treacy
Joined: 2009-02-13,
User offline. Last seen 42 years 45 weeks ago.
Re: scala and reflection

hi jorge,

do you still think you can show me a quick example of what you meant?

thanks,

francisco

On Tue, Mar 17, 2009 at 9:56 PM, francisco treacy
wrote:
> i actually don't have a "json model" , the parser is filling in my
> entities. i'm pasting an example entity here:
>
> class Comment extends Identifiable {
>
>  var id: String = _
>  var author: String = _
>  var url: String = _
>  var message: String = _
>
> }
>
> ... along with the JSONParser i use. (warning: scala rookie experiment
> ahead) => http://paste.pocoo.org/show/108384/
>
> thanks,
>
> francisco
>
>
> On Tue, Mar 17, 2009 at 8:54 PM, Jorge Ortiz wrote:
>> Show me what your json model looks like (how is your data being represented)
>> and I'll show you what I mean.
>>
>> --j
>>
>> On Tue, Mar 17, 2009 at 12:21 PM, francisco treacy
>> wrote:
>>>
>>> naftoli, thanks - yes i was casting everything to AnyRef.
>>>
>>> jorge, i'm afraid i don't exactly get what you suggest.
>>>
>>> would you go something like:
>>>
>>> string match {
>>>  case "address" => instance.address_=(value)
>>>  ...
>>> }
>>>
>>> instead of  setter.invoke(instance, value) ?
>>>
>>> i can't see how to turn a bunch of json values (in a generic fashion)
>>> into its model representation in scala without recurring to
>>> reflection. would you mind developing your thought a bit?
>>>
>>> thanks,
>>>
>>> francisco
>>>
>>>
>>> On Mon, Mar 16, 2009 at 10:11 PM, Jorge Ortiz
>>> wrote:
>>> > You probably want to use pattern matching rather than reflection for
>>> > this.
>>> >
>>> > --j
>>> >
>>> > On Mon, Mar 16, 2009 at 9:04 AM, francisco treacy
>>> > wrote:
>>> >>
>>> >> hi all,
>>> >>
>>> >> coming from java-land i am familiar with java reflection.
>>> >>
>>> >> i now have a scala application that needs to map some json into a
>>> >> domain model. i already got json pairs into their typed scala
>>> >> counterparts through parser combinators, and now need to stick them
>>> >> into my model.
>>> >>
>>> >> before proceeding with java reflection calls, i thought i might ask if
>>> >> scala provides a more elegant way of achieving the same thing - i.e.
>>> >> is package scala.reflect of any help in this usecase (i have found
>>> >> sparse documentation on this subject). is it possible to do so
>>> >> avoiding reflection? (i don't think so - but just double checking)
>>> >>
>>> >> thanks,
>>> >>
>>> >> francisco
>>> >
>>> >
>>
>>
>

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