/
OptimizedClassDescriptor.java
1048 lines (799 loc) · 32.2 KB
/
OptimizedClassDescriptor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ignite.marshaller.optimized;
import org.apache.ignite.internal.util.*;
import org.apache.ignite.internal.util.typedef.*;
import org.apache.ignite.internal.util.typedef.internal.*;
import org.apache.ignite.lang.*;
import org.apache.ignite.marshaller.*;
import sun.misc.*;
import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import static java.lang.reflect.Modifier.*;
import static org.apache.ignite.marshaller.optimized.OptimizedMarshallerUtils.*;
/**
* Class descriptor.
*/
class OptimizedClassDescriptor {
/** Unsafe. */
private static final Unsafe UNSAFE = GridUnsafe.unsafe();
/** */
private static final int TYPE_BYTE = 1;
/** */
private static final int TYPE_SHORT = 2;
/** */
private static final int TYPE_INT = 3;
/** */
private static final int TYPE_LONG = 4;
/** */
private static final int TYPE_FLOAT = 5;
/** */
private static final int TYPE_DOUBLE = 6;
/** */
private static final int TYPE_CHAR = 7;
/** */
private static final int TYPE_BOOLEAN = 8;
/** */
private static final int TYPE_BYTE_ARR = 9;
/** */
private static final int TYPE_SHORT_ARR = 10;
/** */
private static final int TYPE_INT_ARR = 11;
/** */
private static final int TYPE_LONG_ARR = 12;
/** */
private static final int TYPE_FLOAT_ARR = 13;
/** */
private static final int TYPE_DOUBLE_ARR = 14;
/** */
private static final int TYPE_CHAR_ARR = 15;
/** */
private static final int TYPE_BOOLEAN_ARR = 16;
/** */
private static final int TYPE_OBJ_ARR = 17;
/** */
private static final int TYPE_STR = 18;
/** */
private static final int TYPE_ENUM = 19;
/** */
private static final int TYPE_UUID = 20;
/** */
private static final int TYPE_PROPS = 21;
/** */
private static final int TYPE_ARRAY_LIST = 22;
/** */
private static final int TYPE_HASH_MAP = 23;
/** */
private static final int TYPE_HASH_SET = 24;
/** */
private static final int TYPE_LINKED_LIST = 25;
/** */
private static final int TYPE_LINKED_HASH_MAP = 26;
/** */
private static final int TYPE_LINKED_HASH_SET = 27;
/** */
private static final int TYPE_DATE = 28;
/** */
private static final int TYPE_CLS = 29;
/** */
private static final int TYPE_EXTERNALIZABLE = 50;
/** */
private static final int TYPE_SERIALIZABLE = 51;
/** Class. */
private final Class<?> cls;
/** Context. */
private final MarshallerContext ctx;
/** ID mapper. */
private final OptimizedMarshallerIdMapper mapper;
/** Class name. */
private final String name;
/** Type ID. */
private final int typeId;
/** Short ID. */
private final short checksum;
/** Class type. */
private int type;
/** Primitive flag. */
private boolean isPrimitive;
/** Enum flag. */
private boolean isEnum;
/** Serializable flag. */
private boolean isSerial;
/** Excluded flag. */
private boolean excluded;
/** {@code True} if descriptor is for {@link Class}. */
private boolean isCls;
/** Enumeration values. */
private Object[] enumVals;
/** Constructor. */
private Constructor<?> constructor;
/** Fields. */
private Fields fields;
/** {@code writeObject} methods. */
private List<Method> writeObjMtds;
/** {@code writeReplace} method. */
private Method writeReplaceMtd;
/** {@code readObject} methods. */
private List<Method> readObjMtds;
/** {@code readResolve} method. */
private Method readResolveMtd;
/** Defaults field offset. */
private long dfltsFieldOff;
/** Load factor field offset. */
private long loadFactorFieldOff;
/** Map field offset. */
private long mapFieldOff;
/** Access order field offset. */
private long accessOrderFieldOff;
/**
* Creates descriptor for class.
*
* @param cls Class.
* @param ctx Context.
* @param mapper ID mapper.
* @throws IOException In case of error.
*/
@SuppressWarnings("ForLoopReplaceableByForEach")
OptimizedClassDescriptor(Class<?> cls, MarshallerContext ctx, OptimizedMarshallerIdMapper mapper)
throws IOException {
this.cls = cls;
this.ctx = ctx;
this.mapper = mapper;
name = cls.getName();
int typeId;
if (mapper != null) {
typeId = mapper.typeId(name);
if (typeId == 0)
typeId = name.hashCode();
}
else
typeId = name.hashCode();
this.typeId = typeId;
excluded = MarshallerExclusions.isExcluded(cls);
if (!excluded) {
Class<?> parent;
if (cls == byte.class || cls == Byte.class) {
type = TYPE_BYTE;
isPrimitive = true;
}
else if (cls == short.class || cls == Short.class) {
type = TYPE_SHORT;
isPrimitive = true;
}
else if (cls == int.class || cls == Integer.class) {
type = TYPE_INT;
isPrimitive = true;
}
else if (cls == long.class || cls == Long.class) {
type = TYPE_LONG;
isPrimitive = true;
}
else if (cls == float.class || cls == Float.class) {
type = TYPE_FLOAT;
isPrimitive = true;
}
else if (cls == double.class || cls == Double.class) {
type = TYPE_DOUBLE;
isPrimitive = true;
}
else if (cls == char.class || cls == Character.class) {
type = TYPE_CHAR;
isPrimitive = true;
}
else if (cls == boolean.class || cls == Boolean.class) {
type = TYPE_BOOLEAN;
isPrimitive = true;
}
else if (cls == byte[].class)
type = TYPE_BYTE_ARR;
else if (cls == short[].class)
type = TYPE_SHORT_ARR;
else if (cls == int[].class)
type = TYPE_INT_ARR;
else if (cls == long[].class)
type = TYPE_LONG_ARR;
else if (cls == float[].class)
type = TYPE_FLOAT_ARR;
else if (cls == double[].class)
type = TYPE_DOUBLE_ARR;
else if (cls == char[].class)
type = TYPE_CHAR_ARR;
else if (cls == boolean[].class)
type = TYPE_BOOLEAN_ARR;
else if (cls.isArray())
type = TYPE_OBJ_ARR;
else if (cls == String.class)
type = TYPE_STR;
else if (cls.isEnum()) {
type = TYPE_ENUM;
isEnum = true;
enumVals = cls.getEnumConstants();
}
// Support for enum constants, based on anonymous children classes.
else if ((parent = cls.getSuperclass()) != null && parent.isEnum()) {
type = TYPE_ENUM;
isEnum = true;
enumVals = parent.getEnumConstants();
}
else if (cls == UUID.class)
type = TYPE_UUID;
else if (cls == Properties.class) {
type = TYPE_PROPS;
try {
dfltsFieldOff = UNSAFE.objectFieldOffset(Properties.class.getDeclaredField("defaults"));
}
catch (NoSuchFieldException e) {
throw new IOException(e);
}
}
else if (cls == ArrayList.class)
type = TYPE_ARRAY_LIST;
else if (cls == HashMap.class) {
type = TYPE_HASH_MAP;
try {
loadFactorFieldOff = UNSAFE.objectFieldOffset(HashMap.class.getDeclaredField("loadFactor"));
}
catch (NoSuchFieldException e) {
throw new IOException(e);
}
}
else if (cls == HashSet.class) {
type = TYPE_HASH_SET;
try {
loadFactorFieldOff = UNSAFE.objectFieldOffset(HashMap.class.getDeclaredField("loadFactor"));
mapFieldOff = UNSAFE.objectFieldOffset(HashSet.class.getDeclaredField("map"));
}
catch (NoSuchFieldException e) {
throw new IOException(e);
}
}
else if (cls == LinkedList.class)
type = TYPE_LINKED_LIST;
else if (cls == LinkedHashMap.class) {
type = TYPE_LINKED_HASH_MAP;
try {
loadFactorFieldOff = UNSAFE.objectFieldOffset(HashMap.class.getDeclaredField("loadFactor"));
accessOrderFieldOff = UNSAFE.objectFieldOffset(LinkedHashMap.class.getDeclaredField("accessOrder"));
}
catch (NoSuchFieldException e) {
throw new IOException(e);
}
}
else if (cls == LinkedHashSet.class) {
type = TYPE_LINKED_HASH_SET;
try {
loadFactorFieldOff = UNSAFE.objectFieldOffset(HashMap.class.getDeclaredField("loadFactor"));
mapFieldOff = UNSAFE.objectFieldOffset(HashSet.class.getDeclaredField("map"));
}
catch (NoSuchFieldException e) {
throw new IOException(e);
}
}
else if (cls == Date.class)
type = TYPE_DATE;
else if (cls == Class.class) {
type = TYPE_CLS;
isCls = true;
}
else {
Class<?> c = cls;
while ((writeReplaceMtd == null || readResolveMtd == null) && c != null && !c.equals(Object.class)) {
if (writeReplaceMtd == null) {
try {
writeReplaceMtd = c.getDeclaredMethod("writeReplace");
if (!isStatic(writeReplaceMtd.getModifiers()) &&
!(isPrivate(writeReplaceMtd.getModifiers()) && c != cls) &&
writeReplaceMtd.getReturnType().equals(Object.class))
writeReplaceMtd.setAccessible(true);
else
// Set method back to null if it has incorrect signature.
writeReplaceMtd = null;
}
catch (NoSuchMethodException ignored) {
// No-op.
}
}
if (readResolveMtd == null) {
try {
readResolveMtd = c.getDeclaredMethod("readResolve");
if (!isStatic(readResolveMtd.getModifiers()) &&
!(isPrivate(readResolveMtd.getModifiers()) && c != cls) &&
readResolveMtd.getReturnType().equals(Object.class))
readResolveMtd.setAccessible(true);
else
// Set method back to null if it has incorrect signature.
readResolveMtd = null;
}
catch (NoSuchMethodException ignored) {
// No-op.
}
}
c = c.getSuperclass();
}
if (Externalizable.class.isAssignableFrom(cls)) {
type = TYPE_EXTERNALIZABLE;
try {
constructor = !Modifier.isStatic(cls.getModifiers()) && cls.getDeclaringClass() != null ?
cls.getDeclaredConstructor(cls.getDeclaringClass()) :
cls.getDeclaredConstructor();
constructor.setAccessible(true);
}
catch (NoSuchMethodException e) {
throw new IOException("Externalizable class doesn't have default constructor: " + cls, e);
}
}
else {
type = TYPE_SERIALIZABLE;
isSerial = Serializable.class.isAssignableFrom(cls);
writeObjMtds = new ArrayList<>();
readObjMtds = new ArrayList<>();
List<List<Field>> fields = new ArrayList<>();
List<List<T2<OptimizedFieldType, Long>>> fieldOffs = new ArrayList<>();
List<Map<String, IgniteBiTuple<Integer, OptimizedFieldType>>> fieldInfoMaps = new ArrayList<>();
List<List<IgniteBiTuple<Integer, OptimizedFieldType>>> fieldInfoLists = new ArrayList<>();
for (c = cls; c != null && !c.equals(Object.class); c = c.getSuperclass()) {
Method mtd;
try {
mtd = c.getDeclaredMethod("writeObject", ObjectOutputStream.class);
int mod = mtd.getModifiers();
if (!isStatic(mod) && isPrivate(mod) && mtd.getReturnType() == Void.TYPE)
mtd.setAccessible(true);
else
// Set method back to null if it has incorrect signature.
mtd = null;
}
catch (NoSuchMethodException ignored) {
mtd = null;
}
writeObjMtds.add(mtd);
try {
mtd = c.getDeclaredMethod("readObject", ObjectInputStream.class);
int mod = mtd.getModifiers();
if (!isStatic(mod) && isPrivate(mod) && mtd.getReturnType() == Void.TYPE)
mtd.setAccessible(true);
else
// Set method back to null if it has incorrect signature.
mtd = null;
}
catch (NoSuchMethodException ignored) {
mtd = null;
}
readObjMtds.add(mtd);
Field[] clsFields0 = c.getDeclaredFields();
Arrays.sort(clsFields0, new Comparator<Field>() {
@Override public int compare(Field f1, Field f2) {
return f1.getName().compareTo(f2.getName());
}
});
List<Field> clsFields = new ArrayList<>(clsFields0.length);
List<T2<OptimizedFieldType, Long>> clsFieldOffs =
new ArrayList<>(clsFields0.length);
for (int i = 0; i < clsFields0.length; i++) {
Field f = clsFields0[i];
int mod = f.getModifiers();
if (!isStatic(mod) && !isTransient(mod)) {
OptimizedFieldType type = fieldType(f.getType());
clsFields.add(f);
clsFieldOffs.add(new T2<>(type, UNSAFE.objectFieldOffset(f)));
}
}
fields.add(clsFields);
fieldOffs.add(clsFieldOffs);
Map<String, IgniteBiTuple<Integer, OptimizedFieldType>> fieldInfoMap = null;
try {
Field serFieldsDesc = c.getDeclaredField("serialPersistentFields");
int mod = serFieldsDesc.getModifiers();
if (serFieldsDesc.getType() == ObjectStreamField[].class &&
isPrivate(mod) && isStatic(mod) && isFinal(mod)) {
serFieldsDesc.setAccessible(true);
ObjectStreamField[] serFields = (ObjectStreamField[])serFieldsDesc.get(null);
fieldInfoMap = new HashMap<>();
for (int i = 0; i < serFields.length; i++) {
ObjectStreamField serField = serFields[i];
fieldInfoMap.put(serField.getName(), F.t(i, fieldType(serField.getType())));
}
}
}
catch (NoSuchFieldException ignored) {
// No-op.
}
catch (IllegalAccessException e) {
throw new IOException("Failed to get value of 'serialPersistentFields' field in class: " +
cls.getName(), e);
}
if (fieldInfoMap == null) {
fieldInfoMap = new HashMap<>();
for (int i = 0; i < clsFields.size(); i++) {
Field f = clsFields.get(i);
fieldInfoMap.put(f.getName(), F.t(i, fieldType(f.getType())));
}
}
fieldInfoMaps.add(fieldInfoMap);
List<IgniteBiTuple<Integer, OptimizedFieldType>> fieldInfoList =
new ArrayList<>(fieldInfoMap.values());
Collections.sort(fieldInfoList, new Comparator<IgniteBiTuple<Integer, OptimizedFieldType>>() {
@Override public int compare(IgniteBiTuple<Integer, OptimizedFieldType> t1,
IgniteBiTuple<Integer, OptimizedFieldType> t2) {
return t1.get1().compareTo(t2.get1());
}
});
fieldInfoLists.add(fieldInfoList);
}
Collections.reverse(writeObjMtds);
Collections.reverse(readObjMtds);
Collections.reverse(fields);
Collections.reverse(fieldOffs);
Collections.reverse(fieldInfoMaps);
Collections.reverse(fieldInfoLists);
this.fields = new Fields(fields, fieldOffs, fieldInfoLists, fieldInfoMaps);
}
}
}
checksum = computeSerialVersionUid(cls, fields != null ? fields.ownFields() : null);
}
/**
* @return Excluded flag.
*/
boolean excluded() {
return excluded;
}
/**
* @return Class.
*/
Class<?> describedClass() {
return cls;
}
/**
* @return Type ID.
*/
int typeId() {
return typeId;
}
/**
* @return Primitive flag.
*/
boolean isPrimitive() {
return isPrimitive;
}
/**
* @return Enum flag.
*/
boolean isEnum() {
return isEnum;
}
/**
* @return {@code True} if descriptor is for {@link Class}.
*/
boolean isClass() {
return isCls;
}
/**
* Replaces object.
*
* @param obj Object.
* @return Replaced object or {@code null} if there is no {@code writeReplace} method.
* @throws IOException In case of error.
*/
Object replace(Object obj) throws IOException {
if (writeReplaceMtd != null) {
try {
return writeReplaceMtd.invoke(obj);
}
catch (IllegalAccessException | InvocationTargetException e) {
throw new IOException(e);
}
}
return obj;
}
/**
* Writes object to stream.
*
* @param out Output stream.
* @param obj Object.
* @throws IOException In case of error.
*/
@SuppressWarnings("ForLoopReplaceableByForEach")
void write(OptimizedObjectOutputStream out, Object obj) throws IOException {
switch (type) {
case TYPE_BYTE:
out.writeByte((Byte)obj);
break;
case TYPE_SHORT:
out.writeShort((Short)obj);
break;
case TYPE_INT:
out.writeInt((Integer)obj);
break;
case TYPE_LONG:
out.writeLong((Long)obj);
break;
case TYPE_FLOAT:
out.writeFloat((Float)obj);
break;
case TYPE_DOUBLE:
out.writeDouble((Double)obj);
break;
case TYPE_CHAR:
out.writeChar((Character)obj);
break;
case TYPE_BOOLEAN:
out.writeBoolean((Boolean)obj);
break;
case TYPE_BYTE_ARR:
out.writeByteArray((byte[])obj);
break;
case TYPE_SHORT_ARR:
out.writeShortArray((short[])obj);
break;
case TYPE_INT_ARR:
out.writeIntArray((int[])obj);
break;
case TYPE_LONG_ARR:
out.writeLongArray((long[])obj);
break;
case TYPE_FLOAT_ARR:
out.writeFloatArray((float[])obj);
break;
case TYPE_DOUBLE_ARR:
out.writeDoubleArray((double[])obj);
break;
case TYPE_CHAR_ARR:
out.writeCharArray((char[])obj);
break;
case TYPE_BOOLEAN_ARR:
out.writeBooleanArray((boolean[])obj);
break;
case TYPE_OBJ_ARR:
out.writeUTF(obj.getClass().getComponentType().getName());
out.writeArray((Object[])obj);
break;
case TYPE_STR:
out.writeString((String)obj);
break;
case TYPE_ENUM:
out.writeInt(((Enum)obj).ordinal());
break;
case TYPE_UUID:
out.writeUuid((UUID)obj);
break;
case TYPE_PROPS:
out.writeProperties((Properties)obj, dfltsFieldOff);
break;
case TYPE_ARRAY_LIST:
out.writeArrayList((ArrayList<?>)obj);
break;
case TYPE_HASH_MAP:
out.writeHashMap((HashMap<?, ?>)obj, loadFactorFieldOff, false);
break;
case TYPE_HASH_SET:
out.writeHashSet((HashSet<?>)obj, mapFieldOff, loadFactorFieldOff);
break;
case TYPE_LINKED_LIST:
out.writeLinkedList((LinkedList<?>)obj);
break;
case TYPE_LINKED_HASH_MAP:
out.writeLinkedHashMap((LinkedHashMap<?, ?>)obj, loadFactorFieldOff, accessOrderFieldOff, false);
break;
case TYPE_LINKED_HASH_SET:
out.writeLinkedHashSet((LinkedHashSet<?>)obj, mapFieldOff, loadFactorFieldOff);
break;
case TYPE_DATE:
out.writeDate((Date)obj);
break;
case TYPE_CLS:
OptimizedClassDescriptor desc = classDescriptor((Class<?>)obj, ctx, mapper);
out.writeInt(desc.typeId());
break;
case TYPE_EXTERNALIZABLE:
out.writeShort(checksum);
out.writeExternalizable(obj);
break;
case TYPE_SERIALIZABLE:
if (out.requireSerializable() && !isSerial)
throw new NotSerializableException("Must implement java.io.Serializable or " +
"set OptimizedMarshaller.setRequireSerializable() to false " +
"(note that performance may degrade if object is not Serializable): " + name);
out.writeShort(checksum);
out.writeSerializable(obj, writeObjMtds, fields);
break;
default:
throw new IllegalStateException("Invalid class type: " + type);
}
}
/**
* Reads object from stream.
*
* @param in Input stream.
* @return Object.
* @throws ClassNotFoundException If class not found.
* @throws IOException In case of error.
*/
Object read(OptimizedObjectInputStream in) throws ClassNotFoundException, IOException {
switch (type) {
case TYPE_BYTE:
return in.readByte();
case TYPE_SHORT:
return in.readShort();
case TYPE_INT:
return in.readInt();
case TYPE_LONG:
return in.readLong();
case TYPE_FLOAT:
return in.readFloat();
case TYPE_DOUBLE:
return in.readDouble();
case TYPE_CHAR:
return in.readChar();
case TYPE_BOOLEAN:
return in.readBoolean();
case TYPE_BYTE_ARR:
return in.readByteArray();
case TYPE_SHORT_ARR:
return in.readShortArray();
case TYPE_INT_ARR:
return in.readIntArray();
case TYPE_LONG_ARR:
return in.readLongArray();
case TYPE_FLOAT_ARR:
return in.readFloatArray();
case TYPE_DOUBLE_ARR:
return in.readDoubleArray();
case TYPE_CHAR_ARR:
return in.readCharArray();
case TYPE_BOOLEAN_ARR:
return in.readBooleanArray();
case TYPE_OBJ_ARR:
return in.readArray(Class.forName(in.readUTF(), false, in.classLoader()));
case TYPE_STR:
return in.readString();
case TYPE_ENUM:
return enumVals[in.readInt()];
case TYPE_UUID:
return in.readUuid();
case TYPE_PROPS:
return in.readProperties();
case TYPE_ARRAY_LIST:
return in.readArrayList();
case TYPE_HASH_MAP:
return in.readHashMap(false);
case TYPE_HASH_SET:
return in.readHashSet(mapFieldOff);
case TYPE_LINKED_LIST:
return in.readLinkedList();
case TYPE_LINKED_HASH_MAP:
return in.readLinkedHashMap(false);
case TYPE_LINKED_HASH_SET:
return in.readLinkedHashSet(mapFieldOff);
case TYPE_DATE:
return in.readDate();
case TYPE_CLS:
return classDescriptor(in.readInt(), in.classLoader(), ctx, mapper).describedClass();
case TYPE_EXTERNALIZABLE:
verifyChecksum(in.readShort());
return in.readExternalizable(constructor, readResolveMtd);
case TYPE_SERIALIZABLE:
verifyChecksum(in.readShort());
return in.readSerializable(cls, readObjMtds, readResolveMtd, fields);
default:
throw new IllegalStateException("Invalid class type: " + type);
}
}
/**
* @param checksum Checksum.
* @throws ClassNotFoundException If checksum is wrong.
* @throws IOException In case of error.
*/
private void verifyChecksum(short checksum) throws ClassNotFoundException, IOException {
if (checksum != this.checksum)
throw new ClassNotFoundException("Optimized stream class checksum mismatch " +
"(is same version of marshalled class present on all nodes?) " +
"[expected=" + this.checksum + ", actual=" + checksum + ", cls=" + cls + ']');
}
/**
* @param cls Class.
* @return Type.
*/
@SuppressWarnings("IfMayBeConditional")
private OptimizedFieldType fieldType(Class<?> cls) {
OptimizedFieldType type;
if (cls == byte.class)
type = OptimizedFieldType.BYTE;
else if (cls == short.class)
type = OptimizedFieldType.SHORT;
else if (cls == int.class)
type = OptimizedFieldType.INT;
else if (cls == long.class)
type = OptimizedFieldType.LONG;
else if (cls == float.class)
type = OptimizedFieldType.FLOAT;
else if (cls == double.class)
type = OptimizedFieldType.DOUBLE;
else if (cls == char.class)
type = OptimizedFieldType.CHAR;
else if (cls == boolean.class)
type = OptimizedFieldType.BOOLEAN;
else
type = OptimizedFieldType.OTHER;
return type;
}
/**
* Encapsulates data about class fields.
*/
@SuppressWarnings("PackageVisibleInnerClass")
static class Fields {
/** Fields. */
private final List<List<Field>> fields;
/** Fields offsets. */
private final List<List<T2<OptimizedFieldType, Long>>> fieldOffs;
/** Fields details lists. */
private final List<List<IgniteBiTuple<Integer, OptimizedFieldType>>> fieldInfoLists;
/** Fields details maps. */
private final List<Map<String, IgniteBiTuple<Integer, OptimizedFieldType>>> fieldInfoMaps;
/**
* Creates new instance.
*
* @param fields Fields.
* @param fieldOffs Field offsets.
* @param fieldInfoLists List of field details sequences for each type in the object's class hierarchy.
* @param fieldInfoMaps List of field details maps for each type in the object's class hierarchy.
*/
Fields(List<List<Field>> fields, List<List<T2<OptimizedFieldType, Long>>> fieldOffs,