Avi-Levi / kryo

Automatically exported from code.google.com/p/kryo
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

InstantiatorStrategy won't work in DalvikVM (Android) #94

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Deserialize a third-party class without a no-arg constructor. I.e. 
android.content.Intent
2. Call kryo.setInstantiatorStrategy(new StdInstantiatorStrategy()) so it can 
create the object regardless

What is the expected output? What do you see instead?
Expected a working object, an exception is thrown instead: 
java.lang.NoClassDefFoundError: sun.reflect.ReflectionFactory, the LogCat shows 
"Could not find method sun.reflect.ReflectionFactory.getReflectionFactory, 
referenced from method 
org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.<init>"

An approach to achieve this on Dalvik is shown here, would it work for Kryo?
http://stackoverflow.com/questions/3121515/is-it-possible-to-bypass-constructors
-when-instantiating-objects-in-android

What version of the Kryo are you using?
2.20

Please provide any additional information below.
Thanks for the awesome library!

Original issue reported on code.google.com by sofiasno...@gmail.com on 18 Oct 2012 at 4:09

GoogleCodeExporter commented 9 years ago
I'm sorry, this turned out more of a usage problem rather than a bug.
I created a custom InstantiatorStrategy using the hint from that question, it 
works in android 2.x but no 4.x.
Then I tried creating custom serializer for ComponentName class, but now it 
gets stuck serializing Uri.StringUri (a private static inner class), how am I 
supposed to write a serializer for a private class?
These classes have real simple constructors, can't these somehow be inferred 
from the ivars ? Or constructed with empty values, followed by fieldserializer 
filling in the ivars ?

Original comment by sofiasno...@gmail.com on 22 Oct 2012 at 1:31

GoogleCodeExporter commented 9 years ago
You can't rely on a private class having the fields you expect. You can use 
reflection to create what you need. Though, I suggest doing serialization 
differently. Perhaps the objects already have a serialization mechanism (this 
is the whole purpose of Intent) that you can use, then just send those bytes. 
Or maybe you can get the necessary data and use a factory whatever API you are 
using provides to obtain an instance of the private class.

Original comment by nathan.s...@gmail.com on 4 Dec 2012 at 12:30