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 (elemType
instanceof IntegerDeclaration
) {
136 * Array of integers => CTFIntegerArrayField, unless it's a
139 IntegerDeclaration elemIntType
= (IntegerDeclaration
) elemType
;
140 /* Are the integers characters and encoded? */
141 if (elemIntType
.isCharacter()) {
142 /* it's a CTFStringField */
143 field
= new CTFStringField(fieldName
, arrayDef
.toString());
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
));
152 field
= new CTFIntegerArrayField(fieldName
, values
,
153 elemIntType
.getBase(),
154 elemIntType
.isSigned());
157 /* it's a CTFIntegerArrayField */
158 int size
= arrayDef
.getLength();
159 long[] values
= new long[size
];
160 for (int i
= 0; i
< size
; i
++) {
161 IDefinition elem
= arrayDef
.getDefinitions().get(i
);
165 values
[i
] = ((IntegerDefinition
) elem
).getValue();
167 field
= new CTFIntegerArrayField(fieldName
, values
,
168 elemIntType
.getBase(),
169 elemIntType
.isSigned());
172 /* Arrays of elements of any other type */
173 CtfTmfEventField
[] elements
= new CtfTmfEventField
[arrayDef
.getLength()];
174 /* Parse the elements of the array. */
176 List
<Definition
> definitions
= arrayDef
.getDefinitions();
177 for (IDefinition definition
: definitions
) {
178 CtfTmfEventField curField
= CtfTmfEventField
.parseField(
179 definition
, fieldName
+ '[' + i
+ ']');
180 elements
[i
] = curField
;
184 field
= new CTFArrayField(fieldName
, elements
);
186 } else if (fieldDef
instanceof ICompositeDefinition
) {
187 ICompositeDefinition strDef
= (ICompositeDefinition
) fieldDef
;
189 List
<ITmfEventField
> list
= new ArrayList
<>();
190 /* Recursively parse the fields */
191 for (String fn
: strDef
.getFieldNames()) {
192 list
.add(CtfTmfEventField
.parseField((IDefinition
) strDef
.getDefinition(fn
), fn
));
194 field
= new CTFStructField(fieldName
, list
.toArray(new CtfTmfEventField
[list
.size()]));
196 } else if (fieldDef
instanceof VariantDefinition
) {
197 VariantDefinition varDef
= (VariantDefinition
) fieldDef
;
199 String curFieldName
= checkNotNull(varDef
.getCurrentFieldName());
200 IDefinition curFieldDef
= varDef
.getCurrentField();
201 if (curFieldDef
!= null) {
202 CtfTmfEventField subField
= CtfTmfEventField
.parseField(curFieldDef
, curFieldName
);
203 field
= new CTFVariantField(fieldName
, subField
);
205 /* A safe-guard, but curFieldDef should never be null */
206 field
= new CTFStringField(curFieldName
, ""); //$NON-NLS-1$
211 * Safe-guard, to avoid null exceptions later, field is expected not
214 field
= new CTFStringField(fieldName
, Messages
.CtfTmfEventField_UnsupportedType
+ fieldDef
.getClass().toString());
220 public String
toString() {
221 return getName() + '=' + getFormattedValue();
227 * The CTF field implementation for integer fields.
231 final class CTFIntegerField
extends CtfTmfEventField
{
233 private final int fBase
;
234 private final boolean fSigned
;
237 * A CTF "IntegerDefinition" can be an integer of any byte size, so in the
238 * Java parser this is interpreted as a long.
241 * The name of this field
243 * The integer value of this field
245 * Is the value signed or not
247 CTFIntegerField(@NonNull String name
, long longValue
, int base
, boolean signed
) {
248 super(name
, Long
.valueOf(longValue
), null);
254 public Long
getValue() {
255 return (Long
) super.getValue();
259 public String
getFormattedValue() {
260 return IntegerDefinition
.formatNumber(getValue(), fBase
, fSigned
);
266 * The CTF field implementation for string fields
270 final class CTFStringField
extends CtfTmfEventField
{
273 * Constructor for CTFStringField.
276 * The string value of this field
278 * The name of this field
280 CTFStringField(@NonNull String name
, String strValue
) {
281 super(name
, strValue
, null);
285 public String
getValue() {
286 return (String
) super.getValue();
291 * CTF field implementation for arrays of integers.
295 final class CTFIntegerArrayField
extends CtfTmfEventField
{
297 private final int fBase
;
298 private final boolean fSigned
;
299 private String fFormattedValue
= null;
302 * Constructor for CTFIntegerArrayField.
305 * The name of this field
307 * The array of integers (as longs) that compose this field's
310 * Are the values in the array signed or not
312 CTFIntegerArrayField(@NonNull String name
, long[] longValues
, int base
, boolean signed
) {
313 super(name
, longValues
, null);
319 public long[] getValue() {
320 return (long[]) super.getValue();
324 public synchronized String
getFormattedValue() {
325 if (fFormattedValue
== null) {
326 List
<String
> strings
= new ArrayList
<>();
327 for (long value
: getValue()) {
328 strings
.add(IntegerDefinition
.formatNumber(value
, fBase
, fSigned
));
330 fFormattedValue
= strings
.toString();
332 return fFormattedValue
;
338 * CTF field implementation for arrays of arbitrary types.
342 final class CTFArrayField
extends CtfTmfEventField
{
344 private String fFormattedValue
= null;
347 * Constructor for CTFArrayField.
350 * The name of this field
352 * The array elements of this field
354 CTFArrayField(@NonNull String name
, CtfTmfEventField
[] elements
) {
355 super(name
, elements
, elements
);
359 public CtfTmfEventField
[] getValue() {
360 return (CtfTmfEventField
[]) super.getValue();
364 public synchronized String
getFormattedValue() {
365 if (fFormattedValue
== null) {
366 List
<String
> strings
= new ArrayList
<>();
367 for (CtfTmfEventField element
: getValue()) {
368 strings
.add(element
.getFormattedValue());
370 fFormattedValue
= strings
.toString();
372 return fFormattedValue
;
377 * CTF field implementation for floats.
381 final class CTFFloatField
extends CtfTmfEventField
{
384 * Constructor for CTFFloatField.
387 * The float value (actually a double) of this field
389 * The name of this field
391 protected CTFFloatField(@NonNull String name
, double value
) {
392 super(name
, value
, null);
396 public Double
getValue() {
397 return (Double
) super.getValue();
402 * The CTF field implementation for Enum fields
404 * @author Bernd Hufmann
406 final class CTFEnumField
extends CtfTmfEventField
{
409 * Constructor for CTFEnumField.
412 * The Enum value consisting of a pair of Enum value name and its
415 * The name of this field
417 CTFEnumField(@NonNull String name
, CtfEnumPair enumValue
) {
418 super(name
, new CtfEnumPair(enumValue
.getFirst(),
419 enumValue
.getSecond()), null);
423 public CtfEnumPair
getValue() {
424 return (CtfEnumPair
) super.getValue();
429 * The CTF field implementation for struct fields with sub-fields
433 final class CTFStructField
extends CtfTmfEventField
{
436 * Constructor for CTFStructField.
439 * The children of this field
441 * The name of this field
443 CTFStructField(@NonNull String name
, CtfTmfEventField
[] fields
) {
444 super(name
, fields
, fields
);
448 public CtfTmfEventField
[] getValue() {
449 return (CtfTmfEventField
[]) super.getValue();
453 public String
getFormattedValue() {
454 return Arrays
.toString(getValue());
460 * The CTF field implementation for variant fields its child
464 final class CTFVariantField
extends CtfTmfEventField
{
467 * Constructor for CTFVariantField.
470 * The field selected for this variant
472 * The name of this field
474 CTFVariantField(@NonNull String name
, CtfTmfEventField field
) {
475 super(name
, field
, new CtfTmfEventField
[] { field
});
479 public CtfTmfEventField
getValue() {
480 return (CtfTmfEventField
) super.getValue();
485 /* Implement other possible fields types here... */