1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 Ericsson, École Polytechnique de Montréal
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
10 * Matthew Khouzam - Initial API and implementation
11 * Alexandre Montplaisir - Initial API and implementation, extend TmfEventField
12 * Bernd Hufmann - Add Enum field handling
13 * Geneviève Bastien - Add Struct and Variant field handling
14 * Jean-Christian Kouame - Correct handling of unsigned integer fields
15 * François Doray - Add generic array field type
16 *******************************************************************************/
18 package org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
;
20 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
22 import java
.util
.ArrayList
;
23 import java
.util
.Arrays
;
24 import java
.util
.List
;
26 import org
.eclipse
.jdt
.annotation
.NonNull
;
27 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.AbstractArrayDefinition
;
28 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.CompoundDeclaration
;
29 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.Definition
;
30 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.EnumDefinition
;
31 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.FloatDefinition
;
32 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.ICompositeDefinition
;
33 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IDeclaration
;
34 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IDefinition
;
35 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IntegerDeclaration
;
36 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IntegerDefinition
;
37 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StringDefinition
;
38 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.VariantDefinition
;
39 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.types
.ByteArrayDefinition
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventField
;
42 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.CtfEnumPair
;
45 * The CTF implementation of the TMF event field model
48 * @author Matthew Khouzam
49 * @author Alexandre Montplaisir
51 public abstract class CtfTmfEventField
extends TmfEventField
{
53 // ------------------------------------------------------------------------
55 // ------------------------------------------------------------------------
58 * Standard constructor. Only to be used internally, call parseField() to
59 * generate a new field object.
62 * The name of this field
64 * The value of this field. Its type should match the field type.
66 * The children fields. Useful for composite fields
68 protected CtfTmfEventField(@NonNull String name
, Object value
, ITmfEventField
[] fields
) {
69 super(/* Strip the underscore from the field name if there is one */
70 name
.startsWith("_") ? name
.substring(1) : name
, //$NON-NLS-1$
75 // ------------------------------------------------------------------------
77 // ------------------------------------------------------------------------
80 * Factory method to instantiate CtfTmfEventField objects.
83 * The CTF Definition of this event field
85 * String The name to assign to this field
86 * @return The resulting CtfTmfEventField object
87 * @deprecated use {@link CtfTmfEventField#parseField(IDefinition, String)}
90 public static CtfTmfEventField
parseField(Definition fieldDef
,
91 @NonNull String fieldName
) {
92 return parseField((IDefinition
) fieldDef
, fieldName
);
96 * Factory method to instantiate CtfTmfEventField objects.
99 * The CTF Definition of this event field
101 * String The name to assign to this field
102 * @return The resulting CtfTmfEventField object
104 public static @NonNull CtfTmfEventField
parseField(IDefinition fieldDef
,
105 @NonNull String fieldName
) {
106 CtfTmfEventField field
= null;
108 /* Determine the Definition type */
109 if (fieldDef
instanceof IntegerDefinition
) {
110 IntegerDefinition intDef
= (IntegerDefinition
) fieldDef
;
111 int base
= intDef
.getDeclaration().getBase();
112 field
= new CTFIntegerField(fieldName
, intDef
.getValue(), base
, intDef
.getDeclaration().isSigned());
114 } else if (fieldDef
instanceof EnumDefinition
) {
115 EnumDefinition enumDef
= (EnumDefinition
) fieldDef
;
116 field
= new CTFEnumField(fieldName
, new CtfEnumPair(enumDef
.getValue(), enumDef
.getIntegerValue()));
118 } else if (fieldDef
instanceof StringDefinition
) {
119 field
= new CTFStringField(fieldName
, ((StringDefinition
) fieldDef
).getValue());
121 } else if (fieldDef
instanceof FloatDefinition
) {
122 FloatDefinition floatDef
= (FloatDefinition
) fieldDef
;
123 field
= new CTFFloatField(fieldName
, floatDef
.getValue());
125 } else if (fieldDef
instanceof AbstractArrayDefinition
) {
126 AbstractArrayDefinition arrayDef
= (AbstractArrayDefinition
) fieldDef
;
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$
131 CompoundDeclaration arrDecl
= (CompoundDeclaration
) decl
;
132 IDeclaration elemType
= null;
133 elemType
= arrDecl
.getElementType();
134 if (arrayDef
instanceof ByteArrayDefinition
) {
135 ByteArrayDefinition byteArrayDefinition
= (ByteArrayDefinition
) arrayDef
;
136 /* it's a CTFIntegerArrayField */
137 int size
= arrayDef
.getLength();
138 long[] values
= new long[size
];
139 for (int i
= 0; i
< size
; i
++) {
140 values
[i
] = Byte
.toUnsignedLong(byteArrayDefinition
.getByte(i
));
142 field
= new CTFIntegerArrayField(fieldName
, values
,
147 if (elemType
instanceof IntegerDeclaration
) {
149 * Array of integers => CTFIntegerArrayField, unless it's a
152 IntegerDeclaration elemIntType
= (IntegerDeclaration
) elemType
;
153 /* Are the integers characters and encoded? */
154 if (elemIntType
.isCharacter()) {
155 /* it's a CTFStringField */
156 field
= new CTFStringField(fieldName
, arrayDef
.toString());
158 /* it's a CTFIntegerArrayField */
159 int size
= arrayDef
.getLength();
160 long[] values
= new long[size
];
161 for (int i
= 0; i
< size
; i
++) {
162 IDefinition elem
= arrayDef
.getDefinitions().get(i
);
166 values
[i
] = ((IntegerDefinition
) elem
).getValue();
168 field
= new CTFIntegerArrayField(fieldName
, values
,
169 elemIntType
.getBase(),
170 elemIntType
.isSigned());
173 /* Arrays of elements of any other type */
174 CtfTmfEventField
[] elements
= new CtfTmfEventField
[arrayDef
.getLength()];
175 /* Parse the elements of the array. */
177 List
<Definition
> definitions
= arrayDef
.getDefinitions();
178 for (IDefinition definition
: definitions
) {
179 CtfTmfEventField curField
= CtfTmfEventField
.parseField(
180 definition
, fieldName
+ '[' + i
+ ']');
181 elements
[i
] = curField
;
185 field
= new CTFArrayField(fieldName
, elements
);
187 } else if (fieldDef
instanceof ICompositeDefinition
) {
188 ICompositeDefinition strDef
= (ICompositeDefinition
) fieldDef
;
190 List
<ITmfEventField
> list
= new ArrayList
<>();
191 /* Recursively parse the fields */
192 for (String fn
: strDef
.getFieldNames()) {
193 list
.add(CtfTmfEventField
.parseField((IDefinition
) strDef
.getDefinition(fn
), fn
));
195 field
= new CTFStructField(fieldName
, list
.toArray(new CtfTmfEventField
[list
.size()]));
197 } else if (fieldDef
instanceof VariantDefinition
) {
198 VariantDefinition varDef
= (VariantDefinition
) fieldDef
;
200 String curFieldName
= checkNotNull(varDef
.getCurrentFieldName());
201 IDefinition curFieldDef
= varDef
.getCurrentField();
202 if (curFieldDef
!= null) {
203 CtfTmfEventField subField
= CtfTmfEventField
.parseField(curFieldDef
, curFieldName
);
204 field
= new CTFVariantField(fieldName
, subField
);
206 /* A safe-guard, but curFieldDef should never be null */
207 field
= new CTFStringField(curFieldName
, ""); //$NON-NLS-1$
212 * Safe-guard, to avoid null exceptions later, field is expected not
215 field
= new CTFStringField(fieldName
, Messages
.CtfTmfEventField_UnsupportedType
+ fieldDef
.getClass().toString());
221 public String
toString() {
222 return getName() + '=' + getFormattedValue();
228 * The CTF field implementation for integer fields.
232 final class CTFIntegerField
extends CtfTmfEventField
{
234 private final int fBase
;
235 private final boolean fSigned
;
238 * A CTF "IntegerDefinition" can be an integer of any byte size, so in the
239 * Java parser this is interpreted as a long.
242 * The name of this field
244 * The integer value of this field
246 * Is the value signed or not
248 CTFIntegerField(@NonNull String name
, long longValue
, int base
, boolean signed
) {
249 super(name
, Long
.valueOf(longValue
), null);
255 public Long
getValue() {
256 return (Long
) super.getValue();
260 public String
getFormattedValue() {
261 return IntegerDefinition
.formatNumber(getValue(), fBase
, fSigned
);
267 * The CTF field implementation for string fields
271 final class CTFStringField
extends CtfTmfEventField
{
274 * Constructor for CTFStringField.
277 * The string value of this field
279 * The name of this field
281 CTFStringField(@NonNull String name
, String strValue
) {
282 super(name
, strValue
, null);
286 public String
getValue() {
287 return (String
) super.getValue();
292 * CTF field implementation for arrays of integers.
296 final class CTFIntegerArrayField
extends CtfTmfEventField
{
298 private final int fBase
;
299 private final boolean fSigned
;
300 private String fFormattedValue
= null;
303 * Constructor for CTFIntegerArrayField.
306 * The name of this field
308 * The array of integers (as longs) that compose this field's
311 * Are the values in the array signed or not
313 CTFIntegerArrayField(@NonNull String name
, long[] longValues
, int base
, boolean signed
) {
314 super(name
, longValues
, null);
320 public long[] getValue() {
321 return (long[]) super.getValue();
325 public synchronized String
getFormattedValue() {
326 if (fFormattedValue
== null) {
327 List
<String
> strings
= new ArrayList
<>();
328 for (long value
: getValue()) {
329 strings
.add(IntegerDefinition
.formatNumber(value
, fBase
, fSigned
));
331 fFormattedValue
= strings
.toString();
333 return fFormattedValue
;
339 * CTF field implementation for arrays of arbitrary types.
343 final class CTFArrayField
extends CtfTmfEventField
{
345 private String fFormattedValue
= null;
348 * Constructor for CTFArrayField.
351 * The name of this field
353 * The array elements of this field
355 CTFArrayField(@NonNull String name
, CtfTmfEventField
[] elements
) {
356 super(name
, elements
, elements
);
360 public CtfTmfEventField
[] getValue() {
361 return (CtfTmfEventField
[]) super.getValue();
365 public synchronized String
getFormattedValue() {
366 if (fFormattedValue
== null) {
367 List
<String
> strings
= new ArrayList
<>();
368 for (CtfTmfEventField element
: getValue()) {
369 strings
.add(element
.getFormattedValue());
371 fFormattedValue
= strings
.toString();
373 return fFormattedValue
;
378 * CTF field implementation for floats.
382 final class CTFFloatField
extends CtfTmfEventField
{
385 * Constructor for CTFFloatField.
388 * The float value (actually a double) of this field
390 * The name of this field
392 protected CTFFloatField(@NonNull String name
, double value
) {
393 super(name
, value
, null);
397 public Double
getValue() {
398 return (Double
) super.getValue();
403 * The CTF field implementation for Enum fields
405 * @author Bernd Hufmann
407 final class CTFEnumField
extends CtfTmfEventField
{
410 * Constructor for CTFEnumField.
413 * The Enum value consisting of a pair of Enum value name and its
416 * The name of this field
418 CTFEnumField(@NonNull String name
, CtfEnumPair enumValue
) {
419 super(name
, new CtfEnumPair(enumValue
.getFirst(),
420 enumValue
.getSecond()), null);
424 public CtfEnumPair
getValue() {
425 return (CtfEnumPair
) super.getValue();
430 * The CTF field implementation for struct fields with sub-fields
434 final class CTFStructField
extends CtfTmfEventField
{
437 * Constructor for CTFStructField.
440 * The children of this field
442 * The name of this field
444 CTFStructField(@NonNull String name
, CtfTmfEventField
[] fields
) {
445 super(name
, fields
, fields
);
449 public CtfTmfEventField
[] getValue() {
450 return (CtfTmfEventField
[]) super.getValue();
454 public String
getFormattedValue() {
455 return Arrays
.toString(getValue());
461 * The CTF field implementation for variant fields its child
465 final class CTFVariantField
extends CtfTmfEventField
{
468 * Constructor for CTFVariantField.
471 * The field selected for this variant
473 * The name of this field
475 CTFVariantField(@NonNull String name
, CtfTmfEventField field
) {
476 super(name
, field
, new CtfTmfEventField
[] { field
});
480 public CtfTmfEventField
getValue() {
481 return (CtfTmfEventField
) super.getValue();
486 /* Implement other possible fields types here... */