ctf: fix bug 491382. Properly display bytefields
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.tmf.ctf.core / src / org / eclipse / tracecompass / tmf / ctf / core / event / CtfTmfEventField.java
CommitLineData
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 18package org.eclipse.tracecompass.tmf.ctf.core.event;
a3fc8213 19
fafdd006
AM
20import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
21
a6223d74 22import java.util.ArrayList;
7a6cee1a 23import java.util.Arrays;
a6223d74
MK
24import java.util.List;
25
fafdd006 26import org.eclipse.jdt.annotation.NonNull;
15f6223a 27import org.eclipse.tracecompass.ctf.core.event.types.AbstractArrayDefinition;
f357bcd4
AM
28import org.eclipse.tracecompass.ctf.core.event.types.CompoundDeclaration;
29import org.eclipse.tracecompass.ctf.core.event.types.Definition;
30import org.eclipse.tracecompass.ctf.core.event.types.EnumDefinition;
31import org.eclipse.tracecompass.ctf.core.event.types.FloatDefinition;
32import org.eclipse.tracecompass.ctf.core.event.types.ICompositeDefinition;
33import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration;
34import org.eclipse.tracecompass.ctf.core.event.types.IDefinition;
35import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration;
36import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition;
37import org.eclipse.tracecompass.ctf.core.event.types.StringDefinition;
38import org.eclipse.tracecompass.ctf.core.event.types.VariantDefinition;
45261b3b 39import org.eclipse.tracecompass.internal.ctf.core.event.types.ByteArrayDefinition;
2bdf0193
AM
40import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
41import org.eclipse.tracecompass.tmf.core.event.TmfEventField;
9722e5d7 42import 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 51public 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 */
0b1ac3e2 70 name.startsWith("_") ? 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;
7b4f13e6
MK
133 elemType = arrDecl.getElementType();
134 if (elemType instanceof IntegerDeclaration) {
15f6223a
MK
135 /*
136 * Array of integers => CTFIntegerArrayField, unless it's a
137 * CTFStringField
138 */
7b4f13e6 139 IntegerDeclaration elemIntType = (IntegerDeclaration) elemType;
373af311
MK
140 /* Are the integers characters and encoded? */
141 if (elemIntType.isCharacter()) {
142 /* it's a CTFStringField */
143 field = new CTFStringField(fieldName, arrayDef.toString());
c9c50ae1
MK
144 } else if (arrayDef instanceof ByteArrayDefinition) { // unsigned byte array
145 ByteArrayDefinition byteArrayDefinition = (ByteArrayDefinition) arrayDef;
146 /* it's a CTFIntegerArrayField */
147 int size = arrayDef.getLength();
148 long[] values = new long[size];
149 for (int i = 0; i < size; i++) {
150 values[i] = Byte.toUnsignedLong(byteArrayDefinition.getByte(i));
151 }
152 field = new CTFIntegerArrayField(fieldName, values,
153 elemIntType.getBase(),
154 elemIntType.isSigned());
155
373af311
MK
156 } else {
157 /* it's a CTFIntegerArrayField */
45261b3b 158 int size = arrayDef.getLength();
15f6223a
MK
159 long[] values = new long[size];
160 for (int i = 0; i < size; i++) {
373af311
MK
161 IDefinition elem = arrayDef.getDefinitions().get(i);
162 if (elem == null) {
163 break;
164 }
165 values[i] = ((IntegerDefinition) elem).getValue();
7b4f13e6 166 }
373af311
MK
167 field = new CTFIntegerArrayField(fieldName, values,
168 elemIntType.getBase(),
169 elemIntType.isSigned());
7b4f13e6 170 }
4591bed9
FD
171 } else {
172 /* Arrays of elements of any other type */
7b4f13e6 173 CtfTmfEventField[] elements = new CtfTmfEventField[arrayDef.getLength()];
4591bed9 174 /* Parse the elements of the array. */
7b4f13e6 175 int i = 0;
45261b3b 176 List<Definition> definitions = arrayDef.getDefinitions();
cc98c947 177 for (IDefinition definition : definitions) {
4591bed9 178 CtfTmfEventField curField = CtfTmfEventField.parseField(
7b4f13e6 179 definition, fieldName + '[' + i + ']');
4591bed9 180 elements[i] = curField;
7b4f13e6 181 i++;
a3fc8213 182 }
a3fc8213 183
4591bed9
FD
184 field = new CTFArrayField(fieldName, elements);
185 }
009883d7
MK
186 } else if (fieldDef instanceof ICompositeDefinition) {
187 ICompositeDefinition strDef = (ICompositeDefinition) fieldDef;
7a6cee1a 188
a4524c1b 189 List<ITmfEventField> list = new ArrayList<>();
7a6cee1a 190 /* Recursively parse the fields */
0e4f957e 191 for (String fn : strDef.getFieldNames()) {
fafdd006 192 list.add(CtfTmfEventField.parseField((IDefinition) strDef.getDefinition(fn), fn));
7a6cee1a
GB
193 }
194 field = new CTFStructField(fieldName, list.toArray(new CtfTmfEventField[list.size()]));
a0e9eac8 195
404b264a
GB
196 } else if (fieldDef instanceof VariantDefinition) {
197 VariantDefinition varDef = (VariantDefinition) fieldDef;
198
fafdd006 199 String curFieldName = checkNotNull(varDef.getCurrentFieldName());
cc98c947 200 IDefinition curFieldDef = varDef.getCurrentField();
404b264a 201 if (curFieldDef != null) {
51cc7ef4
GB
202 CtfTmfEventField subField = CtfTmfEventField.parseField(curFieldDef, curFieldName);
203 field = new CTFVariantField(fieldName, subField);
404b264a
GB
204 } else {
205 /* A safe-guard, but curFieldDef should never be null */
206 field = new CTFStringField(curFieldName, ""); //$NON-NLS-1$
207 }
208
209 } else {
459f705b
JCK
210 /*
211 * Safe-guard, to avoid null exceptions later, field is expected not
212 * to be null
213 */
91e7f946 214 field = new CTFStringField(fieldName, Messages.CtfTmfEventField_UnsupportedType + fieldDef.getClass().toString());
a3fc8213 215 }
a3fc8213
AM
216 return field;
217 }
218
a3fc8213 219 @Override
68b18f2f 220 public String toString() {
51cc7ef4 221 return getName() + '=' + getFormattedValue();
a3fc8213
AM
222 }
223
a3fc8213
AM
224}
225
226/**
7558a1e1
AM
227 * The CTF field implementation for integer fields.
228 *
229 * @author alexmont
a3fc8213
AM
230 */
231final class CTFIntegerField extends CtfTmfEventField {
232
6fc4ce56
MK
233 private final int fBase;
234 private final boolean fSigned;
a3fc8213
AM
235
236 /**
237 * A CTF "IntegerDefinition" can be an integer of any byte size, so in the
238 * Java parser this is interpreted as a long.
7558a1e1 239 *
7558a1e1
AM
240 * @param name
241 * The name of this field
459f705b
JCK
242 * @param longValue
243 * The integer value of this field
244 * @param signed
245 * Is the value signed or not
a3fc8213 246 */
fafdd006 247 CTFIntegerField(@NonNull String name, long longValue, int base, boolean signed) {
905a5e84 248 super(name, Long.valueOf(longValue), null);
6fc4ce56
MK
249 fSigned = signed;
250 fBase = base;
367bcd2b
AM
251 }
252
a3fc8213
AM
253 @Override
254 public Long getValue() {
68b18f2f 255 return (Long) super.getValue();
a3fc8213
AM
256 }
257
8f86c552
GB
258 @Override
259 public String getFormattedValue() {
6fc4ce56 260 return IntegerDefinition.formatNumber(getValue(), fBase, fSigned);
8f86c552
GB
261 }
262
a3fc8213
AM
263}
264
265/**
7558a1e1
AM
266 * The CTF field implementation for string fields
267 *
268 * @author alexmont
a3fc8213
AM
269 */
270final class CTFStringField extends CtfTmfEventField {
271
b1baa808
MK
272 /**
273 * Constructor for CTFStringField.
7558a1e1
AM
274 *
275 * @param strValue
276 * The string value of this field
277 * @param name
278 * The name of this field
b1baa808 279 */
fafdd006 280 CTFStringField(@NonNull String name, String strValue) {
81ed27a8 281 super(name, strValue, null);
a3fc8213
AM
282 }
283
a3fc8213
AM
284 @Override
285 public String getValue() {
68b18f2f 286 return (String) super.getValue();
a3fc8213
AM
287 }
288}
289
290/**
7558a1e1
AM
291 * CTF field implementation for arrays of integers.
292 *
293 * @author alexmont
a3fc8213
AM
294 */
295final class CTFIntegerArrayField extends CtfTmfEventField {
296
6fc4ce56
MK
297 private final int fBase;
298 private final boolean fSigned;
299 private String fFormattedValue = null;
404b264a 300
b1baa808
MK
301 /**
302 * Constructor for CTFIntegerArrayField.
7558a1e1 303 *
459f705b
JCK
304 * @param name
305 * The name of this field
7558a1e1
AM
306 * @param longValues
307 * The array of integers (as longs) that compose this field's
308 * value
459f705b
JCK
309 * @param signed
310 * Are the values in the array signed or not
b1baa808 311 */
fafdd006 312 CTFIntegerArrayField(@NonNull String name, long[] longValues, int base, boolean signed) {
81ed27a8 313 super(name, longValues, null);
6fc4ce56
MK
314 fBase = base;
315 fSigned = signed;
a3fc8213
AM
316 }
317
a6223d74 318 @Override
cefe3edf
AM
319 public long[] getValue() {
320 return (long[]) super.getValue();
a3fc8213 321 }
404b264a 322
8f86c552 323 @Override
4591bed9 324 public synchronized String getFormattedValue() {
6fc4ce56 325 if (fFormattedValue == null) {
a4524c1b 326 List<String> strings = new ArrayList<>();
cefe3edf 327 for (long value : getValue()) {
6fc4ce56 328 strings.add(IntegerDefinition.formatNumber(value, fBase, fSigned));
8f86c552 329 }
6fc4ce56 330 fFormattedValue = strings.toString();
8f86c552 331 }
6fc4ce56 332 return fFormattedValue;
8f86c552
GB
333 }
334
a3fc8213
AM
335}
336
4591bed9
FD
337/**
338 * CTF field implementation for arrays of arbitrary types.
339 *
340 * @author fdoray
341 */
342final class CTFArrayField extends CtfTmfEventField {
343
6fc4ce56 344 private String fFormattedValue = null;
4591bed9
FD
345
346 /**
347 * Constructor for CTFArrayField.
348 *
349 * @param name
350 * The name of this field
351 * @param elements
352 * The array elements of this field
353 */
fafdd006 354 CTFArrayField(@NonNull String name, CtfTmfEventField[] elements) {
4591bed9
FD
355 super(name, elements, elements);
356 }
357
358 @Override
359 public CtfTmfEventField[] getValue() {
360 return (CtfTmfEventField[]) super.getValue();
361 }
362
363 @Override
364 public synchronized String getFormattedValue() {
6fc4ce56 365 if (fFormattedValue == null) {
a4524c1b 366 List<String> strings = new ArrayList<>();
4591bed9
FD
367 for (CtfTmfEventField element : getValue()) {
368 strings.add(element.getFormattedValue());
369 }
6fc4ce56 370 fFormattedValue = strings.toString();
4591bed9 371 }
6fc4ce56 372 return fFormattedValue;
4591bed9
FD
373 }
374}
375
b1baa808 376/**
7558a1e1
AM
377 * CTF field implementation for floats.
378 *
379 * @author emathko
b1baa808 380 */
a04464b1
MK
381final class CTFFloatField extends CtfTmfEventField {
382
b1baa808
MK
383 /**
384 * Constructor for CTFFloatField.
7558a1e1
AM
385 *
386 * @param value
387 * The float value (actually a double) of this field
388 * @param name
389 * The name of this field
b1baa808 390 */
fafdd006 391 protected CTFFloatField(@NonNull String name, double value) {
81ed27a8 392 super(name, value, null);
a04464b1
MK
393 }
394
a04464b1 395 @Override
81c8e6f7 396 public Double getValue() {
68b18f2f 397 return (Double) super.getValue();
a04464b1 398 }
a04464b1 399}
7558a1e1 400
d4a8d935
BH
401/**
402 * The CTF field implementation for Enum fields
403 *
404 * @author Bernd Hufmann
405 */
406final class CTFEnumField extends CtfTmfEventField {
407
d4a8d935
BH
408 /**
409 * Constructor for CTFEnumField.
410 *
411 * @param enumValue
459f705b
JCK
412 * The Enum value consisting of a pair of Enum value name and its
413 * long value
d4a8d935
BH
414 * @param name
415 * The name of this field
416 */
fafdd006 417 CTFEnumField(@NonNull String name, CtfEnumPair enumValue) {
68b18f2f 418 super(name, new CtfEnumPair(enumValue.getFirst(),
0126a8ca 419 enumValue.getSecond()), null);
d4a8d935
BH
420 }
421
d4a8d935 422 @Override
68b18f2f
AM
423 public CtfEnumPair getValue() {
424 return (CtfEnumPair) super.getValue();
d4a8d935
BH
425 }
426}
427
7a6cee1a 428/**
51cc7ef4 429 * The CTF field implementation for struct fields with sub-fields
7a6cee1a
GB
430 *
431 * @author gbastien
432 */
433final class CTFStructField extends CtfTmfEventField {
434
435 /**
51cc7ef4 436 * Constructor for CTFStructField.
7a6cee1a 437 *
51cc7ef4
GB
438 * @param fields
439 * The children of this field
7a6cee1a
GB
440 * @param name
441 * The name of this field
442 */
fafdd006 443 CTFStructField(@NonNull String name, CtfTmfEventField[] fields) {
81ed27a8 444 super(name, fields, fields);
7a6cee1a
GB
445 }
446
7a6cee1a
GB
447 @Override
448 public CtfTmfEventField[] getValue() {
449 return (CtfTmfEventField[]) super.getValue();
450 }
451
452 @Override
51cc7ef4
GB
453 public String getFormattedValue() {
454 return Arrays.toString(getValue());
7a6cee1a 455 }
51cc7ef4
GB
456
457}
458
459/**
460 * The CTF field implementation for variant fields its child
461 *
462 * @author gbastien
463 */
464final class CTFVariantField extends CtfTmfEventField {
465
466 /**
467 * Constructor for CTFVariantField.
468 *
469 * @param field
470 * The field selected for this variant
471 * @param name
472 * The name of this field
473 */
fafdd006 474 CTFVariantField(@NonNull String name, CtfTmfEventField field) {
459f705b 475 super(name, field, new CtfTmfEventField[] { field });
51cc7ef4
GB
476 }
477
478 @Override
479 public CtfTmfEventField getValue() {
480 return (CtfTmfEventField) super.getValue();
481 }
482
7a6cee1a
GB
483}
484
a3fc8213 485/* Implement other possible fields types here... */
This page took 0.147185 seconds and 5 git commands to generate.