ctf: Succeed at failing on OOME in tests
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfEventField.java
CommitLineData
a3fc8213 1/*******************************************************************************
404b264a 2 * Copyright (c) 2011, 2013 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
a3fc8213
AM
15 *******************************************************************************/
16
17package org.eclipse.linuxtools.tmf.core.ctfadaptor;
18
a6223d74 19import java.util.ArrayList;
7a6cee1a 20import java.util.Arrays;
a6223d74 21import java.util.List;
7a6cee1a 22import java.util.Map.Entry;
a6223d74 23
a3fc8213
AM
24import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration;
25import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition;
26import org.eclipse.linuxtools.ctf.core.event.types.Definition;
21fb02fa 27import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition;
a04464b1 28import org.eclipse.linuxtools.ctf.core.event.types.FloatDefinition;
a3fc8213
AM
29import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
30import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
31import org.eclipse.linuxtools.ctf.core.event.types.SequenceDeclaration;
32import org.eclipse.linuxtools.ctf.core.event.types.SequenceDefinition;
33import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition;
7a6cee1a 34import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
404b264a
GB
35import org.eclipse.linuxtools.ctf.core.event.types.VariantDefinition;
36import org.eclipse.linuxtools.internal.tmf.core.Messages;
7a6cee1a 37import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
68b18f2f 38import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
a3fc8213
AM
39
40/**
7558a1e1
AM
41 * The CTF implementation of the TMF event field model
42 *
d4a8d935 43 * @version 2.0
7558a1e1 44 * @author Matthew Khouzam
d09f973b 45 * @author Alexandre Montplaisir
a3fc8213 46 */
68b18f2f 47public abstract class CtfTmfEventField extends TmfEventField {
a3fc8213 48
a3fc8213 49 // ------------------------------------------------------------------------
7558a1e1 50 // Constructor
a3fc8213
AM
51 // ------------------------------------------------------------------------
52
b1baa808 53 /**
7558a1e1
AM
54 * Standard constructor. Only to be used internally, call parseField() to
55 * generate a new field object.
56 *
57 * @param name
58 * The name of this field
68b18f2f
AM
59 * @param value
60 * The value of this field. Its type should match the field type.
81ed27a8
MK
61 * @param fields
62 * The children fields. Useful for composite fields
68b18f2f 63 * @since 2.0
b1baa808 64 */
81ed27a8 65 protected CtfTmfEventField(String name, Object value, ITmfEventField[] fields) {
68b18f2f
AM
66 super(/* Strip the underscore from the field name if there is one */
67 name.startsWith("_") ? name.substring(1) : name, //$NON-NLS-1$
68 value,
81ed27a8 69 fields);
a3fc8213
AM
70 }
71
72 // ------------------------------------------------------------------------
73 // Operations
74 // ------------------------------------------------------------------------
75
b1baa808 76 /**
7558a1e1
AM
77 * Factory method to instantiate CtfTmfEventField objects.
78 *
79 * @param fieldDef
80 * The CTF Definition of this event field
81 * @param fieldName
82 * String The name to assign to this field
83 * @return The resulting CtfTmfEventField object
b1baa808 84 */
a3fc8213
AM
85 public static CtfTmfEventField parseField(Definition fieldDef,
86 String fieldName) {
87 CtfTmfEventField field = null;
88
89 /* Determine the Definition type */
90 if (fieldDef instanceof IntegerDefinition) {
367bcd2b
AM
91 IntegerDefinition intDef = (IntegerDefinition) fieldDef;
92 int base = intDef.getDeclaration().getBase();
459f705b 93 field = new CTFIntegerField(fieldName, intDef.getValue(), base, intDef.getDeclaration().isSigned());
a3fc8213 94
21fb02fa
MK
95 } else if (fieldDef instanceof EnumDefinition) {
96 EnumDefinition enumDef = (EnumDefinition) fieldDef;
68b18f2f 97 field = new CTFEnumField(fieldName, new CtfEnumPair(enumDef.getValue(), enumDef.getIntegerValue()));
21fb02fa 98
a3fc8213 99 } else if (fieldDef instanceof StringDefinition) {
68b18f2f
AM
100 field = new CTFStringField(fieldName, ((StringDefinition) fieldDef).getValue());
101
102 } else if (fieldDef instanceof FloatDefinition) {
103 FloatDefinition floatDef = (FloatDefinition) fieldDef;
104 field = new CTFFloatField(fieldName, floatDef.getValue());
a3fc8213
AM
105
106 } else if (fieldDef instanceof ArrayDefinition) {
107 ArrayDefinition arrayDef = (ArrayDefinition) fieldDef;
108 ArrayDeclaration arrayDecl = arrayDef.getDeclaration();
109
110 if (arrayDef.isString()) {
111 /* This is an array of UTF-8 bytes, a.k.a. a String! */
68b18f2f 112 field = new CTFStringField(fieldName, fieldDef.toString());
a3fc8213
AM
113
114 } else if (arrayDecl.getElementType() instanceof IntegerDeclaration) {
115 /* This is a an array of CTF Integers */
68b18f2f 116 List<Long> values = new ArrayList<Long>(arrayDecl.getLength());
a3fc8213 117 for (int i = 0; i < arrayDecl.getLength(); i++) {
68b18f2f 118 values.add(((IntegerDefinition) arrayDef.getElem(i)).getValue());
a3fc8213 119 }
cefe3edf
AM
120 long[] valuesArray = convertListToArray(values);
121 field = new CTFIntegerArrayField(fieldName, valuesArray,
122 ((IntegerDeclaration) arrayDecl.getElementType()).getBase(),
459f705b 123 ((IntegerDeclaration) arrayDecl.getElementType()).isSigned());
a3fc8213
AM
124 }
125 /* Add other types of arrays here */
126
127 } else if (fieldDef instanceof SequenceDefinition) {
128 SequenceDefinition seqDef = (SequenceDefinition) fieldDef;
129 SequenceDeclaration seqDecl = seqDef.getDeclaration();
130
131 if (seqDef.getLength() == 0) {
132 /* Some sequences have length = 0. Simply use an empty string */
68b18f2f 133 field = new CTFStringField(fieldName, ""); //$NON-NLS-1$
a3fc8213
AM
134 } else if (seqDef.isString()) {
135 /* Interpret this sequence as a String */
68b18f2f 136 field = new CTFStringField(fieldName, seqDef.toString());
a3fc8213
AM
137 } else if (seqDecl.getElementType() instanceof IntegerDeclaration) {
138 /* Sequence of integers => CTFIntegerArrayField */
68b18f2f 139 List<Long> values = new ArrayList<Long>(seqDef.getLength());
a3fc8213 140 for (int i = 0; i < seqDef.getLength(); i++) {
68b18f2f 141 values.add(((IntegerDefinition) seqDef.getElem(i)).getValue());
a3fc8213 142 }
cefe3edf
AM
143 long[] valuesArray = convertListToArray(values);
144 field = new CTFIntegerArrayField(fieldName, valuesArray,
145 ((IntegerDeclaration) seqDecl.getElementType()).getBase(),
459f705b 146 ((IntegerDeclaration) seqDecl.getElementType()).isSigned());
a3fc8213
AM
147 }
148 /* Add other Sequence types here */
367bcd2b 149
7a6cee1a
GB
150 } else if (fieldDef instanceof StructDefinition) {
151 StructDefinition strDef = (StructDefinition) fieldDef;
152
153 String curFieldName = null;
154 Definition curFieldDef;
155 CtfTmfEventField curField;
156 List<ITmfEventField> list = new ArrayList<ITmfEventField>();
157 /* Recursively parse the fields */
158 for (Entry<String, Definition> entry : strDef.getDefinitions().entrySet()) {
159 curFieldName = entry.getKey();
160 curFieldDef = entry.getValue();
161 curField = CtfTmfEventField.parseField(curFieldDef, curFieldName);
162 list.add(curField);
163 }
164 field = new CTFStructField(fieldName, list.toArray(new CtfTmfEventField[list.size()]));
a0e9eac8 165
404b264a
GB
166 } else if (fieldDef instanceof VariantDefinition) {
167 VariantDefinition varDef = (VariantDefinition) fieldDef;
168
169 String curFieldName = varDef.getCurrentFieldName();
170 Definition curFieldDef = varDef.getDefinitions().get(curFieldName);
171 if (curFieldDef != null) {
51cc7ef4
GB
172 CtfTmfEventField subField = CtfTmfEventField.parseField(curFieldDef, curFieldName);
173 field = new CTFVariantField(fieldName, subField);
404b264a
GB
174 } else {
175 /* A safe-guard, but curFieldDef should never be null */
176 field = new CTFStringField(curFieldName, ""); //$NON-NLS-1$
177 }
178
179 } else {
459f705b
JCK
180 /*
181 * Safe-guard, to avoid null exceptions later, field is expected not
182 * to be null
183 */
404b264a 184 field = new CTFStringField(fieldName, Messages.TmfEventField_UnsupportedType + fieldDef.getClass().toString());
a3fc8213 185 }
a3fc8213
AM
186 return field;
187 }
188
a3fc8213 189 @Override
68b18f2f 190 public String toString() {
51cc7ef4 191 return getName() + '=' + getFormattedValue();
a3fc8213
AM
192 }
193
cefe3edf
AM
194 /**
195 * We cannot use List.toArray(T[]) for primitives types, so do it manually.
196 */
197 private static long[] convertListToArray(List<Long> list) {
198 long[] array = new long[list.size()];
199 for (int i = 0; i < list.size(); i++) {
200 array[i] = list.get(i);
201 }
202 return array;
203 }
204
a3fc8213
AM
205}
206
207/**
7558a1e1
AM
208 * The CTF field implementation for integer fields.
209 *
210 * @author alexmont
a3fc8213
AM
211 */
212final class CTFIntegerField extends CtfTmfEventField {
213
367bcd2b 214 private final int base;
459f705b 215 private final boolean signed;
a3fc8213
AM
216
217 /**
218 * A CTF "IntegerDefinition" can be an integer of any byte size, so in the
219 * Java parser this is interpreted as a long.
7558a1e1 220 *
7558a1e1
AM
221 * @param name
222 * The name of this field
459f705b
JCK
223 * @param longValue
224 * The integer value of this field
225 * @param signed
226 * Is the value signed or not
a3fc8213 227 */
459f705b 228 CTFIntegerField(String name, long longValue, int base, boolean signed) {
81ed27a8 229 super(name, longValue, null);
459f705b 230 this.signed = signed;
367bcd2b
AM
231 this.base = base;
232 }
233
a3fc8213
AM
234 @Override
235 public Long getValue() {
68b18f2f 236 return (Long) super.getValue();
a3fc8213
AM
237 }
238
8f86c552
GB
239 @Override
240 public String getFormattedValue() {
3b11ddc7 241 return IntegerDefinition.formatNumber(getValue(), base, signed);
8f86c552
GB
242 }
243
a3fc8213
AM
244}
245
246/**
7558a1e1
AM
247 * The CTF field implementation for string fields
248 *
249 * @author alexmont
a3fc8213
AM
250 */
251final class CTFStringField extends CtfTmfEventField {
252
b1baa808
MK
253 /**
254 * Constructor for CTFStringField.
7558a1e1
AM
255 *
256 * @param strValue
257 * The string value of this field
258 * @param name
259 * The name of this field
b1baa808 260 */
68b18f2f 261 CTFStringField(String name, String strValue) {
81ed27a8 262 super(name, strValue, null);
a3fc8213
AM
263 }
264
a3fc8213
AM
265 @Override
266 public String getValue() {
68b18f2f 267 return (String) super.getValue();
a3fc8213
AM
268 }
269}
270
271/**
7558a1e1
AM
272 * CTF field implementation for arrays of integers.
273 *
274 * @author alexmont
a3fc8213
AM
275 */
276final class CTFIntegerArrayField extends CtfTmfEventField {
277
404b264a 278 private final int base;
459f705b 279 private final boolean signed;
8f86c552 280 private String formattedValue = null;
404b264a 281
b1baa808
MK
282 /**
283 * Constructor for CTFIntegerArrayField.
7558a1e1 284 *
459f705b
JCK
285 * @param name
286 * The name of this field
7558a1e1
AM
287 * @param longValues
288 * The array of integers (as longs) that compose this field's
289 * value
459f705b
JCK
290 * @param signed
291 * Are the values in the array signed or not
b1baa808 292 */
cefe3edf 293 CTFIntegerArrayField(String name, long[] longValues, int base, boolean signed) {
81ed27a8 294 super(name, longValues, null);
404b264a 295 this.base = base;
459f705b 296 this.signed = signed;
a3fc8213
AM
297 }
298
a6223d74 299 @Override
cefe3edf
AM
300 public long[] getValue() {
301 return (long[]) super.getValue();
a3fc8213 302 }
404b264a 303
8f86c552
GB
304 @Override
305 public String getFormattedValue() {
306 if (formattedValue == null) {
307 List<String> strings = new ArrayList<String>();
cefe3edf 308 for (long value : getValue()) {
3b11ddc7 309 strings.add(IntegerDefinition.formatNumber(value, base, signed));
8f86c552
GB
310 }
311 formattedValue = strings.toString();
312 }
313 return formattedValue;
314 }
315
a3fc8213
AM
316}
317
b1baa808 318/**
7558a1e1
AM
319 * CTF field implementation for floats.
320 *
321 * @author emathko
b1baa808 322 */
a04464b1
MK
323final class CTFFloatField extends CtfTmfEventField {
324
b1baa808
MK
325 /**
326 * Constructor for CTFFloatField.
7558a1e1
AM
327 *
328 * @param value
329 * The float value (actually a double) of this field
330 * @param name
331 * The name of this field
b1baa808 332 */
68b18f2f 333 protected CTFFloatField(String name, double value) {
81ed27a8 334 super(name, value, null);
a04464b1
MK
335 }
336
a04464b1 337 @Override
81c8e6f7 338 public Double getValue() {
68b18f2f 339 return (Double) super.getValue();
a04464b1 340 }
a04464b1 341}
7558a1e1 342
d4a8d935
BH
343/**
344 * The CTF field implementation for Enum fields
345 *
346 * @author Bernd Hufmann
347 */
348final class CTFEnumField extends CtfTmfEventField {
349
d4a8d935
BH
350 /**
351 * Constructor for CTFEnumField.
352 *
353 * @param enumValue
459f705b
JCK
354 * The Enum value consisting of a pair of Enum value name and its
355 * long value
d4a8d935
BH
356 * @param name
357 * The name of this field
358 */
68b18f2f
AM
359 CTFEnumField(String name, CtfEnumPair enumValue) {
360 super(name, new CtfEnumPair(enumValue.getFirst(),
459f705b 361 enumValue.getSecond().longValue()), null);
d4a8d935
BH
362 }
363
d4a8d935 364 @Override
68b18f2f
AM
365 public CtfEnumPair getValue() {
366 return (CtfEnumPair) super.getValue();
d4a8d935
BH
367 }
368}
369
7a6cee1a 370/**
51cc7ef4 371 * The CTF field implementation for struct fields with sub-fields
7a6cee1a
GB
372 *
373 * @author gbastien
374 */
375final class CTFStructField extends CtfTmfEventField {
376
377 /**
51cc7ef4 378 * Constructor for CTFStructField.
7a6cee1a 379 *
51cc7ef4
GB
380 * @param fields
381 * The children of this field
7a6cee1a
GB
382 * @param name
383 * The name of this field
384 */
385 CTFStructField(String name, CtfTmfEventField[] fields) {
81ed27a8 386 super(name, fields, fields);
7a6cee1a
GB
387 }
388
7a6cee1a
GB
389 @Override
390 public CtfTmfEventField[] getValue() {
391 return (CtfTmfEventField[]) super.getValue();
392 }
393
394 @Override
51cc7ef4
GB
395 public String getFormattedValue() {
396 return Arrays.toString(getValue());
7a6cee1a 397 }
51cc7ef4
GB
398
399}
400
401/**
402 * The CTF field implementation for variant fields its child
403 *
404 * @author gbastien
405 */
406final class CTFVariantField extends CtfTmfEventField {
407
408 /**
409 * Constructor for CTFVariantField.
410 *
411 * @param field
412 * The field selected for this variant
413 * @param name
414 * The name of this field
415 */
416 CTFVariantField(String name, CtfTmfEventField field) {
459f705b 417 super(name, field, new CtfTmfEventField[] { field });
51cc7ef4
GB
418 }
419
420 @Override
421 public CtfTmfEventField getValue() {
422 return (CtfTmfEventField) super.getValue();
423 }
424
7a6cee1a
GB
425}
426
a3fc8213 427/* Implement other possible fields types here... */
This page took 0.062661 seconds and 5 git commands to generate.