serialization of type tags

150 views
Skip to first unread message

Eugene Burmako

unread,
Jul 31, 2012, 8:31:00 AM7/31/12
to scala-internals, viktor...@gmail.com, heather...@epfl.ch
I have an anonymous class that subclasses TypeCreator [1], and it
somehow needs to be sent over the wire. Since it's an anonymous class,
it can't be serialized using standard Java functionality (Java
serializer requires that all necessary code is present on the remote
machine).

One of the approaches would be to convert reification code into a
bunch of POJO constructor calls. E.g. if we reify TypeRef(pre, sym,
args), then we emit TypeRefTypeCreator(reify(pre), reify(sym), args
map (reify(_))). Obvious downside is the amount of boilerplate that
needs to be written/generated.

Another way to tackle this challenge would be to somehow serialize the
bytecode itself. However I don't see a straightforward way of
obtaining bytecode for a method or a class.

I'm wondering what would be a good solution. @Akka guys, maybe you
know how to address the problem?

[1]
https://github.com/scalamacros/kepler/blob/f4693871f4aad1fdbdbb743feaed8a848a9e2dca/src/library/scala/reflect/base/TypeCreator.scala#L1

Roland Kuhn

unread,
Jul 31, 2012, 8:34:08 AM7/31/12
to scala-i...@googlegroups.com, viktor...@gmail.com, heather...@epfl.ch

31 jul 2012 kl. 14:31 skrev Eugene Burmako:

> I have an anonymous class that subclasses TypeCreator [1], and it
> somehow needs to be sent over the wire. Since it's an anonymous class,
> it can't be serialized using standard Java functionality (Java
> serializer requires that all necessary code is present on the remote
> machine).
>
Unless you generate that “anonymous” class on-the-fly, you’ll have to have it on the classpath already; and if you do generate it, then you have the bytes ;-)

> One of the approaches would be to convert reification code into a
> bunch of POJO constructor calls. E.g. if we reify TypeRef(pre, sym,
> args), then we emit TypeRefTypeCreator(reify(pre), reify(sym), args
> map (reify(_))). Obvious downside is the amount of boilerplate that
> needs to be written/generated.
>
> Another way to tackle this challenge would be to somehow serialize the
> bytecode itself. However I don't see a straightforward way of
> obtaining bytecode for a method or a class.
>
> I'm wondering what would be a good solution. @Akka guys, maybe you
> know how to address the problem?
>
> [1]
> https://github.com/scalamacros/kepler/blob/f4693871f4aad1fdbdbb743feaed8a848a9e2dca/src/library/scala/reflect/base/TypeCreator.scala#L1

Roland Kuhn
Typesafe – The software stack for applications that scale.
twitter: @rolandkuhn


√iktor Ҡlang

unread,
Jul 31, 2012, 8:36:56 AM7/31/12
to Roland Kuhn, scala-i...@googlegroups.com, heather...@epfl.ch
On Tue, Jul 31, 2012 at 2:34 PM, Roland Kuhn <goo...@rkuhn.info> wrote:

31 jul 2012 kl. 14:31 skrev Eugene Burmako:

> I have an anonymous class that subclasses TypeCreator [1], and it
> somehow needs to be sent over the wire. Since it's an anonymous class,
> it can't be serialized using standard Java functionality (Java
> serializer requires that all necessary code is present on the remote
> machine).
>
Unless you generate that “anonymous” class on-the-fly, you’ll have to have it on the classpath already; and if you do generate it, then you have the bytes ;-)

But if it has na outer-pointer then it can become tricky.

I'd recommend using readResolve and writeReplace: http://www.javalobby.org/java/forums/t17491.html

We use this approach for InvocationHandlers for TypedActors for instance: 

I don't know whether that will fully solve your problem though, at least you've got an additional set of tools.

Cheers,
 

> One of the approaches would be to convert reification code into a
> bunch of POJO constructor calls. E.g. if we reify TypeRef(pre, sym,
> args), then we emit TypeRefTypeCreator(reify(pre), reify(sym), args
> map (reify(_))). Obvious downside is the amount of boilerplate that
> needs to be written/generated.
>
> Another way to tackle this challenge would be to somehow serialize the
> bytecode itself. However I don't see a straightforward way of
> obtaining bytecode for a method or a class.
>
> I'm wondering what would be a good solution. @Akka guys, maybe you
> know how to address the problem?
>
> [1]
> https://github.com/scalamacros/kepler/blob/f4693871f4aad1fdbdbb743feaed8a848a9e2dca/src/library/scala/reflect/base/TypeCreator.scala#L1

Roland Kuhn
Typesafe – The software stack for applications that scale.
twitter: @rolandkuhn





--
Viktor Klang

Akka Tech Lead
Typesafe - The software stack for applications that scale

Twitter: @viktorklang

Reply all
Reply to author
Forward
0 new messages