summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorGeneviève Bastien2013-03-05 16:08:35 (EST)
committer Alexandre Montplaisir2013-03-19 13:01:43 (EDT)
commit2c5f9387eaed40f7a1989045ff58043c2a1eea19 (patch)
treeb313077693d281083aaa1cb2d7b048cd2eb5bf19
parenta84c6eace8fc13891a283e3e4167ea1023a6220e (diff)
downloadorg.eclipse.linuxtools-2c5f9387eaed40f7a1989045ff58043c2a1eea19.zip
org.eclipse.linuxtools-2c5f9387eaed40f7a1989045ff58043c2a1eea19.tar.gz
org.eclipse.linuxtools-2c5f9387eaed40f7a1989045ff58043c2a1eea19.tar.bz2
tmf: Add Variant field handling in CtfTmfEventFieldrefs/changes/72/10872/8
* Also add unit test for variant types * Move the base-formatting of numeric values to parent class Change-Id: I863fb57ea34dafd0ed7e02caa3f698b4cb88f65c Signed-off-by: Geneviève Bastien <gbastien+lttng@versatic.net> Reviewed-on: https://git.eclipse.org/r/10872 Tested-by: Hudson CI Reviewed-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im> IP-Clean: Alexandre Montplaisir <alexmonthy@voxpopuli.im> Tested-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
-rw-r--r--lttng/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfEventFieldTest.java39
-rw-r--r--lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/Messages.java1
-rw-r--r--lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/messages.properties1
-rw-r--r--lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEventField.java106
4 files changed, 113 insertions, 34 deletions
diff --git a/lttng/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfEventFieldTest.java b/lttng/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfEventFieldTest.java
index e81e49c..aff0160 100644
--- a/lttng/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfEventFieldTest.java
+++ b/lttng/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfEventFieldTest.java
@@ -21,6 +21,7 @@ import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration;
import org.eclipse.linuxtools.ctf.core.event.types.Definition;
import org.eclipse.linuxtools.ctf.core.event.types.Encoding;
+import org.eclipse.linuxtools.ctf.core.event.types.EnumDeclaration;
import org.eclipse.linuxtools.ctf.core.event.types.FloatDeclaration;
import org.eclipse.linuxtools.ctf.core.event.types.FloatDefinition;
import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
@@ -28,6 +29,7 @@ import org.eclipse.linuxtools.ctf.core.event.types.SequenceDeclaration;
import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration;
import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
+import org.eclipse.linuxtools.ctf.core.event.types.VariantDeclaration;
import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEventField;
import org.junit.Before;
import org.junit.Test;
@@ -50,6 +52,8 @@ public class CtfTmfEventFieldTest {
private static final String INT = "int";
private static final String NAME = "test";
private static final String STRUCT = "struct";
+ private static final String VARIANT = "variant";
+ private static final String ENUM = "enum";
private StructDefinition fixture;
@@ -67,6 +71,8 @@ public class CtfTmfEventFieldTest {
ArrayDeclaration arrDec = new ArrayDeclaration(2, intDec);
SequenceDeclaration seqDec = new SequenceDeclaration(LEN, intDec);
StructDeclaration structDec = new StructDeclaration(32);
+ EnumDeclaration enumDec = new EnumDeclaration(intDec);
+ VariantDeclaration varDec = new VariantDeclaration();
sDec.addField(INT, intDec);
sDec.addField(LEN, intDec);
sDec.addField(FLOAT, flDec);
@@ -76,6 +82,13 @@ public class CtfTmfEventFieldTest {
structDec.addField(STR,strDec);
structDec.addField(INT, intDec);
sDec.addField(STRUCT, structDec);
+ enumDec.add(0, 1, LEN);
+ enumDec.add(2, 3, FLOAT);
+ sDec.addField(ENUM, enumDec);
+ varDec.addField(LEN, intDec);
+ varDec.addField(FLOAT, flDec);
+ varDec.setTag(ENUM);
+ sDec.addField(VARIANT, varDec);
fixture = sDec.createDefinition(fixture, ROOT);
int capacity = 2048;
java.nio.ByteBuffer bb = java.nio.ByteBuffer.allocateDirect(capacity);
@@ -86,6 +99,8 @@ public class CtfTmfEventFieldTest {
bb.put((byte) 0);
bb.position(40);
bb.put((byte) 0);
+ bb.position(60);
+ bb.put((byte) 0);
bb.position(0);
fixture.read(new BitBuffer(bb));
}
@@ -107,7 +122,7 @@ public class CtfTmfEventFieldTest {
public void testParseField_array() {
Definition fieldDef = fixture.lookupArray(ARRAY);
CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
- assertEquals("test=[2, 2]", result.toString());
+ assertEquals("test=[02, 02]", result.toString());
}
/**
@@ -127,7 +142,7 @@ public class CtfTmfEventFieldTest {
public void testParseField_sequence() {
Definition fieldDef = fixture.lookupDefinition(SEQ);
CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
- assertEquals("test=[2, 2]", result.toString());
+ assertEquals("test=[02, 02]", result.toString());
}
/**
@@ -159,4 +174,24 @@ public class CtfTmfEventFieldTest {
CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
assertEquals("test=[str=, int=02]", result.toString());
}
+
+ /**
+ * Run the CtfTmfEventField parseField(Definition,String) method test.
+ */
+ @Test
+ public void testParseField_enum() {
+ Definition fieldDef = fixture.lookupDefinition(ENUM);
+ CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
+ assertEquals("test=float", result.toString());
+ }
+
+ /**
+ * Run the CtfTmfEventField parseField(Definition,String) method test.
+ */
+ @Test
+ public void testParseField_variant() {
+ Definition fieldDef = fixture.lookupDefinition(VARIANT);
+ CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
+ assertEquals("float=9.551467814359616E-38", result.toString());
+ }
}
diff --git a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/Messages.java b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/Messages.java
index d399cfb..33075c0 100644
--- a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/Messages.java
+++ b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/Messages.java
@@ -20,6 +20,7 @@ public class Messages extends NLS {
private static final String BUNDLE_NAME = "org.eclipse.linuxtools.internal.tmf.core.messages"; //$NON-NLS-1$
public static String TmfCheckpointIndexer_EventsPerSecond;
public static String TmfCheckpointIndexer_Indexing;
+ public static String TmfEventField_UnsupportedType;
static {
// initialize resource bundle
NLS.initializeMessages(BUNDLE_NAME, Messages.class);
diff --git a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/messages.properties b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/messages.properties
index 6e18880..56a1ad9 100644
--- a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/messages.properties
+++ b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/messages.properties
@@ -1,2 +1,3 @@
TmfCheckpointIndexer_EventsPerSecond=events/s
TmfCheckpointIndexer_Indexing=Indexing
+TmfEventField_UnsupportedType=Unsupported field type:
diff --git a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEventField.java b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEventField.java
index f06eb5b..f09b328 100644
--- a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEventField.java
+++ b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEventField.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2011-2013 Ericsson
+ * Copyright (c) 2011, 2013 Ericsson, École Polytechnique de Montréal
*
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License v1.0 which
@@ -8,9 +8,9 @@
*
* Contributors:
* Matthew Khouzam - Initial API and implementation
- * Alexendre Montplaisir - Initial API and implementation, extend TmfEventField
+ * Alexandre Montplaisir - Initial API and implementation, extend TmfEventField
* Bernd Hufmann - Add Enum field handling
- * Geneviève Bastien - Add support for Struct fields
+ * Geneviève Bastien - Add Struct and Variant field handling
*******************************************************************************/
package org.eclipse.linuxtools.tmf.core.ctfadaptor;
@@ -31,6 +31,8 @@ import org.eclipse.linuxtools.ctf.core.event.types.SequenceDeclaration;
import org.eclipse.linuxtools.ctf.core.event.types.SequenceDefinition;
import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition;
import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
+import org.eclipse.linuxtools.ctf.core.event.types.VariantDefinition;
+import org.eclipse.linuxtools.internal.tmf.core.Messages;
import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
@@ -136,7 +138,7 @@ public abstract class CtfTmfEventField extends TmfEventField {
for (int i = 0; i < arrayDecl.getLength(); i++) {
values.add(((IntegerDefinition) arrayDef.getElem(i)).getValue());
}
- field = new CTFIntegerArrayField(fieldName, values);
+ field = new CTFIntegerArrayField(fieldName, values, ((IntegerDeclaration) arrayDecl.getElementType()).getBase());
}
/* Add other types of arrays here */
@@ -156,7 +158,7 @@ public abstract class CtfTmfEventField extends TmfEventField {
for (int i = 0; i < seqDef.getLength(); i++) {
values.add(((IntegerDefinition) seqDef.getElem(i)).getValue());
}
- field = new CTFIntegerArrayField(fieldName, values);
+ field = new CTFIntegerArrayField(fieldName, values, ((IntegerDeclaration) seqDecl.getElementType()).getBase());
}
/* Add other Sequence types here */
@@ -175,6 +177,21 @@ public abstract class CtfTmfEventField extends TmfEventField {
list.add(curField);
}
field = new CTFStructField(fieldName, list.toArray(new CtfTmfEventField[list.size()]));
+ } else if (fieldDef instanceof VariantDefinition) {
+ VariantDefinition varDef = (VariantDefinition) fieldDef;
+
+ String curFieldName = varDef.getCurrentFieldName();
+ Definition curFieldDef = varDef.getDefinitions().get(curFieldName);
+ if (curFieldDef != null) {
+ field = CtfTmfEventField.parseField(curFieldDef, curFieldName);
+ } else {
+ /* A safe-guard, but curFieldDef should never be null */
+ field = new CTFStringField(curFieldName, ""); //$NON-NLS-1$
+ }
+
+ } else {
+ /* Safe-guard, to avoid null exceptions later, field is expected not to be null */
+ field = new CTFStringField(fieldName, Messages.TmfEventField_UnsupportedType + fieldDef.getClass().toString());
}
return field;
}
@@ -195,6 +212,40 @@ public abstract class CtfTmfEventField extends TmfEventField {
*/
public abstract int getFieldType();
+ /**
+ * Print a numeric value as a string in a given base
+ *
+ * @param value
+ * The value to print as string
+ * @param base
+ * The base for this value
+ * @return formatted number string
+ * @since 2.0
+ */
+ protected final static String formatNumber(long value, int base) {
+ String s;
+ /* Format the number correctly according to the integer's base */
+ switch (base) {
+ case 2:
+ s = "0b" + Long.toBinaryString(value); //$NON-NLS-1$
+ break;
+ case 8:
+ s = "0" + Long.toOctalString(value); //$NON-NLS-1$
+ break;
+ case 10:
+ s = Long.toString(value);
+ break;
+ case 16:
+ s = "0x" + Long.toHexString(value); //$NON-NLS-1$
+ break;
+ default:
+ /* Non-standard base, we'll just print it as a decimal number */
+ s = Long.toString(value);
+ break;
+ }
+ return s;
+ }
+
}
/**
@@ -235,32 +286,7 @@ final class CTFIntegerField extends CtfTmfEventField {
*/
@Override
public String toString() {
- StringBuilder sb = new StringBuilder(getName());
- sb.append('=');
-
- /* Format the number correctly according to the integer's base */
- switch (base) {
- case 2:
- sb.append("0b"); //$NON-NLS-1$
- sb.append(Long.toBinaryString(getValue()));
- break;
- case 8:
- sb.append('0');
- sb.append(Long.toOctalString(getValue()));
- break;
- case 10:
- sb.append(getValue());
- break;
- case 16:
- sb.append("0x"); //$NON-NLS-1$
- sb.append(Long.toHexString(getValue()));
- break;
- default:
- /* Non-standard base, we'll just print it as a decimal number */
- sb.append(getValue().toString());
- break;
- }
- return sb.toString();
+ return getName() + '=' + formatNumber(getValue(), base);
}
}
@@ -301,6 +327,8 @@ final class CTFStringField extends CtfTmfEventField {
*/
final class CTFIntegerArrayField extends CtfTmfEventField {
+ private final int base;
+
/**
* Constructor for CTFIntegerArrayField.
*
@@ -310,8 +338,9 @@ final class CTFIntegerArrayField extends CtfTmfEventField {
* @param name
* The name of this field
*/
- CTFIntegerArrayField(String name, List<Long> longValues) {
+ CTFIntegerArrayField(String name, List<Long> longValues, int base) {
super(name, longValues, null);
+ this.base = base;
}
@Override
@@ -323,6 +352,19 @@ final class CTFIntegerArrayField extends CtfTmfEventField {
public List<Long> getValue() {
return (List<Long>) super.getValue();
}
+
+ /**
+ * Custom-format the integer values depending on their base.
+ */
+ @Override
+ public String toString() {
+ List<String> strings = new ArrayList<String>();
+
+ for (Long value : getValue() ) {
+ strings.add(formatNumber(value, base));
+ }
+ return getName() + '=' + strings.toString();
+ }
}
/**