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
|
package org.uic.barcode.asn1.uper;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayDeque;
import java.util.Deque;
import org.uic.barcode.asn1.datatypes.Asn1Default;
import org.uic.barcode.asn1.datatypes.Asn1SequenceOf;
import org.uic.barcode.asn1.datatypes.Sequence;
import org.uic.barcode.asn1.uper.UperEncoder.Asn1ContainerFieldSorter;
class SequenceCoder implements Decoder, Encoder {
@Override public <T> boolean canEncode(T obj, Annotation[] extraAnnotations) {
Class<?> type = obj.getClass();
AnnotationStore annotations = new AnnotationStore(type.getAnnotations(), extraAnnotations);
return annotations.getAnnotation(Sequence.class) != null;
}
@Override public <T> void encode(BitBuffer bitbuffer, T obj, Annotation[] extraAnnotations) throws Asn1EncodingException {
Class<?> type = obj.getClass();
AnnotationStore annotations = new AnnotationStore(type.getAnnotations(), extraAnnotations);
String pos = String.format("%d.%d", bitbuffer.position()/8 , bitbuffer.position() % 8);
UperEncoder.logger.debug(String.format("Position %s: SEQUENCE %s", pos, type.getSimpleName()));
Asn1ContainerFieldSorter sorter = new Asn1ContainerFieldSorter(type);
try {
if (UperEncoder.hasExtensionMarker(annotations)) {
boolean extensionsPresent = !sorter.extensionFields.isEmpty()
&& UperEncoder.hasNonNullExtensions(obj, sorter);
UperEncoder.logger.debug(String.format("with extension marker, %s extensions, extensionBit: <%s>",
extensionsPresent ? "with" : "without", extensionsPresent));
bitbuffer.put(extensionsPresent);
}
// Bitmask for optional fields.
for (Field f : sorter.optionalOrdinaryFields) {
boolean fieldPresent = isPresent(f, f.get(obj));
UperEncoder.logger.debug(String.format("with optional field %s %s, presence encoded as bit <%s>",
f.getName(), fieldPresent ? "present" : "absent", fieldPresent));
bitbuffer.put(fieldPresent); // null means the field is absent.
}
// All ordinary fields (fields within extension root).
for (Field f : sorter.ordinaryFields) {
//CG do not include default values
if (UperEncoder.isMandatory(f) || isPresent(f,f.get(obj))) {
pos = String.format("Position: %d.%d", bitbuffer.position()/8 , bitbuffer.position() % 8);
UperEncoder.logger.debug(String.format("%s: Field %s", pos, f.getName()));
try {
UperEncoder.encode2(bitbuffer, f.get(obj), f.getAnnotations());
} catch (Asn1EncodingException e) {
throw new Asn1EncodingException("." + f.getName(), e);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Illegal value for field " + f.getName(), e);
}
}
}
// Extension fields.
if (UperEncoder.hasExtensionMarker(annotations)
&& !sorter.extensionFields.isEmpty()
&& UperEncoder.hasNonNullExtensions(obj, sorter)) {
// Total extensions count.
int numExtensions = sorter.extensionFields.size();
UperEncoder.logger.debug(String.format("continuing sequence : %d extension(s) are present, encoding length determinant for them...", numExtensions));
UperEncoder.encodeLengthOfBitmask(bitbuffer, numExtensions);
// Bitmask for present extensions.
for (Field f : sorter.extensionFields) {
boolean fieldIsPresent = isPresent(f,f.get(obj));
UperEncoder.logger.debug(String.format("Extension %s is %s, presence encoded as <%s>", f.getName(),
fieldIsPresent ? "present" : "absent", fieldIsPresent ? "1" : "0"));
bitbuffer.put(fieldIsPresent);
}
// Values of extensions themselves.
for (Field f : sorter.extensionFields) {
//CG do not encode default values
if (UperEncoder.isMandatory(f) || isPresent(f,f.get(obj))) {
UperEncoder.logger.debug(String.format("Encoding extension field %s", f.getName()));
try {
UperEncoder.encodeAsOpenType(bitbuffer, f.get(obj), f.getAnnotations());
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Illegal value for extension field " + f.getName(), e);
}
}
}
}
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new IllegalArgumentException("can't encode " + obj, e);
}
sorter.revertAccess();
}
@SuppressWarnings("unchecked")
protected <T> boolean isPresent(Field f, Object fieldObject){
if (fieldObject == null) return false;
boolean fieldPresent = fieldObject != null;
if (fieldObject instanceof Asn1SequenceOf) {
if (((Asn1SequenceOf<T>)fieldObject).size() == 0){
//CG do not encode optional empty sequences
fieldPresent = false;
}
}
if (fieldObject instanceof String) {
if (((String)fieldObject).length() == 0){
//CG do not encode optional empty sequences
fieldPresent = false;
}
}
//CG DEFAULT VALUES
if (fieldPresent && UperEncoder.isDefault(f,fieldObject)) {
UperEncoder.logger.debug(String.format("Field %s has default value", f.getName()));
fieldPresent = false;
}
//CG No ASN1
if (UperEncoder.isNotAsn1(f)) {
fieldPresent = false;
}
return fieldPresent;
}
@Override public <T> boolean canDecode(Class<T> classOfT, Annotation[] extraAnnotations) {
AnnotationStore annotations = new AnnotationStore(classOfT.getAnnotations(),
extraAnnotations);
return annotations.getAnnotation(Sequence.class) != null;
}
@Override public <T> T decode(BitBuffer bitbuffer,
Class<T> classOfT,Field f1,
Annotation[] extraAnnotations) {
UperEncoder.logger.debug(String.format("decode SEQUENCE %s",classOfT.getSimpleName()));
AnnotationStore annotations = new AnnotationStore(classOfT.getAnnotations(),extraAnnotations);
T result = UperEncoder.instantiate(classOfT);
Asn1ContainerFieldSorter sorter = new Asn1ContainerFieldSorter(classOfT);
boolean hasExtensionMarker = UperEncoder.hasExtensionMarker(annotations);
boolean extensionPresent = false;
if (hasExtensionMarker) {
extensionPresent = bitbuffer.get();
UperEncoder.logger.debug(String.format("with extension marker, extension %s", extensionPresent ? "present!" : "absent"));
}
// Bitmask for optional fields.
Deque<Boolean> optionalFieldsMask = new ArrayDeque<>(sorter.optionalOrdinaryFields.size());
for (Field f : sorter.optionalOrdinaryFields) {
optionalFieldsMask.add(bitbuffer.get());
UperEncoder.logger.debug(String.format("with optional field %s %s" , f.getName() , optionalFieldsMask.getLast() ? "present" : "absent"));
}
// All ordinary fields (fields within extension root).
for (Field f : sorter.ordinaryFields) {
if (!UperEncoder.isTestInstrumentation(f) && (UperEncoder.isMandatory(f)
||
(UperEncoder.isOptional(f) && optionalFieldsMask.pop()))) {
UperEncoder.logger.debug(String.format("Field : %s", f.getName()));
try {
f.set(result, UperEncoder.decodeAny(bitbuffer,f.getType(),f, f.getAnnotations()));
} catch (IllegalAccessException e) {
throw new IllegalArgumentException("can't access 'set method' for field " + f + " of class " + classOfT + " " + e, e);
}
} else {
//CG might have a default value
if (f.getAnnotation(Asn1Default.class) != null) {
try {
UperEncoder.logger.debug(String.format(String.format("Retrieve default for element : %s",f.getName())));
f.set(result,UperEncoder.getDefault(f.getType(),f.getAnnotations()));
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("can't decode " + classOfT, e);
} catch (IllegalAccessException e) {
throw new IllegalArgumentException("can't decode " + classOfT, e);
}
}
}
}
if (!hasExtensionMarker) {
//done
sorter.revertAccess();
return result;
}
// Possible extensions
int numExtensions = 0;
if (UperEncoder.hasExtensionMarker(annotations)){
if (extensionPresent) {
// Number of extensions.
numExtensions = (int) UperEncoder.decodeLengthOfBitmask(bitbuffer);
UperEncoder.logger.debug(String.format("sequence has %d extension(s)", numExtensions));
// Bitmask for extensions.
boolean[] bitmaskValueIsPresent = new boolean[numExtensions];
for (int i = 0; i < numExtensions; i++) {
bitmaskValueIsPresent[i] = bitbuffer.get();
UperEncoder.logger.debug(String.format("extension %s is %s", i, bitmaskValueIsPresent[i] ? "present" : "absent"));
}
// Values.
UperEncoder.logger.debug("decoding extensions values...");
for (int i = 0; i < numExtensions; i++) {
UperEncoder.logger.debug(String.format("sequence extension %s %s", i, bitmaskValueIsPresent[i] ? "present" : "absent"));
if (bitmaskValueIsPresent[i]) {
UperEncoder.logger.debug(String.format("decoding extension %d...", i));
Field field = sorter.extensionFields.size() > i ? sorter.extensionFields.get(i) : null;
Class<?> classOfElement = field != null ? field.getType() : null;
if (field != null) {
try {
Object decodedValue = UperEncoder.decodeAsOpenType(bitbuffer, classOfElement,field, field.getAnnotations());
if (field != null) {
field.set(result, decodedValue);
}
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new IllegalArgumentException("can't decode " + classOfT, e);
}
} else {
//CG skip the unknown extension element
UperEncoder.decodeSkipUnknownElement(bitbuffer, classOfT.getSimpleName());
}
} else {
//CG the absent extension filed might have a default value
Field field = sorter.extensionFields.size() > i ? sorter.extensionFields.get(i) : null;
Class<?> classOfElement = field != null ? field.getType() : null;
if (field != null && field.getAnnotation(Asn1Default.class) != null) {
try {
field.set(result,UperEncoder.getDefault(classOfElement,field.getAnnotations()));
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("can't decode " + classOfElement.getSimpleName(), e);
} catch (IllegalAccessException e) {
throw new IllegalArgumentException("can't decode " + classOfElement.getSimpleName(), e);
}
UperEncoder.logger.debug(String.format("Default set for %s", field.getName()));
}
}
}//end of loop on present extension fields
} else {
//CG there is an extension marker but the extension is not present
// then there sill might be an element with a default value
for (Field field : sorter.extensionFields) {
if ( numExtensions <= sorter.extensionFields.indexOf(field)) {
if (field.getAnnotation(Asn1Default.class) != null) {
Class<?> classOfElement = field != null ? field.getType() : null;
try {
field.set(result,UperEncoder.getDefault(classOfElement, field.getAnnotations()));
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("can't decode default" + classOfElement.getSimpleName(), e);
} catch (IllegalAccessException e) {
throw new IllegalArgumentException("can't decode default" + classOfElement.getSimpleName(), e);
}
}
}
}
} // end of extension handling
}
sorter.revertAccess();
return result;
}
@Override
public <T> T getDefault(Class<T> classOfT, Annotation[] annotations) {
throw new IllegalArgumentException("Default Sequence not yet implemented");
}
}
|