-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathWireMarshaller.java
3056 lines (2729 loc) · 121 KB
/
WireMarshaller.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
/*
* Copyright 2016-2020 chronicle.software
*
* https://chronicle.software
*
* 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 net.openhft.chronicle.wire;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.bytes.HexDumpBytesDescription;
import net.openhft.chronicle.core.Jvm;
import net.openhft.chronicle.core.Maths;
import net.openhft.chronicle.core.OS;
import net.openhft.chronicle.core.UnsafeMemory;
import net.openhft.chronicle.core.io.*;
import net.openhft.chronicle.core.scoped.ScopedResource;
import net.openhft.chronicle.core.util.ClassLocal;
import net.openhft.chronicle.core.util.ClassNotFoundRuntimeException;
import net.openhft.chronicle.core.util.ObjectUtils;
import net.openhft.chronicle.core.util.StringUtils;
import net.openhft.chronicle.core.values.IntValue;
import net.openhft.chronicle.core.values.LongValue;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.reflect.*;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static net.openhft.chronicle.core.UnsafeMemory.*;
/**
* The WireMarshaller class is responsible for marshalling and unmarshalling of objects of type T.
* This class provides an efficient mechanism for serialization/deserialization using wire protocols.
* It utilizes field accessors to read and write values directly to and from the fields of the object.
*
* @param <T> The type of the object to be marshalled/unmarshalled.
*/
public class WireMarshaller<T> {
private static final Class[] UNEXPECTED_FIELDS_PARAMETER_TYPES = {Object.class, ValueIn.class};
private static final FieldAccess[] NO_FIELDS = {};
private static Method isRecord;
@NotNull
final FieldAccess[] fields;
// Map for quick field look-up based on their names.
final TreeMap<CharSequence, FieldAccess> fieldMap = new TreeMap<>(WireMarshaller::compare);
// Flag to determine if this marshaller is for a leaf class.
private final boolean isLeaf;
// Default value for the type T.
@Nullable
private final T defaultValue;
static {
if (Jvm.isJava14Plus()) {
try {
isRecord = Jvm.getMethod(Class.class, "isRecord");
} catch (Exception ignored) {
}
}
}
/**
* Constructs a new instance of the WireMarshaller with the specified parameters.
*
* @param tClass The class of the object to be marshalled.
* @param fields An array of field accessors that provide access to the fields of the object.
* @param isLeaf Indicates if the marshaller is for a leaf class.
*/
protected WireMarshaller(@NotNull Class<T> tClass, @NotNull FieldAccess[] fields, boolean isLeaf) {
this(fields, isLeaf, defaultValueForType(tClass));
}
// A class-local storage for caching WireMarshallers for different types.
// Depending on the type of class, it either creates a marshaller for exceptions or a generic one.
public static final ClassLocal<WireMarshaller> WIRE_MARSHALLER_CL = ClassLocal.withInitial
(tClass ->
Throwable.class.isAssignableFrom(tClass)
? WireMarshaller.ofThrowable(tClass)
: WireMarshaller.of(tClass)
);
WireMarshaller(@NotNull FieldAccess[] fields, boolean isLeaf, @Nullable T defaultValue) {
this.fields = fields;
this.isLeaf = isLeaf;
this.defaultValue = defaultValue;
for (FieldAccess field : fields) {
fieldMap.put(field.key.name(), field);
}
}
/**
* Factory method to create an instance of the WireMarshaller for a specific class type.
* Determines the appropriate marshaller type (basic or one that handles unexpected fields)
* based on the characteristics of the provided class.
*
* @param tClass The class type for which the marshaller is to be created.
* @return A new instance of WireMarshaller for the provided class type.
*/
@NotNull
public static <T> WireMarshaller<T> of(@NotNull Class<T> tClass) {
if (tClass.isInterface() || (tClass.isEnum() && !DynamicEnum.class.isAssignableFrom(tClass)))
return new WireMarshaller<>(tClass, NO_FIELDS, true);
T defaultObject = defaultValueForType(tClass);
@NotNull Map<String, Field> map = new LinkedHashMap<>();
getAllField(tClass, map);
final FieldAccess[] fields = map.values().stream()
// for Java 15+ strip "hidden" fields that can't be accessed in Java 15+ this way.
.filter(field -> !(Jvm.isJava15Plus() && field.getName().matches("^.*\\$\\d+$")))
.map(field -> FieldAccess.create(field, defaultObject))
.toArray(FieldAccess[]::new);
Map<String, Long> fieldCount = Stream.of(fields).collect(Collectors.groupingBy(f -> f.field.getName(), Collectors.counting()));
fieldCount.forEach((n, c) -> {
if (c > 1) Jvm.warn().on(tClass, "Has " + c + " fields called '" + n + "'");
});
List<FieldAccess> collect = Stream.of(fields)
.filter(WireMarshaller::leafable)
.collect(Collectors.toList());
boolean isLeaf = collect.isEmpty();
return overridesUnexpectedFields(tClass)
? new WireMarshallerForUnexpectedFields<>(fields, isLeaf, defaultObject)
: new WireMarshaller<>(fields, isLeaf, defaultObject);
}
/**
* Checks if the provided field accessor corresponds to a "leaf" entity.
* An entity is considered a leaf if it doesn't need to be further broken down in the serialization process.
*
* @param c The field accessor to be checked.
* @return {@code true} if the field accessor is leafable, {@code false} otherwise.
*/
protected static boolean leafable(FieldAccess c) {
Class<?> type = c.field.getType();
if (isCollection(type))
return !Boolean.TRUE.equals(c.isLeaf);
if (DynamicEnum.class.isAssignableFrom(type))
return false;
return WriteMarshallable.class.isAssignableFrom(type);
}
/**
* Determines if the provided class overrides the "unexpectedField" method from the ReadMarshallable interface.
*
* @param tClass The class type to be checked.
* @return {@code true} if the class overrides the "unexpectedField" method, {@code false} otherwise.
*/
private static <T> boolean overridesUnexpectedFields(Class<T> tClass) {
try {
Method method = tClass.getMethod("unexpectedField", UNEXPECTED_FIELDS_PARAMETER_TYPES);
return method.getDeclaringClass() != ReadMarshallable.class;
} catch (NoSuchMethodException e) {
return false;
}
}
/**
* Factory method to create an instance of the WireMarshaller for a Throwable class type.
* The method identifies fields that should be marshalled and prepares the marshaller accordingly.
*
* @param tClass The Throwable class type for which the marshaller is to be created.
* @return A new instance of WireMarshaller for the provided Throwable class type.
*/
@NotNull
private static <T> WireMarshaller<T> ofThrowable(@NotNull Class<T> tClass) {
T defaultObject = defaultValueForType(tClass);
@NotNull Map<String, Field> map = new LinkedHashMap<>();
getAllField(tClass, map);
final FieldAccess[] fields = map.values().stream()
.map(field -> FieldAccess.create(field, defaultObject)).toArray(FieldAccess[]::new);
boolean isLeaf = false;
return new WireMarshaller<>(fields, isLeaf, defaultObject);
}
/**
* Determines if the provided class is a collection type, including arrays, standard Collections, or Maps.
*
* @param c The class to be checked.
* @return {@code true} if the class is a collection type, {@code false} otherwise.
*/
private static boolean isCollection(@NotNull Class<?> c) {
return c.isArray() ||
Collection.class.isAssignableFrom(c) ||
Map.class.isAssignableFrom(c);
}
/**
* Recursively fetches all non-static, non-transient fields from the provided class and its superclasses,
* up to but not including Object or AbstractCommonMarshallable, and adds them to the provided map.
* Fields that are flagged for exclusion (e.g., the "ordinal" field for Enum types) are skipped.
*
* @param clazz The class type from which fields are to be extracted.
* @param map The map to populate with field names and their corresponding Field objects.
*/
public static void getAllField(@NotNull Class clazz, @NotNull Map<String, Field> map) {
if (clazz != Object.class && clazz != AbstractCommonMarshallable.class)
getAllField(clazz.getSuperclass(), map);
for (@NotNull Field field : clazz.getDeclaredFields()) {
if ((field.getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT)) != 0)
continue;
String fieldName = field.getName();
if (("ordinal".equals(fieldName) || "hash".equals(fieldName)) && Enum.class.isAssignableFrom(clazz))
continue;
String name = fieldName;
if (name.startsWith("this$0")) {
if (ValidatableUtil.validateEnabled())
Jvm.warn().on(WireMarshaller.class, "Found " + name + ", in " + clazz + " which will be ignored!");
continue;
}
Jvm.setAccessible(field);
map.put(name, field);
}
}
static <T> T defaultValueForType(@NotNull Class<T> tClass) {
// tClass = ObjectUtils.implementationToUse(tClass);
if (ObjectUtils.isConcreteClass(tClass)
&& !tClass.getName().startsWith("java")
&& !tClass.isEnum()
&& !tClass.isArray()) {
T t = ObjectUtils.newInstance(tClass);
Monitorable.unmonitor(t);
return t;
}
if (DynamicEnum.class.isAssignableFrom(tClass)) {
try {
T t = OS.memory().allocateInstance(tClass);
Jvm.getField(Enum.class, "name").set(t, "[unset]");
Jvm.getField(Enum.class, "ordinal").set(t, -1);
IOTools.unmonitor(t);
return t;
} catch (InstantiationException | IllegalAccessException e) {
throw new AssertionError(e);
}
}
return null;
}
/**
* Compares two CharSequences lexicographically. This is a character-by-character comparison
* that returns the difference of the first unmatched characters or the difference in their lengths
* if one sequence is a prefix of the other.
*
* @param cs0 The first CharSequence to be compared.
* @param cs1 The second CharSequence to be compared.
* @return A positive integer if {@code cs0} comes after {@code cs1},
* a negative integer if {@code cs0} comes before {@code cs1},
* or zero if the sequences are equal.
*/
private static int compare(CharSequence cs0, CharSequence cs1) {
for (int i = 0, len = Math.min(cs0.length(), cs1.length()); i < len; i++) {
int cmp = Character.compare(cs0.charAt(i), cs1.charAt(i));
if (cmp != 0)
return cmp;
}
return Integer.compare(cs0.length(), cs1.length());
}
/**
* Computes the actual type arguments for a given field of an interface. This method determines
* the generic type arguments that the field uses based on the interface's type parameters.
*
* @param iface The interface containing the field.
* @param field The field whose type arguments need to be determined.
* @return An array of actual type arguments or the interface's type parameters if no actual arguments can be deduced.
*/
private static Type[] computeActualTypeArguments(Class iface, Field field) {
Type[] actual = consumeActualTypeArguments(new HashMap<>(), iface, field.getGenericType());
if (actual == null)
return iface.getTypeParameters();
return actual;
}
/**
* Determines the actual type arguments used by a class or interface for a given interface type.
* This method recursively inspects the type hierarchy to match type arguments against the
* interface's type parameters. It uses a previously built map of type parameter names to their
* actual types to deduce the correct arguments for the given interface.
*
* @param prevTypeParameters A map containing previously discovered type parameter names
* mapped to their actual types.
* @param iface The interface for which we want to determine the type arguments.
* @param type The type to inspect. This could be an actual class, interface, or
* a parameterized type that uses generic arguments.
*
* @return An array of actual type arguments used by the provided type for the specified interface,
* or null if the type doesn't directly or indirectly implement or extend the given interface.
*/
private static Type[] consumeActualTypeArguments(Map<String, Type> prevTypeParameters, Class iface, Type type) {
Class cls = null;
Map<String, Type> typeParameters = new HashMap<>();
// If the type is a ParameterizedType, retrieve its actual type arguments and
// map them against the declared type parameters.
if (type instanceof ParameterizedType) {
ParameterizedType pType = (ParameterizedType) type;
Type[] typeArguments = pType.getActualTypeArguments();
cls = ((Class) ((ParameterizedType) type).getRawType());
TypeVariable<?>[] typeParamDecls = cls.getTypeParameters();
for (int i = 0; i < Math.min(typeParamDecls.length, typeArguments.length); i++) {
Type value;
// If the actual type argument is another type variable, try to get its actual type
// from the previously discovered type parameters.
if (typeArguments[i] instanceof TypeVariable) {
value = prevTypeParameters.get(((TypeVariable<?>) typeArguments[i]).getName());
// Use a bound type or Object.class if the actual type isn't found.
if (value == null) {
// Fail-safe.
Type[] bounds = ((TypeVariable<?>) typeArguments[i]).getBounds();
value = bounds.length == 0 ? Object.class : bounds[0];
}
} else {
value = typeArguments[i];
}
typeParameters.put(typeParamDecls[i].getName(), value);
}
} else if (type instanceof Class) {
// If the type is a Class (not a ParameterizedType), directly use it.
cls = (Class) type;
}
// If the provided type (or its raw type in case of a ParameterizedType) matches the target
// interface, map the discovered type arguments to the interface's type parameters and return them.
if (iface.equals(cls)) {
TypeVariable[] parameters = iface.getTypeParameters();
Type[] result = new Type[parameters.length];
for (int i = 0; i < result.length; i++) {
Type parameter = typeParameters.get(parameters[i].getName());
result[i] = parameter != null ? parameter : parameters[i];
}
return result;
}
// Recursively inspect the generic interfaces and superclass of the type to discover
// the actual type arguments for the given interface.
if (cls != null) {
for (Type ifaceType : cls.getGenericInterfaces()) {
Type[] result = consumeActualTypeArguments(typeParameters, iface, ifaceType);
if (result != null)
return result;
}
return consumeActualTypeArguments(typeParameters, iface, cls.getGenericSuperclass());
}
return null;
}
/**
* Excludes specified fields from the current marshaller and returns a new instance of the marshaller
* with the remaining fields.
*
* @param fieldNames Names of the fields to be excluded.
* @return A new instance of the with the specified fields excluded.
*/
public WireMarshaller<T> excludeFields(String... fieldNames) {
Set<String> fieldSet = new HashSet<>(Arrays.asList(fieldNames));
return new WireMarshaller(Stream.of(fields)
.filter(f -> !fieldSet.contains(f.field.getName()))
.toArray(FieldAccess[]::new),
isLeaf, defaultValue);
}
/**
* Writes the marshallable representation of the given object to the provided {@link WireOut} destination.
* This will traverse the fields and use their respective {@link FieldAccess} to write each field.
* The method also adjusts the hex dump indentation for better readability in the output.
*
* @param t The object to write.
* @param out The destination {@link WireOut} where the object representation will be written.
* @throws InvalidMarshallableException If the object fails validation checks before serialization.
*/
public void writeMarshallable(T t, @NotNull WireOut out) throws InvalidMarshallableException {
ValidatableUtil.validate(t);
HexDumpBytesDescription bytes = out.bytesComment();
bytes.adjustHexDumpIndentation(+1);
try {
for (@NotNull FieldAccess field : fields)
field.write(t, out);
} catch (IllegalAccessException e) {
throw new AssertionError(e);
}
bytes.adjustHexDumpIndentation(-1);
}
/**
* Writes the marshallable representation of the given object to the provided {@link Bytes} destination.
* Unlike the previous method, this doesn't adjust the hex dump indentation. It's a more direct
* serialization of the fields to bytes.
*
* @param t The object to write.
* @param bytes The destination {@link Bytes} where the object representation will be written.
*/
public void writeMarshallable(T t, Bytes<?> bytes) {
for (@NotNull FieldAccess field : fields) {
try {
field.getAsBytes(t, bytes);
} catch (IllegalAccessException e) {
throw new AssertionError(e);
}
}
}
/**
* @see #writeMarshallable(Object, WireOut, boolean)
* @deprecated To be removed in x.26
*/
@Deprecated(/* To be removed in x.26 */)
public void writeMarshallable(T t, @NotNull WireOut out, T ignored, boolean copy) throws InvalidMarshallableException {
writeMarshallable(t, out, copy);
}
/**
* Writes the values of the fields from the provided object (DTO) to the output. Before writing,
* the object is validated. The method also supports optional copying of the values
* from the source object to a previous instance.
*
* @param t Object whose field values are to be written.
* @param out Output destination where the field values are written to.
* @param copy Flag indicating whether to copy values from the source object to the previous object.
* @throws InvalidMarshallableException If there's an error during marshalling.
*/
public void writeMarshallable(T t, @NotNull WireOut out, boolean copy) throws InvalidMarshallableException {
// Validate the object before writing
ValidatableUtil.validate(t);
try {
// Iterate through all fields and write their values to the output
for (@NotNull FieldAccess field : fields) {
field.write(t, out, defaultValue, copy);
}
} catch (IllegalAccessException e) {
throw new AssertionError(e);
}
}
/**
* @see #readMarshallable(Object, WireIn, boolean)
* @deprecated To be removed in x.26
*/
@Deprecated(/* To be removed in x.26 */)
public void readMarshallable(T t, @NotNull WireIn in, T ignored, boolean overwrite) throws InvalidMarshallableException {
readMarshallable(t, in, overwrite);
}
/**
* Reads and populates the DTO based on the provided input. The input order can be hinted.
* After reading, the object is validated.
*
* @param t Object to populate with read values.
* @param in Input source from which values are read.
* @param overwrite Flag indicating whether to overwrite the existing value in the target object.
* @throws InvalidMarshallableException If there is an error during marshalling.
*/
public void readMarshallable(T t, @NotNull WireIn in, boolean overwrite) throws InvalidMarshallableException {
// Choose the reading method based on the hint
if (in.hintReadInputOrder())
readMarshallableInputOrder(t, in, overwrite);
else
readMarshallableDTOOrder(t, in, overwrite);
// Validate the object after reading
ValidatableUtil.validate(t);
}
/**
* @see #readMarshallableDTOOrder(Object, WireIn, boolean)
* @deprecated To be removed in x.26
*/
@Deprecated(/* To be removed in x.26 */)
public void readMarshallableDTOOrder(T t, @NotNull WireIn in, T ignored, boolean overwrite) throws InvalidMarshallableException {
readMarshallableDTOOrder(t, in, overwrite);
}
/**
* Reads and populates the DTO based on the provided order.
*
* @param t Target object to populate with read values.
* @param in Input source from which values are read.
* @param overwrite Flag indicating whether to overwrite the existing value in the target object.
* @throws InvalidMarshallableException If there is an error during marshalling.
*/
public void readMarshallableDTOOrder(T t, @NotNull WireIn in, boolean overwrite) throws InvalidMarshallableException {
try {
for (@NotNull FieldAccess field : fields) {
ValueIn vin = in.read(field.key);
field.readValue(t, defaultValue, vin, overwrite);
}
ValidatableUtil.validate(t);
} catch (IllegalAccessException e) {
throw new AssertionError(e);
}
}
/**
* @see #readMarshallableInputOrder(Object, WireIn, boolean)
* @deprecated To be removed in x.26
*/
@Deprecated(/* To be removed in x.26 */)
public void readMarshallableInputOrder(T t, @NotNull WireIn in, T ignored, boolean overwrite) throws InvalidMarshallableException {
readMarshallableDTOOrder(t, in, overwrite);
}
/**
* Reads and populates the DTO based on the input's order.
*
* @param t Target object to populate with read values.
* @param in Input source from which values are read.
* @param overwrite Flag indicating whether to overwrite the existing value in the target object.
* @throws InvalidMarshallableException If there is an error during marshalling.
*/
public void readMarshallableInputOrder(T t, @NotNull WireIn in, boolean overwrite) throws InvalidMarshallableException {
try (ScopedResource<StringBuilder> stlSb = Wires.acquireStringBuilderScoped()) {
StringBuilder sb = stlSb.get();
// Iterating over all fields to read their values
for (int i = 0; i < fields.length; i++) {
boolean more = in.hasMore();
FieldAccess field = fields[i];
ValueIn vin = more ? in.read(sb) : null;
// Check if fields are present and in order
if (more && matchesFieldName(sb, field)) {
field.readValue(t, defaultValue, in.getValueIn(), overwrite);
} else {
// If not, copy default values
for (; i < fields.length; i++) {
FieldAccess field2 = fields[i];
field2.setDefaultValue(defaultValue, t);
}
if (vin == null || sb.length() <= 0)
return;
// Read the next set of values if there are any left
do {
FieldAccess fieldAccess = fieldMap.get(sb);
if (fieldAccess == null)
vin.skipValue();
else
fieldAccess.readValue(t, defaultValue, vin, overwrite);
vin = in.read(sb);
} while (in.hasMore());
}
}
} catch (IllegalAccessException e) {
throw new AssertionError(e);
}
}
/**
* Checks if the given field name (represented by a StringBuilder) matches the field name of the provided {@link FieldAccess}.
* If the StringBuilder has a length of 0, it's assumed to match any field name.
*
* @param sb The StringBuilder containing the field name to be checked.
* @param field The {@link FieldAccess} whose field name needs to be matched against.
* @return True if the field name matches or if the StringBuilder is empty; False otherwise.
*/
public boolean matchesFieldName(StringBuilder sb, FieldAccess field) {
return sb.length() == 0 || StringUtils.equalsCaseIgnore(field.field.getName(), sb);
}
/**
* Writes the key representation of the given object to the provided {@link Bytes} destination.
* As per the assumption, only the first field (key) of the object is written.
*
* @param t The object whose key needs to be written.
* @param bytes The destination {@link Bytes} where the object's key representation will be written.
*/
public void writeKey(T t, Bytes<?> bytes) {
// assume one key for now.
try {
fields[0].getAsBytes(t, bytes);
} catch (IllegalAccessException e) {
throw new AssertionError(e);
}
}
/**
* Compares two objects field by field to determine their equality.
* Uses each field's {@link FieldAccess} to perform the equality check.
*
* @param o1 The first object to compare.
* @param o2 The second object to compare.
* @return True if all fields of both objects are equal; False if at least one field differs.
*/
public boolean isEqual(Object o1, Object o2) {
for (@NotNull FieldAccess field : fields) {
if (!field.isEqual(o1, o2))
return false;
}
return true;
}
/**
* Fetches the value of the specified field from the provided object.
*
* @param o The object from which the field value needs to be fetched.
* @param name The name of the field whose value is to be fetched.
* @return The value of the specified field from the object.
* @throws NoSuchFieldException If no field with the specified name is found in the object.
*/
public Object getField(Object o, String name) throws NoSuchFieldException {
try {
FieldAccess field = fieldMap.get(name);
if (field == null)
throw new NoSuchFieldException(name);
return field.field.get(o);
} catch (IllegalAccessException e) {
throw new AssertionError(e);
}
}
/**
* Retrieves the value of a specified field from the provided object and converts it to a long.
* If the field's type is not inherently long or int, it attempts a conversion using the ObjectUtils class.
*
* @param o The object from which the field value is to be retrieved.
* @param name The name of the field whose value needs to be fetched.
* @return The long value of the specified field.
* @throws NoSuchFieldException If no field with the specified name is found in the object.
*/
public long getLongField(@NotNull Object o, String name) throws NoSuchFieldException {
try {
FieldAccess field = fieldMap.get(name);
if (field == null)
throw new NoSuchFieldException(name);
Field field2 = field.field;
return field2.getType() == long.class
? field2.getLong(o)
: field2.getType() == int.class
? field2.getInt(o)
: ObjectUtils.convertTo(Long.class, field2.get(o));
} catch (IllegalAccessException e) {
throw new AssertionError(e);
}
}
/**
* Sets the value of a specified field in the provided object.
* If the type of the value does not directly match the field's type, it attempts a conversion using the ObjectUtils class.
*
* @param o The object in which the field's value needs to be set.
* @param name The name of the field whose value needs to be set.
* @param value The value to set to the field.
* @throws NoSuchFieldException If no field with the specified name is found in the object.
*/
public void setField(Object o, String name, Object value) throws NoSuchFieldException {
try {
FieldAccess field = fieldMap.get(name);
if (field == null)
throw new NoSuchFieldException(name);
@NotNull final Field field2 = field.field;
value = ObjectUtils.convertTo(field2.getType(), value);
field2.set(o, value);
} catch (IllegalAccessException e) {
throw new AssertionError(e);
}
}
/**
* Sets a long value to a specified field in the provided object.
* If the field's type is not inherently long or int, it attempts a conversion using the ObjectUtils class.
*
* @param o The object in which the field's value needs to be set.
* @param name The name of the field whose value needs to be set.
* @param value The long value to set to the field.
* @throws NoSuchFieldException If no field with the specified name is found in the object.
*/
public void setLongField(Object o, String name, long value) throws NoSuchFieldException {
try {
FieldAccess field = fieldMap.get(name);
if (field == null)
throw new NoSuchFieldException(name);
@NotNull final Field field2 = field.field;
if (field2.getType() == long.class)
field2.setLong(o, value);
else if (field2.getType() == int.class)
field2.setInt(o, (int) value);
else
field2.set(o, ObjectUtils.convertTo(field2.getType(), value));
} catch (IllegalAccessException e) {
throw new AssertionError(e);
}
}
/**
* Returns the default value of type T.
*
* @return The default value of type T.
*/
@Nullable
public T defaultValue() {
return defaultValue;
}
/**
* Resets the fields of the given object 'o' to the default value.
*
* @param o The object whose fields are to be reset to the default value.
*/
public void reset(T o) {
try {
for (FieldAccess field : fields)
field.setDefaultValue(defaultValue, o);
} catch (IllegalAccessException e) {
// should never happen as the types should match.
throw new AssertionError(e);
}
}
/**
* Checks if the current WireMarshaller is a leaf.
*
* @return true if the WireMarshaller is a leaf, false otherwise.
*/
public boolean isLeaf() {
return isLeaf;
}
/**
* Provides a field accessor that's specialized for handling fields which require
* conversion between integer values and string representations using a LongConverter.
*/
static class LongConverterFieldAccess extends FieldAccess {
// The LongConverter instance used for conversion operations.
@NotNull
private final LongConverter longConverter;
/**
* Constructor to initialize field access with a specific LongConverter.
*
* @param field The field being accessed.
* @param longConverter The converter to use for this field.
*/
LongConverterFieldAccess(@NotNull Field field, @NotNull LongConverter longConverter) {
super(field);
this.longConverter = longConverter;
}
/**
* Fetches the LongConverter instance associated with a given class.
* Tries to retrieve a static "INSTANCE" field or creates a new instance if not found.
*
* @param clazz The class which presumably has a LongConverter.
* @return The LongConverter instance.
*/
static LongConverter getInstance(Class clazz) {
try {
Field converterField = clazz.getDeclaredField("INSTANCE");
return (LongConverter) converterField.get(null);
} catch (NoSuchFieldException nsfe) {
return (LongConverter) ObjectUtils.newInstance(clazz);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
/**
* Reads the long value from an object and writes it using the provided ValueOut writer.
*
* @param o The source object.
* @param write The writer for output.
* @param previous The previous value (currently not used).
*/
@Override
protected void getValue(Object o, @NotNull ValueOut write, @Nullable Object previous) {
long aLong = getLong(o);
if (write.isBinary()) {
write.int64(aLong);
} else {
try (ScopedResource<StringBuilder> stlSb = Wires.acquireStringBuilderScoped()) {
StringBuilder sb = stlSb.get();
longConverter.append(sb, aLong);
if (!write.isBinary() && sb.length() == 0)
write.text("");
else if (longConverter.allSafeChars() || noUnsafeChars(sb))
write.rawText(sb);
else
write.text(sb);
}
}
}
private boolean noUnsafeChars(StringBuilder sb) {
int index = sb.length() - 1;
if (sb.charAt(0) == ' ' || sb.charAt(index) == ' ')
return false;
for (int i = 0; i < sb.length(); i++) {
if (":'\"#,".indexOf(sb.charAt(i)) >= 0)
return false;
}
return true;
}
/**
* Retrieves the long value from an object.
*
* @param o The object from which to retrieve the value.
* @return The long value of the field.
*/
protected long getLong(Object o) {
return unsafeGetLong(o, offset);
}
/**
* Sets the value of the object's field based on the provided ValueIn reader.
*
* @param o The target object.
* @param read The reader for input.
* @param overwrite Whether to overwrite existing values (currently not used).
*/
@Override
protected void setValue(Object o, @NotNull ValueIn read, boolean overwrite) {
long i;
if (read.isBinary()) {
i = read.int64();
} else {
try (ScopedResource<StringBuilder> stlSb = Wires.acquireStringBuilderScoped()) {
StringBuilder sb = stlSb.get();
read.text(sb);
i = longConverter.parse(sb);
if (!rangeCheck(i))
throw new IORuntimeException("value '" + sb + "' is out of range for a " + field.getType());
}
}
setLong(o, i);
}
/**
* Checks if the provided long value is within acceptable ranges.
*
* @param i The long value to check.
* @return True if the value is within range; otherwise, false.
*/
protected boolean rangeCheck(long i) {
return true;
}
/**
* Sets a long value to the field of an object.
*
* @param o The target object.
* @param i The value to set.
*/
protected void setLong(Object o, long i) {
unsafePutLong(o, offset, i);
}
/**
* Reads a string value from the object and writes its long representation to the provided bytes.
*
* @param o The source object.
* @param bytes The bytes to write the long representation to.
*/
@Override
public void getAsBytes(Object o, @NotNull Bytes<?> bytes) {
try (ScopedResource<StringBuilder> stlSb = Wires.acquireStringBuilderScoped()) {
StringBuilder sb = stlSb.get();
bytes.readUtf8(sb);
long i = longConverter.parse(sb);
bytes.writeLong(i);
}
}
/**
* Checks if two objects have the same long value for the accessed field.
*
* @param o First object.
* @param o2 Second object.
* @return True if values are the same; otherwise, false.
*/
@Override
protected boolean sameValue(Object o, Object o2) {
return getLong(o) == getLong(o2);
}
/**
* Copies the long value of the accessed field from one object to another.
*
* @param from Source object.
* @param to Destination object.
*/
@Override
protected void copy(Object from, Object to) {
setLong(to, getLong(from));
}
}
/**
* Abstract class to manage access to fields of objects.
* This class provides utility methods to read and write fields from/to objects.
*/
abstract static class FieldAccess {
@NotNull
final Field field;
final long offset;
@NotNull
final WireKey key;
Comment commentAnnotation;
Boolean isLeaf;
/**
* Constructor initializing field with given value.
*
* @param field Field to be accessed.
*/
FieldAccess(@NotNull Field field) {
this(field, null);
}
/**
* Constructor initializing field and isLeaf with given values.
*
* @param field Field to be accessed.
* @param isLeaf Flag to indicate whether the field is a leaf node.
*/
FieldAccess(@NotNull Field field, Boolean isLeaf) {
this.field = field;
offset = unsafeObjectFieldOffset(field);
key = field::getName;
this.isLeaf = isLeaf;
try {
commentAnnotation = Jvm.findAnnotation(field, Comment.class);
} catch (NullPointerException ignore) {
}
}
// ... (code continues)
/**
* Create a specific FieldAccess object based on the field type.
*
* @param field Field for which FieldAccess object is created.
* @return FieldAccess object specific to the field type.
*/
@Nullable
public static Object create(@NotNull Field field, @Nullable Object defaultObject) {
Class<?> type = field.getType();
try {
if (type.isArray()) {
if (type.getComponentType() == byte.class)
return new ByteArrayFieldAccess(field);
return new ArrayFieldAccess(field);
}
if (EnumSet.class.isAssignableFrom(type)) {
final Class componentType = extractClass(computeActualTypeArguments(EnumSet.class, field)[0]);
if (componentType == Object.class || Modifier.isAbstract(componentType.getModifiers()))
throw new RuntimeException("Could not get enum constant directory");
boolean isLeaf = !Throwable.class.isAssignableFrom(componentType)
&& WIRE_MARSHALLER_CL.get(componentType).isLeaf;
try {
Object[] values = (Object[]) Jvm.getMethod(componentType, "values").invoke(componentType, null);
return new EnumSetFieldAccess(field, isLeaf, values, componentType);
} catch (IllegalAccessException | InvocationTargetException e) {
throw Jvm.rethrow(e);
}
}
if (Collection.class.isAssignableFrom(type))
return CollectionFieldAccess.of(field);