Commit | Line | Data |
---|---|---|
a3fc8213 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2011, 2014 Ericsson, École Polytechnique de Montréal |
a3fc8213 AM |
3 | * |
4 | * All rights reserved. This program and the accompanying materials are made | |
5 | * available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
d4a8d935 BH |
9 | * Contributors: |
10 | * Matthew Khouzam - Initial API and implementation | |
404b264a | 11 | * Alexandre Montplaisir - Initial API and implementation, extend TmfEventField |
d4a8d935 | 12 | * Bernd Hufmann - Add Enum field handling |
404b264a | 13 | * Geneviève Bastien - Add Struct and Variant field handling |
459f705b | 14 | * Jean-Christian Kouame - Correct handling of unsigned integer fields |
4591bed9 | 15 | * François Doray - Add generic array field type |
a3fc8213 AM |
16 | *******************************************************************************/ |
17 | ||
9722e5d7 | 18 | package org.eclipse.tracecompass.tmf.ctf.core.event; |
a3fc8213 | 19 | |
fafdd006 AM |
20 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
21 | ||
a6223d74 | 22 | import java.util.ArrayList; |
7a6cee1a | 23 | import java.util.Arrays; |
7b4f13e6 | 24 | import java.util.Collection; |
a6223d74 MK |
25 | import java.util.List; |
26 | ||
fafdd006 | 27 | import org.eclipse.jdt.annotation.NonNull; |
15f6223a | 28 | import org.eclipse.tracecompass.ctf.core.event.types.AbstractArrayDefinition; |
f357bcd4 AM |
29 | import org.eclipse.tracecompass.ctf.core.event.types.CompoundDeclaration; |
30 | import org.eclipse.tracecompass.ctf.core.event.types.Definition; | |
31 | import org.eclipse.tracecompass.ctf.core.event.types.EnumDefinition; | |
32 | import org.eclipse.tracecompass.ctf.core.event.types.FloatDefinition; | |
33 | import org.eclipse.tracecompass.ctf.core.event.types.ICompositeDefinition; | |
34 | import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration; | |
35 | import org.eclipse.tracecompass.ctf.core.event.types.IDefinition; | |
36 | import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration; | |
37 | import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition; | |
38 | import org.eclipse.tracecompass.ctf.core.event.types.StringDefinition; | |
39 | import org.eclipse.tracecompass.ctf.core.event.types.VariantDefinition; | |
2bdf0193 AM |
40 | import org.eclipse.tracecompass.tmf.core.event.ITmfEventField; |
41 | import org.eclipse.tracecompass.tmf.core.event.TmfEventField; | |
9722e5d7 | 42 | import org.eclipse.tracecompass.tmf.ctf.core.CtfEnumPair; |
a3fc8213 AM |
43 | |
44 | /** | |
7558a1e1 AM |
45 | * The CTF implementation of the TMF event field model |
46 | * | |
d4a8d935 | 47 | * @version 2.0 |
7558a1e1 | 48 | * @author Matthew Khouzam |
d09f973b | 49 | * @author Alexandre Montplaisir |
a3fc8213 | 50 | */ |
68b18f2f | 51 | public abstract class CtfTmfEventField extends TmfEventField { |
a3fc8213 | 52 | |
a3fc8213 | 53 | // ------------------------------------------------------------------------ |
7558a1e1 | 54 | // Constructor |
a3fc8213 AM |
55 | // ------------------------------------------------------------------------ |
56 | ||
b1baa808 | 57 | /** |
7558a1e1 AM |
58 | * Standard constructor. Only to be used internally, call parseField() to |
59 | * generate a new field object. | |
60 | * | |
61 | * @param name | |
62 | * The name of this field | |
68b18f2f AM |
63 | * @param value |
64 | * The value of this field. Its type should match the field type. | |
81ed27a8 MK |
65 | * @param fields |
66 | * The children fields. Useful for composite fields | |
b1baa808 | 67 | */ |
fafdd006 | 68 | protected CtfTmfEventField(@NonNull String name, Object value, ITmfEventField[] fields) { |
68b18f2f | 69 | super(/* Strip the underscore from the field name if there is one */ |
fafdd006 | 70 | name.startsWith("_") ? checkNotNull(name.substring(1)) : name, //$NON-NLS-1$ |
68b18f2f | 71 | value, |
81ed27a8 | 72 | fields); |
a3fc8213 AM |
73 | } |
74 | ||
75 | // ------------------------------------------------------------------------ | |
76 | // Operations | |
77 | // ------------------------------------------------------------------------ | |
78 | ||
b1baa808 | 79 | /** |
7558a1e1 AM |
80 | * Factory method to instantiate CtfTmfEventField objects. |
81 | * | |
82 | * @param fieldDef | |
83 | * The CTF Definition of this event field | |
84 | * @param fieldName | |
85 | * String The name to assign to this field | |
86 | * @return The resulting CtfTmfEventField object | |
cc98c947 | 87 | * @deprecated use {@link CtfTmfEventField#parseField(IDefinition, String)} |
b1baa808 | 88 | */ |
cc98c947 | 89 | @Deprecated |
a3fc8213 | 90 | public static CtfTmfEventField parseField(Definition fieldDef, |
fafdd006 | 91 | @NonNull String fieldName) { |
cc98c947 MK |
92 | return parseField((IDefinition) fieldDef, fieldName); |
93 | } | |
94 | ||
95 | /** | |
96 | * Factory method to instantiate CtfTmfEventField objects. | |
97 | * | |
98 | * @param fieldDef | |
99 | * The CTF Definition of this event field | |
100 | * @param fieldName | |
101 | * String The name to assign to this field | |
102 | * @return The resulting CtfTmfEventField object | |
cc98c947 | 103 | */ |
aa353506 | 104 | public static @NonNull CtfTmfEventField parseField(IDefinition fieldDef, |
fafdd006 | 105 | @NonNull String fieldName) { |
a3fc8213 AM |
106 | CtfTmfEventField field = null; |
107 | ||
108 | /* Determine the Definition type */ | |
109 | if (fieldDef instanceof IntegerDefinition) { | |
367bcd2b AM |
110 | IntegerDefinition intDef = (IntegerDefinition) fieldDef; |
111 | int base = intDef.getDeclaration().getBase(); | |
459f705b | 112 | field = new CTFIntegerField(fieldName, intDef.getValue(), base, intDef.getDeclaration().isSigned()); |
a3fc8213 | 113 | |
21fb02fa MK |
114 | } else if (fieldDef instanceof EnumDefinition) { |
115 | EnumDefinition enumDef = (EnumDefinition) fieldDef; | |
68b18f2f | 116 | field = new CTFEnumField(fieldName, new CtfEnumPair(enumDef.getValue(), enumDef.getIntegerValue())); |
21fb02fa | 117 | |
a3fc8213 | 118 | } else if (fieldDef instanceof StringDefinition) { |
68b18f2f AM |
119 | field = new CTFStringField(fieldName, ((StringDefinition) fieldDef).getValue()); |
120 | ||
121 | } else if (fieldDef instanceof FloatDefinition) { | |
122 | FloatDefinition floatDef = (FloatDefinition) fieldDef; | |
123 | field = new CTFFloatField(fieldName, floatDef.getValue()); | |
a3fc8213 | 124 | |
15f6223a MK |
125 | } else if (fieldDef instanceof AbstractArrayDefinition) { |
126 | AbstractArrayDefinition arrayDef = (AbstractArrayDefinition) fieldDef; | |
7b4f13e6 MK |
127 | IDeclaration decl = arrayDef.getDeclaration(); |
128 | if (!(decl instanceof CompoundDeclaration)) { | |
129 | throw new IllegalArgumentException("Array definitions should only come from sequence or array declarations"); //$NON-NLS-1$ | |
130 | } | |
131 | CompoundDeclaration arrDecl = (CompoundDeclaration) decl; | |
132 | IDeclaration elemType = null; | |
133 | Collection<Definition> definitions = arrayDef.getDefinitions(); | |
134 | elemType = arrDecl.getElementType(); | |
135 | if (elemType instanceof IntegerDeclaration) { | |
15f6223a MK |
136 | /* |
137 | * Array of integers => CTFIntegerArrayField, unless it's a | |
138 | * CTFStringField | |
139 | */ | |
7b4f13e6 | 140 | IntegerDeclaration elemIntType = (IntegerDeclaration) elemType; |
373af311 MK |
141 | /* Are the integers characters and encoded? */ |
142 | if (elemIntType.isCharacter()) { | |
143 | /* it's a CTFStringField */ | |
144 | field = new CTFStringField(fieldName, arrayDef.toString()); | |
145 | } else { | |
146 | /* it's a CTFIntegerArrayField */ | |
15f6223a MK |
147 | int size = arrayDef.getDefinitions().size(); |
148 | long[] values = new long[size]; | |
149 | for (int i = 0; i < size; i++) { | |
373af311 MK |
150 | IDefinition elem = arrayDef.getDefinitions().get(i); |
151 | if (elem == null) { | |
152 | break; | |
153 | } | |
154 | values[i] = ((IntegerDefinition) elem).getValue(); | |
7b4f13e6 | 155 | } |
373af311 MK |
156 | field = new CTFIntegerArrayField(fieldName, values, |
157 | elemIntType.getBase(), | |
158 | elemIntType.isSigned()); | |
7b4f13e6 | 159 | } |
4591bed9 FD |
160 | } else { |
161 | /* Arrays of elements of any other type */ | |
7b4f13e6 | 162 | CtfTmfEventField[] elements = new CtfTmfEventField[arrayDef.getLength()]; |
4591bed9 | 163 | /* Parse the elements of the array. */ |
7b4f13e6 | 164 | int i = 0; |
cc98c947 | 165 | for (IDefinition definition : definitions) { |
4591bed9 | 166 | CtfTmfEventField curField = CtfTmfEventField.parseField( |
7b4f13e6 | 167 | definition, fieldName + '[' + i + ']'); |
4591bed9 | 168 | elements[i] = curField; |
7b4f13e6 | 169 | i++; |
a3fc8213 | 170 | } |
a3fc8213 | 171 | |
4591bed9 FD |
172 | field = new CTFArrayField(fieldName, elements); |
173 | } | |
009883d7 MK |
174 | } else if (fieldDef instanceof ICompositeDefinition) { |
175 | ICompositeDefinition strDef = (ICompositeDefinition) fieldDef; | |
7a6cee1a | 176 | |
a4524c1b | 177 | List<ITmfEventField> list = new ArrayList<>(); |
7a6cee1a | 178 | /* Recursively parse the fields */ |
a4fa4e36 | 179 | for (String curFieldName : strDef.getFieldNames()) { |
fafdd006 AM |
180 | String fn = checkNotNull(curFieldName); |
181 | list.add(CtfTmfEventField.parseField((IDefinition) strDef.getDefinition(fn), fn)); | |
7a6cee1a GB |
182 | } |
183 | field = new CTFStructField(fieldName, list.toArray(new CtfTmfEventField[list.size()])); | |
a0e9eac8 | 184 | |
404b264a GB |
185 | } else if (fieldDef instanceof VariantDefinition) { |
186 | VariantDefinition varDef = (VariantDefinition) fieldDef; | |
187 | ||
fafdd006 | 188 | String curFieldName = checkNotNull(varDef.getCurrentFieldName()); |
cc98c947 | 189 | IDefinition curFieldDef = varDef.getCurrentField(); |
404b264a | 190 | if (curFieldDef != null) { |
51cc7ef4 GB |
191 | CtfTmfEventField subField = CtfTmfEventField.parseField(curFieldDef, curFieldName); |
192 | field = new CTFVariantField(fieldName, subField); | |
404b264a GB |
193 | } else { |
194 | /* A safe-guard, but curFieldDef should never be null */ | |
195 | field = new CTFStringField(curFieldName, ""); //$NON-NLS-1$ | |
196 | } | |
197 | ||
198 | } else { | |
459f705b JCK |
199 | /* |
200 | * Safe-guard, to avoid null exceptions later, field is expected not | |
201 | * to be null | |
202 | */ | |
91e7f946 | 203 | field = new CTFStringField(fieldName, Messages.CtfTmfEventField_UnsupportedType + fieldDef.getClass().toString()); |
a3fc8213 | 204 | } |
a3fc8213 AM |
205 | return field; |
206 | } | |
207 | ||
a3fc8213 | 208 | @Override |
68b18f2f | 209 | public String toString() { |
51cc7ef4 | 210 | return getName() + '=' + getFormattedValue(); |
a3fc8213 AM |
211 | } |
212 | ||
a3fc8213 AM |
213 | } |
214 | ||
215 | /** | |
7558a1e1 AM |
216 | * The CTF field implementation for integer fields. |
217 | * | |
218 | * @author alexmont | |
a3fc8213 AM |
219 | */ |
220 | final class CTFIntegerField extends CtfTmfEventField { | |
221 | ||
6fc4ce56 MK |
222 | private final int fBase; |
223 | private final boolean fSigned; | |
a3fc8213 AM |
224 | |
225 | /** | |
226 | * A CTF "IntegerDefinition" can be an integer of any byte size, so in the | |
227 | * Java parser this is interpreted as a long. | |
7558a1e1 | 228 | * |
7558a1e1 AM |
229 | * @param name |
230 | * The name of this field | |
459f705b JCK |
231 | * @param longValue |
232 | * The integer value of this field | |
233 | * @param signed | |
234 | * Is the value signed or not | |
a3fc8213 | 235 | */ |
fafdd006 | 236 | CTFIntegerField(@NonNull String name, long longValue, int base, boolean signed) { |
81ed27a8 | 237 | super(name, longValue, null); |
6fc4ce56 MK |
238 | fSigned = signed; |
239 | fBase = base; | |
367bcd2b AM |
240 | } |
241 | ||
a3fc8213 AM |
242 | @Override |
243 | public Long getValue() { | |
68b18f2f | 244 | return (Long) super.getValue(); |
a3fc8213 AM |
245 | } |
246 | ||
8f86c552 GB |
247 | @Override |
248 | public String getFormattedValue() { | |
6fc4ce56 | 249 | return IntegerDefinition.formatNumber(getValue(), fBase, fSigned); |
8f86c552 GB |
250 | } |
251 | ||
a3fc8213 AM |
252 | } |
253 | ||
254 | /** | |
7558a1e1 AM |
255 | * The CTF field implementation for string fields |
256 | * | |
257 | * @author alexmont | |
a3fc8213 AM |
258 | */ |
259 | final class CTFStringField extends CtfTmfEventField { | |
260 | ||
b1baa808 MK |
261 | /** |
262 | * Constructor for CTFStringField. | |
7558a1e1 AM |
263 | * |
264 | * @param strValue | |
265 | * The string value of this field | |
266 | * @param name | |
267 | * The name of this field | |
b1baa808 | 268 | */ |
fafdd006 | 269 | CTFStringField(@NonNull String name, String strValue) { |
81ed27a8 | 270 | super(name, strValue, null); |
a3fc8213 AM |
271 | } |
272 | ||
a3fc8213 AM |
273 | @Override |
274 | public String getValue() { | |
68b18f2f | 275 | return (String) super.getValue(); |
a3fc8213 AM |
276 | } |
277 | } | |
278 | ||
279 | /** | |
7558a1e1 AM |
280 | * CTF field implementation for arrays of integers. |
281 | * | |
282 | * @author alexmont | |
a3fc8213 AM |
283 | */ |
284 | final class CTFIntegerArrayField extends CtfTmfEventField { | |
285 | ||
6fc4ce56 MK |
286 | private final int fBase; |
287 | private final boolean fSigned; | |
288 | private String fFormattedValue = null; | |
404b264a | 289 | |
b1baa808 MK |
290 | /** |
291 | * Constructor for CTFIntegerArrayField. | |
7558a1e1 | 292 | * |
459f705b JCK |
293 | * @param name |
294 | * The name of this field | |
7558a1e1 AM |
295 | * @param longValues |
296 | * The array of integers (as longs) that compose this field's | |
297 | * value | |
459f705b JCK |
298 | * @param signed |
299 | * Are the values in the array signed or not | |
b1baa808 | 300 | */ |
fafdd006 | 301 | CTFIntegerArrayField(@NonNull String name, long[] longValues, int base, boolean signed) { |
81ed27a8 | 302 | super(name, longValues, null); |
6fc4ce56 MK |
303 | fBase = base; |
304 | fSigned = signed; | |
a3fc8213 AM |
305 | } |
306 | ||
a6223d74 | 307 | @Override |
cefe3edf AM |
308 | public long[] getValue() { |
309 | return (long[]) super.getValue(); | |
a3fc8213 | 310 | } |
404b264a | 311 | |
8f86c552 | 312 | @Override |
4591bed9 | 313 | public synchronized String getFormattedValue() { |
6fc4ce56 | 314 | if (fFormattedValue == null) { |
a4524c1b | 315 | List<String> strings = new ArrayList<>(); |
cefe3edf | 316 | for (long value : getValue()) { |
6fc4ce56 | 317 | strings.add(IntegerDefinition.formatNumber(value, fBase, fSigned)); |
8f86c552 | 318 | } |
6fc4ce56 | 319 | fFormattedValue = strings.toString(); |
8f86c552 | 320 | } |
6fc4ce56 | 321 | return fFormattedValue; |
8f86c552 GB |
322 | } |
323 | ||
a3fc8213 AM |
324 | } |
325 | ||
4591bed9 FD |
326 | /** |
327 | * CTF field implementation for arrays of arbitrary types. | |
328 | * | |
329 | * @author fdoray | |
330 | */ | |
331 | final class CTFArrayField extends CtfTmfEventField { | |
332 | ||
6fc4ce56 | 333 | private String fFormattedValue = null; |
4591bed9 FD |
334 | |
335 | /** | |
336 | * Constructor for CTFArrayField. | |
337 | * | |
338 | * @param name | |
339 | * The name of this field | |
340 | * @param elements | |
341 | * The array elements of this field | |
342 | */ | |
fafdd006 | 343 | CTFArrayField(@NonNull String name, CtfTmfEventField[] elements) { |
4591bed9 FD |
344 | super(name, elements, elements); |
345 | } | |
346 | ||
347 | @Override | |
348 | public CtfTmfEventField[] getValue() { | |
349 | return (CtfTmfEventField[]) super.getValue(); | |
350 | } | |
351 | ||
352 | @Override | |
353 | public synchronized String getFormattedValue() { | |
6fc4ce56 | 354 | if (fFormattedValue == null) { |
a4524c1b | 355 | List<String> strings = new ArrayList<>(); |
4591bed9 FD |
356 | for (CtfTmfEventField element : getValue()) { |
357 | strings.add(element.getFormattedValue()); | |
358 | } | |
6fc4ce56 | 359 | fFormattedValue = strings.toString(); |
4591bed9 | 360 | } |
6fc4ce56 | 361 | return fFormattedValue; |
4591bed9 FD |
362 | } |
363 | } | |
364 | ||
b1baa808 | 365 | /** |
7558a1e1 AM |
366 | * CTF field implementation for floats. |
367 | * | |
368 | * @author emathko | |
b1baa808 | 369 | */ |
a04464b1 MK |
370 | final class CTFFloatField extends CtfTmfEventField { |
371 | ||
b1baa808 MK |
372 | /** |
373 | * Constructor for CTFFloatField. | |
7558a1e1 AM |
374 | * |
375 | * @param value | |
376 | * The float value (actually a double) of this field | |
377 | * @param name | |
378 | * The name of this field | |
b1baa808 | 379 | */ |
fafdd006 | 380 | protected CTFFloatField(@NonNull String name, double value) { |
81ed27a8 | 381 | super(name, value, null); |
a04464b1 MK |
382 | } |
383 | ||
a04464b1 | 384 | @Override |
81c8e6f7 | 385 | public Double getValue() { |
68b18f2f | 386 | return (Double) super.getValue(); |
a04464b1 | 387 | } |
a04464b1 | 388 | } |
7558a1e1 | 389 | |
d4a8d935 BH |
390 | /** |
391 | * The CTF field implementation for Enum fields | |
392 | * | |
393 | * @author Bernd Hufmann | |
394 | */ | |
395 | final class CTFEnumField extends CtfTmfEventField { | |
396 | ||
d4a8d935 BH |
397 | /** |
398 | * Constructor for CTFEnumField. | |
399 | * | |
400 | * @param enumValue | |
459f705b JCK |
401 | * The Enum value consisting of a pair of Enum value name and its |
402 | * long value | |
d4a8d935 BH |
403 | * @param name |
404 | * The name of this field | |
405 | */ | |
fafdd006 | 406 | CTFEnumField(@NonNull String name, CtfEnumPair enumValue) { |
68b18f2f | 407 | super(name, new CtfEnumPair(enumValue.getFirst(), |
0126a8ca | 408 | enumValue.getSecond()), null); |
d4a8d935 BH |
409 | } |
410 | ||
d4a8d935 | 411 | @Override |
68b18f2f AM |
412 | public CtfEnumPair getValue() { |
413 | return (CtfEnumPair) super.getValue(); | |
d4a8d935 BH |
414 | } |
415 | } | |
416 | ||
7a6cee1a | 417 | /** |
51cc7ef4 | 418 | * The CTF field implementation for struct fields with sub-fields |
7a6cee1a GB |
419 | * |
420 | * @author gbastien | |
421 | */ | |
422 | final class CTFStructField extends CtfTmfEventField { | |
423 | ||
424 | /** | |
51cc7ef4 | 425 | * Constructor for CTFStructField. |
7a6cee1a | 426 | * |
51cc7ef4 GB |
427 | * @param fields |
428 | * The children of this field | |
7a6cee1a GB |
429 | * @param name |
430 | * The name of this field | |
431 | */ | |
fafdd006 | 432 | CTFStructField(@NonNull String name, CtfTmfEventField[] fields) { |
81ed27a8 | 433 | super(name, fields, fields); |
7a6cee1a GB |
434 | } |
435 | ||
7a6cee1a GB |
436 | @Override |
437 | public CtfTmfEventField[] getValue() { | |
438 | return (CtfTmfEventField[]) super.getValue(); | |
439 | } | |
440 | ||
441 | @Override | |
51cc7ef4 GB |
442 | public String getFormattedValue() { |
443 | return Arrays.toString(getValue()); | |
7a6cee1a | 444 | } |
51cc7ef4 GB |
445 | |
446 | } | |
447 | ||
448 | /** | |
449 | * The CTF field implementation for variant fields its child | |
450 | * | |
451 | * @author gbastien | |
452 | */ | |
453 | final class CTFVariantField extends CtfTmfEventField { | |
454 | ||
455 | /** | |
456 | * Constructor for CTFVariantField. | |
457 | * | |
458 | * @param field | |
459 | * The field selected for this variant | |
460 | * @param name | |
461 | * The name of this field | |
462 | */ | |
fafdd006 | 463 | CTFVariantField(@NonNull String name, CtfTmfEventField field) { |
459f705b | 464 | super(name, field, new CtfTmfEventField[] { field }); |
51cc7ef4 GB |
465 | } |
466 | ||
467 | @Override | |
468 | public CtfTmfEventField getValue() { | |
469 | return (CtfTmfEventField) super.getValue(); | |
470 | } | |
471 | ||
7a6cee1a GB |
472 | } |
473 | ||
a3fc8213 | 474 | /* Implement other possible fields types here... */ |