示例#1
0
 private Date create(Kryo kryo, Class<? extends Date> type, long time) throws KryoException {
   if (type == Date.class || type == null) {
     return new Date(time);
   }
   if (type == Timestamp.class) {
     return new Timestamp(time);
   }
   if (type == java.sql.Date.class) {
     return new java.sql.Date(time);
   }
   if (type == Time.class) {
     return new Time(time);
   }
   // other cases, reflection
   try {
     // Try to avoid invoking the no-args constructor
     // (which is expected to initialize the instance with the current time)
     Constructor<? extends Date> constructor = type.getConstructor(long.class);
     if (!constructor.isAccessible()) {
       try {
         constructor.setAccessible(true);
       } catch (SecurityException se) {
       }
     }
     return constructor.newInstance(time);
   } catch (Exception ex) {
     // default strategy
     Date d = (Date) kryo.newInstance(type);
     d.setTime(time);
     return d;
   }
 }
示例#2
0
 public KryoSerializable read(Kryo kryo, Input input, Class<KryoSerializable> type) {
   KryoSerializable object = kryo.newInstance(type);
   kryo.reference(object);
   object.read(kryo, input);
   return object;
 }