How to do Java serialization without Reflection?
Asked Answered
M

4

7

I am working on a Lego Mindstorm NXT robot, which do not support Java reflection.

For some reason (the parallel creation of a simulator and an actual mindstorm) we want to use Serialization to exchange Java objects.

The problem is that serialization uses reflection, which the JVM on the mindstorm does not support.

Any ideas?

I found this page on Zwong.de, but the source code has been removed.

Microphyte answered 5/3, 2013 at 13:25 Comment(1)
The guy's twitter name is listed on that site (@corneliushald) - may well be worth pinging him and asking. Usually these things go missing by accident more than design.Theorbo
V
4

Make your classes implement Externalizable, then ObjectOuputStream.writeObject() / readObject() will invoke writeExternal(ObjectOutput out) / readExternal(ObjectInput) on your objects directy, without using reflection

Verso answered 5/3, 2013 at 13:48 Comment(1)
We didn't know of the existence of the Externalizable interface, but it sounds promising. Problem is, the leJOS Java Mindstorm API does not provide a ObjectOutputStream or ObjectInputStream class. How can we use Externalizable without those objects? Or should we implement our own classes without using reflection?Microphyte
T
1

I believe Kryo supports reflection-less instantiation of serializable objects. A quick look on their home page seems to confirm it:

When ReflectASM or reflection cannot be used, Kryo can be configured to use an InstantiatorStrategy to handle creating instances of a class. Objenesis provides StdInstantiatorStrategy which uses JVM specific APIs to create an instance of a class without calling any constructor at all. This works on many JVMs.

It sounds from that like you'll need to create your own InstantiatorStrategy, since I'm not sure the standard one will have support for the NXT JVM - worth a try though! I haven't tried this myself, but it sounds like it should be possible in theory.

Theorbo answered 5/3, 2013 at 13:35 Comment(0)
E
0

I found two possible leads. Hopefully these are of some help to you.

1) Doing Java serialization without reflection

2) xml serialization generator for java without using reflection

Egyptology answered 5/3, 2013 at 13:36 Comment(3)
the source code of your first answer is offline, I already mentioned it in my question.Microphyte
I managed to get the Google cached version to load, which is what I linked. It looks doubtful that you could download the source code right now though. Maybe the server will be back up shortly.Egyptology
Please go look at How to Answer and the faq. Your answer has to be self contained. Please improve your answer to reflect that way of thinking.Lindyline
A
0

Serialization and deserialization is simply a way of writing and reading an object. You can always write your own methods that write and read all object data to/from a string/file/stream.

Moreover, custom serialization methods, especially these using binary data, are often less time, memory and processing power consuming than these provided by, ie., Serializable.

Armallas answered 5/3, 2013 at 13:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.