How to do Java serialization without Reflection?

I am working on a Lego Mindstorm NXT robot that does not support Java reflection.

For some reason (parallel simulator creation and actual brainstorming) we want to use serialization to exchange Java objects.

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

Any ideas?

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

+7
source share
4 answers

I believe Kryo supports the non-implementation of creating serializable objects. A quick look at their homepage seems to confirm this:

If ReflectASM or reflection cannot be used, Kryo can be configured to use InstantiatorStrategy to process instances of the class instance. Objenesis provides StdInstantiatorStrategy, which uses JVM-specific APIs to instantiate a class without calling any constructor at all. This works on many JVMs.

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

+1
source

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

+1
source
0
source

Serialization and deserialization is just 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 line / file / stream.

In addition, custom serialization methods, especially those using binary data, often have less time, memory, and processing power than is provided, for example, Serializable.

0
source

All Articles