How to register private inner classe

1,644 views
Skip to first unread message

Mokarrom Hossain

unread,
Jun 3, 2015, 11:14:59 PM6/3/15
to kryo-...@googlegroups.com
I am getting following error when registering in Kryo.

Caused by: java.lang.IllegalArgumentException: Class is not registered: shared.SharedObjectFactory$Segment[]
Note: To register this class use: kryo.register(shared.SharedObjectFactory$Segment[].class);
    at com
.esotericsoftware.kryo.Kryo.getRegistration(Kryo.java:484)
    at com
.esotericsoftware.kryo.Kryo.getSerializer(Kryo.java:502)
    at com
.esotericsoftware.kryo.serializers.ObjectField.write(ObjectField.java:83)
   
... 22 moreEnter code here...

SharedObjectFactory class comes from a third party library. I don't have access it. How could I register private
class Segment? I have looked similar another post, but I'm not understanding.

public class SharedObjectFactory{
   
private final static int DEFAULT_NR_OF_SEGMENTS_BITSIZE = 5;
   
   
private final Segment[] segments = new Segment[1];

           
//...........

   
private final static class Segment{
             
//..........
             
//...........
   
}
}


mongonix

unread,
Jun 4, 2015, 12:23:31 PM6/4/15
to kryo-...@googlegroups.com
How about using:
 Class clazz = Class.forName("shared.SharedObjectFactory$Segment")
to obtain the class and then using 
 kryo.register(clazz) 
to register it?

You may also find this link useful, when it comes to registering an array:

-Leo

Mokarrom Hossain

unread,
Jun 4, 2015, 2:05:41 PM6/4/15
to kryo-...@googlegroups.com
Thank you Leo!

But it's not working. I have also look you link, but unable to get any idea.

mongonix

unread,
Jun 4, 2015, 2:29:15 PM6/4/15
to kryo-...@googlegroups.com


On Thursday, June 4, 2015 at 11:05:41 AM UTC-7, Mokarrom Hossain wrote:
Thank you Leo!

But it's not working. I have also look you link, but unable to get any idea.

I think my suggestion should work. 

Could you post (a simplified version of) your code here? I need something reproducible and self-contained. A JUnit-test reproducing the problem would be ideal.

-Leo

Mokarrom Hossain

unread,
Jun 4, 2015, 3:19:29 PM6/4/15
to kryo-...@googlegroups.com
Reproducing should be easy. Write the class as I explained above. Now register and then serialize & deserialize the obj. FYI, I have set registrationRequired as true.

Class comes from third-party library looks like as follows
package firstmpjtest;


public class SharedObjectFactory {
   
   
private final static int DEFAULT_NR_OF_SEGMENTS_BITSIZE = 5;
       
   
private final Segment[] segments = new Segment[1];

           
//...........

   
private final static class Segment{
             
//..........
             
//...........
   
}

}

My main file
        SharedObjectFactory sObj = new SharedObjectFactory();
       
KryoSerializer.register(SharedObjectFactory.class);
       
Class clazz = Class.forName("firstmpjtest.SharedObjectFactory$Segment");
       
KryoSerializer.register(clazz);
       
       
byte sByteArray[] = KryoSerializer.write(sObj);
       
SharedObjectFactory a = (SharedObjectFactory) KryoSerializer.read(sByteArray);

Kryo wrapper...
public final class KryoSerializer {

   
private static final Kryo kryo = new Kryo();

   
static {
      kryo
.setRegistrationRequired(true);
   
}
   
   
public static void register(Class... classes) {
     
for(Class clazz : classes) {
         kryo
.register(clazz);
     
}
   
}

   
public static byte[] write(Object obj) {
       
ByteArrayOutputStream bos = new ByteArrayOutputStream();
       
Output o = new Output(bos);
       kryo
.writeClassAndObject(o, obj);
       
return o.toBytes();
   
}
   
   
public static Object read(byte[] bytes) {
       
Input in = new Input(bytes);
       
return kryo.readClassAndObject(in);
   
}
}

mongonix

unread,
Jun 4, 2015, 4:37:44 PM6/4/15
to kryo-...@googlegroups.com
Please have a look at this JUnit-Test, I've written, which follows the recommendations from my previous message:

Does it do we you need?

-Leo

Mokarrom Hossain

unread,
Jun 5, 2015, 11:23:20 AM6/5/15
to kryo-...@googlegroups.com
Yes!
Now it's working. You are right.
This is the key that I was missing earlier.
                kryo.register(Array.newInstance(Class.forName("com.esotericsoftware.kryo.PrivateInnerClassTest$ConcreteClass$Segment"), 0)
                        .getClass());

Thank you so much Leo for your swift support!

mongonix

unread,
Jun 5, 2015, 2:30:47 PM6/5/15
to kryo-...@googlegroups.com


On Friday, June 5, 2015 at 8:23:20 AM UTC-7, Mokarrom Hossain wrote:
Yes!
Now it's working. You are right.
This is the key that I was missing earlier.
                kryo.register(Array.newInstance(Class.forName("com.esotericsoftware.kryo.PrivateInnerClassTest$ConcreteClass$Segment"), 0)
                        .getClass());

Thank you so much Leo for your swift support!

You are welcome! I'm glad that my suggestion helped you to solve your problem.

Robert Benjamen Beatty

unread,
Feb 22, 2019, 11:00:07 AM2/22/19
to kryo-users
Just a thanks for helping me solve a similar problem.

+1
Reply all
Reply to author
Forward
0 new messages