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 java
.util
.ArrayList
;
21 import java
.util
.Arrays
;
22 import java
.util
.Collection
;
23 import java
.util
.List
;
25 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.AbstractArrayDefinition
;
26 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.CompoundDeclaration
;
27 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.Definition
;
28 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.EnumDefinition
;
29 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.FloatDefinition
;
30 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.ICompositeDefinition
;
31 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IDeclaration
;
32 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IDefinition
;
33 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IntegerDeclaration
;
34 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IntegerDefinition
;
35 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StringDefinition
;
36 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.VariantDefinition
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventField
;
39 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.CtfEnumPair
;
42 * The CTF implementation of the TMF event field model
45 * @author Matthew Khouzam
46 * @author Alexandre Montplaisir
48 public abstract class CtfTmfEventField
extends TmfEventField
{
50 // ------------------------------------------------------------------------
52 // ------------------------------------------------------------------------
55 * Standard constructor. Only to be used internally, call parseField() to
56 * generate a new field object.
59 * The name of this field
61 * The value of this field. Its type should match the field type.
63 * The children fields. Useful for composite fields
65 protected CtfTmfEventField(String name
, Object value
, ITmfEventField
[] fields
) {
66 super(/* Strip the underscore from the field name if there is one */
67 name
.startsWith("_") ? name
.substring(1) : name
, //$NON-NLS-1$
72 // ------------------------------------------------------------------------
74 // ------------------------------------------------------------------------
77 * Factory method to instantiate CtfTmfEventField objects.
80 * The CTF Definition of this event field
82 * String The name to assign to this field
83 * @return The resulting CtfTmfEventField object
84 * @deprecated use {@link CtfTmfEventField#parseField(IDefinition, String)}
87 public static CtfTmfEventField
parseField(Definition fieldDef
,
89 return parseField((IDefinition
) fieldDef
, fieldName
);
93 * Factory method to instantiate CtfTmfEventField objects.
96 * The CTF Definition of this event field
98 * String The name to assign to this field
99 * @return The resulting CtfTmfEventField object
101 public static CtfTmfEventField
parseField(IDefinition fieldDef
,
103 CtfTmfEventField field
= null;
105 /* Determine the Definition type */
106 if (fieldDef
instanceof IntegerDefinition
) {
107 IntegerDefinition intDef
= (IntegerDefinition
) fieldDef
;
108 int base
= intDef
.getDeclaration().getBase();
109 field
= new CTFIntegerField(fieldName
, intDef
.getValue(), base
, intDef
.getDeclaration().isSigned());
111 } else if (fieldDef
instanceof EnumDefinition
) {
112 EnumDefinition enumDef
= (EnumDefinition
) fieldDef
;
113 field
= new CTFEnumField(fieldName
, new CtfEnumPair(enumDef
.getValue(), enumDef
.getIntegerValue()));
115 } else if (fieldDef
instanceof StringDefinition
) {
116 field
= new CTFStringField(fieldName
, ((StringDefinition
) fieldDef
).getValue());
118 } else if (fieldDef
instanceof FloatDefinition
) {
119 FloatDefinition floatDef
= (FloatDefinition
) fieldDef
;
120 field
= new CTFFloatField(fieldName
, floatDef
.getValue());
122 } else if (fieldDef
instanceof AbstractArrayDefinition
) {
123 AbstractArrayDefinition arrayDef
= (AbstractArrayDefinition
) fieldDef
;
124 IDeclaration decl
= arrayDef
.getDeclaration();
125 if (!(decl
instanceof CompoundDeclaration
)) {
126 throw new IllegalArgumentException("Array definitions should only come from sequence or array declarations"); //$NON-NLS-1$
128 CompoundDeclaration arrDecl
= (CompoundDeclaration
) decl
;
129 IDeclaration elemType
= null;
130 Collection
<Definition
> definitions
= arrayDef
.getDefinitions();
131 elemType
= arrDecl
.getElementType();
132 if (elemType
instanceof IntegerDeclaration
) {
134 * Array of integers => CTFIntegerArrayField, unless it's a
137 IntegerDeclaration elemIntType
= (IntegerDeclaration
) elemType
;
138 /* Are the integers characters and encoded? */
139 if (elemIntType
.isCharacter()) {
140 /* it's a CTFStringField */
141 field
= new CTFStringField(fieldName
, arrayDef
.toString());
143 /* it's a CTFIntegerArrayField */
144 int size
= arrayDef
.getDefinitions().size();
145 long[] values
= new long[size
];
146 for (int i
= 0; i
< size
; i
++) {
147 IDefinition elem
= arrayDef
.getDefinitions().get(i
);
151 values
[i
] = ((IntegerDefinition
) elem
).getValue();
153 field
= new CTFIntegerArrayField(fieldName
, values
,
154 elemIntType
.getBase(),
155 elemIntType
.isSigned());
158 /* Arrays of elements of any other type */
159 CtfTmfEventField
[] elements
= new CtfTmfEventField
[arrayDef
.getLength()];
160 /* Parse the elements of the array. */
162 for (IDefinition definition
: definitions
) {
163 CtfTmfEventField curField
= CtfTmfEventField
.parseField(
164 definition
, fieldName
+ '[' + i
+ ']');
165 elements
[i
] = curField
;
169 field
= new CTFArrayField(fieldName
, elements
);
171 } else if (fieldDef
instanceof ICompositeDefinition
) {
172 ICompositeDefinition strDef
= (ICompositeDefinition
) fieldDef
;
174 List
<ITmfEventField
> list
= new ArrayList
<>();
175 /* Recursively parse the fields */
176 for (String curFieldName
: strDef
.getFieldNames()) {
177 list
.add(CtfTmfEventField
.parseField(strDef
.getDefinition(curFieldName
), curFieldName
));
179 field
= new CTFStructField(fieldName
, list
.toArray(new CtfTmfEventField
[list
.size()]));
181 } else if (fieldDef
instanceof VariantDefinition
) {
182 VariantDefinition varDef
= (VariantDefinition
) fieldDef
;
184 String curFieldName
= varDef
.getCurrentFieldName();
185 IDefinition curFieldDef
= varDef
.getCurrentField();
186 if (curFieldDef
!= null) {
187 CtfTmfEventField subField
= CtfTmfEventField
.parseField(curFieldDef
, curFieldName
);
188 field
= new CTFVariantField(fieldName
, subField
);
190 /* A safe-guard, but curFieldDef should never be null */
191 field
= new CTFStringField(curFieldName
, ""); //$NON-NLS-1$
196 * Safe-guard, to avoid null exceptions later, field is expected not
199 field
= new CTFStringField(fieldName
, Messages
.CtfTmfEventField_UnsupportedType
+ fieldDef
.getClass().toString());
205 public String
toString() {
206 return getName() + '=' + getFormattedValue();
212 * The CTF field implementation for integer fields.
216 final class CTFIntegerField
extends CtfTmfEventField
{
218 private final int fBase
;
219 private final boolean fSigned
;
222 * A CTF "IntegerDefinition" can be an integer of any byte size, so in the
223 * Java parser this is interpreted as a long.
226 * The name of this field
228 * The integer value of this field
230 * Is the value signed or not
232 CTFIntegerField(String name
, long longValue
, int base
, boolean signed
) {
233 super(name
, longValue
, null);
239 public Long
getValue() {
240 return (Long
) super.getValue();
244 public String
getFormattedValue() {
245 return IntegerDefinition
.formatNumber(getValue(), fBase
, fSigned
);
251 * The CTF field implementation for string fields
255 final class CTFStringField
extends CtfTmfEventField
{
258 * Constructor for CTFStringField.
261 * The string value of this field
263 * The name of this field
265 CTFStringField(String name
, String strValue
) {
266 super(name
, strValue
, null);
270 public String
getValue() {
271 return (String
) super.getValue();
276 * CTF field implementation for arrays of integers.
280 final class CTFIntegerArrayField
extends CtfTmfEventField
{
282 private final int fBase
;
283 private final boolean fSigned
;
284 private String fFormattedValue
= null;
287 * Constructor for CTFIntegerArrayField.
290 * The name of this field
292 * The array of integers (as longs) that compose this field's
295 * Are the values in the array signed or not
297 CTFIntegerArrayField(String name
, long[] longValues
, int base
, boolean signed
) {
298 super(name
, longValues
, null);
304 public long[] getValue() {
305 return (long[]) super.getValue();
309 public synchronized String
getFormattedValue() {
310 if (fFormattedValue
== null) {
311 List
<String
> strings
= new ArrayList
<>();
312 for (long value
: getValue()) {
313 strings
.add(IntegerDefinition
.formatNumber(value
, fBase
, fSigned
));
315 fFormattedValue
= strings
.toString();
317 return fFormattedValue
;
323 * CTF field implementation for arrays of arbitrary types.
327 final class CTFArrayField
extends CtfTmfEventField
{
329 private String fFormattedValue
= null;
332 * Constructor for CTFArrayField.
335 * The name of this field
337 * The array elements of this field
339 CTFArrayField(String name
, CtfTmfEventField
[] elements
) {
340 super(name
, elements
, elements
);
344 public CtfTmfEventField
[] getValue() {
345 return (CtfTmfEventField
[]) super.getValue();
349 public synchronized String
getFormattedValue() {
350 if (fFormattedValue
== null) {
351 List
<String
> strings
= new ArrayList
<>();
352 for (CtfTmfEventField element
: getValue()) {
353 strings
.add(element
.getFormattedValue());
355 fFormattedValue
= strings
.toString();
357 return fFormattedValue
;
362 * CTF field implementation for floats.
366 final class CTFFloatField
extends CtfTmfEventField
{
369 * Constructor for CTFFloatField.
372 * The float value (actually a double) of this field
374 * The name of this field
376 protected CTFFloatField(String name
, double value
) {
377 super(name
, value
, null);
381 public Double
getValue() {
382 return (Double
) super.getValue();
387 * The CTF field implementation for Enum fields
389 * @author Bernd Hufmann
391 final class CTFEnumField
extends CtfTmfEventField
{
394 * Constructor for CTFEnumField.
397 * The Enum value consisting of a pair of Enum value name and its
400 * The name of this field
402 CTFEnumField(String name
, CtfEnumPair enumValue
) {
403 super(name
, new CtfEnumPair(enumValue
.getFirst(),
404 enumValue
.getSecond()), null);
408 public CtfEnumPair
getValue() {
409 return (CtfEnumPair
) super.getValue();
414 * The CTF field implementation for struct fields with sub-fields
418 final class CTFStructField
extends CtfTmfEventField
{
421 * Constructor for CTFStructField.
424 * The children of this field
426 * The name of this field
428 CTFStructField(String name
, CtfTmfEventField
[] fields
) {
429 super(name
, fields
, fields
);
433 public CtfTmfEventField
[] getValue() {
434 return (CtfTmfEventField
[]) super.getValue();
438 public String
getFormattedValue() {
439 return Arrays
.toString(getValue());
445 * The CTF field implementation for variant fields its child
449 final class CTFVariantField
extends CtfTmfEventField
{
452 * Constructor for CTFVariantField.
455 * The field selected for this variant
457 * The name of this field
459 CTFVariantField(String name
, CtfTmfEventField field
) {
460 super(name
, field
, new CtfTmfEventField
[] { field
});
464 public CtfTmfEventField
getValue() {
465 return (CtfTmfEventField
) super.getValue();
470 /* Implement other possible fields types here... */