-
Notifications
You must be signed in to change notification settings - Fork 237
Expand file tree
/
Copy pathFSTClazzInfo.java
More file actions
1074 lines (938 loc) · 36.6 KB
/
FSTClazzInfo.java
File metadata and controls
1074 lines (938 loc) · 36.6 KB
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
/*
* Copyright 2014 Ruediger Moeller.
*
* Licensed 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.nustaq.serialization;
import org.nustaq.serialization.annotations.*;
import org.nustaq.serialization.util.FSTMap;
import org.nustaq.serialization.util.FSTUtil;
import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;
/**
* Created with IntelliJ IDEA.
* User: Möller
* Date: 03.11.12
* Time: 13:08
* To change this template use File | Settings | File Templates.
*/
public final class FSTClazzInfo {
// cache constructor per class (big saving in permspace)
public static boolean BufferConstructorMeta = true;
// cache and share class.getDeclaredFields amongst all fstconfigs
public static boolean BufferFieldMeta = true;
/**
* cache + share j.reflect.Field. This can be cleared in case it gets too fat/leaks mem (e.g. class reloading)
*/
public static ConcurrentHashMap<Class,Field[]> sharedFieldSets = new ConcurrentHashMap<>();
public static final Comparator<FSTFieldInfo> defFieldComparator = new Comparator<FSTFieldInfo>() {
@Override
public int compare(FSTFieldInfo o1, FSTFieldInfo o2) {
int res = 0;
if ( o1.getVersion() != o2.getVersion() ) {
return o1.getVersion() < o2.getVersion() ? -1 : 1;
}
// order: version, boolean, primitives, conditionals, object references
if (o1.getType() == boolean.class && o2.getType() != boolean.class) {
return -1;
}
if (o1.getType() != boolean.class && o2.getType() == boolean.class) {
return 1;
}
if (o1.isConditional() && !o2.isConditional()) {
res = 1;
} else if (!o1.isConditional() && o2.isConditional()) {
res = -1;
} else if (o1.isPrimitive() && !o2.isPrimitive()) {
res = -1;
} else if (!o1.isPrimitive() && o2.isPrimitive())
res = 1;
// if (res == 0) // 64 bit / 32 bit issues
// res = (int) (o1.getMemOffset() - o2.getMemOffset());
if (res == 0)
res = o1.getType().getSimpleName().compareTo(o2.getType().getSimpleName());
if (res == 0)
res = o1.getName().compareTo(o2.getName());
if (res == 0) {
return o1.getField().getDeclaringClass().getName().compareTo(o2.getField().getDeclaringClass().getName());
}
return res;
}
};
Class[] predict;
private boolean ignoreAnn;
FSTMap<String, FSTFieldInfo> fieldMap;
Method writeReplaceMethod, readResolveMethod;
FSTMap<Class, FSTCompatibilityInfo> compInfo;
Object decoderAttached; // for decoders
public Object getDecoderAttached() {
return decoderAttached;
}
public void setDecoderAttached(Object decoderAttached) {
this.decoderAttached = decoderAttached;
}
boolean requiresCompatibleMode;
boolean externalizable;
boolean flat; // never share instances of this class
boolean isAsciiNameShortString = false;
boolean requiresInit = false;
boolean hasTransient;
volatile FSTObjectSerializer ser;
FSTFieldInfo fieldInfo[]; // serializable fields
Class clazz;
Object[] enumConstants;
Constructor cons;
int clzId = -1;
int structSize = 0;
FSTConfiguration conf;
protected FSTClassInstantiator instantiator; // initialized from FSTConfiguration in constructor
boolean crossPlatform;
public FSTClazzInfo(FSTConfiguration conf, Class clazz, FSTClazzInfoRegistry infoRegistry, boolean ignoreAnnotations) {
this.conf = conf; // fixme: historically was not bound to conf but now is. Remove redundant state + refs (note: may still be useful because of less pointerchasing)
crossPlatform = conf.isCrossPlatform();
this.clazz = clazz;
enumConstants = clazz.getEnumConstants();
ignoreAnn = ignoreAnnotations;
createFields(clazz);
instantiator = conf.getInstantiator(clazz);
if (Externalizable.class.isAssignableFrom(clazz)) {
externalizable = true;
cons = instantiator.findConstructorForExternalize(clazz);
} else if (Serializable.class.isAssignableFrom(clazz) || clazz == Object.class) {
externalizable = false;
cons = instantiator.findConstructorForSerializable(clazz);
} else {
if (!conf.isStructMode()) {
if ( conf.isForceSerializable() || getSer() != null ) {
externalizable = false;
cons = instantiator.findConstructorForSerializable(clazz);
} else {
throw new RuntimeException("Class " + clazz.getName() + " does not implement Serializable or externalizable");
}
} else {
cons = instantiator.findConstructorForSerializable(clazz);
}
}
if (!ignoreAnnotations) {
Predict annotation = (Predict) clazz.getAnnotation(Predict.class);
if (annotation != null) {
predict = annotation.value();
}
flat = clazz.isAnnotationPresent(Flat.class);
}
if (cons != null) {
cons.setAccessible(true);
}
final String name = clazz.getName();
if (name.length() < 127) {
isAsciiNameShortString = true;
for (int i = 0; i < name.length(); i++) {
if (name.charAt(i) > 127) {
isAsciiNameShortString = false;
break;
}
}
}
requiresInit = isExternalizable() || useCompatibleMode() || hasTransient || conf.isForceClzInit();
if (useCompatibleMode() && crossPlatform && getSer() == null && !clazz.isEnum())
throw new RuntimeException("cannot support legacy JDK serialization methods in crossplatform mode. Define a serializer for this class " + clazz.getName());
}
byte[] bufferedName;
public byte[] getBufferedName() {
if (bufferedName == null) {
bufferedName = getClazz().getName().getBytes();
}
return bufferedName;
}
@Override
public String toString() {
return "FSTClazzInfo{" +
"clazz=" + clazz +
'}';
}
public boolean isAsciiNameShortString() {
return isAsciiNameShortString;
}
public int getClzId() {
return clzId;
}
public void setClzId(int clzId) {
this.clzId = clzId;
}
public int getNumBoolFields() {
FSTFieldInfo[] fis = getFieldInfo();
for (int i = 0; i < fis.length; i++) {
FSTFieldInfo fstFieldInfo = fis[i];
if (fstFieldInfo.getType() != boolean.class) {
return i;
}
}
return fis.length;
}
public boolean isExternalizable() {
return externalizable;
}
public final boolean isFlat() {
return flat;
}
public final Class[] getPredict() {
return predict;
}
public final Object newInstance(boolean doesRequireInit) {
return instantiator.newInstance(clazz, cons, doesRequireInit || requiresInit, conf.isForceSerializable() );
}
/**
* Sideeffect: sets hasTransient
*
* @param c
* @param res
* @return
*/
static ReentrantLock shareLock = new ReentrantLock(false);
public final List<Field> getAllFields(Class c, List<Field> res) {
try {
if ( BufferFieldMeta ) {
shareLock.lock();
}
if (res == null) {
res = new ArrayList<Field>();
}
if (c == null) {
return res;
}
Field[] declaredFields = BufferFieldMeta && !conf.isStructMode() ? sharedFieldSets.get(c) : null;
if (declaredFields == null) {
declaredFields = c.getDeclaredFields();
if (BufferFieldMeta && !conf.isStructMode())
sharedFieldSets.put(c, declaredFields);
}
List<Field> c1 = Arrays.asList(declaredFields);
Collections.reverse(c1);
for (int i = 0; i < c1.size(); i++) {
Field field = c1.get(i);
res.add(0, field);
}
for (int i = 0; i < res.size(); i++) {
Field field = res.get(i);
if (Modifier.isStatic(field.getModifiers()) || isTransient(c, field)) {
if (isTransient(c, field)) {
hasTransient = true;
}
res.remove(i);
i--;
}
}
List<Field> allFields = getAllFields(c.getSuperclass(), res);
return new ArrayList<>(allFields);
} finally {
if ( BufferFieldMeta )
shareLock.unlock();
}
}
private boolean isTransient(Class c, Field field) {
if (Modifier.isTransient(field.getModifiers()))
return true;
while (c.getName().indexOf("$") >= 0) {
c = c.getSuperclass(); // patch fuer reallive queries, kontraktor spore
}
if ( field.getName().startsWith("this$") && c.getAnnotation(AnonymousTransient.class) != null )
return true;
return (c.getAnnotation(Transient.class) != null && field.getAnnotation(Serialize.class) == null);
}
public final FSTFieldInfo[] getFieldInfo() {
return fieldInfo;
}
public final FSTFieldInfo[] getFieldInfoFiltered(Class... toRemove) {
FSTFieldInfo[] fis = getFieldInfo();
int count = 0;
for (int i = 0; i < fis.length; i++) {
FSTFieldInfo fi = fis[i];
boolean skip = false;
for (int j = 0; j < toRemove.length; j++) {
Class aClass = toRemove[j];
if (fi.getField().getDeclaringClass() == aClass) {
skip = true;
break;
}
}
if (!skip) {
count++;
}
}
FSTFieldInfo res[] = new FSTFieldInfo[count];
count = 0;
for (int i = 0; i < fis.length; i++) {
FSTFieldInfo fi = fis[i];
boolean skip = false;
for (int j = 0; j < toRemove.length; j++) {
Class aClass = toRemove[j];
if (fi.getField().getDeclaringClass() == aClass) {
skip = true;
break;
}
}
if (!skip) {
res[count++] = fis[i];
}
}
return res;
}
public final FSTFieldInfo getFieldInfo(String name, Class declaringClass) {
if ( fieldMap != null ) {
if (declaringClass == null) {
return fieldMap.get(name);
}
return fieldMap.get(declaringClass.getName() + "#" + name); //FIXME: THIS IS VERY SLOW (only used by JSON / compatibility mode)
} else {
synchronized (this) {
fieldMap = buildFieldMap();
return getFieldInfo(name,declaringClass);
}
}
}
private FSTMap buildFieldMap() {
FSTMap res = new FSTMap<>(fieldInfo.length);
for (int i = 0; i < fieldInfo.length; i++) {
Field field = fieldInfo[i].getField();
if ( field != null ) {
res.put(field.getDeclaringClass().getName() + "#" + field.getName(), fieldInfo[i]);
res.put(field.getName(), fieldInfo[i]);
}
}
return res;
}
private void createFields(Class c) {
if (c.isInterface() || c.isPrimitive()) {
return;
}
List<Field> fields = getAllFields(c, null);
fieldInfo = new FSTFieldInfo[fields.size()];
for (int i = 0; i < fields.size(); i++) {
Field field = fields.get(i);
fieldInfo[i] = createFieldInfo(field);
}
// compatibility info sort order
Comparator<FSTFieldInfo> infocomp = new Comparator<FSTFieldInfo>() {
@Override
public int compare(FSTFieldInfo o1, FSTFieldInfo o2) {
int res = 0;
res = o1.getType().getSimpleName().compareTo(o2.getType().getSimpleName());
if (res == 0)
res = o1.getType().getName().compareTo(o2.getType().getName());
if (res == 0) {
Class declaringClass = o1.getType().getDeclaringClass();
Class declaringClass1 = o2.getType().getDeclaringClass();
if (declaringClass == null && declaringClass1 == null) {
return 0;
}
if (declaringClass != null && declaringClass1 == null) {
return 1;
}
if (declaringClass == null && declaringClass1 != null) {
return -1;
}
if (res == 0) {
return declaringClass.getName().compareTo(declaringClass1.getName());
}
}
return res;
}
};
// check if we actually need to build up compatibility info (memory intensive)
boolean requiresCompatibilityData = false;
if ( ! Externalizable.class.isAssignableFrom(c) && getSerNoStore() == null ) {
Class tmpCls = c;
while( tmpCls != Object.class ) {
if ( FSTUtil.findPrivateMethod(tmpCls, "writeObject", new Class<?>[]{ObjectOutputStream.class}, Void.TYPE) != null ||
FSTUtil.findPrivateMethod(tmpCls, "readObject", new Class<?>[]{ObjectInputStream.class},Void.TYPE) != null ||
FSTUtil.findDerivedMethod(tmpCls, "writeReplace", null, Object.class) != null ||
FSTUtil.findDerivedMethod(tmpCls, "readResolve", null, Object.class) != null ) {
requiresCompatibilityData = true;
break;
}
tmpCls = tmpCls.getSuperclass();
}
}
if (!conf.isStructMode() && requiresCompatibilityData ) {
getCompInfo();
fieldMap = buildFieldMap();
Class curCl = c;
fields.clear();
while (curCl != Object.class) {
ObjectStreamClass os = null;
try {
os = ObjectStreamClass.lookup(curCl);
} catch (Exception e) {
FSTUtil.<RuntimeException>rethrow(e);
}
if (os != null) {
final ObjectStreamField[] fi = os.getFields();
List<FSTFieldInfo> curClzFields = new ArrayList<FSTFieldInfo>();
if (fi != null) {
for (int i = 0; i < fi.length; i++) {
ObjectStreamField objectStreamField = fi[i];
String ff = objectStreamField.getName();
final FSTFieldInfo fstFieldInfo = fieldMap.get(curCl.getName() + "#" + ff);
if (fstFieldInfo != null && fstFieldInfo.getField() != null) {
curClzFields.add(fstFieldInfo);
fields.add(fstFieldInfo.getField());
} else {
FSTFieldInfo fake = new FSTFieldInfo(null, null, true );
fake.type = objectStreamField.getType();
fake.fakeName = objectStreamField.getName();
curClzFields.add(fake);
}
}
}
Collections.sort(curClzFields, infocomp);
FSTCompatibilityInfo info = new FSTCompatibilityInfo(curClzFields, curCl);
getCompInfo().put(curCl, info);
if (info.needsCompatibleMode()) {
requiresCompatibleMode = true;
}
}
curCl = curCl.getSuperclass();
}
}
// default sort order
Comparator<FSTFieldInfo> comp = defFieldComparator;
if (!conf.isStructMode())
Arrays.sort(fieldInfo, comp);
int off = 8; // object header: length + clzId
for (int i = 0; i < fieldInfo.length; i++) {
FSTFieldInfo fstFieldInfo = fieldInfo[i];
fstFieldInfo.setStructOffset(off);
off += fstFieldInfo.getStructSize();
}
structSize = off;
writeReplaceMethod = FSTUtil.findDerivedMethod(
c, "writeReplace", null, Object.class);
readResolveMethod = FSTUtil.findDerivedMethod(
c, "readResolve", null, Object.class);
if (writeReplaceMethod != null) {
writeReplaceMethod.setAccessible(true);
}
if (readResolveMethod != null) {
readResolveMethod.setAccessible(true);
}
for (int i = 0; i < fieldInfo.length; i++) {
FSTFieldInfo fstFieldInfo = fieldInfo[i];
fstFieldInfo.indexId = i;
}
}
public int getStructSize() {
return structSize;
}
public boolean useCompatibleMode() {
return requiresCompatibleMode || writeReplaceMethod != null || readResolveMethod != null;
}
static AtomicInteger fiCount = new AtomicInteger(0);
static AtomicInteger missCount = new AtomicInteger(0);
protected FSTFieldInfo createFieldInfo(Field field) {
FSTConfiguration.FieldKey key = null;
if ( conf.fieldInfoCache != null ) {
key = new FSTConfiguration.FieldKey(field.getDeclaringClass(), field.getName());
FSTFieldInfo res = conf.fieldInfoCache.get(key);
if ( res != null ) {
fiCount.incrementAndGet();
return res;
}
}
field.setAccessible(true);
Predict predict = crossPlatform ? null : field.getAnnotation(Predict.class); // needs to be iognored cross platform
FSTFieldInfo result = new FSTFieldInfo(predict != null ? predict.value() : null, field, ignoreAnn);
if ( conf.fieldInfoCache != null && key != null ) {
conf.fieldInfoCache.put(key,result);
}
missCount.incrementAndGet();
return result;
}
public final Method getReadResolveMethod() {
return readResolveMethod;
}
public final Method getWriteReplaceMethod() {
return writeReplaceMethod;
}
public final Class getClazz() {
return clazz;
}
public Object[] getEnumConstants() {
return enumConstants;
}
public FSTMap<Class, FSTCompatibilityInfo> getCompInfo() {
if (compInfo == null)
compInfo = new FSTMap<>(3); // just avoid edge case NPE's
return compInfo;
}
public final static class FSTFieldInfo {
final public static int BOOL = 1;
final public static int BYTE = 2;
final public static int CHAR = 3;
final public static int SHORT = 4;
final public static int INT = 5;
final public static int LONG = 6;
final public static int FLOAT = 7;
final public static int DOUBLE = 8;
Class possibleClasses[];
FSTClazzInfo lastInfo; // cache last class stored (can save a hash lookup)
String oneOf[] = null;
int arrayDim;
Class arrayType;
boolean flat = false;
boolean isConditional = false;
final Field field;
Class type;
boolean integral = false;
boolean primitive = false;
boolean isArr = false;
byte version;
int integralType;
long memOffset = -1;
boolean isAndroid = FSTConfiguration.isAndroid; // hope for better locality
int structOffset = 0;
int indexId; // position in serializable fields array
int align = 0;
int alignPad = 0;
Object bufferedName; // cache byte rep of field name (used for cross platform)
// hack required for compatibility with ancient JDK mechanics (cross JDK, e.g. Android <=> OpenJDK ).
// in rare cases, a field used in putField is not present as a real field
// in this case only these of a fieldinfo are set
public String fakeName;
public FSTFieldInfo(Class[] possibleClasses, Field fi, boolean ignoreAnnotations) {
this.possibleClasses = possibleClasses;
field = fi;
if (fi == null) {
isArr = false;
} else {
isArr = field.getType().isArray();
type = fi.getType();
primitive = type.isPrimitive();
if (FSTUtil.unFlaggedUnsafe != null ) {
fi.setAccessible(true);
if (!Modifier.isStatic(fi.getModifiers())) {
try {
memOffset = (int) FSTUtil.unFlaggedUnsafe.objectFieldOffset(fi);
} catch (Throwable th) {
//throw FSTUtil.rethrow(th);
}
}
}
}
if (isArray()) {
String clName = field.getType().getName();
arrayDim = 1 + clName.lastIndexOf('[');
arrayType = calcComponentType(field.getType());
}
calcIntegral();
if (fi != null && !ignoreAnnotations) {
version = (byte) (fi.isAnnotationPresent(Version.class) ? fi.getAnnotation(Version.class).value() : 0);
flat = fi.isAnnotationPresent(Flat.class);
isConditional = fi.isAnnotationPresent(Conditional.class);
if (isIntegral()) {
isConditional = false;
}
OneOf annotation = fi.getAnnotation(OneOf.class);
if (annotation != null) {
oneOf = annotation.value();
}
}
}
public byte getVersion() {
return version;
}
public Object getBufferedName() {
return bufferedName;
}
public void setBufferedName(Object bufferedName) {
this.bufferedName = bufferedName;
}
public int align(int off) {
while ((off / align) * align != off)
off++;
return off;
}
public int getIndexId() {
return indexId;
}
public int getStructOffset() {
return structOffset;
}
public void setStructOffset(int structOffset) {
this.structOffset = structOffset;
}
public String[] getOneOf() {
return oneOf;
}
public long getMemOffset() {
return memOffset;
}
public int getAlign() {
return align;
}
public int getAlignPad() {
return alignPad;
}
public boolean isConditional() {
return isConditional;
}
public FSTClazzInfo getLastInfo() {
return lastInfo;
}
public void setLastInfo(FSTClazzInfo lastInfo) {
this.lastInfo = lastInfo;
}
Class calcComponentType(Class c) {
if (c.isArray()) {
return calcComponentType(c.getComponentType());
}
return c;
}
public boolean isVolatile() {
return Modifier.isVolatile(getField().getModifiers());
}
public final Class getType() {
return type;
}
public boolean isArray() {
return isArr;
}
public int getArrayDepth() {
return arrayDim;
}
public Class getArrayType() {
return arrayType;
}
public Class[] getPossibleClasses() {
return possibleClasses;
}
void setPossibleClasses(Class[] possibleClasses) {
this.possibleClasses = possibleClasses;
}
public Field getField() {
return field;
}
public void calcIntegral() {
if (field == null) {
return;
}
if (isArray()) {
integral = isIntegral(getArrayType());
} else {
integral = isIntegral(field.getType());
Class type = field.getType();
integralType = getIntegralCode(type);
}
}
public static int getIntegralCode(Class type) {
if (type == boolean.class) {
return BOOL;
} else if (type == byte.class) {
return BYTE;
} else if (type == char.class) {
return CHAR;
} else if (type == short.class) {
return SHORT;
} else if (type == int.class) {
return INT;
} else if (type == long.class) {
return LONG;
} else if (type == float.class) {
return FLOAT;
} else if (type == double.class) {
return DOUBLE;
}
return 0;
}
/**
* only set if is not an array, but a direct native field type
*
* @return
*/
public int getIntegralType() {
return integralType;
}
public boolean isIntegral(Class type) {
return type.isPrimitive();
}
/**
* @return wether this is primitive or an array of primitives
*/
public boolean isIntegral() {
return integral;
}
public String getDesc() {
return field != null ? "<" + field.getName() + " of " + field.getDeclaringClass().getSimpleName() + ">" : "<undefined referencee>";
}
public String toString() {
return getDesc();
}
public boolean isFlat() {
return flat;
}
public int getComponentStructSize() {
if (arrayType == boolean.class || arrayType == byte.class)
return 1;
if (arrayType == char.class || arrayType == short.class)
return 2;
if (arrayType == int.class || arrayType == float.class)
return 4;
if (arrayType == long.class || arrayType == double.class)
return 8;
return 0; // object => cannot decide
}
public int getStructSize() {
if (type == boolean.class || type == byte.class)
return 1;
if (type == char.class || type == short.class)
return 2;
if (type == int.class || type == float.class)
return 4;
if (type == long.class || type == double.class)
return 8;
if (isArray()) {
if (isIntegral())
return 8; // pointer+length
else // object array
return 16; // pointer+length+elemsiz+pointertype
}
return 4;
}
public boolean isPrimitive() {
return primitive;
}
public final int getByteValue(Object obj) throws IllegalAccessException {
if (!isAndroid && memOffset >= 0) {
return FSTUtil.unFlaggedUnsafe.getByte(obj, memOffset);
}
return field.getByte(obj);
}
public final int getCharValue(Object obj) throws IllegalAccessException {
if (!isAndroid && memOffset >= 0) {
return FSTUtil.unFlaggedUnsafe.getChar(obj, memOffset);
}
return field.getChar(obj);
}
public final int getShortValue(Object obj) throws IllegalAccessException {
if (!isAndroid && memOffset >= 0) {
return FSTUtil.unFlaggedUnsafe.getShort(obj, memOffset);
}
return field.getShort(obj);
}
public final int getIntValueUnsafe(Object obj) throws IllegalAccessException {
return FSTUtil.unFlaggedUnsafe.getInt(obj, memOffset);
}
public final long getLongValueUnsafe(Object obj) throws IllegalAccessException {
return FSTUtil.unFlaggedUnsafe.getLong(obj, memOffset);
}
public final boolean getBooleanValue(Object obj) throws IllegalAccessException {
if (!isAndroid && memOffset >= 0) {
return FSTUtil.unFlaggedUnsafe.getBoolean(obj, memOffset);
}
return field.getBoolean(obj);
}
/**
* Warning: crashes if not an object ref !
* use getField().get() for a safe version ..
*
* @param obj
* @return
* @throws IllegalAccessException
*/
public final Object getObjectValue(Object obj) throws IllegalAccessException {
if (!isAndroid && memOffset >= 0) {
return FSTUtil.unFlaggedUnsafe.getObject(obj, memOffset);
}
return field.get(obj);
}
public final float getFloatValue(Object obj) throws IllegalAccessException {
if (!isAndroid && memOffset >= 0) {
return FSTUtil.unFlaggedUnsafe.getFloat(obj, memOffset);
}
return field.getFloat(obj);
}
public final void setCharValue(Object newObj, char c) throws IllegalAccessException {
if (!isAndroid && memOffset >= 0) {
FSTUtil.unFlaggedUnsafe.putChar(newObj, memOffset, c);
return;
}
field.setChar(newObj, c);
}
public final void setShortValue(Object newObj, short i1) throws IllegalAccessException {
if (!isAndroid && memOffset >= 0) {
FSTUtil.unFlaggedUnsafe.putShort(newObj, memOffset, i1);
return;
}
field.setShort(newObj, i1);
}
public final void setObjectValue(Object target, Object value) throws IllegalAccessException {
if (!isAndroid && memOffset >= 0) {
FSTUtil.unFlaggedUnsafe.putObject(target, memOffset, value);
return;
}
field.set(target, value);
}
public final void setFloatValue(Object newObj, float l) throws IllegalAccessException {
if (!isAndroid && memOffset >= 0) {
FSTUtil.unFlaggedUnsafe.putFloat(newObj, memOffset, l);
return;
}
field.setFloat(newObj, l);
}
public final void setDoubleValue(Object newObj, double l) throws IllegalAccessException {
if (!isAndroid && memOffset >= 0) {
FSTUtil.unFlaggedUnsafe.putDouble(newObj, memOffset, l);
return;
}
field.setDouble(newObj, l);
}
public final void setLongValue(Object newObj, long i1) throws IllegalAccessException {
if (!isAndroid && memOffset >= 0) {
FSTUtil.unFlaggedUnsafe.putLong(newObj, memOffset, i1);
return;
}
field.setLong(newObj, i1);
}
public final long getLongValue(Object obj) throws IllegalAccessException {
if (!isAndroid && memOffset >= 0) {
return FSTUtil.unFlaggedUnsafe.getLong(obj, memOffset);
}
return field.getLong(obj);
}
public final double getDoubleValue(Object obj) throws IllegalAccessException {
if (!isAndroid && memOffset >= 0) {
return FSTUtil.unFlaggedUnsafe.getDouble(obj, memOffset);
}
return field.getDouble(obj);
}
public final void setIntValue(Object newObj, int i1) throws IllegalAccessException {
if (!isAndroid && memOffset >= 0) {
FSTUtil.unFlaggedUnsafe.putInt(newObj, memOffset, i1);
return;
}
field.setInt(newObj, i1);
}
public final int getIntValue(Object obj) throws IllegalAccessException {
if (!isAndroid && memOffset >= 0) {
return FSTUtil.unFlaggedUnsafe.getInt(obj, memOffset);
}
return field.getInt(obj);
}
public final void setBooleanValue(Object newObj, boolean i1) throws IllegalAccessException {
if (!isAndroid && memOffset >= 0) {
FSTUtil.unFlaggedUnsafe.putBoolean(newObj, memOffset, i1);
return;
}
field.setBoolean(newObj, i1);
}
public final void setByteValue(Object newObj, byte b) throws IllegalAccessException {
if (!isAndroid && memOffset >= 0) {
FSTUtil.unFlaggedUnsafe.putByte(newObj, memOffset, b);
return;
}
field.setByte(newObj, b);
}
public String getName() {
return field != null ? field.getName() : fakeName;
}
}
/**
* sideeffecting: if no ser is found, next lookup will return null immediate
* @return
*/
public FSTObjectSerializer getSer() {
FSTObjectSerializer serializer = ser;
if (serializer == null) {
if (clazz == null) {
return null;
}
serializer = getSerNoStore();
if (serializer == null) {
ser = FSTSerializerRegistry.NULL;
return null;
}
ser = serializer;
} else if (serializer == FSTSerializerRegistry.NULL) {
return null;
}
return serializer;
}
// no sideffecting lookup
public FSTObjectSerializer getSerNoStore() {
return conf.getCLInfoRegistry().getSerializerRegistry().getSerializer(clazz);
}
static class FSTCompatibilityInfo {