ctf: Make events immutable
authorMatthew Khouzam <matthew.khouzam@ericsson.com>
Fri, 21 Mar 2014 22:14:03 +0000 (18:14 -0400)
committerMatthew Khouzam <matthew.khouzam@ericsson.com>
Fri, 2 May 2014 02:37:52 +0000 (22:37 -0400)
This patch does some major changes to the ctf parser

The event definitions are now immutable and can no longer be modified.
The declarations will read the bitbuffer and then create event
definitions. The event definitions are going to have fields. Initial
tests show a slight speedup but the parser is 66% slower without
eclipse.

Change-Id: I52b8c0de9776fa7cd2b333628c2bb6d3dd2c86ac
Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/23740
Tested-by: Hudson CI
Reviewed-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
Tested-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
72 files changed:
org.eclipse.linuxtools.ctf.core.tests/.settings/org.eclipse.jdt.core.prefs
org.eclipse.linuxtools.ctf.core.tests/META-INF/MANIFEST.MF
org.eclipse.linuxtools.ctf.core.tests/build.properties
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/AllCtfCoreTests.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/event/CTFEventFieldTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/headless/ReadTrace.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/scope/LexicalScopeTest.java [new file with mode: 0644]
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/scope/TestAll.java [new file with mode: 0644]
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/trace/CTFTraceGrowingStreamTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/trace/StreamInputReaderTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/trace/StreamInputTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/ArrayDeclarationTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/ArrayDefinitionTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/DefinitionTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/EnumDeclarationTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/EnumDefinitionTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/EventDeclarationTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/FloatDefinitionTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/IntegerDeclarationTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/IntegerDefinitionTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/IntegerEndiannessTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/SequenceDeclarationTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/SequenceDefinitionTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/StringDeclarationTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/StringDefinitionTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/StructDeclarationTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/StructDefinitionTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/VariantDeclarationTest.java
org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/types/VariantDefinitionTest.java
org.eclipse.linuxtools.ctf.core/META-INF/MANIFEST.MF
org.eclipse.linuxtools.ctf.core/build.properties
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/CTFStrings.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/EventDefinition.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/IEventDeclaration.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/scope/IDefinitionScope.java [new file with mode: 0644]
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/scope/LexicalScope.java [new file with mode: 0644]
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/ArrayDeclaration.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/ArrayDefinition.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/Declaration.java [new file with mode: 0644]
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/Definition.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/Encoding.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/EnumDeclaration.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/EnumDefinition.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/FloatDeclaration.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/FloatDefinition.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IDeclaration.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IDefinitionScope.java [deleted file]
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IntegerDeclaration.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IntegerDefinition.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/SequenceDeclaration.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/SequenceDefinition.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/SimpleDatatypeDefinition.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/StringDeclaration.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/StringDefinition.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/StructDeclaration.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/StructDefinition.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/VariantDeclaration.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/VariantDefinition.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFTrace.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/StreamInput.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/StreamInputPacketReader.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/StreamInputReader.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/Utils.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/event/EventDeclaration.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/internal/ctf/core/event/metadata/IOStructGen.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/ITmfEvent.java
org.eclipse.linuxtools.tmf.ctf.core.tests/src/org/eclipse/linuxtools/tmf/ctf/core/tests/CtfTmfEventFieldTest.java
org.eclipse.linuxtools.tmf.ctf.core.tests/src/org/eclipse/linuxtools/tmf/ctf/core/tests/CtfTmfEventTest.java
org.eclipse.linuxtools.tmf.ctf.core/src/org/eclipse/linuxtools/tmf/ctf/core/CtfTmfEvent.java
org.eclipse.linuxtools.tmf.ctf.core/src/org/eclipse/linuxtools/tmf/ctf/core/CtfTmfEventFactory.java
org.eclipse.linuxtools.tmf.ctf.core/src/org/eclipse/linuxtools/tmf/ctf/core/CtfTmfEventField.java
org.eclipse.linuxtools.tmf.ctf.core/src/org/eclipse/linuxtools/tmf/ctf/core/CtfTmfLostEvent.java

index 38427276f61a61ed33cdc82a42e7d0a04910f6a4..ca28311011622fe2ebb18c90506f4688757b9780 100644 (file)
@@ -4,7 +4,7 @@ org.eclipse.jdt.core.compiler.annotation.missingNonNullByDefaultAnnotation=ignor
 org.eclipse.jdt.core.compiler.annotation.nonnull=org.eclipse.jdt.annotation.NonNull
 org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=org.eclipse.jdt.annotation.NonNullByDefault
 org.eclipse.jdt.core.compiler.annotation.nullable=org.eclipse.jdt.annotation.Nullable
-org.eclipse.jdt.core.compiler.annotation.nullanalysis=disabled
+org.eclipse.jdt.core.compiler.annotation.nullanalysis=enabled
 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
 org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
index 5cffa7d0b576d120e5921c522203cdab5e1339b8..aabf9aec402a9118627a319352dc626e194c0b65 100644 (file)
@@ -16,8 +16,10 @@ Export-Package: org.eclipse.linuxtools.ctf.core.tests;x-friends:="org.eclipse.li
  org.eclipse.linuxtools.ctf.core.tests.event;x-internal:=true,
  org.eclipse.linuxtools.ctf.core.tests.headless;x-internal:=true,
  org.eclipse.linuxtools.ctf.core.tests.io;x-internal:=true,
+ org.eclipse.linuxtools.ctf.core.tests.scope,
  org.eclipse.linuxtools.ctf.core.tests.shared,
  org.eclipse.linuxtools.ctf.core.tests.synthetictraces;x-internal:=true,
  org.eclipse.linuxtools.ctf.core.tests.trace;x-internal:=true,
  org.eclipse.linuxtools.ctf.core.tests.types;x-internal:=true
-Import-Package: org.antlr.runtime;version="3.2.0"
+Import-Package: com.google.common.collect,
+ org.antlr.runtime;version="3.2.0"
index 75279868cbf34333d9fa113c246fa96c93304498..7006a6f0fe89fbd90a68f730275f0f8a387c957c 100644 (file)
@@ -18,3 +18,5 @@ bin.includes = META-INF/,\
                plugin.properties,\
                traces/get-traces.xml
 src.includes = about.html
+additional.bundles = org.eclipse.jdt.annotation
+jars.extra.classpath = platform:/plugin/org.eclipse.jdt.annotation
index b4867d9eba771b5a82a0d3a8fe6fc3a689c3fde4..c096537d72e218124a538a35e7214904ca30e673 100644 (file)
@@ -24,13 +24,13 @@ import org.junit.runners.Suite;
  */
 @RunWith(Suite.class)
 @Suite.SuiteClasses({
-    CtfCorePluginTest.class,
-    org.eclipse.linuxtools.ctf.core.tests.ctftestsuite.TestAll.class,
-    org.eclipse.linuxtools.ctf.core.tests.event.TestAll.class,
-    org.eclipse.linuxtools.ctf.core.tests.io.TestAll.class,
-    org.eclipse.linuxtools.ctf.core.tests.trace.TestAll.class,
-    org.eclipse.linuxtools.ctf.core.tests.types.TestAll.class
+        CtfCorePluginTest.class,
+        org.eclipse.linuxtools.ctf.core.tests.ctftestsuite.TestAll.class,
+        org.eclipse.linuxtools.ctf.core.tests.event.TestAll.class,
+        org.eclipse.linuxtools.ctf.core.tests.io.TestAll.class,
+        org.eclipse.linuxtools.ctf.core.tests.scope.TestAll.class,
+        org.eclipse.linuxtools.ctf.core.tests.trace.TestAll.class,
+        org.eclipse.linuxtools.ctf.core.tests.types.TestAll.class
 })
 public class AllCtfCoreTests {
-
 }
index 379dee49546b472e51521a3ed6c367d7c1a0483c..2e7224009c0ecd28837b985baa93841dbfa3293d 100644 (file)
@@ -17,15 +17,13 @@ import static org.junit.Assert.assertNotNull;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 
+import org.eclipse.jdt.annotation.NonNull;
 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.ArrayDefinition;
 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.IntegerDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
 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.StringDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
@@ -33,6 +31,8 @@ import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
 import org.junit.Test;
 
+import com.google.common.collect.ImmutableList;
+
 /**
  * The class <code>CTFEventFieldTest</code> contains tests for the class
  * <code>{@link CTFEventField}</code>.
@@ -43,44 +43,63 @@ import org.junit.Test;
 @SuppressWarnings("javadoc")
 public class CTFEventFieldTest {
 
-    private static final String fieldName = "id";
+    @NonNull private static final String fieldName = "id";
 
     /**
      * Run the CTFEventField parseField(Definition,String) method test.
+     *
      * @throws CTFReaderException
      */
     @Test
     public void testParseField_complex() throws CTFReaderException {
         int len = 32;
-        IntegerDeclaration id = new IntegerDeclaration(len, false, len,
-                ByteOrder.LITTLE_ENDIAN, Encoding.ASCII, null, 32);
+        IntegerDeclaration id = IntegerDeclaration.createDeclaration(
+                len,
+                false,
+                len,
+                ByteOrder.LITTLE_ENDIAN,
+                Encoding.ASCII,
+                "",
+                len);
         String lengthName = "LengthName";
         StructDeclaration structDec = new StructDeclaration(0);
         structDec.addField(lengthName, id);
-        StructDefinition structDef = new StructDefinition(structDec, null,
-                lengthName);
+        StructDefinition structDef = new StructDefinition(
+                structDec,
+                null,
+                lengthName,
+                ImmutableList.of(lengthName),
+                new Definition[] {
+                        new IntegerDefinition(
+                                id,
+                                null,
+                                lengthName,
+                                32)
+                });
 
-        structDef.lookupInteger(lengthName).setValue(32);
         SequenceDeclaration sd = new SequenceDeclaration(lengthName, id);
-        Definition fieldDef = new SequenceDefinition(sd, structDef, "TestX");
         ByteBuffer byb = ByteBuffer.allocate(1024);
         for (int i = 0; i < 1024; i++) {
             byb.put((byte) i);
         }
         BitBuffer bb = new BitBuffer(byb);
-        fieldDef.read(bb);
+        Definition fieldDef = sd.createDefinition(structDef, "fff-fffield", bb);
 
         assertNotNull(fieldDef);
     }
 
     /**
      * Run the CTFEventField parseField(Definition,String) method test.
+     *
      * @throws CTFReaderException
      */
     @Test
-    public void testParseField_simple() {
+    public void testParseField_simple() throws CTFReaderException {
         final StringDeclaration elemType = new StringDeclaration();
-        Definition fieldDef = elemType.createDefinition(null, fieldName);
+        byte[] bytes = { 'T', 'e', 's', 't', '\0' };
+        ByteBuffer bb = ByteBuffer.wrap(bytes);
+
+        Definition fieldDef = elemType.createDefinition(null, fieldName, new BitBuffer(bb));
 
         assertNotNull(fieldDef);
     }
@@ -91,9 +110,8 @@ public class CTFEventFieldTest {
     @Test
     public void testParseField_simple2() {
         IntegerDefinition fieldDef = new IntegerDefinition(
-                new IntegerDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN,
-                        Encoding.ASCII, null, 8), null, fieldName);
-        fieldDef.setValue(1L);
+                IntegerDeclaration.createDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN,
+                        Encoding.ASCII, "", 8), null, fieldName, 1L);
 
         assertNotNull(fieldDef);
     }
@@ -104,61 +122,11 @@ public class CTFEventFieldTest {
     @Test
     public void testParseField_simple3() {
         StringDefinition fieldDef = new StringDefinition(
-                new StringDeclaration(), null, fieldName);
-        fieldDef.setValue("Hello World");
+                new StringDeclaration(), null, fieldName, "Hello World");
 
         String other = "\"Hello World\"";
         assertNotNull(fieldDef);
         assertEquals(fieldDef.toString(), other);
     }
 
-    /**
-     * Run the CTFEventField parseField(Definition,String) method test.
-     */
-    @Test
-    public void testParseField_manual() {
-        Definition fieldDef = new ArrayDefinition(new ArrayDeclaration(20,
-                new IntegerDeclaration(8, false, 8, null, Encoding.UTF8, null, 8)),
-                null, fieldName);
-        ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[0]).setValue('H');
-        ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[1]).setValue('e');
-        ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[2]).setValue('l');
-        ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[3]).setValue('l');
-        ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[4]).setValue('o');
-        ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[5]).setValue(' ');
-        ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[6]).setValue('W');
-        ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[7]).setValue('o');
-        ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[8]).setValue('r');
-        ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[9]).setValue('l');
-        ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[10]).setValue('d');
-        ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[11]).setValue(0);
-
-        assertNotNull(fieldDef);
-    }
-
-    /**
-     * Run the CTFEventField parseField(Definition,String) method test.
-     */
-    @Test
-    public void testParseField_manual2() {
-        Definition fieldDef = new ArrayDefinition(new ArrayDeclaration(12,
-                new IntegerDeclaration(32, false, 32, null, null, null, 8)), null,
-                fieldName);
-        ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[0]).setValue('H');
-        ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[1]).setValue('e');
-        ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[2]).setValue('l');
-        ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[3]).setValue('l');
-        ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[4]).setValue('o');
-        ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[5]).setValue(' ');
-        ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[6]).setValue('W');
-        ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[7]).setValue('o');
-        ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[8]).setValue('r');
-        ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[9]).setValue('l');
-        ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[10]).setValue('d');
-        ((IntegerDefinition) ((ArrayDefinition) fieldDef).getDefinitions()[11]).setValue(0);
-
-        assertNotNull(fieldDef);
-        String other = "[ 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 0 ]";
-        assertEquals(other, fieldDef.toString());
-    }
 }
\ No newline at end of file
index 8eb578acbb7996582cc1b0b546bf665c25421376..2541d9da60231ea6d02ec2d132201e60f87648b8 100644 (file)
@@ -12,6 +12,7 @@
 
 package org.eclipse.linuxtools.ctf.core.tests.headless;
 
+import java.io.FileNotFoundException;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.Date;
@@ -27,14 +28,15 @@ public class ReadTrace {
 
     /**
      * @param args
+     * @throws FileNotFoundException
      */
-    public static void main(String[] args) {
+    public static void main(String[] args) throws FileNotFoundException {
         final String TRACE_PATH = "traces/kernel";
 
         // Change this to enable text output
         final boolean USE_TEXT = false;
 
-        final int LOOP_COUNT = 1;
+        final int LOOP_COUNT = 10;
 
         // Work variables
         long nbEvent = 0L;
@@ -46,34 +48,29 @@ public class ReadTrace {
                 nbEvent = 0L;
                 trace = new CTFTrace(TRACE_PATH);
             } catch (CTFReaderException e) {
-                // do nothing
+                throw new FileNotFoundException(TRACE_PATH);
             }
             start = System.nanoTime();
             if (USE_TEXT) {
                 System.out.println("Event, " + " Time, " + " type, " + " CPU ");
             }
             try (CTFTraceReader traceReader = new CTFTraceReader(trace);) {
-                if (trace != null) {
-                    start = System.nanoTime();
+                start = System.nanoTime();
 
-                    while (traceReader.hasMoreEvents()) {
-                        EventDefinition ed = traceReader.getCurrentEventDef();
-                        nbEvent++;
-                        if (USE_TEXT) {
-                            String output = formatDate(ed.getTimestamp()
-                                    + trace.getOffset());
-                            System.out.println(nbEvent + ", "
-                                    + output + ", " + ed.getDeclaration().getName()
-                                    + ", " + ed.getCPU() + ed.getFields().toString());
-                        }
-                        // long endTime = traceReader.getEndTime();
-                        // long timestamp =
-                        // traceReader.getCurrentEventDef().getTimestamp();
-                        traceReader.advance();
+                while (traceReader.hasMoreEvents()) {
+                    EventDefinition ed = traceReader.getCurrentEventDef();
+                    nbEvent++;
+                    if (USE_TEXT) {
+                        String output = formatDate(ed.getTimestamp()
+                                + trace.getOffset());
+                        System.out.println(nbEvent + ", "
+                                + output + ", " + ed.getDeclaration().getName()
+                                + ", " + ed.getCPU() + ed.getFields().toString());
                     }
-                    // Map<Long, Stream> streams =
-                    // traceReader.getTrace().getStreams();
+
+                    traceReader.advance();
                 }
+
                 stop = System.nanoTime();
 
                 System.out.print('.');
diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/scope/LexicalScopeTest.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/scope/LexicalScopeTest.java
new file mode 100644 (file)
index 0000000..9fafff9
--- /dev/null
@@ -0,0 +1,46 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Matthew Khouzam - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.ctf.core.tests.scope;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope;
+import org.junit.Test;
+
+/**
+ * Lexical test
+ *
+ * @author Matthew Khouzam
+ */
+public class LexicalScopeTest {
+
+    /**
+     * Root test
+     */
+    @Test
+    public void testRoot(){
+        LexicalScope scope = LexicalScope.ROOT;
+        assertNotNull(scope);
+    }
+
+    /**
+     * Test a more complex node
+     */
+    @Test
+    public void testComplexNode(){
+        LexicalScope scope = LexicalScope.STREAM_EVENT_CONTEXT;
+        assertEquals("context", scope.getName());
+        assertEquals("stream.event.context", scope.toString());
+    }
+}
diff --git a/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/scope/TestAll.java b/org.eclipse.linuxtools.ctf.core.tests/src/org/eclipse/linuxtools/ctf/core/tests/scope/TestAll.java
new file mode 100644 (file)
index 0000000..aa95a4e
--- /dev/null
@@ -0,0 +1,31 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Matthew Khouzam - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.ctf.core.tests.scope;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
+
+/**
+ * The class <code>TestAll</code> builds a suite that can be used to run all of
+ * the tests within its package as well as within any subpackages of its
+ * package.
+ *
+ * @author Matthew Khouzam
+ * @version $Revision: 1.0 $
+ */
+@RunWith(Suite.class)
+@Suite.SuiteClasses({
+    LexicalScopeTest.class
+})
+public class TestAll {
+
+}
index da3d2786579e2901b96a2004dc51b45e6800e196..85ef1ecde0d76e2271791b51615324aabc7da28a 100644 (file)
@@ -124,14 +124,14 @@ public class CTFTraceGrowingStreamTest {
     public void testGrowingLive() throws CTFReaderException, FileNotFoundException, IOException {
         try (CTFTraceReader reader = new CTFTraceReader(fFixture);) {
             reader.setLive(true);
-            assertEquals("0x29", reader.getCurrentEventDef().getFields().getDefinitions().get("f").toString());
+            assertEquals("0x29", reader.getCurrentEventDef().getFields().getDefinition("f").toString());
             reader.advance();
             try (FileOutputStream fos = new FileOutputStream(fGrowingStream, true)) {
                 fos.write(fPackets[1]);
             }
             reader.advance();
             assertNotNull(reader.getCurrentEventDef());
-            assertEquals("0xbab4face", reader.getCurrentEventDef().getFields().getDefinitions().get("f").toString());
+            assertEquals("0xbab4face", reader.getCurrentEventDef().getFields().getDefinition("f").toString());
         }
     }
 
@@ -146,7 +146,7 @@ public class CTFTraceGrowingStreamTest {
     public void testGrowingNotLive() throws CTFReaderException, FileNotFoundException, IOException {
         try (CTFTraceReader reader = new CTFTraceReader(fFixture);) {
             reader.setLive(false);
-            assertEquals("0x29", reader.getCurrentEventDef().getFields().getDefinitions().get("f").toString());
+            assertEquals("0x29", reader.getCurrentEventDef().getFields().getDefinition("f").toString());
             reader.advance();
             try (FileOutputStream fos = new FileOutputStream(fGrowingStream, true)) {
                 fos.write(fPackets[1]);
index bc0c9b4ab87e68258d88ab1c08031e6bdb6dd451..44732da1fe2722127c0bd7e180192e8f9cda6e9f 100644 (file)
@@ -20,6 +20,10 @@ import java.nio.channels.FileChannel;
 import java.util.Set;
 
 import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
+import org.eclipse.linuxtools.ctf.core.event.types.Definition;
+import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration;
+import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition;
+import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
 import org.eclipse.linuxtools.ctf.core.tests.shared.CtfTestTrace;
 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
@@ -32,6 +36,8 @@ import org.eclipse.linuxtools.internal.ctf.core.event.EventDeclaration;
 import org.junit.Before;
 import org.junit.Test;
 
+import com.google.common.collect.ImmutableList;
+
 /**
  * The class <code>StreamInputReaderTest</code> contains tests for the class
  * <code>{@link StreamInputReader}</code>.
@@ -46,6 +52,10 @@ public class StreamInputReaderTest {
 
     private StreamInputReader fixture;
 
+    private static ImmutableList<String> wrap(String s) {
+        return ImmutableList.<String> builder().add(s).build();
+    }
+
     /**
      * Perform pre-test initialization.
      *
@@ -56,7 +66,15 @@ public class StreamInputReaderTest {
         fixture = getStreamInputReader();
         fixture.setName(1);
         fixture.setCurrentEvent(new EventDefinition(new EventDeclaration(),
-                getStreamInputReader()));
+                getStreamInputReader(), 0, null, null,
+                new StructDefinition(
+                        new StructDeclaration(0),
+                        null,
+                        "packet",
+                        wrap( "field" ),
+                        new Definition[] { new StringDefinition(new StringDeclaration(), null, "field", "test") }),
+                null)
+                );
     }
 
     private static StreamInputReader getStreamInputReader() throws CTFReaderException {
@@ -125,7 +143,7 @@ public class StreamInputReaderTest {
      */
     @Test
     public void testGetCurrentPacketContext() {
-        StructDefinition result = fixture.getCurrentPacketContext();
+        StructDefinition result = fixture.getCurrentEvent().getPacketContext();
         assertNotNull(result);
     }
 
@@ -140,27 +158,31 @@ public class StreamInputReaderTest {
 
     /**
      * Run the void goToLastEvent() method test.
-     * @throws CTFReaderException error
+     *
+     * @throws CTFReaderException
+     *             error
      */
     @Test
     public void testGoToLastEvent1() throws CTFReaderException {
         final long endTimestamp = goToEnd();
         final long endTime = 4287422460315L;
-        assertEquals(endTime , endTimestamp  );
+        assertEquals(endTime, endTimestamp);
     }
 
     /**
      * Run the void goToLastEvent() method test.
-     * @throws CTFReaderException error
+     *
+     * @throws CTFReaderException
+     *             error
      */
     @Test
     public void testGoToLastEvent2() throws CTFReaderException {
         long timestamp = -1;
-        while(fixture.readNextEvent().equals(CTFResponse.OK)) {
+        while (fixture.readNextEvent().equals(CTFResponse.OK)) {
             timestamp = fixture.getCurrentEvent().getTimestamp();
         }
         long endTimestamp = goToEnd();
-        assertEquals(0 , timestamp- endTimestamp );
+        assertEquals(0, timestamp - endTimestamp);
     }
 
     private long goToEnd() throws CTFReaderException {
@@ -170,7 +192,9 @@ public class StreamInputReaderTest {
 
     /**
      * Run the boolean readNextEvent() method test.
-     * @throws CTFReaderException error
+     *
+     * @throws CTFReaderException
+     *             error
      */
     @Test
     public void testReadNextEvent() throws CTFReaderException {
@@ -179,7 +203,9 @@ public class StreamInputReaderTest {
 
     /**
      * Run the void seek(long) method test. Seek by direct timestamp
-     * @throws CTFReaderException error
+     *
+     * @throws CTFReaderException
+     *             error
      */
     @Test
     public void testSeek_timestamp() throws CTFReaderException {
@@ -196,8 +222,7 @@ public class StreamInputReaderTest {
     @Test
     public void testSeek_eventDefinition() throws CTFReaderException {
         EventDefinition eventDefinition = new EventDefinition(
-                new EventDeclaration(), getStreamInputReader());
-        eventDefinition.setTimestamp(1L);
+                new EventDeclaration(), getStreamInputReader(), 1L, null, null, null, null);
         fixture.setCurrentEvent(eventDefinition);
     }
 }
\ No newline at end of file
index 8a7c8772335d56d4e91f9c58a44ab28bd1b928b5..641db4babb0d54b12359611f6bb891a793b1e6d6 100644 (file)
@@ -82,7 +82,7 @@ public class StreamInputTest {
      */
     @Test
     public void testGetPath() {
-        String result = fixture.getPath();
+        String result = fixture.getScopePath().toString();
         assertNotNull(result);
     }
 
index 336cae2809397dd5ab40bf1207ff95f3c270428c..aed530bade4cbb2f6ec746c1b23d61bfde5c8fd6 100644 (file)
@@ -13,15 +13,18 @@ package org.eclipse.linuxtools.ctf.core.tests.types;
 
 import static org.junit.Assert.*;
 
+import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 
+import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
 import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.Encoding;
 import org.eclipse.linuxtools.ctf.core.event.types.IDeclaration;
-import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope;
 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration;
+import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -63,13 +66,19 @@ public class ArrayDeclarationTest {
     /**
      * Run the ArrayDefinition createDefinition(DefinitionScope,String) method
      * test.
+     *
+     * @throws CTFReaderException
+     *             error in the bitbuffer
      */
     @Test
-    public void testCreateDefinition() {
+    public void testCreateDefinition() throws CTFReaderException {
         String fieldName = "";
         IDefinitionScope definitionScope = null;
         ArrayDefinition result;
-        result = fixture.createDefinition(definitionScope, fieldName);
+        byte[] array = { 't', 'e', 's', 't', '\0', 't', 'h', 'i', 's', '\0' };
+        ByteBuffer byb = ByteBuffer.wrap(array);
+        BitBuffer bb = new BitBuffer(byb);
+        result = fixture.createDefinition(definitionScope, fieldName, bb);
 
         assertNotNull(result);
     }
@@ -97,7 +106,7 @@ public class ArrayDeclarationTest {
      */
     @Test
     public void testIsString_ownDefs() {
-        //it's an array of strings, not a string
+        // it's an array of strings, not a string
         assertFalse(fixture.isString());
     }
 
@@ -106,8 +115,8 @@ public class ArrayDeclarationTest {
      */
     @Test
     public void testIsString_complex() {
-        final IntegerDeclaration id = new IntegerDeclaration(8, false, 16,
-                ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, null, 8);
+        final IntegerDeclaration id = IntegerDeclaration.createDeclaration(8, false, 16,
+                ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, "", 8);
         ArrayDeclaration ad = new ArrayDeclaration(0, id);
 
         boolean result = ad.isString();
index 49d62954b8ae1b779599a61cbf78bbdebfb0eb66..822e5125f5a10e1a481dccc7138f32367228fa39 100644 (file)
@@ -15,14 +15,19 @@ import static org.junit.Assert.assertNotNull;
 
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
 
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
+import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope;
 import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition;
 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.IDeclaration;
-import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope;
 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration;
@@ -41,7 +46,7 @@ import org.junit.Test;
  */
 public class ArrayDefinitionTest {
 
-    private CTFTrace trace;
+    @NonNull private CTFTrace trace = new CTFTrace();
     private ArrayDefinition charArrayFixture;
     private ArrayDefinition stringArrayFixture;
     private ArrayDefinition longArrayFixture;
@@ -60,56 +65,52 @@ public class ArrayDefinitionTest {
     }
 
     private ArrayDefinition createLongArray() {
-        IntegerDeclaration decl = new IntegerDeclaration(32, false, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "none",8);
-        IntegerDefinition[] defs = createIntDefs(10, 32);
+        IntegerDeclaration decl = IntegerDeclaration.createDeclaration(32, false, 10, ByteOrder.BIG_ENDIAN, Encoding.NONE, "none", 8);
+        List<Definition> defs = createIntDefs(10, 32);
         ArrayDefinition temp = setUpDeclaration(decl, defs);
         return temp;
     }
 
     private ArrayDefinition createCharArray() {
-        IntegerDeclaration decl = new IntegerDeclaration(8, false, 10, ByteOrder.BIG_ENDIAN, Encoding.UTF8, "none",8);
-        IntegerDefinition[] defs = createIntDefs(4,8);
+        IntegerDeclaration decl = IntegerDeclaration.createDeclaration(8, false, 10, ByteOrder.BIG_ENDIAN, Encoding.UTF8, "none", 8);
+        List<Definition> defs = createIntDefs(4, 8);
         ArrayDefinition temp = setUpDeclaration(decl, defs);
         return temp;
     }
 
     private ArrayDefinition createStringArray() {
         StringDeclaration strDecl = new StringDeclaration();
-        StringDefinition[] defs = createDefs();
+        List<Definition> defs = createDefs();
         ArrayDefinition temp = setUpDeclaration(strDecl, defs);
         return temp;
     }
 
     private ArrayDefinition setUpDeclaration(IDeclaration decl,
-            Definition[] defs) {
+            @NonNull List<Definition> defs) {
         ArrayDeclaration ad = new ArrayDeclaration(0, decl);
-        ArrayDefinition temp = new ArrayDefinition(ad , this.trace , "Testx");
-        temp.setDefinitions(defs);
+        ArrayDefinition temp = new ArrayDefinition(ad, this.trace, "Testx", defs);
         return temp;
     }
 
-
-    private static IntegerDefinition[] createIntDefs(int size, int bits) {
-        IntegerDefinition[] defs = new IntegerDefinition[size];
+    private @NonNull
+    static List<Definition> createIntDefs(int size, int bits) {
+        List<Definition> defs = new ArrayList<>(size);
         for (int i = 0; i < size; i++) {
-
             String content = "test" + i;
-            defs[i] = new IntegerDefinition(new IntegerDeclaration(bits, false,
-                    16, ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, content, 24), null, content);
-            defs[i].setValue(i);
+            defs.add(new IntegerDefinition(IntegerDeclaration.createDeclaration(bits, false,
+                    16, ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, content, 24), null, content, i));
         }
         return defs;
     }
 
-    private static StringDefinition[] createDefs() {
+    private @NonNull
+    static List<Definition> createDefs() {
         int size = 4;
-        StringDefinition[] defs = new StringDefinition[size];
+        List<Definition> defs = new ArrayList<>();
         for (int i = 0; i < size; i++) {
-
             String content = "test" + i;
-            defs[i] = new StringDefinition(
-                    new StringDeclaration(Encoding.UTF8), null, content);
-            defs[i].setValue(content);
+            defs.add(new StringDefinition(
+                    new StringDeclaration(Encoding.UTF8), null, content, content));
         }
         return defs;
     }
@@ -123,7 +124,8 @@ public class ArrayDefinitionTest {
         ArrayDeclaration declaration = charArrayFixture.getDeclaration();
         String fieldName = "";
 
-        ArrayDefinition result = new ArrayDefinition(declaration, this.trace, fieldName);
+        @SuppressWarnings("null")
+        ArrayDefinition result = new ArrayDefinition(declaration, this.trace, fieldName, Arrays.asList(new Definition[0]));
         assertNotNull(result);
     }
 
@@ -135,10 +137,11 @@ public class ArrayDefinitionTest {
     public void testArrayDefinition_newDeclaration() {
         ArrayDeclaration declaration = new ArrayDeclaration(0,
                 new StringDeclaration());
-        IDefinitionScope definitionScope = null;
-        String fieldName = "";
+        IDefinitionScope definitionScope = getDefinitionScope();
 
-        ArrayDefinition result = new ArrayDefinition(declaration, definitionScope, fieldName);
+        String fieldName = "";
+        @SuppressWarnings("null")
+        ArrayDefinition result = new ArrayDefinition(declaration, definitionScope, fieldName , Arrays.asList(new Definition[0]));
         assertNotNull(result);
     }
 
@@ -147,7 +150,6 @@ public class ArrayDefinitionTest {
      */
     @Test
     public void testGetDeclaration() {
-        charArrayFixture.setDefinitions(new Definition[] {});
         ArrayDeclaration result = charArrayFixture.getDeclaration();
 
         assertNotNull(result);
@@ -169,36 +171,41 @@ public class ArrayDefinitionTest {
      */
     @Test
     public void testGetElem_withDefs() {
-        Definition defs[] = createDefs();
-        charArrayFixture.setDefinitions(defs);
+        List<Definition> defs = createDefs();
+        IDefinitionScope definitionScope = getDefinitionScope();
+        ArrayDefinition ad = new ArrayDefinition(charArrayFixture.getDeclaration(), definitionScope, "test", defs);
         int j = 1;
 
-        Definition result = charArrayFixture.getElem(j);
+        Definition result = ad.getElem(j);
 
         assertNotNull(result);
     }
 
-    /**
-     * Run the void read(BitBuffer) method test.
-     * @throws CTFReaderException error
-     */
-    @Test
-    public void testRead_noDefs() throws CTFReaderException {
-        BitBuffer input = new BitBuffer(ByteBuffer.allocateDirect(128));
+    @NonNull private static IDefinitionScope getDefinitionScope() {
+        return new IDefinitionScope() {
+
+            @Override
+            public Definition lookupDefinition(String lookupPath) {
+                return null;
+            }
 
-        charArrayFixture.read(input);
+            @Override
+            public LexicalScope getScopePath() {
+                return null;
+            }
+        };
     }
 
     /**
      * Run the void read(BitBuffer) method test.
-     * @throws CTFReaderException error
+     *
+     * @throws CTFReaderException
+     *             error
      */
     @Test
-    public void testRead_withDefs() throws CTFReaderException {
-        charArrayFixture.setDefinitions(new Definition[] {});
-        BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128));
-
-        charArrayFixture.read(input);
+    public void testRead_noDefs() throws CTFReaderException {
+        BitBuffer input = new BitBuffer(ByteBuffer.allocateDirect(128));
+        charArrayFixture.getDeclaration().createDefinition(null, "test", input);
     }
 
     /**
@@ -209,6 +216,7 @@ public class ArrayDefinitionTest {
         String result = charArrayFixture.toString();
         assertNotNull(result);
     }
+
     /**
      * Run the String toString() method test.
      */
@@ -226,6 +234,7 @@ public class ArrayDefinitionTest {
         String result = stringArrayFixture.toString();
         assertNotNull(result);
     }
+
     /**
      * Run the String toString() method test.
      */
@@ -235,6 +244,7 @@ public class ArrayDefinitionTest {
 
         assertNotNull(result);
     }
+
     /**
      * Run the String toString() method test.
      */
index 6c67c52d29ed1f1eb5398c2cb335e433b569d32f..c388301051e04fb407f65d83fe378d63d103b0f7 100644 (file)
@@ -13,45 +13,46 @@ package org.eclipse.linuxtools.ctf.core.tests.types;
 
 import static org.junit.Assert.assertNotNull;
 
-import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
 import org.eclipse.linuxtools.ctf.core.event.types.IDeclaration;
-import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope;
+import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration;
 import org.junit.Test;
 
 /**
  * The class <code>DefinitionTest</code> contains tests for the class
  * <code>{@link Definition}</code>.
  *
- * @author ematkho
+ * @author Matthew Khouzam
  * @version $Revision: 1.0 $
  */
-@SuppressWarnings("javadoc")
 public class DefinitionTest {
 
     /**
      * Since Definition is abstract, we'll minimally extend it here to
      * instantiate it.
      */
-    class DefTest extends Definition {
+    static class DefTest extends Definition {
 
-        public DefTest(IDefinitionScope definitionScope, String fieldName) {
-            super(definitionScope, fieldName);
-        }
+        @NonNull
+        private static final StringDeclaration STRINGDEF = new StringDeclaration();
 
-        @Override
-        public void read(BitBuffer input) {
-            /* Just a test, no need to implement anything */
+        public DefTest(IDefinitionScope definitionScope, @NonNull String fieldName) {
+            super(DefTest.STRINGDEF, definitionScope, fieldName);
         }
 
         @Override
+        @NonNull
         public IDeclaration getDeclaration() {
-            // TODO Auto-generated method stub
-            return null;
+            return DefTest.STRINGDEF;
         }
 
     }
 
+    /**
+     * Test a definition
+     */
     @Test
     public void testToString() {
         Definition fixture = new DefTest(null, "Hello");
index 9c8528c48c2a57feb94f265dc558d4929163a929..cf9f940276b06b846fb333ca09428ea2a8602770 100644 (file)
@@ -16,13 +16,16 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
+import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 
+import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
 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.EnumDefinition;
-import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope;
 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
+import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -42,8 +45,8 @@ public class EnumDeclarationTest {
      */
     @Before
     public void setUp() {
-        fixture = new EnumDeclaration(new IntegerDeclaration(1, false, 1,
-                ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 8));
+        fixture = new EnumDeclaration(IntegerDeclaration.createDeclaration(1, false, 1,
+                ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 8));
     }
 
     /**
@@ -51,8 +54,8 @@ public class EnumDeclarationTest {
      */
     @Test
     public void testEnumDeclaration() {
-        IntegerDeclaration containerType = new IntegerDeclaration(1, false, 1,
-                ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 8);
+        IntegerDeclaration containerType = IntegerDeclaration.createDeclaration(1, false, 1,
+                ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 8);
 
         EnumDeclaration result = new EnumDeclaration(containerType);
 
@@ -78,14 +81,20 @@ public class EnumDeclarationTest {
     /**
      * Run the EnumDefinition createDefinition(DefinitionScope,String) method
      * test.
+     *
+     * @throws CTFReaderException
+     *             out of bounds error, won't happen
      */
     @Test
-    public void testCreateDefinition() {
+    public void testCreateDefinition() throws CTFReaderException {
         IDefinitionScope definitionScope = null;
         String fieldName = "";
+        byte[] array = { 't', 'e', 's', 't', '\0', 't', 'h', 'i', 's', '\0' };
+        ByteBuffer byb = ByteBuffer.wrap(array);
+        BitBuffer bb = new BitBuffer(byb);
 
         EnumDefinition result = fixture.createDefinition(definitionScope,
-                fieldName);
+                fieldName, bb);
 
         assertNotNull(result);
     }
index 5b5aa26695da229d5d1fc9594cb987ef1622900e..7525f7be0729594597d2d6f74cb42bd55146ad27 100644 (file)
@@ -13,17 +13,14 @@ package org.eclipse.linuxtools.ctf.core.tests.types;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
 
-import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 
-import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
 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.EnumDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
-import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
+import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -36,21 +33,24 @@ import org.junit.Test;
  */
 public class EnumDefinitionTest {
 
-    private EnumDefinition fixture;
+    private EnumDefinition fixtureA;
+    private EnumDefinition fixtureB;
 
     /**
      * Perform pre-test initialization.
      */
     @Before
     public void setUp() {
+        IntegerDeclaration integerDeclaration = IntegerDeclaration.createDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN,
+                Encoding.ASCII, "", 8);
         EnumDeclaration declaration = new EnumDeclaration(
-                new IntegerDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN,
-                        Encoding.ASCII, null, 8));
+                integerDeclaration);
         declaration.add(0, 10, "a");
         declaration.add(11, 20, "b");
         String fieldName = "";
 
-        fixture = new EnumDefinition(declaration, null, fieldName);
+        fixtureA = new EnumDefinition(declaration, null, fieldName, new IntegerDefinition(integerDeclaration, null, fieldName, 4));
+        fixtureB = new EnumDefinition(declaration, null, fieldName, new IntegerDefinition(integerDeclaration, null, fieldName, 12));
     }
 
     /**
@@ -59,7 +59,8 @@ public class EnumDefinitionTest {
      */
     @Test
     public void testEnumDefinition() {
-        assertNotNull(fixture);
+        assertNotNull(fixtureA);
+        assertNotNull(fixtureB);
     }
 
     /**
@@ -67,9 +68,10 @@ public class EnumDefinitionTest {
      */
     @Test
     public void testGetValue() {
-        String result = fixture.getValue();
+        String result = fixtureA.getValue();
 
         assertNotNull(result);
+        assertEquals("a", result);
     }
 
     /**
@@ -77,33 +79,8 @@ public class EnumDefinitionTest {
      */
     @Test
     public void testGetIntegerValue_one() {
-        fixture.setIntegerValue(1L);
-        long result = fixture.getIntegerValue();
-
-        assertEquals(1L, result);
-    }
-
-    /**
-     * Run the String getValue() method test.
-     */
-    @Test
-    public void testGetIntegerValue_zero() {
-        fixture.setIntegerValue(0);
-        long result = fixture.getIntegerValue();
-
-        assertTrue(0 == result);
-    }
-
-    /**
-     * Run the void read(BitBuffer) method test.
-     * @throws CTFReaderException error
-     */
-    @Test
-    public void testRead() throws CTFReaderException {
-        fixture.setIntegerValue(1L);
-        BitBuffer input = new BitBuffer(ByteBuffer.allocateDirect(128));
-
-        fixture.read(input);
+        long result = fixtureA.getIntegerValue();
+        assertEquals(4L, result);
     }
 
     /**
@@ -111,9 +88,8 @@ public class EnumDefinitionTest {
      */
     @Test
     public void testToString() {
-        fixture.setIntegerValue(16);
-        String result = fixture.toString();
+        String result = fixtureB.toString();
 
-        assertEquals("{ value = b, container = 16 }", result);
+        assertEquals("{ value = b, container = 12 }", result);
     }
 }
\ No newline at end of file
index dad15826879589fbcb68646f378aaa694c9b1c84..66a258e8df737f9a50dd30ae1f3d3c24e807a964 100644 (file)
@@ -317,26 +317,23 @@ public class EventDeclarationTest {
     @Test
     public void testEventDefinition() throws CTFReaderException {
         CTFTrace trace = testTrace.getTrace();
-        EventDefinition ed = new EventDefinition(null, null);
+        EventDefinition ed = null;
         try (CTFTraceReader tr = new CTFTraceReader(trace);) {
             tr.advance();
             ed = tr.getCurrentEventDef();
         }
         assertNotNull(ed);
-        assertNotNull(ed.getPath());
+        assertNotNull(ed.getScopePath());
         assertNotNull(ed.getDeclaration());
         assertNotNull(ed.getFields());
         assertNull(ed.getContext());
         assertNotNull(ed.getPacketContext());
         assertNotNull(ed.getCPU());
-        assertNotNull(ed.getPacketContext());
         assertNotNull(ed.getStreamInputReader());
         assertNull(ed.lookupDefinition("context"));
         assertNotNull(ed.lookupDefinition("fields"));
         assertNull(ed.lookupDefinition("other"));
         assertNotNull(ed.toString());
-        ed.setContext(ed.getFields());
-        assertNotNull(ed.toString());
     }
 
     EventDeclaration e1;
index 2635d3e629a64c954f5550334693630c10bb9dc2..77fbaf8d5001c12b62015567bcd5af837855576a 100644 (file)
@@ -13,10 +13,11 @@ package org.eclipse.linuxtools.ctf.core.tests.types;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
 
+import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
 import org.eclipse.linuxtools.ctf.core.event.types.FloatDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.FloatDefinition;
@@ -28,7 +29,7 @@ import org.junit.Test;
 /**
  * The class <code>IntegerDefinitionTest</code> contains tests for the class
  * <code>{@link IntegerDefinition}</code>.
- *
+ * 
  * @author ematkho
  * @version $Revision: 1.0 $
  */
@@ -37,68 +38,73 @@ public class FloatDefinitionTest {
 
     private FloatDefinition fixture;
     private FloatDefinition singleFixture;
-    private FloatDefinition doubleFixture; //all the way.
+    private FloatDefinition doubleFixture; // all the way.
     private FloatDeclaration parent;
+    @NonNull
     private static final String fieldName = "float";
 
     /**
      * Perform pre-test initialization.
+     * 
+     * @throws CTFReaderException
+     *             error creating floats
      */
     @Before
-    public void setUp(){
+    public void setUp() throws CTFReaderException {
         testFloat248();
         testFloat5311();
     }
 
     @Test
-    public void testFloat248() {
+    public void testFloat248() throws CTFReaderException {
         parent = new FloatDeclaration(8, 24, ByteOrder.nativeOrder(), 0);
-        singleFixture = parent.createDefinition(null, fieldName);
+        BitBuffer bb = create32BitFloatByteBuffer();
+        singleFixture = parent.createDefinition(null, fieldName, bb);
         assertNotNull(singleFixture);
     }
 
-
-
     @Test
-    public void testFloat5311() {
+    public void testFloat5311() throws CTFReaderException {
         parent = new FloatDeclaration(11, 53, ByteOrder.nativeOrder(), 0);
-        doubleFixture = parent.createDefinition(null, fieldName);
+        BitBuffer bb = create64BitFloatByteBuffer();
+        doubleFixture = parent.createDefinition(null, fieldName, bb);
         assertNotNull(doubleFixture);
     }
 
     @Test
-    public void testFloat32Bit(){
-        for(int i = 1; i < 31 ; i++) {
-            parent = new FloatDeclaration(i, 32-i, ByteOrder.nativeOrder(), 0);
-            fixture = parent.createDefinition(null, fieldName);
+    public void testFloat32Bit() throws CTFReaderException {
+        for (int i = 1; i < 31; i++) {
+            parent = new FloatDeclaration(i, 32 - i, ByteOrder.nativeOrder(), 0);
+
+            fixture = parent.createDefinition(null, fieldName, create32BitFloatByteBuffer());
             assertNotNull(fixture);
-            fixture.setValue(2.0);
-            assertTrue(fixture.toString().contains("2"));
+            assertEquals("test" + i, "2.0", fixture.toString());
         }
     }
 
     @Test
-    public void testFloat64Bit() throws CTFReaderException{
-        for(int i = 1; i < 63 ; i++) {
-            parent = new FloatDeclaration(i, 64-i, ByteOrder.nativeOrder(), 0);
-            fixture = parent.createDefinition(null, fieldName);
+    public void testFloat64Bit() throws CTFReaderException {
+        for (int i = 1; i < 63; i++) {
+            parent = new FloatDeclaration(i, 64 - i, ByteOrder.nativeOrder(), 0);
+            fixture = parent.createDefinition(null, fieldName, create64BitFloatByteBuffer());
             assertNotNull(fixture);
-            BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128));
-            fixture.read(input);
-            fixture.setValue(2.0);
-            assertTrue(fixture.toString().contains("2"));
+            if (i <= 32) {
+                assertEquals("test" + i, "2.0", fixture.toString());
+            } else if (i == 33) {
+                assertEquals("test" + i, "1.0", fixture.toString());
+            } else {
+                assertNotNull(fixture.getValue());
+            }
+
         }
     }
 
     @Test
-    public void testFloat48Bit() throws CTFReaderException{
+    public void testFloat48Bit() throws CTFReaderException {
         parent = new FloatDeclaration(12, 32, ByteOrder.nativeOrder(), 0);
-        fixture = parent.createDefinition(null, fieldName);
+        fixture = parent.createDefinition(null, fieldName, create64BitFloatByteBuffer());
         assertNotNull(fixture);
-        BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128));
-        fixture.read(input);
-
-        assertEquals(Double.NaN ,fixture.getValue(),0.1);
+        assertEquals(Double.NaN, fixture.getValue(), 0.1);
     }
 
     /**
@@ -106,7 +112,6 @@ public class FloatDefinitionTest {
      */
     @Test
     public void testGetDeclaration() {
-        singleFixture.setValue(2.0);
         FloatDeclaration result = singleFixture.getDeclaration();
         assertNotNull(result);
     }
@@ -116,20 +121,8 @@ public class FloatDefinitionTest {
      */
     @Test
     public void testGetValue() {
-        singleFixture.setValue(2.0);
         double result = singleFixture.getValue();
-        assertEquals(2.0, result,0.1);
-    }
-
-    /**
-     * Run the void read(BitBuffer) method test.
-     * @throws CTFReaderException error
-     */
-    @Test
-    public void testRead() throws CTFReaderException {
-        singleFixture.setValue(2.0);
-        BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128));
-        singleFixture.read(input);
+        assertEquals(2.0, result, 0.1);
     }
 
     /**
@@ -137,8 +130,36 @@ public class FloatDefinitionTest {
      */
     @Test
     public void testToString() {
-        singleFixture.setValue(222.22);
         String result = singleFixture.toString();
         assertNotNull(result);
+        assertEquals("2.0", result);
+    }
+
+    @NonNull
+    private static BitBuffer create32BitFloatByteBuffer() {
+        float[] data = new float[2];
+        data[0] = 2.0f;
+        data[1] = 3.14f;
+        ByteBuffer byb = ByteBuffer.allocate(128);
+        byb.mark();
+        byb.putFloat(data[0]);
+        byb.putFloat(data[1]);
+        byb.reset();
+        BitBuffer bb = new BitBuffer(byb);
+        return bb;
+    }
+
+    @NonNull
+    private static BitBuffer create64BitFloatByteBuffer() {
+        double[] data = new double[2];
+        data[0] = 2.0f;
+        data[1] = 3.14f;
+        ByteBuffer byb = ByteBuffer.allocate(128);
+        byb.mark();
+        byb.putDouble(data[0]);
+        byb.putDouble(data[1]);
+        byb.reset();
+        BitBuffer bb = new BitBuffer(byb);
+        return bb;
     }
 }
index 06e58362c6e20ea5f64095085fc74b3897943a9d..e32600823a94c7b7f57cfca55b6d8cad5d3a0fc1 100644 (file)
@@ -39,8 +39,8 @@ public class IntegerDeclarationTest {
      */
     @Before
     public void setUp() {
-        fixture = new IntegerDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN,
-                Encoding.ASCII, null, 32);
+        fixture = IntegerDeclaration.createDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN,
+                Encoding.ASCII, "", 32);
     }
 
     /**
@@ -55,8 +55,8 @@ public class IntegerDeclarationTest {
         ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;
         Encoding encoding = Encoding.ASCII;
 
-        IntegerDeclaration result = new IntegerDeclaration(len, signed, base,
-                byteOrder, encoding, null, 16);
+        IntegerDeclaration result = IntegerDeclaration.createDeclaration(len, signed, base,
+                byteOrder, encoding, "", 16);
 
         assertNotNull(result);
         assertEquals(1, result.getBase());
@@ -78,7 +78,7 @@ public class IntegerDeclarationTest {
         int base = 1;
         ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;
         Encoding encoding = Encoding.ASCII;
-        new IntegerDeclaration(len, signed, base, byteOrder, encoding, null, 16);
+        IntegerDeclaration.createDeclaration(len, signed, base, byteOrder, encoding, "", 16);
     }
 
     /**
@@ -91,7 +91,7 @@ public class IntegerDeclarationTest {
         int base = 1;
         ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;
         Encoding encoding = Encoding.ASCII;
-        new IntegerDeclaration(len, signed, base, byteOrder, encoding, null, 16);
+        IntegerDeclaration.createDeclaration(len, signed, base, byteOrder, encoding, "", 16);
     }
 
     /**
@@ -148,8 +148,8 @@ public class IntegerDeclarationTest {
      */
     @Test
     public void testIsCharacter_8bytes() {
-        IntegerDeclaration fixture8 = new IntegerDeclaration(8, true, 1,
-                ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 8);
+        IntegerDeclaration fixture8 = IntegerDeclaration.createDeclaration(8, true, 1,
+                ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 8);
 
         boolean result = fixture8.isCharacter();
         assertEquals(true, result);
@@ -160,8 +160,8 @@ public class IntegerDeclarationTest {
      */
     @Test
     public void testIsSigned_signed() {
-        IntegerDeclaration fixtureSigned = new IntegerDeclaration(2, true,
-                1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 8);
+        IntegerDeclaration fixtureSigned = IntegerDeclaration.createDeclaration(2, true,
+                1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 8);
         boolean result = fixtureSigned.isSigned();
         assertEquals(true, result);
     }
@@ -193,22 +193,22 @@ public class IntegerDeclarationTest {
     public void testMaxValue() {
         assertEquals(BigInteger.ONE, fixture.getMaxValue());
 
-        IntegerDeclaration signed8bit = new IntegerDeclaration(8, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32);
+        IntegerDeclaration signed8bit = IntegerDeclaration.createDeclaration(8, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
         assertEquals(BigInteger.valueOf(127), signed8bit.getMaxValue());
 
-        IntegerDeclaration unsigned8bit = new IntegerDeclaration(8, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32);
+        IntegerDeclaration unsigned8bit = IntegerDeclaration.createDeclaration(8, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
         assertEquals(BigInteger.valueOf(255), unsigned8bit.getMaxValue());
 
-        IntegerDeclaration signed32bit = new IntegerDeclaration(32, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32);
+        IntegerDeclaration signed32bit = IntegerDeclaration.createDeclaration(32, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
         assertEquals(BigInteger.valueOf(2147483647), signed32bit.getMaxValue());
 
-        IntegerDeclaration unsigned32bit = new IntegerDeclaration(32, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32);
+        IntegerDeclaration unsigned32bit = IntegerDeclaration.createDeclaration(32, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
         assertEquals(BigInteger.valueOf(4294967295l), unsigned32bit.getMaxValue());
 
-        IntegerDeclaration signed64bit = new IntegerDeclaration(64, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32);
+        IntegerDeclaration signed64bit = IntegerDeclaration.createDeclaration(64, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
         assertEquals(BigInteger.valueOf(9223372036854775807L), signed64bit.getMaxValue());
 
-        IntegerDeclaration unsigned64bit = new IntegerDeclaration(64, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32);
+        IntegerDeclaration unsigned64bit = IntegerDeclaration.createDeclaration(64, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
         assertEquals(BigInteger.valueOf(2).pow(64).subtract(BigInteger.ONE), unsigned64bit.getMaxValue());
     }
 
@@ -219,22 +219,22 @@ public class IntegerDeclarationTest {
     public void testMinValue() {
         assertEquals(BigInteger.ZERO, fixture.getMinValue());
 
-        IntegerDeclaration signed8bit = new IntegerDeclaration(8, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32);
+        IntegerDeclaration signed8bit = IntegerDeclaration.createDeclaration(8, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
         assertEquals(BigInteger.valueOf(-128), signed8bit.getMinValue());
 
-        IntegerDeclaration unsigned8bit = new IntegerDeclaration(8, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32);
+        IntegerDeclaration unsigned8bit = IntegerDeclaration.createDeclaration(8, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
         assertEquals(BigInteger.ZERO, unsigned8bit.getMinValue());
 
-        IntegerDeclaration signed32bit = new IntegerDeclaration(32, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32);
+        IntegerDeclaration signed32bit = IntegerDeclaration.createDeclaration(32, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
         assertEquals(BigInteger.valueOf(-2147483648), signed32bit.getMinValue());
 
-        IntegerDeclaration unsigned32bit = new IntegerDeclaration(32, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32);
+        IntegerDeclaration unsigned32bit = IntegerDeclaration.createDeclaration(32, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
         assertEquals(BigInteger.ZERO, unsigned32bit.getMinValue());
 
-        IntegerDeclaration signed64bit = new IntegerDeclaration(64, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32);
+        IntegerDeclaration signed64bit = IntegerDeclaration.createDeclaration(64, true, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
         assertEquals(BigInteger.valueOf(-9223372036854775808L), signed64bit.getMinValue());
 
-        IntegerDeclaration unsigned64bit = new IntegerDeclaration(64, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 32);
+        IntegerDeclaration unsigned64bit = IntegerDeclaration.createDeclaration(64, false, 1, ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 32);
         assertEquals(BigInteger.ZERO, unsigned64bit.getMinValue());
     }
 }
\ No newline at end of file
index 5ee378894cdc1b8f0330e7f9381f5908da193733..1522c90dd5f13549b744f041ac7c902b1748bdbd 100644 (file)
@@ -14,11 +14,13 @@ package org.eclipse.linuxtools.ctf.core.tests.types;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 
+import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
 import org.eclipse.linuxtools.ctf.core.event.types.Encoding;
-import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope;
 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
@@ -35,16 +37,24 @@ import org.junit.Test;
 public class IntegerDefinitionTest {
 
     private IntegerDefinition fixture;
-    String name = "testInt";
-    String clockName = "clock";
+    @NonNull private static final String NAME = "testInt";
+    @NonNull private static final String clockName = "clock";
 
     /**
      * Perform pre-test initialization.
+     *
+     * @throws CTFReaderException
+     *             won't happen
      */
     @Before
-    public void setUp() {
-        IntegerDeclaration id = new IntegerDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN, Encoding.NONE, clockName, 8);
-        fixture = id.createDefinition(null, name);
+    public void setUp() throws CTFReaderException {
+        IntegerDeclaration id = IntegerDeclaration.createDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN, Encoding.NONE, clockName, 8);
+        ByteBuffer byb = ByteBuffer.allocate(128);
+        byb.mark();
+        byb.putInt(1);
+        byb.reset();
+        BitBuffer bb = new BitBuffer(byb);
+        fixture = id.createDefinition(null, NAME, bb);
     }
 
     /**
@@ -53,13 +63,13 @@ public class IntegerDefinitionTest {
      */
     @Test
     public void testIntegerDefinition() {
-        IntegerDeclaration declaration = new IntegerDeclaration(1, false, 1,
-                ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 8);
+        IntegerDeclaration declaration = IntegerDeclaration.createDeclaration(1, false, 1,
+                ByteOrder.BIG_ENDIAN, Encoding.ASCII, "", 8);
         IDefinitionScope definitionScope = null;
         String fieldName = "";
 
         IntegerDefinition result = new IntegerDefinition(declaration,
-                definitionScope, fieldName);
+                definitionScope, fieldName, 1);
         assertNotNull(result);
     }
 
@@ -68,8 +78,6 @@ public class IntegerDefinitionTest {
      */
     @Test
     public void testGetDeclaration() {
-        fixture.setValue(1L);
-
         IntegerDeclaration result = fixture.getDeclaration();
         assertNotNull(result);
     }
@@ -79,22 +87,8 @@ public class IntegerDefinitionTest {
      */
     @Test
     public void testGetValue() {
-        fixture.setValue(1L);
-
         long result = fixture.getValue();
-        assertEquals(1L, result);
-    }
-
-    /**
-     * Run the void read(BitBuffer) method test.
-     * @throws CTFReaderException error
-     */
-    @Test
-    public void testRead() throws CTFReaderException {
-        fixture.setValue(1L);
-        BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128));
-
-        fixture.read(input);
+        assertEquals(0L, result);
     }
 
     /**
@@ -102,10 +96,8 @@ public class IntegerDefinitionTest {
      */
     @Test
     public void testToString() {
-        fixture.setValue(1L);
-
         String result = fixture.toString();
-        assertNotNull(result);
+        assertEquals("0", result);
     }
 
     /**
index 8801313148791554ddd8312ea69a0a60f5398baf..9ce8bc81d57b4f5f4aeab95dcb5ce4202a8844c9 100644 (file)
@@ -19,6 +19,7 @@ import static org.junit.Assert.assertEquals;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
 import org.eclipse.linuxtools.ctf.core.event.types.Encoding;
 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
@@ -34,11 +35,11 @@ import org.junit.Test;
  */
 public class IntegerEndiannessTest {
 
-    private static String name = "testInt";
-    private static String clockName = "clock";
+    @NonNull private static final String name = "testInt";
+    @NonNull private static final String clockName = "clock";
 
     private ByteBuffer bb;
-    private BitBuffer input;
+    @NonNull private BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocate(0));
 
     /**
      * Set up the bit-buffer to be used
@@ -65,9 +66,8 @@ public class IntegerEndiannessTest {
      */
     @Test
     public void test32BE() throws CTFReaderException {
-        IntegerDeclaration be = new IntegerDeclaration(32, true, 1, ByteOrder.BIG_ENDIAN, Encoding.NONE, clockName, 8);
-        IntegerDefinition fixture_be = be.createDefinition(null, name);
-        fixture_be.read(input);
+        IntegerDeclaration be = IntegerDeclaration.createDeclaration(32, true, 1, ByteOrder.BIG_ENDIAN, Encoding.NONE, clockName, 8);
+        IntegerDefinition fixture_be = be.createDefinition(null, name, input);
         assertEquals(0xabcdef12, fixture_be.getValue());
     }
 
@@ -79,9 +79,8 @@ public class IntegerEndiannessTest {
      */
     @Test
     public void test64BE() throws CTFReaderException {
-        IntegerDeclaration be = new IntegerDeclaration(64, true, 1, ByteOrder.BIG_ENDIAN, Encoding.NONE, clockName, 8);
-        IntegerDefinition fixture_be = be.createDefinition(null, name);
-        fixture_be.read(input);
+        IntegerDeclaration be = IntegerDeclaration.createDeclaration(64, true, 1, ByteOrder.BIG_ENDIAN, Encoding.NONE, clockName, 8);
+        IntegerDefinition fixture_be = be.createDefinition(null, name, input);
         assertEquals(0xabcdef123456789aL, fixture_be.getValue());
     }
 
@@ -93,9 +92,8 @@ public class IntegerEndiannessTest {
      */
     @Test
     public void test32LE() throws CTFReaderException {
-        IntegerDeclaration le = new IntegerDeclaration(32, true, 1, ByteOrder.LITTLE_ENDIAN, Encoding.NONE, clockName, 8);
-        IntegerDefinition fixture_le = le.createDefinition(null, name);
-        fixture_le.read(input);
+        IntegerDeclaration le = IntegerDeclaration.createDeclaration(32, true, 1, ByteOrder.LITTLE_ENDIAN, Encoding.NONE, clockName, 8);
+        IntegerDefinition fixture_le = le.createDefinition(null, name, input);
         assertEquals(0x12efcdab, fixture_le.getValue());
     }
 
@@ -107,9 +105,8 @@ public class IntegerEndiannessTest {
      */
     @Test
     public void test64LE() throws CTFReaderException {
-        IntegerDeclaration le = new IntegerDeclaration(64, true, 1, ByteOrder.LITTLE_ENDIAN, Encoding.NONE, clockName, 8);
-        IntegerDefinition fixture_le = le.createDefinition(null, name);
-        fixture_le.read(input);
+        IntegerDeclaration le = IntegerDeclaration.createDeclaration(64, true, 1, ByteOrder.LITTLE_ENDIAN, Encoding.NONE, clockName, 8);
+        IntegerDefinition fixture_le = le.createDefinition(null, name, input);
         assertEquals(0x9a78563412efcdabL, fixture_le.getValue());
     }
 }
index f732ffd3bcc8a13c6bcddd3cb740b62b1ce520be..dbdf621cd703a2f0d5771e34a1b344274916c1e3 100644 (file)
@@ -14,19 +14,27 @@ package org.eclipse.linuxtools.ctf.core.tests.types;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 
+import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
+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.IDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
+import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
 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.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.trace.CTFReaderException;
 import org.junit.Before;
 import org.junit.Test;
 
+import com.google.common.collect.ImmutableList;
+
 /**
  * The class <code>SequenceDeclarationTest</code> contains tests for the class
  * <code>{@link SequenceDeclaration}</code>.
@@ -37,13 +45,17 @@ import org.junit.Test;
 @SuppressWarnings("javadoc")
 public class SequenceDeclarationTest {
 
-    private SequenceDeclaration fixture;
+    @NonNull private static final String FIELD_NAME = "LengthName";
 
-    static final String fieldName = "LengthName";
+    private SequenceDeclaration fixture;
+    @NonNull private BitBuffer input = new BitBuffer();
 
     @Before
     public void setUp() {
-        fixture = new SequenceDeclaration(fieldName, new StringDeclaration());
+        fixture = new SequenceDeclaration(FIELD_NAME, new StringDeclaration());
+        byte array[] = { 't', 'e', 's', 't', '\0', 't', 'h', 'i', 's', '\0' };
+        ByteBuffer byb = ByteBuffer.wrap(array);
+        input = new BitBuffer(byb);
     }
 
     /**
@@ -63,18 +75,30 @@ public class SequenceDeclarationTest {
     /**
      * Run the SequenceDefinition createDefinition(DefinitionScope,String)
      * method test.
+     *
+     * @throws CTFReaderException
+     *             an error in the bitbuffer
      */
     @Test
-    public void testCreateDefinition() {
-        IntegerDeclaration id = new IntegerDeclaration(8, false, 8,
-                ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, null, 32);
-
+    public void testCreateDefinition() throws CTFReaderException {
+        long seqLen = 2;
+        IntegerDeclaration id = IntegerDeclaration.createDeclaration(8, false, 8,
+                ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, "", 32);
         StructDeclaration structDec = new StructDeclaration(0);
-        structDec.addField(fieldName, id);
-        StructDefinition structDef = new StructDefinition(structDec, null, "x");
-        long seqLen = 10;
-        structDef.lookupInteger(fieldName).setValue(seqLen);
-        SequenceDefinition result = this.fixture.createDefinition(structDef, fieldName);
+        structDec.addField(FIELD_NAME, id);
+        StructDefinition structDef = new StructDefinition(
+                structDec,
+                null,
+                "x",
+                ImmutableList.of(FIELD_NAME),
+                new Definition[] {
+                        new IntegerDefinition(
+                                id,
+                                null,
+                                FIELD_NAME,
+                                seqLen)
+                });
+        SequenceDefinition result = fixture.createDefinition(structDef, FIELD_NAME, input);
         assertNotNull(result);
     }
 
index 073e5d4a6d80fa9c51c91ab433417ef49d25942a..4a5e5adfc6f18025c4936363d44d4aba1fd275ad 100644 (file)
@@ -21,6 +21,7 @@ import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
 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.IntegerDeclaration;
+import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
 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.StructDeclaration;
@@ -29,6 +30,8 @@ import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
 import org.junit.Before;
 import org.junit.Test;
 
+import com.google.common.collect.ImmutableList;
+
 /**
  * The class <code>SequenceDefinitionTest</code> contains tests for the class
  * <code>{@link SequenceDefinition}</code>.
@@ -42,8 +45,13 @@ public class SequenceDefinitionTest {
     private SequenceDefinition fixture;
     private final static int seqLen = 15;
 
+    private static ImmutableList<String> wrap(String s) {
+        return ImmutableList.<String> builder().add(s).build();
+    }
+
     /**
      * Perform pre-test initialization.
+     *
      * @throws CTFReaderException
      */
     @Before
@@ -51,22 +59,23 @@ public class SequenceDefinitionTest {
         StructDeclaration structDec;
         StructDefinition structDef;
 
-        IntegerDeclaration id = new IntegerDeclaration(8, false, 8,
-                ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, null, 8);
+        IntegerDeclaration id = IntegerDeclaration.createDeclaration(8, false, 8,
+                ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, "", 8);
         String lengthName = "LengthName";
         structDec = new StructDeclaration(0);
         structDec.addField(lengthName, id);
-        structDef = new StructDefinition(structDec, null, "x");
+        structDef = new StructDefinition(structDec, null, "x",
+                wrap(lengthName),
+                new Definition[] { new IntegerDefinition(id, null, lengthName, seqLen) });
 
-        structDef.lookupInteger(lengthName).setValue(seqLen);
         SequenceDeclaration sd = new SequenceDeclaration(lengthName, id);
-        fixture = new SequenceDefinition(sd, structDef, "TestX");
         BitBuffer input = new BitBuffer(
                 java.nio.ByteBuffer.allocateDirect(seqLen * 8));
         for (int i = 0; i < seqLen; i++) {
             input.putInt(i);
         }
-        fixture.read(input);
+
+        fixture = sd.createDefinition(structDef, "TestX", input);
         assert (fixture != null);
     }
 
@@ -75,22 +84,22 @@ public class SequenceDefinitionTest {
         StructDefinition structDef;
 
         int len = 32;
-        IntegerDeclaration id = new IntegerDeclaration(len, false, len,
-                ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, null,8);
+        IntegerDeclaration id = IntegerDeclaration.createDeclaration(len, false, len,
+                ByteOrder.LITTLE_ENDIAN, Encoding.UTF8, "", 8);
         String lengthName = "LengthName";
         structDec = new StructDeclaration(0);
         structDec.addField(lengthName, id);
-        structDef = new StructDefinition(structDec, null, "x");
 
-        structDef.lookupInteger(lengthName).setValue(seqLen);
+        structDef = new StructDefinition(structDec, null, "x", wrap(lengthName), new Definition[] { new IntegerDefinition(id, null, lengthName, seqLen) });
+
         SequenceDeclaration sd = new SequenceDeclaration(lengthName, id);
-        SequenceDefinition ret = new SequenceDefinition(sd, structDef, "TestX");
         BitBuffer input = new BitBuffer(
                 java.nio.ByteBuffer.allocateDirect(seqLen * len));
         for (int i = 0; i < seqLen; i++) {
             input.putInt(i);
         }
-        ret.read(input);
+
+        SequenceDefinition ret = sd.createDefinition(structDef, "TestX", input);
         assertNotNull(ret);
         return ret;
     }
@@ -138,20 +147,10 @@ public class SequenceDefinitionTest {
      */
     @Test
     public void testIsString() {
-        boolean result = fixture.isString();
+        boolean result = fixture.getDeclaration().isString();
         assertTrue(result);
     }
 
-    /**
-     * Run the void read(BitBuffer) method test.
-     * @throws CTFReaderException error
-     */
-    @Test
-    public void testRead() throws CTFReaderException {
-        BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128));
-        fixture.read(input);
-    }
-
     /**
      * Run the String toString() method test.
      */
index 856bf49f2351dfc0ad42a297227b085fd95a6854..57c7015b8e94d4515c5a7607a49e60a70be84a12 100644 (file)
@@ -14,10 +14,14 @@ package org.eclipse.linuxtools.ctf.core.tests.types;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 
+import java.nio.ByteBuffer;
+
+import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
 import org.eclipse.linuxtools.ctf.core.event.types.Encoding;
-import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope;
 import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition;
+import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -68,13 +72,15 @@ public class StringDeclarationTest {
     /**
      * Run the StringDefinition createDefinition(DefinitionScope,String) method
      * test.
+     * @throws CTFReaderException out of buffer exception
      */
     @Test
-    public void testCreateDefinition() {
+    public void testCreateDefinition() throws CTFReaderException {
         IDefinitionScope definitionScope = null;
         String fieldName = "id";
+        BitBuffer bb = new BitBuffer(ByteBuffer.allocate(100));
         StringDefinition result = fixture.createDefinition(definitionScope,
-                fieldName);
+                fieldName, bb);
 
         assertNotNull(result);
     }
@@ -92,16 +98,7 @@ public class StringDeclarationTest {
         assertEquals(1, result.ordinal());
     }
 
-    /**
-     * Run the void setEncoding(Encoding) method test.
-     */
-    @Test
-    public void testSetEncoding() {
-        Encoding encoding = Encoding.ASCII;
-        fixture.setEncoding(encoding);
-    }
-
-    /**
+     /**
      * Run the String toString() method test.
      */
     @Test
index 2d263ec20e4d3cd6bfdbc2519abd6eb44f4de26e..0b46d80fbf5ce4851e2d45f5e090237f7a5a358f 100644 (file)
@@ -14,8 +14,10 @@ package org.eclipse.linuxtools.ctf.core.tests.types;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 
+import java.nio.ByteBuffer;
+
 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
-import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
 import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition;
 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
@@ -32,15 +34,24 @@ import org.junit.Test;
 public class StringDefinitionTest {
 
     private StringDefinition fixture;
+    private String testString;
 
     /**
      * Perform pre-test initialization.
+     *
+     * @throws CTFReaderException won't happen
      */
     @Before
-    public void setUp() {
+    public void setUp() throws CTFReaderException {
         String name = "testString";
         StringDeclaration stringDec = new StringDeclaration();
-        fixture = stringDec.createDefinition(null, name);
+        ByteBuffer byteBuffer = ByteBuffer.allocate(100);
+        BitBuffer bb = new BitBuffer(byteBuffer);
+        byteBuffer.mark();
+        testString = new String("testString");
+        byteBuffer.put(testString.getBytes());
+        byteBuffer.reset();
+        fixture = stringDec.createDefinition(null, name, bb);
     }
 
     /**
@@ -54,7 +65,7 @@ public class StringDefinitionTest {
         String fieldName = "";
 
         StringDefinition result = new StringDefinition(declaration,
-                definitionScope, fieldName);
+                definitionScope, fieldName, "");
 
         assertNotNull(result);
     }
@@ -82,29 +93,10 @@ public class StringDefinitionTest {
      */
     @Test
     public void testSetValue() {
-        fixture.setValue("dummy");
+
         String result = fixture.getValue();
         assertNotNull(result);
-        assertEquals("dummy", result);
-    }
-
-    /**
-     * Run the void read(BitBuffer) method test.
-     * @throws CTFReaderException error
-     */
-    @Test
-    public void testRead() throws CTFReaderException {
-        BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128));
-        fixture.read(input);
-    }
-
-    /**
-     * Run the void setDeclaration(StringDeclaration) method test.
-     */
-    @Test
-    public void testSetDeclaration() {
-        StringDeclaration declaration = new StringDeclaration();
-        fixture.setDeclaration(declaration);
+        assertEquals("testString", result);
     }
 
     /**
index e170e4423d90e8ece452ec33f8714ceaa651ab5b..98ebb77770627f286c782188deaf9a50e48396b8 100644 (file)
@@ -15,13 +15,15 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
-import java.util.List;
+import java.nio.ByteBuffer;
 import java.util.Map;
 
+import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
 import org.eclipse.linuxtools.ctf.core.event.types.IDeclaration;
 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.trace.CTFReaderException;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -69,11 +71,15 @@ public class StructDeclarationTest {
     /**
      * Run the StructDefinition createDefinition(DefinitionScope,String) method
      * test.
+     *
+     * @throws CTFReaderException
+     *             out of bounds
      */
     @Test
-    public void testCreateDefinition() {
+    public void testCreateDefinition() throws CTFReaderException {
         String fieldName = "";
-        StructDefinition result = fixture.createDefinition(null, fieldName);
+        BitBuffer bb = new BitBuffer(ByteBuffer.allocate(100));
+        StructDefinition result = fixture.createDefinition(null, fieldName, bb);
         assertNotNull(result);
     }
 
@@ -93,10 +99,10 @@ public class StructDeclarationTest {
      */
     @Test
     public void testGetFieldsList() {
-        List<String> result = fixture.getFieldsList();
+        Iterable<String> result = fixture.getFieldsList();
 
         assertNotNull(result);
-        assertEquals(0, result.size());
+        assertEquals(false, result.iterator().hasNext());
     }
 
     /**
index de01dc1acde4c1ef2bd3d28367cd1ed2f396b3ab..ce4a57afcb2dfb38a3067a700206947842b227c9 100644 (file)
@@ -16,13 +16,11 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 
 import java.nio.ByteBuffer;
-import java.nio.ByteOrder;
-import java.util.Map;
 
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
 import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition;
 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.EnumDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
@@ -48,29 +46,32 @@ import org.junit.Test;
  */
 public class StructDefinitionTest {
 
-    private static final String TEST_STRUCT_ID = "testStruct";
-    private static final String ENUM_2 = "y";
-    private static final String ENUM_1 = "x";
-    private static final String TAG_ID = "Tag";
-    private static final String INT_ID = "_id";
-    private static final String STRING_ID = "_args";
-    private static final String ENUM_ID = "_enumArgs";
-    private static final String SEQUENCE_ID = "_seq";
-    private static final String LENGTH_SEQ = "_len";
+    @NonNull private static final String TEST_STRUCT_ID = "testStruct";
+    @NonNull private static final String ENUM_2 = "y";
+    @NonNull private static final String ENUM_1 = "x";
+    @NonNull private static final String TAG_ID = "Tag";
+    @NonNull private static final String INT_ID = "_id";
+    @NonNull private static final String STRING_ID = "_args";
+    @NonNull private static final String ENUM_ID = "_enumArgs";
+    @NonNull private static final String SEQUENCE_ID = "_seq";
+    @NonNull private static final String LENGTH_SEQ = "_len";
+    @NonNull private static final String VAR_FIELD_NAME = "SomeVariant";
 
     private StructDefinition fixture;
     private StructDefinition emptyStruct;
     private StructDefinition simpleStruct;
-    private static final String VAR_FIELD_NAME = "SomeVariant";
 
     /**
      * Perform pre-test initialization.
+     *
+     * @throws CTFReaderException
+     *             won't happen
      */
     @Before
-    public void setUp() {
+    public void setUp() throws CTFReaderException {
         StructDeclaration sDec = new StructDeclaration(12);
-        IntegerDeclaration id = new IntegerDeclaration(32, false, 32, ByteOrder.BIG_ENDIAN, Encoding.NONE, null, 8);
-        IntegerDeclaration lenDec = new IntegerDeclaration(8, false, 8, ByteOrder.BIG_ENDIAN, Encoding.NONE, null, 8);
+        IntegerDeclaration id = IntegerDeclaration.INT_32B_DECL;
+        IntegerDeclaration lenDec = IntegerDeclaration.UINT_8_DECL;
         StringDeclaration sd = new StringDeclaration();
         EnumDeclaration ed = new EnumDeclaration(id);
         SequenceDeclaration seqDec = new SequenceDeclaration(LENGTH_SEQ, id);
@@ -88,20 +89,26 @@ public class StructDefinitionTest {
         sDec.addField(LENGTH_SEQ, lenDec);
         sDec.addField(SEQUENCE_ID, seqDec);
         sDec.addField(VAR_FIELD_NAME, varDec);
-        fixture = sDec.createDefinition(null, TEST_STRUCT_ID);
-        EnumDefinition eDef = tagDec.createDefinition(fixture, TAG_ID);
-        VariantDefinition vd = varDec.createDefinition(fixture,VAR_FIELD_NAME );
-        vd.setTagDefinition(eDef);
-
+        byte bytes[] = new byte[100];
+        bytes[4] = 1;
+        bytes[8] = 2;
+        bytes[13] = 3;
+        ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
+        BitBuffer bb = new BitBuffer(byteBuffer);
+        fixture = sDec.createDefinition(null, TEST_STRUCT_ID, bb);
+        EnumDefinition eDef = tagDec.createDefinition(fixture, TAG_ID, bb);
+        assertNotNull(eDef);
+        VariantDefinition vd = varDec.createDefinition(fixture, VAR_FIELD_NAME, bb);
+        assertNotNull(vd);
         // Create an empty struct
         StructDeclaration esDec = new StructDeclaration(32);
-        emptyStruct = esDec.createDefinition(null, TEST_STRUCT_ID);
+        emptyStruct = esDec.createDefinition(null, TEST_STRUCT_ID, bb);
 
         // Create a simple struct with two items
         StructDeclaration ssDec = new StructDeclaration(32);
         ssDec.addField(INT_ID, id);
         ssDec.addField(STRING_ID, sd);
-        simpleStruct = ssDec.createDefinition(null, TEST_STRUCT_ID);
+        simpleStruct = ssDec.createDefinition(null, TEST_STRUCT_ID, bb);
     }
 
     /**
@@ -118,7 +125,7 @@ public class StructDefinitionTest {
      */
     @Test
     public void testGetDefinitions_1() {
-        Map<String, Definition> result = fixture.getDefinitions();
+        Definition result = fixture.getDefinition("_id");
         assertNotNull(result);
     }
 
@@ -217,19 +224,6 @@ public class StructDefinitionTest {
         assertNotNull(result);
     }
 
-    /**
-     * Run the void read(BitBuffer) method test.
-     * @throws CTFReaderException error
-     */
-    @Test
-    public void testRead_() throws CTFReaderException {
-        ByteBuffer bb = ByteBuffer.allocateDirect(128);
-        bb.put((byte) 20);
-        BitBuffer input = new BitBuffer(bb);
-
-        fixture.read(input);
-    }
-
     /**
      * Run the String toString() method test.
      */
index dd9126927435887762d4c0f77d40ffbb3c7bed98..70e154b6385d3a626fd7aa74fdc965425faef04c 100644 (file)
@@ -15,9 +15,18 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assume.assumeTrue;
 
+import java.nio.ByteBuffer;
+
+import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
+import org.eclipse.linuxtools.ctf.core.event.types.Definition;
+import org.eclipse.linuxtools.ctf.core.event.types.EnumDeclaration;
+import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.IDeclaration;
-import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope;
+import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
+import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration;
+import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition;
 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;
@@ -27,6 +36,8 @@ import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
 import org.junit.Before;
 import org.junit.Test;
 
+import com.google.common.collect.ImmutableList;
+
 /**
  * The class <code>VariantDeclarationTest</code> contains tests for the class
  * <code>{@link VariantDeclaration}</code>.
@@ -48,6 +59,52 @@ public class VariantDeclarationTest {
         fixture = new VariantDeclaration();
     }
 
+    private static IDefinitionScope createDefinitionScope() throws CTFReaderException {
+        assumeTrue(testTrace.exists());
+        StructDeclaration declaration = new StructDeclaration(8);
+        VariantDeclaration variantDeclaration = new VariantDeclaration();
+        variantDeclaration.addField("a", IntegerDeclaration.INT_32B_DECL);
+        variantDeclaration.addField("b", IntegerDeclaration.INT_32L_DECL);
+        variantDeclaration.setTag("a");
+
+        EnumDeclaration enumDeclaration = new EnumDeclaration(IntegerDeclaration.UINT_8_DECL);
+        enumDeclaration.add(0, 1, "a");
+        enumDeclaration.add(2, 2, "b");
+        declaration.addField("tag", enumDeclaration);
+        declaration.addField("variant", variantDeclaration);
+        EnumDefinition tagDef = new EnumDefinition(
+                enumDeclaration,
+                null,
+                "tag",
+                new IntegerDefinition(
+                        IntegerDeclaration.UINT_8_DECL,
+                        null,
+                        "test",
+                        0)
+                );
+        VariantDefinition variantDefinition = new VariantDefinition(
+                variantDeclaration,
+                testTrace.getTrace(),
+                "tag",
+                "tag",
+                new StringDefinition(
+                        new StringDeclaration(),
+                        null,
+                        "f",
+                        "tag"
+                ));
+
+        IDefinitionScope definitionScope = new StructDefinition(
+                declaration,
+                variantDefinition,
+                "",
+                ImmutableList.of("tag", variantDefinition.getCurrentFieldName()),
+                new Definition[] { tagDef, variantDefinition }
+                );
+
+        return definitionScope;
+    }
+
     /**
      * Run the VariantDeclaration() constructor test.
      */
@@ -74,35 +131,21 @@ public class VariantDeclarationTest {
      * Run the VariantDefinition createDefinition(DefinitionScope,String) method
      * test.
      *
-     * @throws CTFReaderException Should not happen
+     * @throws CTFReaderException
+     *             Should not happen
      */
     @Test
     public void testCreateDefinition() throws CTFReaderException {
-        fixture.setTag("");
+        fixture.setTag("tag");
+        fixture.addField("a", IntegerDeclaration.UINT_64B_DECL);
         IDefinitionScope definitionScope = createDefinitionScope();
         String fieldName = "";
-        VariantDefinition result = fixture.createDefinition(definitionScope, fieldName);
+        BitBuffer bb = new BitBuffer(ByteBuffer.allocate(100));
+        VariantDefinition result = fixture.createDefinition(definitionScope, fieldName, bb);
 
         assertNotNull(result);
     }
 
-    private static IDefinitionScope createDefinitionScope() throws CTFReaderException {
-        assumeTrue(testTrace.exists());
-        VariantDeclaration declaration = new VariantDeclaration();
-        declaration.setTag("");
-        VariantDeclaration variantDeclaration = new VariantDeclaration();
-        variantDeclaration.setTag("");
-        VariantDefinition variantDefinition = new VariantDefinition(
-                variantDeclaration, testTrace.getTrace(), "");
-        IDefinitionScope definitionScope = new StructDefinition(
-                new StructDeclaration(1L), variantDefinition, "");
-        String fieldName = "";
-
-        VariantDefinition result = new VariantDefinition(declaration,
-                definitionScope, fieldName);
-        return result;
-    }
-
     /**
      * Run the boolean hasField(String) method test.
      */
index c70acad279b85c2e119081321c32129693c14c28..934c1ae50be22962741bd4e806a79160097538c8 100644 (file)
@@ -15,10 +15,12 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 
+import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
-import java.util.HashMap;
-import java.util.Map;
 
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
 import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
@@ -26,7 +28,6 @@ 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.EnumDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.FloatDeclaration;
-import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope;
 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.SequenceDefinition;
@@ -36,9 +37,12 @@ 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.ctf.core.event.types.VariantDefinition;
+import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
 import org.junit.Before;
 import org.junit.Test;
 
+import com.google.common.collect.ImmutableList;
+
 /**
  * The class <code>VariantDefinitionTest</code> contains tests for the class
  * <code>{@link VariantDefinition}</code>.
@@ -50,42 +54,39 @@ public class VariantDefinitionTest {
 
     private VariantDefinition fixture;
 
-    StructDefinition structDefinition;
-    private static final String TEST_STRUCT_ID = "testStruct";
-
-    private static final String ENUM_7 = "g";
-    private static final String ENUM_6 = "f";
-    private static final String ENUM_5 = "e";
-    private static final String ENUM_4 = "d";
-    private static final String ENUM_3 = "c";
-    private static final String ENUM_2 = "b";
-    private static final String ENUM_1 = "a";
+    StructDefinition fStructDefinition;
+    @NonNull private static final String TEST_STRUCT_ID = "testStruct";
 
-    private static final String TAG_ID = "a";
+    @NonNull private static final String ENUM_7 = "g";
+    @NonNull private static final String ENUM_6 = "f";
+    @NonNull private static final String ENUM_5 = "e";
+    @NonNull private static final String ENUM_4 = "d";
+    @NonNull private static final String ENUM_3 = "c";
+    @NonNull private static final String ENUM_2 = "b";
+    @NonNull private static final String ENUM_1 = "a";
 
-//    private static final String INT_ID = "_id";
-//    private static final String STRING_ID = "_args";
-//    private static final String ENUM_ID = "_enumArgs";
-//    private static final String SEQUENCE_ID = "_seq";
+    @NonNull private static final String TAG_ID = "a";
 
-    private static final String LENGTH_SEQ = "_len";
-    private static final String VAR_FIELD_NAME = "var";
+    @NonNull private static final String LENGTH_SEQ = "_len";
+    @NonNull private static final String VAR_FIELD_NAME = "var";
     private static final String ENUM_8 = null;
 
     /**
      * Perform pre-test initialization.
      *
      * Not sure it needs to be that complicated, oh well...
+     *
+     * @throws CTFReaderException
+     *             won't happen
      */
     @Before
-    public void setUp() {
+    public void setUp() throws CTFReaderException {
         StructDeclaration sDec = new StructDeclaration(12);
         StructDeclaration smallStruct = new StructDeclaration(8);
-        IntegerDeclaration iDec = new IntegerDeclaration(32, false, 32, ByteOrder.BIG_ENDIAN, Encoding.NONE, null, 8);
-        IntegerDeclaration lenDec = new IntegerDeclaration(8, false, 8, ByteOrder.BIG_ENDIAN, Encoding.NONE, null, 8);
+        IntegerDeclaration iDec = IntegerDeclaration.createDeclaration(32, false, 32, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 8);
+        IntegerDeclaration lenDec = IntegerDeclaration.createDeclaration(8, false, 8, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 8);
         StringDeclaration strDec = new StringDeclaration();
         EnumDeclaration enDec = new EnumDeclaration(iDec);
-//        SequenceDeclaration seqDec = new SequenceDeclaration(LENGTH_SEQ, iDec);
         VariantDeclaration varDec = new VariantDeclaration();
         EnumDeclaration tagDec = new EnumDeclaration(iDec);
         ArrayDeclaration arrDec = new ArrayDeclaration(2, iDec);
@@ -93,7 +94,6 @@ public class VariantDefinitionTest {
         tagDec.add(0, 1, ENUM_1);
         tagDec.add(2, 3, ENUM_2);
         tagDec.add(4, 5, ENUM_3);
-        //tagDec.add(6, 7, ENUM_4); // this should not work
         tagDec.add(8, 9, ENUM_5);
         tagDec.add(10, 11, ENUM_6);
         tagDec.add(12, 13, ENUM_7);
@@ -101,39 +101,74 @@ public class VariantDefinitionTest {
         varDec.addField(ENUM_7, fDec);
         varDec.addField(ENUM_6, smallStruct);
         varDec.addField(ENUM_5, enDec);
-        //varDec.addField(ENUM_4, seqDec);// this should not work
         varDec.addField(ENUM_3, arrDec);
         varDec.addField(ENUM_2, iDec);
         varDec.addField(ENUM_1, strDec);
 
         sDec.addField(TAG_ID, tagDec);
         sDec.addField(LENGTH_SEQ, lenDec);
-//        sDec.addField(SEQUENCE_ID, seqDec);
 
         sDec.addField(VAR_FIELD_NAME, varDec);
         varDec.setTag(TAG_ID);
 
-        structDefinition = sDec.createDefinition(null, TEST_STRUCT_ID);
-        fixture = (VariantDefinition) structDefinition.getDefinitions().get(VAR_FIELD_NAME);
+        ByteBuffer byteBuffer = ByteBuffer.allocate(100);
+        BitBuffer bb = new BitBuffer(byteBuffer);
+        byteBuffer.mark();
+        byteBuffer.putInt(1);
+        byteBuffer.putInt(2);
+        byteBuffer.putInt(3);
+        byteBuffer.reset();
+        fStructDefinition = sDec.createDefinition(null, TEST_STRUCT_ID, bb);
+        fixture = (VariantDefinition) fStructDefinition.getDefinition(VAR_FIELD_NAME);
     }
 
     /**
      * Run the VariantDefinition(VariantDeclaration,DefinitionScope,String)
+     *
+     * @throws CTFReaderException
+     *             should not happen
      */
     @Test
-    public void testVariantDefinition() {
+    public void testVariantDefinition() throws CTFReaderException {
         VariantDeclaration declaration = new VariantDeclaration();
         declaration.setTag("");
         VariantDeclaration variantDeclaration = new VariantDeclaration();
-        variantDeclaration.setTag("");
-        VariantDefinition variantDefinition = new VariantDefinition(
-                variantDeclaration, structDefinition, "");
+        variantDeclaration.addField("", new EnumDeclaration(IntegerDeclaration.INT_32B_DECL));
+        variantDeclaration.addField("a", IntegerDeclaration.INT_64B_DECL);
+        declaration.addField(ENUM_3, new StringDeclaration());
+        variantDeclaration.setTag("a");
+
+        byte[] bytes = new byte[128];
+        ByteBuffer byb = ByteBuffer.wrap(bytes);
+        byb.mark();
+        byb.putInt(0);
+        byb.putShort((short) 2);
+        byb.put(new String("hello").getBytes());
+        byb.reset();
+        BitBuffer bb = new BitBuffer(byb);
+        VariantDefinition variantDefinition = variantDeclaration.createDefinition(fStructDefinition, "field", bb);
+        EnumDeclaration declaration2 = new EnumDeclaration(IntegerDeclaration.INT_8_DECL);
+        declaration2.add(0, 2, ENUM_3);
+        EnumDefinition enumDefinition = new EnumDefinition(
+                declaration2,
+                null,
+                "a",
+                new IntegerDefinition(
+                        IntegerDeclaration.INT_8_DECL,
+                        null,
+                        "A",
+                        1
+                ));
         IDefinitionScope definitionScope = new StructDefinition(
-                new StructDeclaration(1L), variantDefinition, "");
+                new StructDeclaration(1L),
+                variantDefinition,
+                "",
+                ImmutableList.<String> of("", "variant"),
+                new Definition[] { enumDefinition, variantDefinition }
+                );
         String fieldName = "";
-
-        VariantDefinition result = new VariantDefinition(declaration,
-                definitionScope, fieldName);
+        declaration.setTag("");
+        VariantDefinition result = declaration.createDefinition(definitionScope, fieldName, bb);
         assertNotNull(result);
     }
 
@@ -143,9 +178,6 @@ public class VariantDefinitionTest {
     @Test
     public void testGetCurrentField() {
         Definition result = fixture.getCurrentField();
-        assertNull(result);
-        fixture.setCurrentField(ENUM_1);
-        result = fixture.getCurrentField();
         assertNotNull(result);
     }
 
@@ -154,7 +186,6 @@ public class VariantDefinitionTest {
      */
     @Test
     public void testGetCurrentFieldName() {
-        fixture.setCurrentField(ENUM_1);
         String result = fixture.getCurrentFieldName();
         assertNotNull(result);
     }
@@ -173,7 +204,7 @@ public class VariantDefinitionTest {
      */
     @Test
     public void testGetDefinitions() {
-        Map<String, Definition> result = fixture.getDefinitions();
+        Definition result = fixture.getCurrentField();
         assertNotNull(result);
     }
 
@@ -182,16 +213,7 @@ public class VariantDefinitionTest {
      */
     @Test
     public void testGetPath() {
-        String result = fixture.getPath();
-        assertNotNull(result);
-    }
-
-    /**
-     * Run the EnumDefinition getTagDefinition() method test.
-     */
-    @Test
-    public void testGetTagDefinition() {
-        EnumDefinition result = fixture.getTagDefinition();
+        String result = fixture.getScopePath().toString();
         assertNotNull(result);
     }
 
@@ -201,7 +223,7 @@ public class VariantDefinitionTest {
     @Test
     public void testLookupArray() {
         ArrayDefinition result = fixture.lookupArray(ENUM_3);
-        assertNotNull(result);
+        assertNull(result);
     }
 
     /**
@@ -211,6 +233,7 @@ public class VariantDefinitionTest {
     public void testLookupDefinition() {
         Definition result = fixture.lookupDefinition(ENUM_1);
         assertNotNull(result);
+        assertEquals("a", ((EnumDefinition) result).getStringValue());
     }
 
     /**
@@ -219,7 +242,7 @@ public class VariantDefinitionTest {
     @Test
     public void testLookupEnum() {
         EnumDefinition result = fixture.lookupEnum(ENUM_5);
-        assertNotNull(result);
+        assertNull(result);
     }
 
     /**
@@ -228,7 +251,7 @@ public class VariantDefinitionTest {
     @Test
     public void testLookupInteger() {
         IntegerDefinition result = fixture.lookupInteger(ENUM_2);
-        assertNotNull(result);
+        assertNull(result);
     }
 
     /**
@@ -246,7 +269,7 @@ public class VariantDefinitionTest {
     @Test
     public void testLookupString() {
         StringDefinition result = fixture.lookupString(ENUM_1);
-        assertNotNull(result);
+        assertNull(result);
     }
 
     /**
@@ -255,7 +278,7 @@ public class VariantDefinitionTest {
     @Test
     public void testLookupStruct() {
         StructDefinition result = fixture.lookupStruct(ENUM_6);
-        assertNotNull(result);
+        assertNull(result);
     }
 
     /**
@@ -267,64 +290,12 @@ public class VariantDefinitionTest {
         assertNull(result);
     }
 
-    /**
-     * Run the void setCurrentField(String) method test.
-     */
-    @Test
-    public void testSetCurrentField() {
-        fixture.setCurrentField(ENUM_1);
-    }
-
-    /**
-     * Run the void setDeclaration(VariantDeclaration) method test.
-     */
-    @Test
-    public void testSetDeclaration() {
-        VariantDeclaration declaration = new VariantDeclaration();
-        fixture.setDeclaration(declaration);
-    }
-
-    /**
-     * Run the void setDefinitions(HashMap<String,Definition>) method test.
-     */
-    @Test
-    public void testSetDefinitions() {
-        HashMap<String, Definition> definitions = new HashMap<>();
-        fixture.setDefinitions(definitions);
-    }
-
-    /**
-     * Run the void setTagDefinition(EnumDefinition) method test.
-     */
-    @Test
-    public void testSetTagDefinition(){
-        VariantDeclaration vDecl;
-        VariantDefinition vDef;
-        StructDefinition structDef;
-        EnumDefinition tagDefinition;
-        String fName = "";
-
-        vDecl = new VariantDeclaration();
-        vDecl.setTag(fName);
-        vDef = new VariantDefinition(vDecl, structDefinition, fName);
-        structDef = new StructDefinition(new StructDeclaration(1L), vDef, fName);
-        tagDefinition = new EnumDefinition(new EnumDeclaration(
-                new IntegerDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN,
-                        Encoding.ASCII, fName, 8)), structDef, fName);
-
-        fixture.setTagDefinition(tagDefinition);
-    }
-
     /**
      * Run the String toString() method test.
      */
     @Test
     public void testToString() {
         String result = fixture.toString();
-        assertEquals("{ null = null }", result);
-
-        fixture.setCurrentField(ENUM_2);
-        result = fixture.toString();
-        assertEquals("{ b = 0 }", result);
+        assertEquals("{ a = \"\" }", result);
     }
 }
index 5dd80186a024e375f4461f7d7752fcf5b3f0dc1c..1bbbc192f5fe7b2a07d4ee11cc6d09da7f2cda56 100644 (file)
@@ -13,6 +13,7 @@ Require-Bundle: org.eclipse.core.runtime,
 Export-Package: org.eclipse.linuxtools.ctf.core,
  org.eclipse.linuxtools.ctf.core.event,
  org.eclipse.linuxtools.ctf.core.event.io,
+ org.eclipse.linuxtools.ctf.core.event.scope,
  org.eclipse.linuxtools.ctf.core.event.types,
  org.eclipse.linuxtools.ctf.core.trace,
  org.eclipse.linuxtools.internal.ctf.core;x-friends:="org.eclipse.linuxtools.ctf.core.tests",
@@ -20,5 +21,7 @@ Export-Package: org.eclipse.linuxtools.ctf.core,
  org.eclipse.linuxtools.internal.ctf.core.event.metadata;x-friends:="org.eclipse.linuxtools.ctf.core.tests",
  org.eclipse.linuxtools.internal.ctf.core.event.metadata.exceptions;x-friends:="org.eclipse.linuxtools.ctf.core.tests",
  org.eclipse.linuxtools.internal.ctf.core.trace;x-friends:="org.eclipse.linuxtools.ctf.core.tests"
-Import-Package: org.antlr.runtime;version="3.2.0",
+Import-Package: com.google.common.base,
+ com.google.common.collect,
+ org.antlr.runtime;version="3.2.0",
  org.antlr.runtime.tree;version="3.2.0"
index ddfbe617f6424e9ee0b8263ab5261016daa88bf4..7a2207a1a0e644689300029cc1e98e8234f6eeb3 100644 (file)
@@ -17,3 +17,5 @@ bin.includes = META-INF/,\
                about.html,\
                plugin.properties
 src.includes = about.html
+additional.bundles = org.eclipse.jdt.annotation
+jars.extra.classpath = platform:/plugin/org.eclipse.jdt.annotation
\ No newline at end of file
index 3c18db44ca6f38a5593f5e55c05131e7c851a69a..fd42aa1597aa559bfc0a462730f730eb521181a9 100644 (file)
@@ -12,6 +12,8 @@
 
 package org.eclipse.linuxtools.ctf.core;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
+
 /**
  * Non-externalized strings for use with the CTF plugin (event names, field
  * names, etc.)
@@ -20,6 +22,7 @@ package org.eclipse.linuxtools.ctf.core;
  * @since 2.2
  */
 @SuppressWarnings("nls")
+@NonNullByDefault
 public interface CTFStrings {
 
     /** Event name for lost events */
index b5ade19544b79c6d2839086c77c4513ae5c36db5..183bfc02216df93648bdfbd28004716eac56fa1f 100644 (file)
 
 package org.eclipse.linuxtools.ctf.core.event;
 
+import java.util.ArrayList;
 import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
 
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
+import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope;
 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
-import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope;
 import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
 import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader;
+import org.eclipse.linuxtools.internal.ctf.core.event.EventDeclaration;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableList.Builder;
 
 /**
  * Representation of a particular instance of an event.
  */
-public class EventDefinition implements IDefinitionScope {
+public final class EventDefinition implements IDefinitionScope {
 
     // ------------------------------------------------------------------------
     // Attributes
     // ------------------------------------------------------------------------
 
+    /**
+     * A null event, can be used for testing or poison pilling
+     *
+     * @since 3.0
+     */
+    @NonNull
+    public static final EventDefinition NULL_EVENT = new EventDefinition(new EventDeclaration(), null, -1L, null, null, null, null);
+
     /**
      * The corresponding event declaration.
      */
@@ -39,17 +52,21 @@ public class EventDefinition implements IDefinitionScope {
     /**
      * The timestamp of the current event.
      */
-    private long fTimestamp;
+    private final long fTimestamp;
 
     /**
      * The event context structure definition.
      */
-    private StructDefinition fContext;
+    private final StructDefinition fEventContext;
+
+    private final StructDefinition fStreamContext;
+
+    private final StructDefinition fPacketContext;
 
     /**
      * The event fields structure definition.
      */
-    private StructDefinition fFields;
+    private final StructDefinition fFields;
 
     /**
      * The StreamInputReader that reads this event definition.
@@ -67,21 +84,52 @@ public class EventDefinition implements IDefinitionScope {
      *            The corresponding event declaration
      * @param streamInputReader
      *            The SIR from where this EventDef was read
-     * @since 2.0
+     * @param timestamp
+     *            event timestamp
+     * @param eventContext
+     *            The event context
+     * @param packetContext
+     *            the packet context
+     * @param streamContext
+     *            the stream context
+     * @param fields
+     *            The event fields
+     * @since 3.0
      */
     public EventDefinition(IEventDeclaration declaration,
-            StreamInputReader streamInputReader) {
+            StreamInputReader streamInputReader,
+            long timestamp,
+            StructDefinition streamContext,
+            StructDefinition eventContext,
+            StructDefinition packetContext,
+            StructDefinition fields) {
         fDeclaration = declaration;
         fStreamInputReader = streamInputReader;
+        fTimestamp = timestamp;
+        fFields = fields;
+        fEventContext = eventContext;
+        fPacketContext = packetContext;
+        fStreamContext = streamContext;
     }
 
     // ------------------------------------------------------------------------
     // Getters/Setters/Predicates
     // ------------------------------------------------------------------------
 
+    /**
+     * @since 3.0
+     */
     @Override
-    public String getPath() {
-        return "event"; //$NON-NLS-1$
+    public LexicalScope getScopePath() {
+        String eventName = fDeclaration.getName();
+        if (eventName == null) {
+            return null;
+        }
+        LexicalScope myScope = LexicalScope.EVENT.getChild(eventName);
+        if (myScope == null) {
+            myScope = new LexicalScope(LexicalScope.EVENT, eventName);
+        }
+        return myScope;
     }
 
     /**
@@ -110,7 +158,7 @@ public class EventDefinition implements IDefinitionScope {
      * @since 1.2
      */
     public StructDefinition getEventContext() {
-        return fContext;
+        return fEventContext;
     }
 
     /**
@@ -119,48 +167,52 @@ public class EventDefinition implements IDefinitionScope {
      * @return the context in struct form
      */
     public StructDefinition getContext() {
-        final StructDefinition streamContext =
-                fStreamInputReader.getPacketReader().getStreamEventContextDef();
 
         /* Most common case so far */
-        if (streamContext == null) {
-            return fContext;
+        if (fStreamContext == null) {
+            return fEventContext;
         }
 
         /* streamContext is not null, but the context of the event is null */
-        if (fContext == null) {
-            return streamContext;
+        if (fEventContext == null) {
+            return fStreamContext;
         }
 
+        // TODO: cache if this is a performance issue
+
         /* The stream context and event context are assigned. */
         StructDeclaration mergedDeclaration = new StructDeclaration(1);
 
-        /* Add fields from the stream */
-        Map<String, Definition> defs = streamContext.getDefinitions();
-        for (Entry<String, Definition> entry : defs.entrySet()) {
-            mergedDeclaration.addField(entry.getKey(), entry.getValue().getDeclaration());
-        }
+        Builder<String> builder = ImmutableList.<String> builder();
+        List<Definition> fieldValues = new ArrayList<>();
 
-        /* Add fields from the event context, overwrite the stream ones if needed. */
-        for (Entry<String, Definition> entry : fContext.getDefinitions().entrySet()) {
-            mergedDeclaration.addField(entry.getKey(), entry.getValue().getDeclaration());
+        /* Add fields from the stream */
+        for (String fieldName : fStreamContext.getFieldNames()) {
+            Definition definition = fStreamContext.getDefinition(fieldName);
+            mergedDeclaration.addField(fieldName, definition.getDeclaration());
+            builder.add(fieldName);
+            fieldValues.add(definition);
         }
 
-        StructDefinition mergedContext = mergedDeclaration.createDefinition(null, "context"); //$NON-NLS-1$
-        for (String key : mergedContext.getDefinitions().keySet()) {
-            final Definition lookupDefinition = fContext.lookupDefinition(key);
-            /*
-             * If the key is in the event context, add it from there, if it is
-             * not, then it's in the stream. There is a priority with scoping so
-             * if there is a field like "context" in both stream and context,
-             * you display the context.
-             */
-            if (lookupDefinition != null) {
-                mergedContext.getDefinitions().put(key, lookupDefinition);
+        ImmutableList<String> fieldNames = builder.build();
+        /*
+         * Add fields from the event context, overwrite the stream ones if
+         * needed.
+         */
+        for (String fieldName : fEventContext.getFieldNames()) {
+            Definition definition = fEventContext.getDefinition(fieldName);
+            mergedDeclaration.addField(fieldName, definition.getDeclaration());
+            if (fieldNames.contains(fieldName)) {
+                fieldValues.set((fieldNames.indexOf(fieldName)), definition);
             } else {
-                mergedContext.getDefinitions().put(key, streamContext.lookupDefinition(key));
+                builder.add(fieldName);
+                fieldValues.add(definition);
             }
         }
+        fieldNames = builder.build();
+        StructDefinition mergedContext = new StructDefinition(mergedDeclaration, this, "context", //$NON-NLS-1$
+                fieldNames,
+                fieldValues.toArray(new Definition[fieldValues.size()]));
         return mergedContext;
     }
 
@@ -179,7 +231,7 @@ public class EventDefinition implements IDefinitionScope {
      * @return the packet context
      */
     public StructDefinition getPacketContext() {
-        return fStreamInputReader.getCurrentPacketContext();
+        return fPacketContext;
     }
 
     /**
@@ -198,30 +250,6 @@ public class EventDefinition implements IDefinitionScope {
         return fTimestamp;
     }
 
-    /**
-     * @param timestamp
-     *            the timestamp to set
-     */
-    public void setTimestamp(long timestamp) {
-        fTimestamp = timestamp;
-    }
-
-    /**
-     * @param context
-     *            the context to set
-     */
-    public void setContext(StructDefinition context) {
-        fContext = context;
-    }
-
-    /**
-     * @param fields
-     *            the fields to set
-     */
-    public void setFields(StructDefinition fields) {
-        fFields = fields;
-    }
-
     // ------------------------------------------------------------------------
     // Operations
     // ------------------------------------------------------------------------
@@ -229,7 +257,7 @@ public class EventDefinition implements IDefinitionScope {
     @Override
     public Definition lookupDefinition(String lookupPath) {
         if (lookupPath.equals("context")) { //$NON-NLS-1$
-            return fContext;
+            return fEventContext;
         } else if (lookupPath.equals("fields")) { //$NON-NLS-1$
             return fFields;
         } else {
@@ -239,31 +267,28 @@ public class EventDefinition implements IDefinitionScope {
 
     @Override
     public String toString() {
-        Map<String, Definition> definitions;
-        List<String> list;
+        Iterable<String> list;
         StringBuilder retString = new StringBuilder();
         final String cr = System.getProperty("line.separator");//$NON-NLS-1$
 
         retString.append("Event type: " + fDeclaration.getName() + cr); //$NON-NLS-1$
         retString.append("Timestamp: " + Long.toString(fTimestamp) + cr); //$NON-NLS-1$
 
-        if (fContext != null) {
-            definitions = fContext.getDefinitions();
-            list = fContext.getDeclaration().getFieldsList();
+        if (fEventContext != null) {
+            list = fEventContext.getDeclaration().getFieldsList();
 
             for (String field : list) {
                 retString.append(field
-                        + " : " + definitions.get(field).toString() + cr); //$NON-NLS-1$
+                        + " : " + fEventContext.getDefinition(field).toString() + cr); //$NON-NLS-1$
             }
         }
 
         if (fFields != null) {
-            definitions = fFields.getDefinitions();
             list = fFields.getDeclaration().getFieldsList();
 
             for (String field : list) {
                 retString.append(field
-                        + " : " + definitions.get(field).toString() + cr); //$NON-NLS-1$
+                        + " : " + fFields.getDefinition(field).toString() + cr); //$NON-NLS-1$
             }
         }
 
index 4956e0db84e556f36d468fb604f22f44af12b5ab..e10470d685f8e0f49d3e6b995be88a045dfcef75 100644 (file)
@@ -13,7 +13,10 @@ package org.eclipse.linuxtools.ctf.core.event;
 
 import java.util.Set;
 
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
 import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
+import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
 import org.eclipse.linuxtools.ctf.core.trace.Stream;
 import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader;
 
@@ -31,9 +34,17 @@ public interface IEventDeclaration {
      *
      * @param streamInputReader
      *            The StreamInputReader for which this definition is created.
+     * @param input
+     *            the bitbuffer input source
+     * @param timestamp
+     *            The timestamp when the event was taken
      * @return A new EventDefinition.
+     * @throws CTFReaderException
+     *             As a bitbuffer is used to read, it could have wrapped
+     *             IOExceptions.
+     * @since 3.0
      */
-    EventDefinition createDefinition(StreamInputReader streamInputReader);
+    EventDefinition createDefinition(StreamInputReader streamInputReader, @NonNull BitBuffer input, long timestamp) throws CTFReaderException;
 
     /**
      * Gets the name of an event declaration
diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/scope/IDefinitionScope.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/scope/IDefinitionScope.java
new file mode 100644 (file)
index 0000000..d73e6bd
--- /dev/null
@@ -0,0 +1,43 @@
+/*******************************************************************************
+ * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others
+ *
+ * All rights reserved. This program and the accompanying materials are made
+ * available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors: Matthew Khouzam - Initial API and implementation
+ * Contributors: Simon Marchi - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.ctf.core.event.scope;
+
+import org.eclipse.linuxtools.ctf.core.event.types.Definition;
+
+/**
+ * The scope of a CTF definition. Used for compound types.
+ *
+ * @version 1.0
+ * @author Matthew Khouzam
+ * @author Simon Marchi
+ * @since 3.0
+ */
+public interface IDefinitionScope {
+
+    /**
+     * Gets the path in a C style for the scope.
+     *
+     * @return the path
+     * @since 3.0
+     */
+    LexicalScope getScopePath();
+
+    /**
+     * Looks up in this definition scope.
+     *
+     * @param lookupPath
+     *            The path to look up
+     * @return The Definition that was read
+     */
+    Definition lookupDefinition(String lookupPath);
+}
diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/scope/LexicalScope.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/scope/LexicalScope.java
new file mode 100644 (file)
index 0000000..e2b6e1a
--- /dev/null
@@ -0,0 +1,283 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are made
+ * available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors: Matthew Khouzam - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.ctf.core.event.scope;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+
+import com.google.common.base.Joiner;
+
+/**
+ * A node of a lexical scope
+ *
+ * @author Matthew Khouzam
+ * @since 3.0
+ */
+@NonNullByDefault
+public class LexicalScope implements Comparable<LexicalScope> {
+    /**
+     * Empty string
+     *
+     * @since 3.0
+     */
+    public static final LexicalScope ROOT = new LexicalScope(null, ""); //$NON-NLS-1$
+
+    /**
+     * Trace string
+     *
+     * @since 3.0
+     */
+    public static final LexicalScope TRACE = new LexicalScope(ROOT, "trace"); //$NON-NLS-1$
+
+    /**
+     * Env string
+     *
+     * @since 3.0
+     */
+    public static final LexicalScope ENV = new LexicalScope(ROOT, "env"); //$NON-NLS-1$
+
+    /**
+     * Stream string
+     *
+     * @since 3.0
+     */
+    public static final LexicalScope STREAM = new LexicalScope(ROOT, "stream"); //$NON-NLS-1$
+
+    /**
+     * Event string
+     *
+     * @since 3.0
+     */
+    public static final LexicalScope EVENT = new LexicalScope(ROOT, "event"); //$NON-NLS-1$
+
+    /**
+     * Variant string
+     *
+     * @since 3.0
+     */
+    public static final LexicalScope VARIANT = new LexicalScope(ROOT, "variant"); //$NON-NLS-1$
+
+    /**
+     * packet string
+     *
+     * @since 3.0
+     */
+    public static final LexicalScope PACKET = new LexicalScope(ROOT, "packet"); //$NON-NLS-1$
+
+    /**
+     * Packet header string
+     *
+     * @since 3.0
+     *
+     */
+    public static final LexicalScope PACKET_HEADER = new LexicalScope(PACKET, "header"); //$NON-NLS-1$
+
+    /**
+     * Stream packet scope
+     *
+     * @since 3.0
+     */
+    public static final LexicalScope STREAM_PACKET = new LexicalScope(STREAM, "packet"); //$NON-NLS-1$
+
+    /**
+     * Stream Packet header string
+     *
+     * @since 3.0
+     */
+    public static final LexicalScope STREAM_PACKET_CONTEXT = new LexicalScope(STREAM_PACKET, "context"); //$NON-NLS-1$
+
+    /**
+     * Trace packet scope
+     *
+     * @since 3.0
+     */
+    public static final LexicalScope TRACE_PACKET = new LexicalScope(TRACE, "packet"); //$NON-NLS-1$
+
+    /**
+     * Stream event scope
+     *
+     * @since 3.0
+     */
+    public static final LexicalScope STREAM_EVENT = new LexicalScope(STREAM, "event"); //$NON-NLS-1$
+
+    /**
+     * Trace packet header string
+     *
+     * @since 3.0
+     */
+    public static final LexicalScope TRACE_PACKET_HEADER = new LexicalScope(STREAM_EVENT, "header"); //$NON-NLS-1$
+
+    /**
+     * Stream event context
+     *
+     * @since 3.0
+     */
+    public static final LexicalScope STREAM_EVENT_CONTEXT = new LexicalScope(STREAM_EVENT, "context"); //$NON-NLS-1$
+
+    /**
+     * Stream event header
+     *
+     * @since 3.0
+     */
+    public static final LexicalScope STREAM_EVENT_HEADER = new LexicalScope(TRACE_PACKET, "header"); //$NON-NLS-1$
+
+    /**
+     * Fields in an event
+     *
+     * @since 3.0
+     */
+    public static final LexicalScope FIELDS = new LexicalScope(ROOT, "fields"); //$NON-NLS-1$
+
+    /**
+     * Context of an event
+     *
+     * @since 3.0
+     */
+    public static final LexicalScope CONTEXT = new LexicalScope(ROOT, "context"); //$NON-NLS-1$
+
+    /**
+     * Sorted list of parent paths
+     *
+     * @since 3.0
+     */
+    public static final LexicalScope[] PARENT_PATHS = {
+            ROOT,
+            CONTEXT,
+            FIELDS,
+            PACKET_HEADER,
+            STREAM_EVENT_CONTEXT,
+            STREAM_EVENT_HEADER,
+            STREAM_PACKET_CONTEXT,
+            TRACE_PACKET_HEADER
+    };
+
+    private int hash = 0;
+    private final String fName;
+    private final String fPath;
+    private final Map<String, LexicalScope> fChildren;
+
+    /**
+     * The scope constructor
+     *
+     * @param parent
+     *            The parent node, can be null, but shouldn't
+     * @param name
+     *            the name of the field
+     */
+    public LexicalScope(@Nullable LexicalScope parent, String name) {
+        fName = name;
+        if (parent != null) {
+            String pathString = Joiner.on('.').skipNulls().join(parent.fPath, parent.getName());
+            if (pathString.startsWith(".")) { //$NON-NLS-1$
+                pathString = pathString.substring(1);
+            }
+            if (pathString == null) {
+                // we should get an NPE on pathString.startsWith before getting this
+                throw new IllegalStateException(
+                        "Lexical scope constructor had null pathstring for " + //$NON-NLS-1$
+                                parent.toString() + " and " + name); //$NON-NLS-1$
+            }
+            fPath = pathString;
+            parent.addChild(fName, this);
+        } else {
+            fPath = ""; //$NON-NLS-1$
+        }
+
+        @SuppressWarnings("null")
+        @NonNull
+        Map<String, LexicalScope> children =
+                Collections.synchronizedMap(new HashMap<String, LexicalScope>());
+        fChildren = children;
+    }
+
+    /**
+     * Adds a child lexical scope
+     *
+     * @param name
+     *            the name of the child
+     * @param child
+     *            the child
+     */
+    private void addChild(String name, LexicalScope child) {
+        fChildren.put(name, child);
+    }
+
+    /**
+     * Get the name
+     *
+     * @return the name
+     */
+    public String getName() {
+        return fName;
+    }
+
+    /**
+     * Gets a child of a given name
+     *
+     * @param name
+     *            the child
+     * @return the scope, can be null
+     */
+    @Nullable
+    public LexicalScope getChild(String name) {
+        return fChildren.get(name);
+    }
+
+    @Override
+    public String toString() {
+        return fPath + '.' + fName;
+    }
+
+    @Override
+    public int compareTo(@Nullable LexicalScope other) {
+        if (other == null) {
+            throw new IllegalArgumentException();
+        }
+        int comp = fPath.compareTo(other.fPath);
+        if (comp == 0) {
+            return fName.compareTo(other.fName);
+        }
+        return comp;
+    }
+
+    @Override
+    public synchronized int hashCode() {
+        if (hash == 0) {
+            final int prime = 31;
+            hash = prime * (prime + fName.hashCode()) + fPath.hashCode();
+        }
+        return hash;
+    }
+
+    @Override
+    public boolean equals(@Nullable Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        LexicalScope other = (LexicalScope) obj;
+        if (!fName.equals(other.fName)) {
+            return false;
+        }
+        return fPath.equals(other.fPath);
+    }
+}
index 5189fb5ed4f9acf83d589c279faaa2923ebdf517..d0d9691c8d4d905a73bb252482bc2b4d9981c9d5 100644 (file)
 
 package org.eclipse.linuxtools.ctf.core.event.types;
 
+import java.util.List;
+
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
+import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
+
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableList.Builder;
+import com.google.common.collect.Multimap;
+
 /**
  * A CTF array declaration
  *
@@ -24,14 +36,26 @@ package org.eclipse.linuxtools.ctf.core.event.types;
  * @author Matthew Khouzam
  * @author Simon Marchi
  */
-public class ArrayDeclaration implements IDeclaration {
+public class ArrayDeclaration extends Declaration {
 
     // ------------------------------------------------------------------------
     // Attributes
     // ------------------------------------------------------------------------
 
-    private final int length;
-    private final IDeclaration elemType;
+    private final int fLength;
+    private final IDeclaration fElemType;
+
+    /**
+     * <pre>
+     * Cache where we can pre-generate the children names
+     * Key&colon; parent name
+     * Value&colon; children names
+     * ex: field &#8594; &lbrace;field&lbrack;0&rbrack;, field&lbrack;1&rbrack;, &hellip; field&lbrack;n&rbrack;&rbrace;
+     * </pre>
+     *
+     * TODO: investigate performance
+     */
+    private final Multimap<String, String> fChildrenNames = ArrayListMultimap.<String, String> create();
 
     // ------------------------------------------------------------------------
     // Constructors
@@ -46,8 +70,8 @@ public class ArrayDeclaration implements IDeclaration {
      *            what type of element is in the array
      */
     public ArrayDeclaration(int length, IDeclaration elemType) {
-        this.length = length;
-        this.elemType = elemType;
+        fLength = length;
+        fElemType = elemType;
     }
 
     // ------------------------------------------------------------------------
@@ -59,7 +83,7 @@ public class ArrayDeclaration implements IDeclaration {
      * @return the type of element in the array
      */
     public IDeclaration getElementType() {
-        return elemType;
+        return fElemType;
     }
 
     /**
@@ -67,7 +91,7 @@ public class ArrayDeclaration implements IDeclaration {
      * @return how many elements in the array
      */
     public int getLength() {
-        return length;
+        return fLength;
     }
 
     /**
@@ -79,12 +103,12 @@ public class ArrayDeclaration implements IDeclaration {
      * @since 3.0
      */
     public boolean isString() {
-        if (elemType instanceof IntegerDeclaration) {
+        if (fElemType instanceof IntegerDeclaration) {
             /*
              * If the first byte is a "character", we'll consider the whole
              * array a character string.
              */
-            IntegerDeclaration elemInt = (IntegerDeclaration) elemType;
+            IntegerDeclaration elemInt = (IntegerDeclaration) fElemType;
             if (elemInt.isCharacter()) {
                 return true;
             }
@@ -101,10 +125,15 @@ public class ArrayDeclaration implements IDeclaration {
     // Operations
     // ------------------------------------------------------------------------
 
+    /**
+     * @since 3.0
+     */
     @Override
     public ArrayDefinition createDefinition(IDefinitionScope definitionScope,
-            String fieldName) {
-        return new ArrayDefinition(this, definitionScope, fieldName);
+            @NonNull String fieldName, BitBuffer input) throws CTFReaderException {
+        alignRead(input);
+        List<Definition> definitions = read(input, definitionScope, fieldName);
+        return new ArrayDefinition(this, definitionScope, fieldName, definitions);
     }
 
     @Override
@@ -113,4 +142,34 @@ public class ArrayDeclaration implements IDeclaration {
         return "[declaration] array[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$
     }
 
+    @NonNull
+    private List<Definition> read(@NonNull BitBuffer input, IDefinitionScope definitionScope, String fieldName) throws CTFReaderException {
+        Builder<Definition> definitions = new ImmutableList.Builder<>();
+        if (!fChildrenNames.containsKey(fieldName)) {
+            for (int i = 0; i < fLength; i++) {
+                fChildrenNames.put(fieldName, fieldName + '[' + i + ']');
+            }
+        }
+        List<String> elemNames = (List<String>) fChildrenNames.get(fieldName);
+        for (int i = 0; i < fLength; i++) {
+            String name = elemNames.get(i);
+            if (name == null) {
+                throw new IllegalStateException();
+            }
+            definitions.add(fElemType.createDefinition(definitionScope, name, input));
+        }
+        @SuppressWarnings("null")
+        @NonNull ImmutableList<Definition> ret = definitions.build();
+        return ret;
+    }
+
+    /**
+     * @since 3.0
+     */
+    @Override
+    public int getMaximumSize() {
+        long val = (long) fLength * fElemType.getMaximumSize();
+        return (int) Math.min(Integer.MAX_VALUE, val);
+    }
+
 }
index fab0012600afb73c09a8bacbb2d541d388191d70..695de3cddd8c25866e37db09d8be183cd6460900 100644 (file)
 
 package org.eclipse.linuxtools.ctf.core.event.types;
 
-import java.util.Arrays;
+import java.util.List;
 
-import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
-import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
+
+import com.google.common.base.Joiner;
+import com.google.common.collect.ImmutableList;
 
 /**
  * A CTF array definition
  *
- * Arrays are fixed-length. Their length is declared in the type
- * declaration within the meta-data. They contain an array of "inner type"
- * elements, which can refer to any type not containing the type of the
- * array being declared (no circular dependency). The length is the number
- * of elements in an array.
+ * Arrays are fixed-length. Their length is declared in the type declaration
+ * within the meta-data. They contain an array of "inner type" elements, which
+ * can refer to any type not containing the type of the array being declared (no
+ * circular dependency). The length is the number of elements in an array.
  *
  * @version 1.0
  * @author Matthew Khouzam
  * @author Simon Marchi
  */
-public class ArrayDefinition extends Definition {
+@NonNullByDefault
+public final class ArrayDefinition extends Definition {
 
     // ------------------------------------------------------------------------
     // Attributes
     // ------------------------------------------------------------------------
 
-    private final ArrayDeclaration declaration;
-    private Definition definitions[];
+    private final ImmutableList<Definition> fDefinitions;
 
     // ------------------------------------------------------------------------
     // Constructors
@@ -45,22 +49,26 @@ public class ArrayDefinition extends Definition {
 
     /**
      * Constructor
-     * @param declaration the parent declaration
-     * @param definitionScope the parent scope
-     * @param fieldName the field name
+     *
+     * @param declaration
+     *            the parent declaration
+     * @param definitionScope
+     *            the parent scope
+     * @param fieldName
+     *            the field name
+     * @param definitions
+     *            the content of the array
+     * @since 3.0
      */
     public ArrayDefinition(ArrayDeclaration declaration,
-            IDefinitionScope definitionScope, String fieldName) {
-        super(definitionScope, fieldName);
-
-        this.declaration = declaration;
-
-        definitions = new Definition[declaration.getLength()];
+            @Nullable IDefinitionScope definitionScope,
+            String fieldName,
+            List<Definition> definitions) {
+        super(declaration, definitionScope, fieldName);
+        @SuppressWarnings("null")
+        @NonNull ImmutableList<Definition> list = ImmutableList.copyOf(definitions);
+        fDefinitions = list;
 
-        for (int i = 0; i < declaration.getLength(); i++) {
-            definitions[i] = declaration.getElementType().createDefinition(
-                    definitionScope, fieldName + "[" + i + "]"); //$NON-NLS-1$ //$NON-NLS-2$
-        }
     }
 
     // ------------------------------------------------------------------------
@@ -69,54 +77,42 @@ public class ArrayDefinition extends Definition {
 
     /**
      * @return the definitions
+     * @since 3.0
      */
-    public Definition[] getDefinitions() {
-        return Arrays.copyOf(definitions, definitions.length);
-    }
-
-    /**
-     * @param definitions
-     *            the definitions to set
-     */
-    public void setDefinitions(Definition[] definitions) {
-        this.definitions = Arrays.copyOf(definitions, definitions.length);
+    public List<Definition> getDefinitions() {
+        return fDefinitions;
     }
 
     /**
      * Get the element at i
+     *
      * @param i the index (cannot be negative)
      * @return The element at I, if I &gt; length, null, if I &lt; 0, the method throws an out of bounds exception
      */
+    @Nullable
     public Definition getElem(int i) {
-        if (i > definitions.length) {
+        if (i > fDefinitions.size()) {
             return null;
         }
 
-        return definitions[i];
+        return fDefinitions.get(i);
     }
 
     @Override
     public ArrayDeclaration getDeclaration() {
-        return declaration;
+        return (ArrayDeclaration) super.getDeclaration();
     }
 
     // ------------------------------------------------------------------------
     // Operations
     // ------------------------------------------------------------------------
 
-    @Override
-    public void read(BitBuffer input) throws CTFReaderException {
-        for (Definition definition : definitions) {
-            definition.read(input);
-        }
-    }
-
     @Override
     public String toString() {
         StringBuilder b = new StringBuilder();
 
-        if (declaration.isString()) {
-            for (Definition def : definitions) {
+        if (getDeclaration().isString()) {
+            for (Definition def : fDefinitions) {
                 IntegerDefinition character = (IntegerDefinition) def;
 
                 if (character.getValue() == 0) {
@@ -125,20 +121,15 @@ public class ArrayDefinition extends Definition {
 
                 b.append(character.toString());
             }
-        } else if (definitions == null) {
-            b.append("[ ]"); //$NON-NLS-1$
         } else {
             b.append('[');
-            for (int i = 0; i < (definitions.length - 1); i++) {
-                b.append(' ');
-                b.append(definitions[i].toString());
-                b.append(',');
-            }
-            b.append(' ');
-            b.append(definitions[definitions.length - 1].toString());
-            b.append(" ]"); //$NON-NLS-1$
+            Joiner joiner = Joiner.on(", ").skipNulls(); //$NON-NLS-1$
+            b.append(joiner.join(fDefinitions));
+            b.append(']');
         }
 
-        return b.toString();
+        @SuppressWarnings("null")
+        @NonNull String ret = b.toString();
+        return ret;
     }
 }
diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/Declaration.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/Declaration.java
new file mode 100644 (file)
index 0000000..df30eaf
--- /dev/null
@@ -0,0 +1,69 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are made
+ * available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors: Matthew Khouzam - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.ctf.core.event.types;
+
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
+import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope;
+import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
+
+/**
+ * Declaration base, it helps for basic functionality that is often called, so
+ * performance is often a high priority in this class
+ *
+ * @author Matthew Khouzam
+ * @since 3.0
+ */
+public abstract class Declaration implements IDeclaration {
+
+    @Override
+    public LexicalScope getPath(IDefinitionScope definitionScope, @NonNull String fieldName) {
+        if (definitionScope != null) {
+            final LexicalScope parentPath = definitionScope.getScopePath();
+            if (parentPath != null) {
+                LexicalScope myScope = parentPath.getChild(fieldName);
+                if (myScope == null) {
+                    myScope = new LexicalScope(parentPath, fieldName);
+                }
+                return myScope;
+            }
+        }
+        LexicalScope child = LexicalScope.ROOT.getChild(fieldName);
+        if (child != null) {
+            return child;
+        }
+        return new LexicalScope(LexicalScope.ROOT, fieldName);
+    }
+
+    /**
+     * Offset the buffer position wrt the current alignment.
+     *
+     * @param input
+     *            The bitbuffer that is being read
+     * @throws CTFReaderException
+     *             Happens when there is an out of bounds exception
+     * @since 3.0
+     */
+    protected final void alignRead(BitBuffer input) throws CTFReaderException {
+        long mask = getAlignment() - 1;
+        /*
+         * The alignment is a power of 2
+         */
+        long pos = input.position();
+        if ((pos & mask) == 0) {
+            return;
+        }
+        pos = (pos + mask) & ~mask;
+        input.position(pos);
+    }
+}
index df89d7cce828d696e82356bc02b299c95be7d98d..a690b46b87741b8ea92eca4df9d739a60939e1d1 100644 (file)
@@ -12,8 +12,9 @@
 
 package org.eclipse.linuxtools.ctf.core.event.types;
 
-import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
-import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
+import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope;
 
 /**
  * A CTF definition
@@ -36,12 +37,15 @@ public abstract class Definition {
     // Attributes
     // ------------------------------------------------------------------------
 
-    private final String fieldName;
+    private final String fFieldName;
 
     /** The complete path of this field */
-    private final String path;
+    private final LexicalScope fPath;
 
-    private final IDefinitionScope definitionScope;
+    private final IDefinitionScope fDefinitionScope;
+
+    @NonNull
+    private final IDeclaration fDeclaration;
 
     // ------------------------------------------------------------------------
     // Constructors
@@ -50,25 +54,21 @@ public abstract class Definition {
     /**
      * Constructor
      *
+     * @param declaration
+     *            the event declaration
+     *
      * @param definitionScope
      *            the definition is in a scope, (normally a struct) what is it?
      * @param fieldName
      *            the name of the definition. (it is a field in the parent
      *            scope)
+     * @since 3.0
      */
-    public Definition(IDefinitionScope definitionScope, String fieldName) {
-        this.definitionScope = definitionScope;
-        this.fieldName = fieldName;
-        if (definitionScope != null) {
-            String parentPath = definitionScope.getPath();
-            if (parentPath.length() > 0) {
-                path = parentPath + "." + fieldName; //$NON-NLS-1$
-            } else {
-                path = fieldName;
-            }
-        } else {
-            path = fieldName;
-        }
+    public Definition(@NonNull IDeclaration declaration, IDefinitionScope definitionScope, @NonNull String fieldName) {
+        fDeclaration = declaration;
+        fDefinitionScope = definitionScope;
+        fFieldName = fieldName;
+        fPath = fDeclaration.getPath(definitionScope, fieldName);
     }
 
     // ------------------------------------------------------------------------
@@ -82,17 +82,17 @@ public abstract class Definition {
      * @since 2.0
      */
     protected String getFieldName() {
-        return fieldName;
+        return fFieldName;
     }
 
     /**
      * Get the complete path of this field.
      *
      * @return The path
-     * @since 2.0
+     * @since 3.0
      */
-    public String getPath() {
-        return path;
+    public LexicalScope getScopePath() {
+        return fPath;
     }
 
     /**
@@ -102,10 +102,10 @@ public abstract class Definition {
      * scope DOT the name of the definition (name of the field in its container)
      *
      * @return The definition scope
-     * @since 2.0
+     * @since 3.0
      */
     protected IDefinitionScope getDefinitionScope() {
-        return definitionScope;
+        return fDefinitionScope;
     }
 
     // ------------------------------------------------------------------------
@@ -117,46 +117,13 @@ public abstract class Definition {
      * @return gets the declaration of a datatype
      *
      */
-    public abstract IDeclaration getDeclaration();
-
-    /**
-     * Read the definition from a bitbuffer
-     *
-     * @param input
-     *            the bitbuffer containing the data to read.
-     * @throws CTFReaderException
-     *             An error occurred reading the data. If the buffer is reading
-     *             beyond its end, this exception will be raised.
-     * @since 2.0
-     */
-    public abstract void read(BitBuffer input) throws CTFReaderException;
-
-    /**
-     * Offset the buffer position wrt the current alignment.
-     *
-     * @param input
-     *            The bitbuffer that is being read
-     * @param declaration
-     *            The declaration which has an alignment
-     * @throws CTFReaderException
-     *            Happens when there is an out of bounds exception
-     * @since 2.2
-     */
-    protected static void alignRead(BitBuffer input, IDeclaration declaration) throws CTFReaderException{
-        long mask = declaration.getAlignment() -1;
-        /*
-         * The alignment is a power of 2
-         */
-        long pos = input.position();
-        if ((pos & mask) == 0) {
-            return;
-        }
-        pos = (pos + mask) & ~mask;
-        input.position(pos);
+    @NonNull
+    public IDeclaration getDeclaration() {
+        return fDeclaration;
     }
 
     @Override
     public String toString() {
-        return path + '[' + Integer.toHexString(hashCode()) + ']';
+        return fPath.toString() + '[' + Integer.toHexString(hashCode()) + ']';
     }
 }
index e77b890d9796e6d65bc1564c50f0ba5100b3788f..a529d1ee6d8ed700a3e5857646ebaaa91fca56a2 100644 (file)
@@ -12,6 +12,8 @@
 
 package org.eclipse.linuxtools.ctf.core.event.types;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
+
 /**
  * CTF encoding types
  *
@@ -19,6 +21,7 @@ package org.eclipse.linuxtools.ctf.core.event.types;
  * @author Matthew Khouzam
  * @author Simon Marchi
  */
+@NonNullByDefault
 public enum Encoding {
     /** UTF-8 encoding */
     UTF8,
index 89d28d8d0eef7ae1d6434784420a22ba7e6abe60..6d250986f3ebb23266ce042aa56ebeb434b2cacb 100644 (file)
@@ -18,6 +18,10 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Set;
 
+import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
+import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
+
 /**
  * A CTF enum declaration.
  *
@@ -28,7 +32,7 @@ import java.util.Set;
  * @author Matthew Khouzam
  * @author Simon Marchi
  */
-public class EnumDeclaration implements IDeclaration {
+public final class EnumDeclaration extends Declaration {
 
     // ------------------------------------------------------------------------
     // Attributes
@@ -72,14 +76,26 @@ public class EnumDeclaration implements IDeclaration {
         return this.getContainerType().getAlignment();
     }
 
+    /**
+     * @since 3.0
+     */
+    @Override
+    public int getMaximumSize() {
+        return fContainerType.getMaximumSize();
+    }
+
     // ------------------------------------------------------------------------
     // Operations
     // ------------------------------------------------------------------------
 
+    /**
+     * @since 3.0
+     */
     @Override
-    public EnumDefinition createDefinition(IDefinitionScope definitionScope,
-            String fieldName) {
-        return new EnumDefinition(this, definitionScope, fieldName);
+    public EnumDefinition createDefinition(IDefinitionScope definitionScope, String fieldName, BitBuffer input) throws CTFReaderException {
+        alignRead(input);
+        IntegerDefinition value = getContainerType().createDefinition(definitionScope, fieldName, input);
+        return new EnumDefinition(this, definitionScope, fieldName, value);
     }
 
     /**
index 82d5230989c4a78c3cfaf7fb6c359f765d1364e2..09f6927f50443b2bba28eb29d26ed036044be60e 100644 (file)
 
 package org.eclipse.linuxtools.ctf.core.event.types;
 
-import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
-import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
 
 /**
  * A CTF enum definition.
  *
- * The definition of a enum point basic data type. It will take the data
- * from a trace and store it (and make it fit) as an integer and a string.
+ * The definition of a enum point basic data type. It will take the data from a
+ * trace and store it (and make it fit) as an integer and a string.
  *
  * @version 1.0
  * @author Matthew Khouzam
  * @author Simon Marchi
  */
-public class EnumDefinition extends SimpleDatatypeDefinition {
+public final class EnumDefinition extends SimpleDatatypeDefinition {
 
     // ------------------------------------------------------------------------
     // Attributes
     // ------------------------------------------------------------------------
 
-    private final EnumDeclaration declaration;
+    private final long fIntegerValue;
 
-    private final IntegerDefinition integerValue;
-
-    private String value;
+    private final String fValue;
 
     // ------------------------------------------------------------------------
     // Constructors
@@ -43,19 +41,23 @@ public class EnumDefinition extends SimpleDatatypeDefinition {
 
     /**
      * Constructor
-     * @param declaration the parent declaration
-     * @param definitionScope the parent scope
-     * @param fieldName the field name
+     *
+     * @param declaration
+     *            the parent declaration
+     * @param definitionScope
+     *            the parent scope
+     * @param fieldName
+     *            the field name
+     * @param intValue
+     *            the value of the enum
+     * @since 3.0
      */
-    public EnumDefinition(EnumDeclaration declaration,
-            IDefinitionScope definitionScope, String fieldName) {
-        super(definitionScope, fieldName);
-
-        this.declaration = declaration;
+    public EnumDefinition(@NonNull EnumDeclaration declaration,
+            IDefinitionScope definitionScope, @NonNull String fieldName, IntegerDefinition intValue) {
+        super(declaration, definitionScope, fieldName);
 
-        integerValue = declaration.getContainerType().createDefinition(
-                definitionScope, fieldName);
-        value = declaration.query(integerValue.getValue());
+        fIntegerValue = intValue.getValue();
+        fValue = declaration.query(fIntegerValue);
     }
 
     // ------------------------------------------------------------------------
@@ -63,60 +65,44 @@ public class EnumDefinition extends SimpleDatatypeDefinition {
     // ------------------------------------------------------------------------
 
     /**
-     * Gets the value of the enum in string format so "Enum a{DAY="0", NIGHT="1"}; will return "DAY"
+     * Gets the value of the enum in string format so
+     * "Enum a{DAY="0", NIGHT="1"}; will return "DAY"
+     *
      * @return the value of the enum.
      */
     public String getValue() {
-        return value;
+        return fValue;
     }
 
     @Override
-    public String getStringValue(){
+    public String getStringValue() {
         return getValue();
     }
 
     /**
-     * Gets the value of the enum in string format so "Enum a{DAY="0", NIGHT="1"}; will return 0
+     * Gets the value of the enum in string format so
+     * "Enum a{DAY="0", NIGHT="1"}; will return 0
+     *
      * @return the value of the enum.
      */
     @Override
     public Long getIntegerValue() {
-        return integerValue.getValue();
-    }
-
-    /**
-     * Sets the value of the enum in string format so "Enum a{DAY="0", NIGHT="1"}; will set 0
-     * @param value The value of the enum.
-     */
-    public void setIntegerValue(long value) {
-        integerValue.setValue(value);
-        this.value = declaration.query(value);
+        return fIntegerValue;
     }
 
     @Override
     public EnumDeclaration getDeclaration() {
-        return declaration;
+        return (EnumDeclaration) super.getDeclaration();
     }
 
     // ------------------------------------------------------------------------
     // Operations
     // ------------------------------------------------------------------------
 
-    @Override
-    public void read(BitBuffer input) throws CTFReaderException {
-        alignRead(input, this.declaration);
-        integerValue.read(input);
-        long val = integerValue.getValue();
-
-        // TODO: what to do if the integer value maps to no string for this
-        // integer ?
-        value = declaration.query(val);
-    }
-
     @Override
     public String toString() {
         return "{ value = " + getValue() + //$NON-NLS-1$
-                ", container = " + integerValue.toString() + //$NON-NLS-1$
+                ", container = " + fIntegerValue + //$NON-NLS-1$
                 " }"; //$NON-NLS-1$
     }
 }
index f9ca2ceed58fe9421d3569032f37560d940e3bff..18d0f9c9c884f382ed0685f4d6e9191cb3b8bcde 100644 (file)
@@ -13,6 +13,10 @@ package org.eclipse.linuxtools.ctf.core.event.types;
 
 import java.nio.ByteOrder;
 
+import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
+import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
+
 /**
  * A CTF float declaration.
  *
@@ -21,16 +25,16 @@ import java.nio.ByteOrder;
  * @version 1.0
  * @author Matthew Khouzam
  */
-public class FloatDeclaration implements IDeclaration {
+public final class FloatDeclaration extends Declaration {
 
     // ------------------------------------------------------------------------
     // Attributes
     // ------------------------------------------------------------------------
 
-    private final int mant;
-    private final int exp;
-    private final ByteOrder byteOrder;
-    private final long alignment;
+    private final int fMantissa;
+    private final int fExponent;
+    private final ByteOrder fByteOrder;
+    private final long fAlignement;
 
     // ------------------------------------------------------------------------
     // Constructors
@@ -50,10 +54,10 @@ public class FloatDeclaration implements IDeclaration {
      */
     public FloatDeclaration(int exponent, int mantissa, ByteOrder byteOrder,
             long alignment) {
-        mant = mantissa;
-        exp = exponent;
-        this.byteOrder = byteOrder;
-        this.alignment = Math.max(alignment, 1);
+        fMantissa = mantissa;
+        fExponent = exponent;
+        fByteOrder = byteOrder;
+        fAlignement = Math.max(alignment, 1);
 
     }
 
@@ -65,36 +69,49 @@ public class FloatDeclaration implements IDeclaration {
      * @return the mant
      */
     public int getMantissa() {
-        return mant;
+        return fMantissa;
     }
 
     /**
      * @return the exp
      */
     public int getExponent() {
-        return exp;
+        return fExponent;
     }
 
     /**
      * @return the byteOrder
      */
     public ByteOrder getByteOrder() {
-        return byteOrder;
+        return fByteOrder;
     }
 
     @Override
     public long getAlignment() {
-        return alignment;
+        return fAlignement;
+    }
+
+    /**
+     * @since 3.0
+     */
+    @Override
+    public int getMaximumSize() {
+        return fMantissa + fExponent + 1;
     }
 
     // ------------------------------------------------------------------------
     // Operations
     // ------------------------------------------------------------------------
 
+    /**
+     * @since 3.0
+     */
     @Override
     public FloatDefinition createDefinition(IDefinitionScope definitionScope,
-            String fieldName) {
-        return new FloatDefinition(this, definitionScope, fieldName);
+            String fieldName, BitBuffer input) throws CTFReaderException {
+        alignRead(input);
+        double value = read(input);
+        return new FloatDefinition(this, definitionScope, fieldName, value);
     }
 
     @Override
@@ -102,4 +119,57 @@ public class FloatDeclaration implements IDeclaration {
         /* Only used for debugging */
         return "[declaration] float[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$
     }
+
+    private double read(BitBuffer input) throws CTFReaderException {
+        /* Offset the buffer position wrt the current alignment */
+        alignRead(input);
+        final int exp = getExponent();
+        final int mant = getMantissa();
+        double value = Double.NaN;
+        if ((exp + mant) == 32) {
+            value = readRawFloat32(input, mant, exp);
+        } else if ((exp + mant) == 64) {
+            value = readRawFloat64(input, mant, exp);
+        }
+        return value;
+    }
+
+    private static double readRawFloat32(BitBuffer input, final int manBits,
+            final int expBits) throws CTFReaderException {
+        long temp = input.get(32, false);
+        return createFloat(temp, manBits - 1, expBits);
+    }
+
+    private static double readRawFloat64(BitBuffer input, final int manBits,
+            final int expBits) throws CTFReaderException {
+        long temp = input.get(64, false);
+        return createFloat(temp, manBits - 1, expBits);
+    }
+
+    /**
+     * Create a float from the raw value, Mathematicians beware.
+     *
+     * @param rawValue
+     *            The raw value( up to 64 bits)
+     * @param manBits
+     *            number of bits in the mantissa
+     * @param expBits
+     *            number of bits in the exponent
+     */
+    private static double createFloat(long rawValue, final int manBits,
+            final int expBits) {
+        long manShift = 1L << (manBits);
+        long manMask = manShift - 1;
+        long expMask = (1L << expBits) - 1;
+
+        int exp = (int) ((rawValue >> (manBits)) & expMask) + 1;
+        long man = (rawValue & manMask);
+        final int offsetExponent = exp - (1 << (expBits - 1));
+        double expPow = Math.pow(2.0, offsetExponent);
+        double ret = man * 1.0f;
+        ret /= manShift;
+        ret += 1.0;
+        ret *= expPow;
+        return ret;
+    }
 }
index 3bbf92f286b3be6edca8c2f434be68b78487b5c9..aa6d6db27de8607bdf127a65090d73138ecd6678 100644 (file)
@@ -11,8 +11,8 @@
 
 package org.eclipse.linuxtools.ctf.core.event.types;
 
-import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
-import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
 
 /**
  * A CTF float definition.
@@ -24,13 +24,12 @@ import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
  * @author Matthew Khouzam
  * @author Simon Marchi
  */
-public class FloatDefinition extends Definition {
+public final class FloatDefinition extends Definition {
     // ------------------------------------------------------------------------
     // Attributes
     // ------------------------------------------------------------------------
 
-    private final FloatDeclaration declaration;
-    private double value;
+    private final double fValue;
 
     // ------------------------------------------------------------------------
     // Constructors
@@ -45,11 +44,14 @@ public class FloatDefinition extends Definition {
      *            the parent scope
      * @param fieldName
      *            the field name
+     * @param value
+     *            field value
+     * @since 3.0
      */
-    public FloatDefinition(FloatDeclaration declaration,
-            IDefinitionScope definitionScope, String fieldName) {
-        super(definitionScope, fieldName);
-        this.declaration = declaration;
+    public FloatDefinition(@NonNull FloatDeclaration declaration,
+            IDefinitionScope definitionScope, @NonNull String fieldName, double value) {
+        super(declaration, definitionScope, fieldName);
+        fValue = value;
     }
 
     // ------------------------------------------------------------------------
@@ -63,80 +65,20 @@ public class FloatDefinition extends Definition {
      * @return the value of the float field fit into a double.
      */
     public double getValue() {
-        return value;
-    }
-
-    /**
-     * Sets the value of the float
-     *
-     * @param val
-     *            the value of the float
-     */
-    public void setValue(double val) {
-        value = val;
+        return fValue;
     }
 
     @Override
     public FloatDeclaration getDeclaration() {
-        return declaration;
+        return (FloatDeclaration) super.getDeclaration();
     }
 
     // ------------------------------------------------------------------------
     // Operations
     // ------------------------------------------------------------------------
 
-    @Override
-    public void read(BitBuffer input) throws CTFReaderException {
-        /* Offset the buffer position wrt the current alignment */
-        alignRead(input, this.declaration);
-        final int exp = declaration.getExponent();
-        final int mant = declaration.getMantissa();
-
-        if ((exp + mant) == 32) {
-            value = readRawFloat32(input, mant, exp);
-        } else if ((exp + mant) == 64) {
-            value = readRawFloat64(input, mant, exp);
-        } else {
-            value = Double.NaN;
-        }
-    }
-
-    private static double readRawFloat64(BitBuffer input, final int manBits,
-            final int expBits) throws CTFReaderException {
-        long temp = input.get(64, false);
-        return createFloat(temp, manBits - 1, expBits);
-    }
-
-    /**
-     * @param rawValue
-     * @param manBits
-     * @param expBits
-     */
-    private static double createFloat(long rawValue, final int manBits,
-            final int expBits) {
-        long manShift = 1L << (manBits);
-        long manMask = manShift - 1;
-        long expMask = (1L << expBits) - 1;
-
-        int exp = (int) ((rawValue >> (manBits)) & expMask) + 1;
-        long man = (rawValue & manMask);
-        final int offsetExponent = exp - (1 << (expBits - 1));
-        double expPow = Math.pow(2.0, offsetExponent);
-        double ret = man * 1.0f;
-        ret /= manShift;
-        ret += 1.0;
-        ret *= expPow;
-        return ret;
-    }
-
-    private static double readRawFloat32(BitBuffer input, final int manBits,
-            final int expBits) throws CTFReaderException {
-        long temp = input.get(32, false);
-        return createFloat(temp, manBits - 1, expBits);
-    }
-
     @Override
     public String toString() {
-        return String.valueOf(value);
+        return String.valueOf(fValue);
     }
 }
index 98925524994a0678c5bb774f7930b4e918d0e013..5e1115cfbdc8348e1f94760afa760bbbc8a2fce4 100644 (file)
 
 package org.eclipse.linuxtools.ctf.core.event.types;
 
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
+import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope;
+import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
+
 /**
  * A CTF data type declaration.
  *
@@ -35,9 +41,26 @@ public interface IDeclaration {
      *            placed
      * @param fieldName
      *            the name of the definition
+     * @param input
+     *            a bitbuffer to read from
      * @return a reference to the definition
+     * @throws CTFReaderException
+     *             error in reading
+     * @since 3.0
      */
-    Definition createDefinition(IDefinitionScope definitionScope, String fieldName);
+    Definition createDefinition(IDefinitionScope definitionScope, @NonNull String fieldName, @NonNull BitBuffer input) throws CTFReaderException;
+
+    /**
+     * Get the path of a definition
+     *
+     * @param definitionScope
+     *            the scope of the definition
+     * @param fieldName
+     *            the name of the definition
+     * @return the path of the definition
+     * @since 3.0
+     */
+    public LexicalScope getPath(IDefinitionScope definitionScope, @NonNull String fieldName);
 
     /**
      * The minimum alignment. if the field is 32 bits, the definition will pad
@@ -46,4 +69,13 @@ public interface IDeclaration {
      * @return the alignment in bits
      */
     long getAlignment();
+
+    /**
+     * The MAXIMUM size of this declaration
+     *
+     * @return the maximum size
+     * @since 3.0
+     */
+    int getMaximumSize();
+
 }
diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IDefinitionScope.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/IDefinitionScope.java
deleted file mode 100644 (file)
index c50603a..0000000
+++ /dev/null
@@ -1,38 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others
- *
- * All rights reserved. This program and the accompanying materials are made
- * available under the terms of the Eclipse Public License v1.0 which
- * accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors: Matthew Khouzam - Initial API and implementation
- * Contributors: Simon Marchi - Initial API and implementation
- *******************************************************************************/
-
-package org.eclipse.linuxtools.ctf.core.event.types;
-
-/**
- * The scope of a CTF definition. Used for compound types.
- *
- * @version 1.0
- * @author Matthew Khouzam
- * @author Simon Marchi
- */
-public interface IDefinitionScope {
-
-    /**
-     * Gets the path in a C style for the scope.
-     * @return the path
-     */
-    String getPath();
-
-    /**
-     * Looks up in this definition scope.
-     *
-     * @param lookupPath
-     *            The path to look up
-     * @return The Definition that was read
-     */
-    Definition lookupDefinition(String lookupPath);
-}
index 5469a7b3cd0cd146bb554430cee18ce19090801f..ceeb2ded2e4e31f198a9b068b070e81fa4d0f8c4 100644 (file)
@@ -17,6 +17,13 @@ package org.eclipse.linuxtools.ctf.core.event.types;
 import java.math.BigInteger;
 import java.nio.ByteOrder;
 
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
+import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
+
 /**
  * A CTF integer declaration.
  *
@@ -26,24 +33,143 @@ import java.nio.ByteOrder;
  * @author Matthew Khouzam
  * @author Simon Marchi
  */
-public class IntegerDeclaration implements IDeclaration {
+@NonNullByDefault
+public class IntegerDeclaration extends Declaration {
+
+    // ------------------------------------------------------------------------
+    // Helpers
+    // ------------------------------------------------------------------------
+
+    /**
+     * unsigned int 32 bits big endian
+     *
+     * @since 3.0
+     */
+    public static final IntegerDeclaration UINT_32B_DECL = new IntegerDeclaration(32, false, ByteOrder.BIG_ENDIAN);
+    /**
+     * unsigned int 32 bits little endian
+     *
+     * @since 3.0
+     */
+    public static final IntegerDeclaration UINT_32L_DECL = new IntegerDeclaration(32, false, ByteOrder.LITTLE_ENDIAN);
+    /**
+     * signed int 32 bits big endian
+     *
+     * @since 3.0
+     */
+    public static final IntegerDeclaration INT_32B_DECL = new IntegerDeclaration(32, true, ByteOrder.BIG_ENDIAN);
+    /**
+     * signed int 32 bits little endian
+     *
+     * @since 3.0
+     */
+    public static final IntegerDeclaration INT_32L_DECL = new IntegerDeclaration(32, true, ByteOrder.LITTLE_ENDIAN);
+    /**
+     * unsigned int 32 bits big endian
+     *
+     * @since 3.0
+     */
+    public static final IntegerDeclaration UINT_64B_DECL = new IntegerDeclaration(64, false, ByteOrder.BIG_ENDIAN);
+    /**
+     * unsigned int 64 bits little endian
+     *
+     * @since 3.0
+     */
+    public static final IntegerDeclaration UINT_64L_DECL = new IntegerDeclaration(64, false, ByteOrder.LITTLE_ENDIAN);
+    /**
+     * signed int 64 bits big endian
+     *
+     * @since 3.0
+     */
+    public static final IntegerDeclaration INT_64B_DECL = new IntegerDeclaration(64, true, ByteOrder.BIG_ENDIAN);
+    /**
+     * signed int 64 bits little endian
+     *
+     * @since 3.0
+     */
+    public static final IntegerDeclaration INT_64L_DECL = new IntegerDeclaration(64, true, ByteOrder.LITTLE_ENDIAN);
+    /**
+     * unsigned 8 bit int endianness doesn't matter since it's 8 bits (byte)
+     *
+     * @since 3.0
+     */
+    public static final IntegerDeclaration UINT_8_DECL = new IntegerDeclaration(8, false, ByteOrder.BIG_ENDIAN);
+    /**
+     * signed 8 bit int endianness doesn't matter since it's 8 bits (char)
+     *
+     * @since 3.0
+     */
+    public static final IntegerDeclaration INT_8_DECL = new IntegerDeclaration(8, true, ByteOrder.BIG_ENDIAN);
 
     // ------------------------------------------------------------------------
     // Attributes
     // ------------------------------------------------------------------------
 
-    private final int length;
-    private final boolean signed;
-    private final int base;
-    private final ByteOrder byteOrder;
-    private final Encoding encoding;
-    private final long alignment;
-    private final String clock;
+    private final int fLength;
+    private final boolean fSigned;
+    private final int fBase;
+    private final ByteOrder fByteOrder;
+    private final Encoding fEncoding;
+    private final long fAlignment;
+    private final String fClock;
 
     // ------------------------------------------------------------------------
     // Constructors
     // ------------------------------------------------------------------------
 
+    /**
+     * Factory, some common types cached
+     *
+     * @param len
+     *            The length in bits
+     * @param signed
+     *            Is the integer signed? false == unsigned
+     * @param base
+     *            The base (10-16 are most common)
+     * @param byteOrder
+     *            Big-endian little-endian or other
+     * @param encoding
+     *            ascii, utf8 or none.
+     * @param clock
+     *            The clock path, can be null
+     * @param alignment
+     *            The minimum alignment. Should be >= 1
+     * @return the integer declaration
+     * @since 3.0
+     */
+    public static IntegerDeclaration createDeclaration(int len, boolean signed, int base,
+            @Nullable ByteOrder byteOrder, Encoding encoding, String clock, long alignment) {
+        if (encoding.equals(Encoding.NONE) && (alignment == 8) && (clock.equals("")) && base == 10) { //$NON-NLS-1$
+            if (len == 8) {
+                return signed ? INT_8_DECL : UINT_8_DECL;
+            }
+            if (len == 32) {
+                if (signed) {
+                    if (byteOrder != null && byteOrder.equals(ByteOrder.BIG_ENDIAN)) {
+                        return INT_32B_DECL;
+                    }
+                    return INT_32L_DECL;
+                }
+                if (byteOrder != null && byteOrder.equals(ByteOrder.BIG_ENDIAN)) {
+                    return UINT_32B_DECL;
+                }
+                return UINT_32L_DECL;
+            } else if (len == 64) {
+                if (signed) {
+                    if (byteOrder != null && byteOrder.equals(ByteOrder.BIG_ENDIAN)) {
+                        return INT_64B_DECL;
+                    }
+                    return INT_64L_DECL;
+                }
+                if (byteOrder != null && byteOrder.equals(ByteOrder.BIG_ENDIAN)) {
+                    return UINT_64B_DECL;
+                }
+                return UINT_64L_DECL;
+            }
+        }
+        return new IntegerDeclaration(len, signed, base, byteOrder, encoding, clock, alignment);
+    }
+
     /**
      * Constructor
      *
@@ -62,18 +188,27 @@ public class IntegerDeclaration implements IDeclaration {
      * @param alignment
      *            The minimum alignment. Should be &ge; 1
      */
-    public IntegerDeclaration(int len, boolean signed, int base,
-            ByteOrder byteOrder, Encoding encoding, String clock, long alignment) {
+    private IntegerDeclaration(int len, boolean signed, int base,
+            @Nullable ByteOrder byteOrder, Encoding encoding, String clock, long alignment) {
         if (len <= 0 || len == 1 && signed) {
             throw new IllegalArgumentException();
         }
-        this.length = len;
-        this.signed = signed;
-        this.base = base;
-        this.byteOrder = byteOrder;
-        this.encoding = encoding;
-        this.clock = clock;
-        this.alignment = Math.max(alignment, 1);
+
+        fLength = len;
+        fSigned = signed;
+        fBase = base;
+
+        @SuppressWarnings("null")
+        @NonNull ByteOrder actualByteOrder = (byteOrder == null ? ByteOrder.nativeOrder() : byteOrder);
+        fByteOrder = actualByteOrder;
+
+        fEncoding = encoding;
+        fClock = clock;
+        fAlignment = Math.max(alignment, 1);
+    }
+
+    private IntegerDeclaration(int len, boolean signed, @Nullable ByteOrder byteOrder) {
+        this(len, signed, 10, byteOrder, Encoding.NONE, "", 8); //$NON-NLS-1$
     }
 
     // ------------------------------------------------------------------------
@@ -86,7 +221,7 @@ public class IntegerDeclaration implements IDeclaration {
      * @return the is the integer signed
      */
     public boolean isSigned() {
-        return signed;
+        return fSigned;
     }
 
     /**
@@ -95,7 +230,7 @@ public class IntegerDeclaration implements IDeclaration {
      * @return the integer base
      */
     public int getBase() {
-        return base;
+        return fBase;
     }
 
     /**
@@ -104,7 +239,7 @@ public class IntegerDeclaration implements IDeclaration {
      * @return the byte order
      */
     public ByteOrder getByteOrder() {
-        return byteOrder;
+        return fByteOrder;
     }
 
     /**
@@ -113,7 +248,7 @@ public class IntegerDeclaration implements IDeclaration {
      * @return the encoding
      */
     public Encoding getEncoding() {
-        return encoding;
+        return fEncoding;
     }
 
     /**
@@ -122,7 +257,7 @@ public class IntegerDeclaration implements IDeclaration {
      * @return is the integer a char
      */
     public boolean isCharacter() {
-        return (length == 8) && (encoding != Encoding.NONE);
+        return (fLength == 8) && (fEncoding != Encoding.NONE);
     }
 
     /**
@@ -131,12 +266,12 @@ public class IntegerDeclaration implements IDeclaration {
      * @return the length of the integer
      */
     public int getLength() {
-        return length;
+        return fLength;
     }
 
     @Override
     public long getAlignment() {
-        return alignment;
+        return fAlignment;
     }
 
     /**
@@ -145,17 +280,29 @@ public class IntegerDeclaration implements IDeclaration {
      * @return the integer's clock, can be null. (most often it is)
      */
     public String getClock() {
-        return clock;
+        return fClock;
+    }
+
+    /**
+     * @since 3.0
+     */
+    @Override
+    public int getMaximumSize() {
+        return fLength;
     }
 
     // ------------------------------------------------------------------------
     // Operations
     // ------------------------------------------------------------------------
 
+    /**
+     * @since 3.0
+     */
     @Override
-    public IntegerDefinition createDefinition(IDefinitionScope definitionScope,
-            String fieldName) {
-        return new IntegerDefinition(this, definitionScope, fieldName);
+    public IntegerDefinition createDefinition(@Nullable IDefinitionScope definitionScope,
+            String fieldName, BitBuffer input) throws CTFReaderException {
+        long value = read(input);
+        return new IntegerDefinition(this, definitionScope, fieldName, value);
     }
 
     @Override
@@ -165,7 +312,7 @@ public class IntegerDeclaration implements IDeclaration {
     }
 
     /**
-     * Get the maximum value for this integer declaration
+     * Get the maximum value for this integer declaration.
      *
      * @return The maximum value for this integer declaration
      * @since 2.0
@@ -175,35 +322,76 @@ public class IntegerDeclaration implements IDeclaration {
          * Compute the number of bits able to represent an unsigned number,
          * ignoring sign bit.
          */
-        int significant_bits = length - (signed ? 1 : 0);
+        int significantBits = fLength - (fSigned ? 1 : 0);
         /*
-         * For a given N significant bits, compute the maximal value which is
-         * (1 << N) - 1.
+         * For a given N significant bits, compute the maximal value which is (1
+         * << N) - 1.
          */
-        return BigInteger.ONE.shiftLeft(significant_bits).subtract(BigInteger.ONE);
+
+        @SuppressWarnings("null")
+        @NonNull BigInteger ret = BigInteger.ONE.shiftLeft(significantBits).subtract(BigInteger.ONE);
+        return ret;
     }
 
     /**
-     * Get the minimum value for this integer declaration
+     * Get the minimum value for this integer declaration.
      *
      * @return The minimum value for this integer declaration
      * @since 2.0
      */
     public BigInteger getMinValue() {
-        if (!signed) {
-            return BigInteger.ZERO;
+        if (!fSigned) {
+            @SuppressWarnings("null")
+            @NonNull BigInteger ret = BigInteger.ZERO;
+            return ret;
         }
 
         /*
          * Compute the number of bits able to represent an unsigned number,
          * without the sign bit.
          */
-        int significant_bits = length - 1;
+        int significantBits = fLength - 1;
+        /*
+         * For a given N significant bits, compute the minimal value which is -
+         * (1 << N).
+         */
+        @SuppressWarnings("null")
+        @NonNull BigInteger ret = BigInteger.ONE.shiftLeft(significantBits).negate();
+        return ret;
+    }
+
+    private long read(BitBuffer input) throws CTFReaderException {
+        /* Offset the buffer position wrt the current alignment */
+        alignRead(input);
+
+        boolean signed = isSigned();
+        int length = getLength();
+        long bits = 0;
+
+        /*
+         * Is the endianness of this field the same as the endianness of the
+         * input buffer? If not, then temporarily set the buffer's endianness to
+         * this field's just to read the data
+         */
+        ByteOrder previousByteOrder = input.getByteOrder();
+        if ((getByteOrder() != input.getByteOrder())) {
+            input.setByteOrder(getByteOrder());
+        }
+
+        if (length > 64) {
+            throw new CTFReaderException("Cannot read an integer with over 64 bits. Length given: " + length); //$NON-NLS-1$
+        }
+
+        bits = input.get(length, signed);
+
         /*
-         * For a given N significant bits, compute the minimal value which is
-         * - (1 << N).
+         * Put the input buffer's endianness back to original if it was changed
          */
-        return BigInteger.ONE.shiftLeft(significant_bits).negate();
+        if (previousByteOrder != input.getByteOrder()) {
+            input.setByteOrder(previousByteOrder);
+        }
+
+        return bits;
     }
 
 }
index 1560ea2a09dd89782f6b1ae96a39328135cd6fc4..15486c7c7ef40abc0164890ca370bfb532dc4ade 100644 (file)
 package org.eclipse.linuxtools.ctf.core.event.types;
 
 import java.math.BigInteger;
-import java.nio.ByteOrder;
 
-import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
-import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
 
 /**
  * A CTF integer definition.
@@ -28,14 +27,13 @@ import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
  * @author Matthew Khouzam
  * @author Simon Marchi
  */
-public class IntegerDefinition extends SimpleDatatypeDefinition {
+public final class IntegerDefinition extends SimpleDatatypeDefinition {
 
     // ------------------------------------------------------------------------
     // Attributes
     // ------------------------------------------------------------------------
 
-    private final IntegerDeclaration declaration;
-    private long value;
+    private final long fValue;
 
     // ------------------------------------------------------------------------
     // Constructors
@@ -50,11 +48,14 @@ public class IntegerDefinition extends SimpleDatatypeDefinition {
      *            the parent scope
      * @param fieldName
      *            the field name
+     * @param value
+     *            integer value
+     * @since 3.0
      */
-    public IntegerDefinition(IntegerDeclaration declaration,
-            IDefinitionScope definitionScope, String fieldName) {
-        super(definitionScope, fieldName);
-        this.declaration = declaration;
+    public IntegerDefinition(@NonNull IntegerDeclaration declaration,
+            IDefinitionScope definitionScope, @NonNull String fieldName, long value) {
+        super(declaration, definitionScope, fieldName);
+        fValue = value;
     }
 
     // ------------------------------------------------------------------------
@@ -67,22 +68,12 @@ public class IntegerDefinition extends SimpleDatatypeDefinition {
      * @return the value of the integer (in long)
      */
     public long getValue() {
-        return value;
-    }
-
-    /**
-     * Sets the value of an integer
-     *
-     * @param val
-     *            the value
-     */
-    public void setValue(long val) {
-        value = val;
+        return fValue;
     }
 
     @Override
     public IntegerDeclaration getDeclaration() {
-        return declaration;
+        return (IntegerDeclaration) super.getDeclaration();
     }
 
     // ------------------------------------------------------------------------
@@ -99,49 +90,13 @@ public class IntegerDefinition extends SimpleDatatypeDefinition {
         return this.toString();
     }
 
-    @Override
-    public void read(BitBuffer input) throws CTFReaderException {
-        /* Offset the buffer position wrt the current alignment */
-        alignRead(input, this.declaration);
-
-        boolean signed = declaration.isSigned();
-        int length = declaration.getLength();
-        long bits = 0;
-
-        /*
-         * Is the endianness of this field the same as the endianness of the
-         * input buffer? If not, then temporarily set the buffer's endianness to
-         * this field's just to read the data
-         */
-        ByteOrder previousByteOrder = input.getByteOrder();
-        if ((this.declaration.getByteOrder() != null) &&
-                (this.declaration.getByteOrder() != input.getByteOrder())) {
-            input.setByteOrder(this.declaration.getByteOrder());
-        }
-
-        if (length > 64) {
-            throw new CTFReaderException("Cannot read an integer with over 64 bits. Length given: " + length); //$NON-NLS-1$
-        }
-
-        bits = input.get(length, signed);
-
-        /*
-         * Put the input buffer's endianness back to original if it was changed
-         */
-        if (previousByteOrder != input.getByteOrder()) {
-            input.setByteOrder(previousByteOrder);
-        }
-
-        value = bits;
-    }
-
     @Override
     public String toString() {
-        if (declaration.isCharacter()) {
-            char c = (char) value;
+        if (getDeclaration().isCharacter()) {
+            char c = (char) fValue;
             return Character.toString(c);
         }
-        return formatNumber(value, declaration.getBase(), declaration.isSigned());
+        return formatNumber(fValue, getDeclaration().getBase(), getDeclaration().isSigned());
     }
 
     /**
index 66957087b47fce7f43f5914426a6e43569d86f47..811321df5baa7007d0da249a7fed8dafdb988312 100644 (file)
 
 package org.eclipse.linuxtools.ctf.core.event.types;
 
+import java.util.Collection;
+import java.util.List;
+
+import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
 
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableList.Builder;
+import com.google.common.collect.Multimap;
+
 /**
  * A CTF sequence declaration.
  *
@@ -24,14 +34,15 @@ import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
  * @author Matthew Khouzam
  * @author Simon Marchi
  */
-public class SequenceDeclaration implements IDeclaration {
+public class SequenceDeclaration extends Declaration {
 
     // ------------------------------------------------------------------------
     // Attributes
     // ------------------------------------------------------------------------
 
-    private final IDeclaration elemType;
-    private final String lengthName;
+    private final IDeclaration fElemType;
+    private final String fLengthName;
+    private final Multimap<String, String> fPaths = ArrayListMultimap.<String, String>create();
 
     // ------------------------------------------------------------------------
     // Constructors
@@ -46,8 +57,8 @@ public class SequenceDeclaration implements IDeclaration {
      *            The element type
      */
     public SequenceDeclaration(String lengthName, IDeclaration elemType) {
-        this.elemType = elemType;
-        this.lengthName = lengthName;
+        fElemType = elemType;
+        fLengthName = lengthName;
     }
 
     // ------------------------------------------------------------------------
@@ -56,18 +67,20 @@ public class SequenceDeclaration implements IDeclaration {
 
     /**
      * Gets the element type
+     *
      * @return the element type
      */
     public IDeclaration getElementType() {
-        return elemType;
+        return fElemType;
     }
 
     /**
      * Gets the name of the length field
+     *
      * @return the name of the length field
      */
     public String getLengthName() {
-        return lengthName;
+        return fLengthName;
     }
 
     @Override
@@ -75,22 +88,70 @@ public class SequenceDeclaration implements IDeclaration {
         return getElementType().getAlignment();
     }
 
+
     // ------------------------------------------------------------------------
     // Operations
     // ------------------------------------------------------------------------
 
+    /**
+     * Is the Sequence a string?
+     * @return true, if the elements are chars, false otherwise
+     * @since 3.0
+     */
+    public boolean isString(){
+        IntegerDeclaration elemInt;
+        IDeclaration elementType = getElementType();
+        if (elementType instanceof IntegerDeclaration) {
+            elemInt = (IntegerDeclaration) elementType;
+            if (elemInt.isCharacter()) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * @since 3.0
+     */
+    @SuppressWarnings("null") // immutablelist
     @Override
     public SequenceDefinition createDefinition(
-            IDefinitionScope definitionScope, String fieldName) {
-        SequenceDefinition ret = null;
-        try {
-            ret = new SequenceDefinition(this, definitionScope, fieldName);
-        } catch (CTFReaderException e) {
-            // Temporarily catch this here, eventually this should be thrown
-            // up the call stack
-            e.printStackTrace();
+            IDefinitionScope definitionScope, String fieldName, BitBuffer input) throws CTFReaderException {
+        Definition lenDef = null;
+
+        if (definitionScope != null) {
+            lenDef = definitionScope.lookupDefinition(getLengthName());
+        }
+
+        if (lenDef == null) {
+            throw new CTFReaderException("Sequence length field not found"); //$NON-NLS-1$
         }
-        return ret;
+
+        if (!(lenDef instanceof IntegerDefinition)) {
+            throw new CTFReaderException("Sequence length field not integer"); //$NON-NLS-1$
+        }
+
+        IntegerDefinition lengthDefinition = (IntegerDefinition) lenDef;
+
+        if (lengthDefinition.getDeclaration().isSigned()) {
+            throw new CTFReaderException("Sequence length must not be signed"); //$NON-NLS-1$
+        }
+
+        long length = lengthDefinition.getValue();
+        if ((length > Integer.MAX_VALUE) || (!input.canRead((int) length * fElemType.getMaximumSize()))) {
+            throw new CTFReaderException("Sequence length too long " + length); //$NON-NLS-1$
+        }
+
+        Collection<String> collection = fPaths.get(fieldName);
+        while (collection.size() < length) {
+            fPaths.put(fieldName, fieldName + '[' + collection.size() + ']');
+        }
+        List<String> paths = (List<String>) fPaths.get(fieldName);
+        Builder<Definition> definitions = new ImmutableList.Builder<>();
+        for (int i = 0; i < length; i++) {
+            definitions.add(fElemType.createDefinition(definitionScope, paths.get(i), input));
+        }
+        return new SequenceDefinition(this, definitionScope, fieldName, definitions.build());
     }
 
     @Override
@@ -99,4 +160,12 @@ public class SequenceDeclaration implements IDeclaration {
         return "[declaration] sequence[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$
     }
 
+    /**
+     * @since 3.0
+     */
+    @Override
+    public int getMaximumSize() {
+        return Integer.MAX_VALUE;
+    }
+
 }
index c6b004377d0a356eb150f37edc13816edac09aa1..44e2fdf4c1feefc379a9a7b31a309fa3a5be4f08 100644 (file)
 
 package org.eclipse.linuxtools.ctf.core.event.types;
 
-import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
-import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
+import java.util.List;
+
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
+
+import com.google.common.base.Joiner;
+import com.google.common.collect.ImmutableList;
 
 /**
  * A CTF sequence definition (a fixed-size array).
@@ -25,16 +30,15 @@ import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
  * @author Matthew Khouzam
  * @author Simon Marchi
  */
-public class SequenceDefinition extends Definition {
+public final class SequenceDefinition extends Definition {
+
+    // TODO: investigate merging with arraydefinition
 
     // ------------------------------------------------------------------------
     // Attributes
     // ------------------------------------------------------------------------
 
-    private final SequenceDeclaration declaration;
-    private final IntegerDefinition lengthDefinition;
-    private Definition definitions[];
-    private int currentLength;
+    private final ImmutableList<Definition> fDefinitions;
 
     // ------------------------------------------------------------------------
     // Constructors
@@ -49,35 +53,13 @@ public class SequenceDefinition extends Definition {
      *            the parent scope
      * @param fieldName
      *            the field name
-     * @throws CTFReaderException
-     *             If the sequence field was malformatted
+     * @param definitions
+     *            Definitions
+     * @since 3.0
      */
-    public SequenceDefinition(SequenceDeclaration declaration,
-            IDefinitionScope definitionScope, String fieldName)
-            throws CTFReaderException {
-        super(definitionScope, fieldName);
-        Definition lenDef = null;
-
-        this.declaration = declaration;
-
-        if (definitionScope != null) {
-            lenDef = definitionScope.lookupDefinition(declaration
-                    .getLengthName());
-        }
-
-        if (lenDef == null) {
-            throw new CTFReaderException("Sequence length field not found"); //$NON-NLS-1$
-        }
-
-        if (!(lenDef instanceof IntegerDefinition)) {
-            throw new CTFReaderException("Sequence length field not integer"); //$NON-NLS-1$
-        }
-
-        lengthDefinition = (IntegerDefinition) lenDef;
-
-        if (this.lengthDefinition.getDeclaration().isSigned()) {
-            throw new CTFReaderException("Sequence length must not be signed"); //$NON-NLS-1$
-        }
+    public SequenceDefinition(@NonNull SequenceDeclaration declaration, IDefinitionScope definitionScope, @NonNull String fieldName, List<Definition> definitions) {
+        super(declaration, definitionScope, fieldName);
+        fDefinitions = ImmutableList.copyOf(definitions);
     }
 
     // ------------------------------------------------------------------------
@@ -86,7 +68,7 @@ public class SequenceDefinition extends Definition {
 
     @Override
     public SequenceDeclaration getDeclaration() {
-        return declaration;
+        return (SequenceDeclaration) super.getDeclaration();
     }
 
     /**
@@ -96,7 +78,7 @@ public class SequenceDefinition extends Definition {
      * @return the length of the sequence
      */
     public int getLength() {
-        return currentLength;
+        return fDefinitions.size();
     }
 
     /**
@@ -108,67 +90,23 @@ public class SequenceDefinition extends Definition {
      *         throws an out of bounds exception
      */
     public Definition getElem(int i) {
-        if (i > definitions.length) {
+        if (i > fDefinitions.size()) {
             return null;
         }
-
-        return definitions[i];
-    }
-
-    /**
-     * Is the sequence a null terminated string?
-     * @return true == is a string, false == is not a string
-     */
-    public boolean isString() {
-        IntegerDeclaration elemInt;
-
-        if (declaration.getElementType() instanceof IntegerDeclaration) {
-            elemInt = (IntegerDeclaration) declaration.getElementType();
-            if (elemInt.isCharacter()) {
-                return true;
-            }
-        }
-        return false;
+        return fDefinitions.get(i);
     }
 
     // ------------------------------------------------------------------------
     // Operations
     // ------------------------------------------------------------------------
 
-    @Override
-    public void read(BitBuffer input) throws CTFReaderException {
-        currentLength = (int) lengthDefinition.getValue();
-
-        if ((definitions == null) || (definitions.length < currentLength)) {
-            Definition newDefinitions[] = new Definition[currentLength];
-
-            int i = 0;
-
-            if (definitions != null) {
-                System.arraycopy(definitions, 0, newDefinitions, 0, definitions.length);
-            }
-
-            for (; i < currentLength; i++) {
-                newDefinitions[i] = declaration.getElementType()
-                        .createDefinition(getDefinitionScope(),
-                                getFieldName() + "[" + i + "]"); //$NON-NLS-1$ //$NON-NLS-2$
-            }
-
-            definitions = newDefinitions;
-        }
-
-        for (int i = 0; i < currentLength; i++) {
-            definitions[i].read(input);
-        }
-    }
-
     @Override
     public String toString() {
         StringBuilder b = new StringBuilder();
 
-        if (this.isString()) {
-            for (int i = 0; i < currentLength; i++) {
-                IntegerDefinition character = (IntegerDefinition) definitions[i];
+        if (getDeclaration().isString()) {
+            for (Definition def : fDefinitions) {
+                IntegerDefinition character = (IntegerDefinition) def;
 
                 if (character.getValue() == 0) {
                     break;
@@ -178,17 +116,9 @@ public class SequenceDefinition extends Definition {
             }
         } else {
             b.append('[');
-            if (currentLength > 0) {
-                for (int i = 0; i < (currentLength - 1); i++) {
-                    b.append(' ');
-                    b.append(definitions[i].toString());
-                    b.append(',');
-                }
-                b.append(' ');
-                b.append(definitions[currentLength - 1].toString());
-            }
-            b.append(" ]"); //$NON-NLS-1$
-
+            Joiner joiner = Joiner.on(", ").skipNulls(); //$NON-NLS-1$
+            b.append(joiner.join(fDefinitions));
+            b.append(']');
         }
 
         return b.toString();
index f4ed1e0695ad1a19fb4ddf24be4c2366140d31d4..492d19f20cd8e822a4a66c8fa90814940caf1deb 100644 (file)
@@ -11,6 +11,9 @@
 
 package org.eclipse.linuxtools.ctf.core.event.types;
 
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
+
 /**
  * Simple Datatype definition is a datatype that allows the addition of
  * getIntegerValue and getStringValue to a class.
@@ -23,15 +26,18 @@ public abstract class SimpleDatatypeDefinition extends Definition {
     /**
      * Create a new SimpleDatatypeDefinition
      *
+     * @param declaration
+     *            definition's declaration
      * @param definitionScope
      *            The scope of this definition
      * @param fieldName
      *            The name of the field matching this definition in the parent
      *            scope
+     * @since 3.0
      */
-    public SimpleDatatypeDefinition(IDefinitionScope definitionScope,
-            String fieldName) {
-        super(definitionScope, fieldName);
+    public SimpleDatatypeDefinition(@NonNull IDeclaration declaration, IDefinitionScope definitionScope,
+            @NonNull String fieldName) {
+        super(declaration, definitionScope, fieldName);
     }
 
     /**
index 972d2829a694895be865ea4d308b01690234151f..710f75aa4e1e8b3f3a1f4ba4bd6d495ad09552ef 100644 (file)
 
 package org.eclipse.linuxtools.ctf.core.event.types;
 
+import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
+import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
+
 /**
  * A CTF string declaration.
  *
@@ -23,13 +27,13 @@ package org.eclipse.linuxtools.ctf.core.event.types;
  * @author Matthew Khouzam
  * @author Simon Marchi
  */
-public class StringDeclaration implements IDeclaration {
+public class StringDeclaration extends Declaration {
 
     // ------------------------------------------------------------------------
     // Attributes
     // ------------------------------------------------------------------------
 
-    private Encoding encoding = Encoding.UTF8;
+    private final Encoding fEncoding;
 
     // ------------------------------------------------------------------------
     // Constructors
@@ -39,6 +43,7 @@ public class StringDeclaration implements IDeclaration {
      * Generate a UTF8 string declaration
      */
     public StringDeclaration() {
+        fEncoding = Encoding.UTF8;
     }
 
     /**
@@ -46,7 +51,7 @@ public class StringDeclaration implements IDeclaration {
      * @param encoding the encoding, utf8 or ascii
      */
     public StringDeclaration(Encoding encoding) {
-        this.encoding = encoding;
+        fEncoding = encoding;
     }
 
     // ------------------------------------------------------------------------
@@ -58,15 +63,7 @@ public class StringDeclaration implements IDeclaration {
      * @return the character encoding.
      */
     public Encoding getEncoding() {
-        return encoding;
-    }
-
-    /**
-     *
-     * @param encoding the character encoding to set
-     */
-    public void setEncoding(Encoding encoding) {
-        this.encoding = encoding;
+        return fEncoding;
     }
 
     @Override
@@ -74,16 +71,40 @@ public class StringDeclaration implements IDeclaration {
         // See ctf 4.2.5: Strings are always aligned on byte size.
         return 8;
     }
+
+    /**
+     * @since 3.0
+     */
+    @Override
+    public int getMaximumSize() {
+        return Integer.MAX_VALUE;
+    }
     // ------------------------------------------------------------------------
     // Operations
     // ------------------------------------------------------------------------
 
+    /**
+     * @since 3.0
+     */
     @Override
     public StringDefinition createDefinition(IDefinitionScope definitionScope,
-            String fieldName) {
-        return new StringDefinition(this, definitionScope, fieldName);
+            String fieldName, BitBuffer input) throws CTFReaderException {
+        String value = read(input);
+        return new StringDefinition(this, definitionScope, fieldName, value);
     }
 
+    private String read(BitBuffer input) throws CTFReaderException {
+        /* Offset the buffer position wrt the current alignment */
+        alignRead(input);
+
+        StringBuilder sb = new StringBuilder();
+        char c = (char) input.get(8, false);
+        while (c != 0) {
+            sb.append(c);
+            c = (char) input.get(8, false);
+        }
+        return sb.toString();
+    }
     @Override
     public String toString() {
         /* Only used for debugging */
index 258e88c32d61925cb4d22983224159a4aab3ffcc..853605248cef2d94bcec81441cfb9cde5f184cbd 100644 (file)
@@ -12,8 +12,8 @@
 
 package org.eclipse.linuxtools.ctf.core.event.types;
 
-import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
-import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
 
 /**
  * A CTF string definition (similar to a C null-terminated byte array).
@@ -26,15 +26,13 @@ import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
  * @author Matthew Khouzam
  * @author Simon Marchi
  */
-public class StringDefinition extends Definition {
+public final class StringDefinition extends Definition {
 
     // ------------------------------------------------------------------------
     // Attributes
     // ------------------------------------------------------------------------
 
-    private StringDeclaration fDeclaration;
-
-    private String fString;
+    private final String fString;
 
     // ------------------------------------------------------------------------
     // Constructors
@@ -49,14 +47,14 @@ public class StringDefinition extends Definition {
      *            the parent scope
      * @param fieldName
      *            the field name
+     * @param value
+     *            The String value
+     * @since 3.0
      */
-    public StringDefinition(StringDeclaration declaration,
-            IDefinitionScope definitionScope, String fieldName) {
-        super(definitionScope, fieldName);
-
-        fDeclaration = declaration;
-
-        fString = ""; //$NON-NLS-1$
+    public StringDefinition(@NonNull StringDeclaration declaration,
+            IDefinitionScope definitionScope, @NonNull String fieldName, String value) {
+        super(declaration, definitionScope, fieldName);
+        fString = value;
     }
 
     // ------------------------------------------------------------------------
@@ -65,17 +63,7 @@ public class StringDefinition extends Definition {
 
     @Override
     public StringDeclaration getDeclaration() {
-        return fDeclaration;
-    }
-
-    /**
-     * Sets the string declaration
-     *
-     * @param declaration
-     *            the declaration
-     */
-    public void setDeclaration(StringDeclaration declaration) {
-        fDeclaration = declaration;
+        return (StringDeclaration) super.getDeclaration();
     }
 
     /**
@@ -87,34 +75,10 @@ public class StringDefinition extends Definition {
         return fString;
     }
 
-    /**
-     * Sets the string (value)
-     *
-     * @param str the string
-     * @since 3.0
-     */
-    public void setValue(String str) {
-        fString = str;
-    }
-
     // ------------------------------------------------------------------------
     // Operations
     // ------------------------------------------------------------------------
 
-    @Override
-    public void read(BitBuffer input) throws CTFReaderException {
-        /* Offset the buffer position wrt the current alignment */
-        alignRead(input, fDeclaration);
-
-        StringBuilder sb = new StringBuilder();
-        char c = (char) input.get(8, false);
-        while (c != 0) {
-            sb.append(c);
-            c = (char) input.get(8, false);
-        }
-        fString = sb.toString();
-    }
-
     @Override
     public String toString() {
         return '\"' + getValue() + '\"';
index ec235cbdc8d70e6dbc4dfae1933e22a2b6c5eacd..03a682936a6959984034b63d5f3ba8377caf0b6a 100644 (file)
 
 package org.eclipse.linuxtools.ctf.core.event.types;
 
-import java.util.HashMap;
-import java.util.LinkedList;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
+import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
+
+import com.google.common.collect.ImmutableList;
+
 /**
  * A CTF structure declaration.
  *
@@ -28,15 +34,23 @@ import java.util.Map;
  * @author Matthew Khouzam
  * @author Simon Marchi
  */
-public class StructDeclaration implements IDeclaration {
+public class StructDeclaration extends Declaration {
 
     // ------------------------------------------------------------------------
     // Attributes
     // ------------------------------------------------------------------------
 
-    private final Map<String, IDeclaration> fields = new HashMap<>();
-    private final List<String> fieldsList = new LinkedList<>();
-    private long maxAlign;
+    /** linked list of field names. So fieldName->fieldValue */
+    private final Map<String, IDeclaration> fFieldMap = new LinkedHashMap<>();
+
+    /** List of strings for acceleration */
+    @NonNull
+    private ImmutableList<String> fFieldNames;
+    /** array declaration for acceleration */
+    private List<IDeclaration> fFieldDeclarations;
+
+    /** maximum bit alignment */
+    private long fMaxAlign;
 
     // ------------------------------------------------------------------------
     // Constructors
@@ -50,8 +64,30 @@ public class StructDeclaration implements IDeclaration {
      *            aligned and has a 32 bit aligned field, the struct becomes 32
      *            bit aligned.
      */
+    @SuppressWarnings("null")
+    // ImmutableList.of()
     public StructDeclaration(long align) {
-        this.maxAlign = Math.max(align, 1);
+        fMaxAlign = Math.max(align, 1);
+        fFieldNames = ImmutableList.of();
+    }
+
+    /**
+     * Struct declaration constructor
+     *
+     * @param names
+     *            the names of all the fields
+     * @param declarations
+     *            all the fields
+     * @since 3.0
+     */
+    @SuppressWarnings("null")
+    // ImmutableList.of()
+    public StructDeclaration(String[] names, Declaration[] declarations) {
+        fMaxAlign = 1;
+        fFieldNames = ImmutableList.of();
+        for (int i = 0; i < names.length; i++) {
+            addField(names[i], declarations[i]);
+        }
     }
 
     // ------------------------------------------------------------------------
@@ -60,62 +96,100 @@ public class StructDeclaration implements IDeclaration {
 
     /**
      * Get current alignment
+     *
      * @return the alignment of the struct and all its fields
      */
     public long getMaxAlign() {
-        return maxAlign;
+        return fMaxAlign;
     }
 
     /**
      * Query if the struct has a given field
-     * @param name the name of the field, scopeless please
+     *
+     * @param name
+     *            the name of the field, scopeless please
      * @return does the field exist?
      */
     public boolean hasField(String name) {
-        return this.fields.containsKey(name);
+        return fFieldMap.containsKey(name);
     }
 
     /**
      * get the fields of the struct in a map. Faster access time than a list.
+     *
      * @return a HashMap of the fields (key is the name)
      * @since 2.0
      */
     public Map<String, IDeclaration> getFields() {
-        return this.fields;
+        return fFieldMap;
     }
 
     /**
-     * Gets the field list. Very important since the map of fields does not retain the order of the fields.
+     * Gets the field list. Very important since the map of fields does not
+     * retain the order of the fields.
+     *
      * @return the field list.
+     * @since 3.0
      */
-    public List<String> getFieldsList() {
-        return this.fieldsList;
+    public Iterable<String> getFieldsList() {
+        return fFieldMap.keySet();
     }
 
     @Override
     public long getAlignment() {
-        return this.maxAlign;
+        return this.fMaxAlign;
+    }
+
+    /**
+     * @since 3.0
+     */
+    @Override
+    public int getMaximumSize() {
+        int maxSize = 0;
+        if (fFieldDeclarations != null) {
+            for (IDeclaration field : fFieldDeclarations) {
+                maxSize += field.getMaximumSize();
+            }
+        }
+        return Math.min(maxSize, Integer.MAX_VALUE);
     }
 
     // ------------------------------------------------------------------------
     // Operations
     // ------------------------------------------------------------------------
 
+    /**
+     * @since 3.0
+     */
+    @SuppressWarnings("null")
+    // immutablelist
     @Override
     public StructDefinition createDefinition(IDefinitionScope definitionScope,
-            String fieldName) {
-        return new StructDefinition(this, definitionScope, fieldName);
+            String fieldName, BitBuffer input) throws CTFReaderException {
+        alignRead(input);
+        final Definition[] myFields = new Definition[fFieldNames.size()];
+        StructDefinition structDefinition = new StructDefinition(this, definitionScope, fieldName, fFieldNames, myFields);
+        for (int i = 0; i < fFieldNames.size(); i++) {
+            myFields[i] = fFieldDeclarations.get(i).createDefinition(structDefinition, fFieldNames.get(i), input);
+        }
+        return structDefinition;
     }
 
     /**
      * Add a field to the struct
-     * @param name the name of the field, scopeless
-     * @param declaration the declaration of the field
+     *
+     * @param name
+     *            the name of the field, scopeless
+     * @param declaration
+     *            the declaration of the field
      */
+    @SuppressWarnings("null")
+    // Immutable list copyof cannot return null
     public void addField(String name, IDeclaration declaration) {
-        this.fields.put(name, declaration);
-        this.fieldsList.add(name);
-        maxAlign = Math.max(maxAlign, declaration.getAlignment());
+        fFieldMap.put(name, declaration);
+        fMaxAlign = Math.max(fMaxAlign, declaration.getAlignment());
+        fFieldNames = ImmutableList.copyOf(fFieldMap.keySet());
+        fFieldDeclarations = ImmutableList.<IDeclaration>copyOf(fFieldMap.values());
     }
 
     @Override
@@ -128,8 +202,8 @@ public class StructDeclaration implements IDeclaration {
     public int hashCode() {
         final int prime = 31;
         int result = 1;
-        result = (prime * result) + fieldsList.hashCode();
-        result = (prime * result) + (int) (maxAlign ^ (maxAlign >>> 32));
+        result = (prime * result) + fFieldMap.entrySet().hashCode();
+        result = (prime * result) + (int) (fMaxAlign ^ (fMaxAlign >>> 32));
         return result;
     }
 
@@ -145,10 +219,10 @@ public class StructDeclaration implements IDeclaration {
             return false;
         }
         StructDeclaration other = (StructDeclaration) obj;
-        if (!fieldsList.equals(other.fieldsList)) {
+        if (!fFieldMap.entrySet().equals(other.fFieldMap.entrySet())) {
             return false;
         }
-        if (maxAlign != other.maxAlign) {
+        if (fMaxAlign != other.fMaxAlign) {
             return false;
         }
         return true;
index 1dc01f09abbd273c6ee25a5f30fd99a85185389e..258ad8f2f20809a988108143296cc8ca11f1b5ea 100644 (file)
 
 package org.eclipse.linuxtools.ctf.core.event.types;
 
-import java.util.LinkedHashMap;
+import java.util.Collections;
+import java.util.LinkedList;
 import java.util.List;
-import java.util.ListIterator;
 import java.util.Map;
 
-import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
-import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
+
+import com.google.common.base.Joiner;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableMap.Builder;
 
 /**
  * A CTF structure definition (similar to a C structure).
@@ -31,14 +36,15 @@ import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
  * @author Matthew Khouzam
  * @author Simon Marchi
  */
-public class StructDefinition extends Definition implements IDefinitionScope {
+public final class StructDefinition extends Definition implements IDefinitionScope {
 
     // ------------------------------------------------------------------------
     // Attributes
     // ------------------------------------------------------------------------
 
-    private final StructDeclaration declaration;
-    private final Map<String, Definition> definitions = new LinkedHashMap<>();
+    private final ImmutableList<String> fFieldNames;
+    private final Definition[] fDefinitions;
+    private Map<String, Definition> fDefinitionsMap = null;
 
     // ------------------------------------------------------------------------
     // Constructors
@@ -53,19 +59,19 @@ public class StructDefinition extends Definition implements IDefinitionScope {
      *            the parent scope
      * @param structFieldName
      *            the field name
+     * @param fieldNames
+     *            the list of fields
+     * @param definitions
+     *            the definitions
+     * @since 3.0
      */
-    public StructDefinition(StructDeclaration declaration,
-            IDefinitionScope definitionScope, String structFieldName) {
-        super(definitionScope, structFieldName);
-
-        this.declaration = declaration;
-
-        for (String fName : declaration.getFieldsList()) {
-            IDeclaration fieldDecl = declaration.getFields().get(fName);
-            assert (fieldDecl != null);
-
-            Definition def = fieldDecl.createDefinition(this, fName);
-            definitions.put(fName, def);
+    public StructDefinition(@NonNull StructDeclaration declaration,
+            IDefinitionScope definitionScope, @NonNull String structFieldName, List<String> fieldNames, Definition[] definitions) {
+        super(declaration, definitionScope, structFieldName);
+        fFieldNames = ImmutableList.copyOf(fieldNames);
+        fDefinitions = definitions;
+        if (fFieldNames == null) {
+            fDefinitionsMap = Collections.EMPTY_MAP;
         }
     }
 
@@ -74,33 +80,49 @@ public class StructDefinition extends Definition implements IDefinitionScope {
     // ------------------------------------------------------------------------
 
     /**
+     * Gets the definition of the field
+     *
+     * @param fieldName
+     *            the fieldname
      * @return The definitions of all the fields
-     * @since 2.0
+     * @since 3.0
+     */
+    public Definition getDefinition(String fieldName) {
+        if (fDefinitionsMap == null) {
+            buildFieldsMap();
+        }
+        return fDefinitionsMap.get(fieldName);
+    }
+
+    private void buildFieldsMap() {
+        Builder<String, Definition> mapBuilder = new ImmutableMap.Builder<>();
+        for (int i = 0; i < fFieldNames.size(); i++) {
+            if (fDefinitions[i] != null) {
+                mapBuilder.put(fFieldNames.get(i), fDefinitions[i]);
+            }
+        }
+        fDefinitionsMap = mapBuilder.build();
+    }
+
+    /**
+     * Gets an array of the field names
+     *
+     * @return the field names array
+     * @since 3.0
      */
-    public Map<String, Definition> getDefinitions() {
-        return definitions;
+    public List<String> getFieldNames() {
+        return fFieldNames;
     }
 
     @Override
     public StructDeclaration getDeclaration() {
-        return declaration;
+        return (StructDeclaration) super.getDeclaration();
     }
 
     // ------------------------------------------------------------------------
     // Operations
     // ------------------------------------------------------------------------
 
-    @Override
-    public void read(BitBuffer input) throws CTFReaderException {
-        alignRead(input, this.declaration);
-        final List<String> fieldList = declaration.getFieldsList();
-        for (String fName : fieldList) {
-            Definition def = definitions.get(fName);
-            assert (def != null);
-            def.read(input);
-        }
-    }
-
     @Override
     public Definition lookupDefinition(String lookupPath) {
         /*
@@ -108,11 +130,16 @@ public class StructDefinition extends Definition implements IDefinitionScope {
          * sequence refers to a field that is after it, the field's definition
          * will not be there yet in the hashmap.
          */
-        Definition retVal = definitions.get(lookupPath);
-        if (retVal == null) {
-            retVal = definitions.get("_" + lookupPath); //$NON-NLS-1$
+        int val = fFieldNames.indexOf(lookupPath);
+        if (val != -1) {
+            return fDefinitions[val];
         }
-        return retVal;
+        String lookupUnderscored = "_" + lookupPath; //$NON-NLS-1$
+        val = fFieldNames.indexOf(lookupUnderscored);
+        if (val != -1) {
+            return fDefinitions[val];
+        }
+        return null;
     }
 
     /**
@@ -170,8 +197,8 @@ public class StructDefinition extends Definition implements IDefinitionScope {
     }
 
     /**
-     * Lookup a string in a struct. If the name returns a non-string (like
-     * an int) than the method returns null
+     * Lookup a string in a struct. If the name returns a non-string (like an
+     * int) than the method returns null
      *
      * @param name
      *            the name of the string
@@ -184,8 +211,8 @@ public class StructDefinition extends Definition implements IDefinitionScope {
     }
 
     /**
-     * Lookup a struct in a struct. If the name returns a non-struct (like
-     * an int) than the method returns null
+     * Lookup a struct in a struct. If the name returns a non-struct (like an
+     * int) than the method returns null
      *
      * @param name
      *            the name of the struct
@@ -198,8 +225,8 @@ public class StructDefinition extends Definition implements IDefinitionScope {
     }
 
     /**
-     * Lookup a variant in a struct. If the name returns a non-variant (like
-     * an int) than the method returns null
+     * Lookup a variant in a struct. If the name returns a non-variant (like an
+     * int) than the method returns null
      *
      * @param name
      *            the name of the variant
@@ -217,23 +244,19 @@ public class StructDefinition extends Definition implements IDefinitionScope {
 
         builder.append("{ "); //$NON-NLS-1$
 
-        ListIterator<String> listIterator = this.declaration.getFieldsList()
-                .listIterator();
-
-        while (listIterator.hasNext()) {
-            String field = listIterator.next();
-
-            builder.append(field);
-            builder.append(" = "); //$NON-NLS-1$
-            builder.append(lookupDefinition(field).toString());
-
-            if (listIterator.hasNext()) {
-                builder.append(", "); //$NON-NLS-1$
+        if (fFieldNames != null) {
+            List<String> fields = new LinkedList<>();
+            for (String field : fFieldNames) {
+                String appendee = field + " = " + lookupDefinition(field).toString(); //$NON-NLS-1$
+                fields.add(appendee);
             }
+            Joiner joiner = Joiner.on(", ").skipNulls(); //$NON-NLS-1$
+            builder.append(joiner.join(fields));
         }
 
         builder.append(" }"); //$NON-NLS-1$
 
         return builder.toString();
     }
+
 }
index 4169943b87d8947fd3065b4d505d979f6b92cecc..b32572ca92f35a45bd76157b317ec74014873862 100644 (file)
 
 package org.eclipse.linuxtools.ctf.core.event.types;
 
+import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
+import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
+import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
+
 /**
  * A CTFC variant declaration.
  *
@@ -26,15 +32,18 @@ import java.util.Map;
  * @author Matthew Khouzam
  * @author Simon Marchi
  */
-public class VariantDeclaration implements IDeclaration {
+public class VariantDeclaration extends Declaration {
 
     // ------------------------------------------------------------------------
     // Attributes
     // ------------------------------------------------------------------------
 
-    private String tag = null;
-    private static final long alignment = 1;
-    private final Map<String, IDeclaration> fields = new HashMap<>();
+    private String fTag = null;
+    private static final long ALIGNMENT = 1;
+    private final Map<String, IDeclaration> fFields = Collections.synchronizedMap(new HashMap<String, IDeclaration>());
+    private EnumDefinition fTagDef;
+    private IDeclaration fDeclarationToPopulate;
+    private IDefinitionScope fPrevDefinitionScope;
 
     // ------------------------------------------------------------------------
     // Constructors
@@ -54,58 +63,88 @@ public class VariantDeclaration implements IDeclaration {
      * @return Does the variant have a tag
      */
     public boolean isTagged() {
-        return tag != null;
+        return fTag != null;
     }
 
     /**
      * Lookup if a field exists in the variant
-     * @param fieldTag the field tag name
+     *
+     * @param fieldTag
+     *            the field tag name
      * @return true = field tag exists
      */
     public boolean hasField(String fieldTag) {
-        return fields.containsKey(fieldTag);
+        return fFields.containsKey(fieldTag);
     }
 
     /**
      * Sets the tag in a variant
-     * @param tag the tag
+     *
+     * @param tag
+     *            the tag
      */
     public void setTag(String tag) {
-        this.tag = tag;
+        fTag = tag;
+        fTagDef = null;
     }
 
     /**
      * Gets current variant tag
+     *
      * @return the variant tag.
      */
     public String getTag() {
-        return this.tag;
+        return fTag;
     }
 
     /**
      * Gets the fields of the variant
+     *
      * @return the fields of the variant
      * @since 2.0
      */
     public Map<String, IDeclaration> getFields() {
-        return this.fields;
+        return this.fFields;
     }
 
     @Override
     public long getAlignment() {
-        return alignment;
+        return ALIGNMENT;
     }
+
     // ------------------------------------------------------------------------
     // Operations
     // ------------------------------------------------------------------------
 
+    /**
+     * @since 3.0
+     */
     @Override
     public VariantDefinition createDefinition(IDefinitionScope definitionScope,
-            String fieldName) {
-        return new VariantDefinition(this, definitionScope, fieldName);
+            String fieldName, BitBuffer input) throws CTFReaderException {
+        alignRead(input);
+        if (fPrevDefinitionScope != definitionScope) {
+            fTagDef = null;
+            fPrevDefinitionScope = definitionScope;
+        }
+        EnumDefinition tagDef = fTagDef;
+        if (tagDef == null) {
+            Definition def = definitionScope.lookupDefinition(fTag);
+            tagDef = (EnumDefinition) ((def instanceof EnumDefinition) ? def : null);
+        }
+        if (tagDef == null) {
+            throw new CTFReaderException("Tag is not defined " + fTag); //$NON-NLS-1$
+        }
+        String varFieldName = tagDef.getStringValue();
+        fDeclarationToPopulate = fFields.get(varFieldName);
+        if (fDeclarationToPopulate == null) {
+            throw new CTFReaderException("Unknown enum selector for variant " + //$NON-NLS-1$
+                    definitionScope.getScopePath().toString());
+        }
+        Definition fieldValue = fDeclarationToPopulate.createDefinition(definitionScope, fieldName, input);
+        return new VariantDefinition(this, definitionScope, varFieldName, fieldName, fieldValue);
     }
 
-
     /**
      * Add a field to this CTF Variant
      *
@@ -115,7 +154,30 @@ public class VariantDeclaration implements IDeclaration {
      *            The Declaration of this new field
      */
     public void addField(String fieldTag, IDeclaration declaration) {
-        fields.put(fieldTag, declaration);
+        fFields.put(fieldTag, declaration);
+    }
+
+    /**
+     * gets the tag definition
+     *
+     * @return the fTagDef
+     * @since 3.0
+     */
+    public EnumDefinition getTagDef() {
+        return fTagDef;
+    }
+
+    /**
+     * @since 3.0
+     */
+    @Override
+    public int getMaximumSize() {
+        Collection<IDeclaration> values = fFields.values();
+        int maxSize = 0;
+        for (IDeclaration field : values) {
+            maxSize = Math.max(maxSize, field.getMaximumSize());
+        }
+        return maxSize;
     }
 
     @Override
index 2b7190233de5bb960eb9c55f3a3bd90744002bf1..5cbc94a4796631ee326d46485a734de5705ea222 100644 (file)
 
 package org.eclipse.linuxtools.ctf.core.event.types;
 
-import java.util.HashMap;
-import java.util.Map;
-
-import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
-import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
 
 /**
  * A CTF variant definition (similar to a C union).
@@ -29,17 +26,15 @@ import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
  * @author Matthew Khouzam
  * @author Simon Marchi
  */
-public class VariantDefinition extends Definition implements IDefinitionScope {
+public final class VariantDefinition extends Definition implements IDefinitionScope {
 
     // ------------------------------------------------------------------------
     // Attributes
     // ------------------------------------------------------------------------
 
-    private VariantDeclaration declaration;
-
-    private EnumDefinition tagDefinition;
-    private Map<String, Definition> definitions = new HashMap<>();
-    private String currentField;
+    private final Definition fDefinition;
+    private final String fCurrentField;
+    private final String fFieldName;
 
     // ------------------------------------------------------------------------
     // Constructors
@@ -47,24 +42,27 @@ public class VariantDefinition extends Definition implements IDefinitionScope {
 
     /**
      * Constructor
-     * @param declaration the parent declaration
-     * @param definitionScope the parent scope
-     * @param fieldName the field name
+     *
+     * @param declaration
+     *            the parent declaration
+     * @param definitionScope
+     *            the parent scope
+     * @param selectedField
+     *            the selected field
+     * @param fieldName
+     *            the field name
+     * @param fieldValue
+     *            the field value
+     * @since 3.0
      */
-    public VariantDefinition(VariantDeclaration declaration,
-            IDefinitionScope definitionScope, String fieldName) {
-        super(definitionScope, fieldName);
+    public VariantDefinition(@NonNull VariantDeclaration declaration,
+            IDefinitionScope definitionScope, String selectedField, @NonNull String fieldName, Definition fieldValue) {
+        super(declaration, definitionScope, fieldName);
 
-        this.declaration = declaration;
+        fFieldName = fieldName;
+        fCurrentField = selectedField;
+        fDefinition = fieldValue;
 
-        Definition tagDef = definitionScope.lookupDefinition(declaration.getTag());
-        this.tagDefinition = (EnumDefinition) tagDef;
-
-        for (Map.Entry<String, IDeclaration> field : declaration.getFields().entrySet()) {
-            Definition fieldDef = field.getValue().createDefinition(this,
-                    field.getKey());
-            definitions.put(field.getKey(), fieldDef);
-        }
     }
 
     // ------------------------------------------------------------------------
@@ -73,97 +71,42 @@ public class VariantDefinition extends Definition implements IDefinitionScope {
 
     @Override
     public VariantDeclaration getDeclaration() {
-        return declaration;
-    }
-
-    /**
-     * Sets the variant declaration
-     * @param declaration the variant declaration
-     */
-    public void setDeclaration(VariantDeclaration declaration) {
-        this.declaration = declaration;
-    }
-
-    /**
-     * Gets the tag
-     * @return the tag definition
-     */
-    public EnumDefinition getTagDefinition() {
-        return tagDefinition;
-    }
-
-    /**
-     * Sets the tag
-     * @param tagDefinition the tag
-     */
-    public void setTagDefinition(EnumDefinition tagDefinition) {
-        this.tagDefinition = tagDefinition;
-    }
-
-    /**
-     * Get the definitions in the variant
-     * @return the definitions
-     * @since 2.0
-     */
-    public Map<String, Definition> getDefinitions() {
-        return definitions;
-    }
-
-    /**
-     * Set the definitions in a variant
-     * @param definitions the definitions
-     * @since 2.0
-     */
-    public void setDefinitions(Map<String, Definition> definitions) {
-        this.definitions = definitions;
-    }
-
-    /**
-     * Set the current field
-     * @param currentField the current field
-     */
-    public void setCurrentField(String currentField) {
-        this.currentField = currentField;
+        return (VariantDeclaration) super.getDeclaration();
     }
 
     /**
      * Get the current field name
+     *
      * @return the current field name
      */
     public String getCurrentFieldName() {
-        return currentField;
+        return fCurrentField;
     }
 
     /**
      * Get the current field
+     *
      * @return the current field
      */
     public Definition getCurrentField() {
-        return definitions.get(currentField);
+        return fDefinition;
     }
 
-
     // ------------------------------------------------------------------------
     // Operations
     // ------------------------------------------------------------------------
 
-    @Override
-    public void read(BitBuffer input) throws CTFReaderException {
-        currentField = tagDefinition.getValue();
-
-        Definition field = definitions.get(currentField);
-        if (field == null) {
-            throw new CTFReaderException("Variant was not defined for: "+ currentField); //$NON-NLS-1$
-        }
-        field.read(input);
-    }
-
     @Override
     public Definition lookupDefinition(String lookupPath) {
-        return definitions.get(lookupPath);
+        if (lookupPath == null) {
+            return null;
+        }
+        if (lookupPath.equals(fFieldName)) {
+            return fDefinition;
+        }
+        return getDefinitionScope().lookupDefinition(lookupPath);
     }
 
-
     /**
      * Lookup an array in a struct. if the name returns a non-array (like an
      * int) than the method returns null
@@ -219,8 +162,8 @@ public class VariantDefinition extends Definition implements IDefinitionScope {
     }
 
     /**
-     * Lookup a string in a struct. if the name returns a non-string (like
-     * an int) than the method returns null
+     * Lookup a string in a struct. if the name returns a non-string (like an
+     * int) than the method returns null
      *
      * @param name
      *            the name of the string
@@ -233,8 +176,8 @@ public class VariantDefinition extends Definition implements IDefinitionScope {
     }
 
     /**
-     * Lookup a struct in a struct. if the name returns a non-struct (like
-     * an int) than the method returns null
+     * Lookup a struct in a struct. if the name returns a non-struct (like an
+     * int) than the method returns null
      *
      * @param name
      *            the name of the struct
@@ -247,8 +190,8 @@ public class VariantDefinition extends Definition implements IDefinitionScope {
     }
 
     /**
-     * Lookup a variant in a struct. if the name returns a non-variant (like
-     * an int) than the method returns null
+     * Lookup a variant in a struct. if the name returns a non-variant (like an
+     * int) than the method returns null
      *
      * @param name
      *            the name of the variant
index 3788c634d08a87a274ebcf57c69a6b9326d5a5dd..757dc917ab1c8e3e43b48fd2875acde79584d96f 100644 (file)
@@ -40,9 +40,10 @@ import org.eclipse.linuxtools.ctf.core.event.CTFCallsite;
 import org.eclipse.linuxtools.ctf.core.event.CTFClock;
 import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
+import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope;
 import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
-import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope;
 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
@@ -62,12 +63,11 @@ import org.eclipse.linuxtools.internal.ctf.core.event.metadata.exceptions.ParseE
  */
 public class CTFTrace implements IDefinitionScope, AutoCloseable {
 
-    @SuppressWarnings("nls")
     @Override
     public String toString() {
         /* Only for debugging, shouldn't be externalized */
-        return "CTFTrace [path=" + fPath + ", major=" + fMajor + ", minor="
-                + fMinor + ", uuid=" + fUuid + "]";
+        return "CTFTrace [path=" + fPath + ", major=" + fMajor + ", minor=" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+                + fMinor + ", uuid=" + fUuid + "]"; //$NON-NLS-1$ //$NON-NLS-2$
     }
 
     /**
@@ -194,20 +194,10 @@ public class CTFTrace implements IDefinitionScope, AutoCloseable {
      */
     public CTFTrace() {
         fPath = null;
-        init();
-    }
-
-    private void init() {
-        /* Create the definitions needed to read things from the files */
-        if (fPacketHeaderDecl != null) {
-            fPacketHeaderDef = fPacketHeaderDecl.createDefinition(this, "packet.header"); //$NON-NLS-1$
-        }
     }
 
     private void init(File path) throws CTFReaderException {
 
-        init();
-
         /* Open all the trace files */
 
         /* List files not called metadata and not hidden. */
@@ -464,11 +454,18 @@ public class CTFTrace implements IDefinitionScope, AutoCloseable {
      * @return String the path of the trace directory, in string format.
      * @see java.io.File#getPath()
      */
-    @Override
     public String getPath() {
         return (fPath != null) ? fPath.getPath() : ""; //$NON-NLS-1$
     }
 
+    /**
+     * @since 3.0
+     */
+    @Override
+    public LexicalScope getScopePath() {
+        return LexicalScope.TRACE;
+    }
+
     // ------------------------------------------------------------------------
     // Operations
     // ------------------------------------------------------------------------
@@ -536,31 +533,22 @@ public class CTFTrace implements IDefinitionScope, AutoCloseable {
         /* Create a BitBuffer with this mapping and the trace byte order */
         streamBitBuffer = new BitBuffer(byteBuffer, this.getByteOrder());
 
-        if (fPacketHeaderDef != null) {
+        if (fPacketHeaderDecl != null) {
             /* Read the packet header */
-            fPacketHeaderDef.read(streamBitBuffer);
+            fPacketHeaderDef = fPacketHeaderDecl.createDefinition(null, LexicalScope.PACKET_HEADER.getName(), streamBitBuffer);
 
             /* Check the magic number */
-            IntegerDefinition magicDef = (IntegerDefinition) fPacketHeaderDef
-                    .lookupDefinition("magic"); //$NON-NLS-1$
+            IntegerDefinition magicDef = (IntegerDefinition) fPacketHeaderDef.lookupDefinition("magic"); //$NON-NLS-1$
             int magic = (int) magicDef.getValue();
             if (magic != Utils.CTF_MAGIC) {
                 throw new CTFReaderException("CTF magic mismatch"); //$NON-NLS-1$
             }
 
             /* Check UUID */
-            ArrayDefinition uuidDef = (ArrayDefinition) fPacketHeaderDef
-                    .lookupDefinition("uuid"); //$NON-NLS-1$
+            Definition lookupDefinition = fPacketHeaderDef.lookupDefinition("uuid"); //$NON-NLS-1$
+            ArrayDefinition uuidDef = (ArrayDefinition) lookupDefinition;
             if (uuidDef != null) {
-                byte[] uuidArray = new byte[Utils.UUID_LEN];
-
-                for (int i = 0; i < Utils.UUID_LEN; i++) {
-                    IntegerDefinition uuidByteDef = (IntegerDefinition) uuidDef
-                            .getElem(i);
-                    uuidArray[i] = (byte) uuidByteDef.getValue();
-                }
-
-                UUID otheruuid = Utils.makeUUID(uuidArray);
+                UUID otheruuid = Utils.getUUIDfromDefinition(uuidDef);
 
                 if (!fUuid.equals(otheruuid)) {
                     throw new CTFReaderException("UUID mismatch"); //$NON-NLS-1$
@@ -603,7 +591,7 @@ public class CTFTrace implements IDefinitionScope, AutoCloseable {
      * @param lookupPath
      *            String
      * @return Definition
-     * @see org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope#lookupDefinition(String)
+     * @see org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope#lookupDefinition(String)
      */
     @Override
     public Definition lookupDefinition(String lookupPath) {
@@ -637,12 +625,6 @@ public class CTFTrace implements IDefinitionScope, AutoCloseable {
      * @since 2.0
      */
     public void addStream(Stream stream) throws ParseException {
-
-        /*
-         * Init if not done before
-         */
-        init();
-
         /*
          * If there is already a stream without id (the null key), it must be
          * the only one
index a6ac0857085ac5781e61ff4a297a04dc67bdc171..5ae963aa25c8057c4b39b1e762d8c4bae6d8dc60 100644 (file)
@@ -19,14 +19,17 @@ import java.nio.channels.FileChannel;
 import java.nio.channels.FileChannel.MapMode;
 import java.util.UUID;
 
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
+import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope;
 import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
 import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.FloatDefinition;
-import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope;
 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition;
+import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
 import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputPacketIndex;
 import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputPacketIndexEntry;
@@ -69,12 +72,12 @@ public class StreamInput implements IDefinitionScope {
     /*
      * Definition of trace packet header
      */
-    private StructDefinition fTracePacketHeaderDef = null;
+    private StructDeclaration fTracePacketHeaderDecl = null;
 
     /*
      * Definition of trace stream packet context
      */
-    private StructDefinition fStreamPacketContextDef = null;
+    private StructDeclaration fStreamPacketContextDecl = null;
 
     /*
      * Total number of lost events in this stream
@@ -156,10 +159,12 @@ public class StreamInput implements IDefinitionScope {
 
     /**
      * Useless for streaminputs
+     *
+     * @since 3.0
      */
     @Override
-    public String getPath() {
-        return ""; //$NON-NLS-1$
+    public LexicalScope getScopePath() {
+        return LexicalScope.STREAM;
     }
 
     // ------------------------------------------------------------------------
@@ -187,13 +192,11 @@ public class StreamInput implements IDefinitionScope {
          * Create the definitions we need to read the packet headers + contexts
          */
         if (getStream().getTrace().getPacketHeader() != null) {
-            fTracePacketHeaderDef = getStream().getTrace().getPacketHeader()
-                    .createDefinition(this, "trace.packet.header"); //$NON-NLS-1$
+            fTracePacketHeaderDecl = getStream().getTrace().getPacketHeader();
         }
 
         if (getStream().getPacketContextDecl() != null) {
-            fStreamPacketContextDef = getStream().getPacketContextDecl()
-                    .createDefinition(this, "stream.packet.context"); //$NON-NLS-1$
+            fStreamPacketContextDecl = getStream().getPacketContextDecl();
         }
 
     }
@@ -219,7 +222,7 @@ public class StreamInput implements IDefinitionScope {
             StreamInputPacketIndexEntry packetIndex = new StreamInputPacketIndexEntry(
                     currentPos);
             createPacketIndexEntry(fileSize, currentPos, packetIndex,
-                    fTracePacketHeaderDef, fStreamPacketContextDef, bitBuffer);
+                    fTracePacketHeaderDecl, fStreamPacketContextDecl, bitBuffer);
             fIndex.addEntry(packetIndex);
             return true;
         }
@@ -232,8 +235,8 @@ public class StreamInput implements IDefinitionScope {
 
     private long createPacketIndexEntry(long fileSizeBytes,
             long packetOffsetBytes, StreamInputPacketIndexEntry packetIndex,
-            StructDefinition tracePacketHeaderDef,
-            StructDefinition streamPacketContextDef, BitBuffer bitBuffer)
+            StructDeclaration tracePacketHeaderDecl,
+            StructDeclaration streamPacketContextDecl, @NonNull BitBuffer bitBuffer)
             throws CTFReaderException {
 
         /*
@@ -245,15 +248,15 @@ public class StreamInput implements IDefinitionScope {
         /*
          * Read the trace packet header if it exists.
          */
-        if (tracePacketHeaderDef != null) {
-            parseTracePacketHeader(tracePacketHeaderDef, bitBuffer);
+        if (tracePacketHeaderDecl != null) {
+            parseTracePacketHeader(tracePacketHeaderDecl, bitBuffer);
         }
 
         /*
          * Read the stream packet context if it exists.
          */
-        if (streamPacketContextDef != null) {
-            parsePacketContext(fileSizeBytes, streamPacketContextDef,
+        if (streamPacketContextDecl != null) {
+            parsePacketContext(fileSizeBytes, streamPacketContextDecl,
                     bitBuffer, packetIndex);
         } else {
             setPacketContextNull(fileSizeBytes, packetIndex);
@@ -334,9 +337,9 @@ public class StreamInput implements IDefinitionScope {
         return bb;
     }
 
-    private void parseTracePacketHeader(StructDefinition tracePacketHeaderDef,
-            BitBuffer bitBuffer) throws CTFReaderException {
-        tracePacketHeaderDef.read(bitBuffer);
+    private void parseTracePacketHeader(StructDeclaration tracePacketHeaderDecl,
+            @NonNull BitBuffer bitBuffer) throws CTFReaderException {
+        StructDefinition tracePacketHeaderDef = tracePacketHeaderDecl.createDefinition(null, LexicalScope.TRACE_PACKET_HEADER.getName(), bitBuffer);
 
         /*
          * Check the CTF magic number
@@ -357,14 +360,7 @@ public class StreamInput implements IDefinitionScope {
         ArrayDefinition uuidDef =
                 (ArrayDefinition) tracePacketHeaderDef.lookupDefinition("uuid"); //$NON-NLS-1$
         if (uuidDef != null) {
-            byte[] uuidArray = new byte[16];
-
-            for (int i = 0; i < uuidArray.length; i++) {
-                IntegerDefinition uuidByteDef = (IntegerDefinition) uuidDef.getElem(i);
-                uuidArray[i] = (byte) uuidByteDef.getValue();
-            }
-
-            UUID uuid = Utils.makeUUID(uuidArray);
+            UUID uuid = Utils.getUUIDfromDefinition(uuidDef);
 
             if (!getStream().getTrace().getUUID().equals(uuid)) {
                 throw new CTFReaderException("UUID mismatch"); //$NON-NLS-1$
@@ -396,9 +392,9 @@ public class StreamInput implements IDefinitionScope {
     }
 
     private void parsePacketContext(long fileSizeBytes,
-            StructDefinition streamPacketContextDef, BitBuffer bitBuffer,
+            StructDeclaration streamPacketContextDecl, @NonNull BitBuffer bitBuffer,
             StreamInputPacketIndexEntry packetIndex) throws CTFReaderException {
-        streamPacketContextDef.read(bitBuffer);
+        StructDefinition streamPacketContextDef = streamPacketContextDecl.createDefinition(null, LexicalScope.STREAM_PACKET_CONTEXT.getName(), bitBuffer);
 
         for (String field : streamPacketContextDef.getDeclaration()
                 .getFieldsList()) {
@@ -436,7 +432,6 @@ public class StreamInput implements IDefinitionScope {
             packetIndex.setContentSizeBits((int) (fileSizeBytes * 8));
         }
 
-
         /* Read the packet size in bits */
         if (packetSize != null) {
             packetIndex.setPacketSizeBits(packetSize.intValue());
@@ -446,7 +441,6 @@ public class StreamInput implements IDefinitionScope {
             packetIndex.setPacketSizeBits((int) (fileSizeBytes * 8));
         }
 
-
         /* Read the begin timestamp */
         if (tsBegin != null) {
             packetIndex.setTimestampBegin(tsBegin.longValue());
index ea2557b6283045d1fff8cf7d6f5d90afc26d74f9..6ad4ebcf0b6b0b584bb38baee6b9f2baec3b5856 100644 (file)
@@ -14,11 +14,15 @@ package org.eclipse.linuxtools.ctf.core.trace;
 import java.io.IOException;
 import java.nio.ByteBuffer;
 
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.linuxtools.ctf.core.CTFStrings;
 import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
+import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
+import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope;
 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
-import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope;
+import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.SimpleDatatypeDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
@@ -27,6 +31,8 @@ import org.eclipse.linuxtools.ctf.core.event.types.VariantDefinition;
 import org.eclipse.linuxtools.internal.ctf.core.event.EventDeclaration;
 import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputPacketIndexEntry;
 
+import com.google.common.collect.ImmutableList;
+
 /**
  * CTF trace packet reader. Reads the events of a packet of a trace file.
  *
@@ -41,23 +47,27 @@ public class StreamInputPacketReader implements IDefinitionScope, AutoCloseable
     // ------------------------------------------------------------------------
 
     /** BitBuffer used to read the trace file. */
+    @NonNull
     private final BitBuffer fBitBuffer;
 
     /** StreamInputReader that uses this StreamInputPacketReader. */
     private final StreamInputReader fStreamInputReader;
 
     /** Trace packet header. */
-    private final StructDefinition fTracePacketHeaderDef;
+    private final StructDeclaration fTracePacketHeaderDecl;
 
     /** Stream packet context definition. */
-    private final StructDefinition fStreamPacketContextDef;
+    private final StructDeclaration fStreamPacketContextDecl;
 
     /** Stream event header definition. */
-    private final StructDefinition fStreamEventHeaderDef;
+    private final StructDeclaration fStreamEventHeaderDecl;
 
     /** Stream event context definition. */
-    private final StructDefinition fStreamEventContextDef;
+    private final StructDeclaration fStreamEventContextDecl;
 
+    private StructDefinition fCurrentTracePacketHeaderDef;
+    private StructDefinition fCurrentStreamEventHeaderDef;
+    private Definition fCurrentStreamPacketContextDef;
     /** Reference to the index entry of the current packet. */
     private StreamInputPacketIndexEntry fCurrentPacket = null;
 
@@ -95,39 +105,67 @@ public class StreamInputPacketReader implements IDefinitionScope, AutoCloseable
         fBitBuffer = new BitBuffer();
         fBitBuffer.setByteOrder(streamInputReader.getByteOrder());
 
-        /* Create trace packet header definition. */
         final Stream currentStream = streamInputReader.getStreamInput().getStream();
-        StructDeclaration tracePacketHeaderDecl = currentStream.getTrace().getPacketHeader();
-        if (tracePacketHeaderDecl != null) {
-            fTracePacketHeaderDef = tracePacketHeaderDecl.createDefinition(this, "trace.packet.header"); //$NON-NLS-1$
-        } else {
-            fTracePacketHeaderDef = null;
-        }
+        fTracePacketHeaderDecl = currentStream.getTrace().getPacketHeader();
+        fStreamPacketContextDecl = currentStream.getPacketContextDecl();
+        fStreamEventHeaderDecl = currentStream.getEventHeaderDecl();
+        fStreamEventContextDecl = currentStream.getEventContextDecl();
+    }
 
-        /* Create stream packet context definition. */
-        StructDeclaration streamPacketContextDecl = currentStream.getPacketContextDecl();
-        if (streamPacketContextDecl != null) {
-            fStreamPacketContextDef = streamPacketContextDecl.createDefinition(this, "stream.packet.context"); //$NON-NLS-1$
-        } else {
-            fStreamPacketContextDef = null;
-        }
+    /**
+     * Get the event context defintiion
+     *
+     * @param input
+     *            the bitbuffer to read from
+     * @return an context definition, can be null
+     * @throws CTFReaderException
+     *             out of bounds exception or such
+     * @since 3.0
+     */
+    public StructDefinition getEventContextDefinition(@NonNull BitBuffer input) throws CTFReaderException {
+        return fStreamEventContextDecl.createDefinition(this, LexicalScope.STREAM_EVENT_CONTEXT.getName(), input);
+    }
 
-        /* Create stream event header definition. */
-        StructDeclaration streamEventHeaderDecl = currentStream.getEventHeaderDecl();
-        if (streamEventHeaderDecl != null) {
-            fStreamEventHeaderDef = streamEventHeaderDecl.createDefinition(this, "stream.event.header"); //$NON-NLS-1$
-        } else {
-            fStreamEventHeaderDef = null;
-        }
+    /**
+     * Get the stream context defintiion
+     *
+     * @param input
+     *            the bitbuffer to read from
+     * @return an context definition, can be null
+     * @throws CTFReaderException
+     *             out of bounds exception or such
+     * @since 3.0
+     */
+    public StructDefinition getStreamEventHeaderDefinition(@NonNull BitBuffer input) throws CTFReaderException {
+        return fStreamEventHeaderDecl.createDefinition(this, LexicalScope.STREAM_EVENT_HEADER.getName(), input);
+    }
 
-        /* Create stream event context definition. */
-        StructDeclaration streamEventContextDecl = currentStream.getEventContextDecl();
-        if (streamEventContextDecl != null) {
-            fStreamEventContextDef = streamEventContextDecl.createDefinition(this, "stream.event.context"); //$NON-NLS-1$
-        } else {
-            fStreamEventContextDef = null;
-        }
+    /**
+     * Get the packet context defintiion
+     *
+     * @param input
+     *            the bitbuffer to read from
+     * @return an context definition, can be null
+     * @throws CTFReaderException
+     *             out of bounds exception or such
+     * @since 3.0
+     */
+    public StructDefinition getStreamPacketContextDefinition(@NonNull BitBuffer input) throws CTFReaderException {
+        return fStreamPacketContextDecl.createDefinition(this, LexicalScope.STREAM_PACKET_CONTEXT.getName(), input);
+    }
 
+    /**
+     * Get the event header defintiion
+     *
+     * @param input
+     *            the bitbuffer to read from
+     * @return an header definition, can be null
+     * @throws CTFReaderException
+     *             out of bounds exception or such
+     * @since 3.0
+     */
+    public StructDefinition getTracePacketHeaderDefinition(@NonNull BitBuffer input) throws CTFReaderException {
+        return fTracePacketHeaderDecl.createDefinition(this, LexicalScope.TRACE_PACKET_HEADER.getName(), input);
     }
 
     /**
@@ -153,24 +191,6 @@ public class StreamInputPacketReader implements IDefinitionScope, AutoCloseable
         return fCurrentPacket;
     }
 
-    /**
-     * Gets the steamPacketContext Definition
-     *
-     * @return steamPacketContext Definition
-     */
-    public StructDefinition getStreamPacketContextDef() {
-        return fStreamPacketContextDef;
-    }
-
-    /**
-     * Gets the stream's event context definition.
-     *
-     * @return The streamEventContext definition
-     */
-    public StructDefinition getStreamEventContextDef() {
-        return fStreamEventContextDef;
-    }
-
     /**
      * Gets the CPU (core) number
      *
@@ -180,9 +200,12 @@ public class StreamInputPacketReader implements IDefinitionScope, AutoCloseable
         return fCurrentCpu;
     }
 
+    /**
+     * @since 3.0
+     */
     @Override
-    public String getPath() {
-        return ""; //$NON-NLS-1$
+    public LexicalScope getScopePath() {
+        return LexicalScope.PACKET;
     }
 
     // ------------------------------------------------------------------------
@@ -208,8 +231,8 @@ public class StreamInputPacketReader implements IDefinitionScope, AutoCloseable
             ByteBuffer bb = null;
             try {
                 bb = fStreamInputReader.getStreamInput().getByteBufferAt(
-                                fCurrentPacket.getOffsetBytes(),
-                                (fCurrentPacket.getPacketSizeBits() + 7) / 8);
+                        fCurrentPacket.getOffsetBytes(),
+                        (fCurrentPacket.getPacketSizeBits() + 7) / 8);
             } catch (IOException e) {
                 throw new CTFReaderException(e.getMessage(), e);
             }
@@ -219,15 +242,15 @@ public class StreamInputPacketReader implements IDefinitionScope, AutoCloseable
             /*
              * Read trace packet header.
              */
-            if (fTracePacketHeaderDef != null) {
-                fTracePacketHeaderDef.read(fBitBuffer);
+            if (fTracePacketHeaderDecl != null) {
+                fCurrentTracePacketHeaderDef = getTracePacketHeaderDefinition(fBitBuffer);
             }
 
             /*
              * Read stream packet context.
              */
-            if (getStreamPacketContextDef() != null) {
-                getStreamPacketContextDef().read(fBitBuffer);
+            if (fStreamPacketContextDecl != null) {
+                fCurrentStreamPacketContextDef = getStreamPacketContextDefinition(fBitBuffer);
 
                 /* Read CPU ID */
                 if (getCurrentPacket().getTarget() != null) {
@@ -293,22 +316,47 @@ public class StreamInputPacketReader implements IDefinitionScope, AutoCloseable
         long timestamp = 0;
         if (fHasLost) {
             fHasLost = false;
-            EventDefinition eventDef = EventDeclaration.getLostEventDeclaration().createDefinition(fStreamInputReader);
-            ((IntegerDefinition) eventDef.getFields().getDefinitions().get(CTFStrings.LOST_EVENTS_FIELD)).setValue(fLostEventsInThisPacket);
-            ((IntegerDefinition) eventDef.getFields().getDefinitions().get(CTFStrings.LOST_EVENTS_DURATION)).setValue(fLostEventsDuration);
-            eventDef.setTimestamp(fLastTimestamp);
-            return eventDef;
+            EventDeclaration lostEventDeclaration = EventDeclaration.getLostEventDeclaration();
+            StructDeclaration lostFields = lostEventDeclaration.getFields();
+            // this is a hard coded map, we know it's not null
+            IntegerDeclaration lostFieldsDecl = (IntegerDeclaration) lostFields.getFields().get(CTFStrings.LOST_EVENTS_FIELD);
+            if (lostFieldsDecl == null)
+            {
+                throw new IllegalStateException("Lost events count not declared!"); //$NON-NLS-1$
+            }
+            IntegerDeclaration lostEventsDurationDecl = (IntegerDeclaration) lostFields.getFields().get(CTFStrings.LOST_EVENTS_DURATION);
+            if (lostEventsDurationDecl == null) {
+                throw new IllegalStateException("Lost events duration not declared!"); //$NON-NLS-1$
+            }
+            IntegerDefinition lostDurationDef = new IntegerDefinition(lostFieldsDecl, null, CTFStrings.LOST_EVENTS_DURATION, fLostEventsDuration);
+            IntegerDefinition lostCountDef = new IntegerDefinition(lostEventsDurationDecl, null, CTFStrings.LOST_EVENTS_FIELD, fLostEventsInThisPacket);
+            IntegerDefinition[] fields = new IntegerDefinition[] { lostCountDef, lostDurationDef };
+            /* this is weird notation, but it's the java notation */
+            final ImmutableList<String> fieldNameList = ImmutableList.<String> builder().add(CTFStrings.LOST_EVENTS_FIELD).add(CTFStrings.LOST_EVENTS_DURATION).build();
+            return new EventDefinition(
+                    lostEventDeclaration,
+                    fStreamInputReader,
+                    fLastTimestamp,
+                    null,
+                    null,
+                    null,
+                    new StructDefinition(
+                            lostFields,
+                            this, "fields", //$NON-NLS-1$
+                            fieldNameList,
+                            fields
+                    ));
+
         }
 
-        final StructDefinition sehd = fStreamEventHeaderDef;
         final BitBuffer currentBitBuffer = fBitBuffer;
         final long posStart = currentBitBuffer.position();
         /* Read the stream event header. */
-        if (sehd != null) {
-            sehd.read(currentBitBuffer);
+        if (fStreamEventHeaderDecl != null) {
+            fCurrentStreamEventHeaderDef = getStreamEventHeaderDefinition(currentBitBuffer);
 
             /* Check for the event id. */
-            Definition idDef = sehd.lookupDefinition("id"); //$NON-NLS-1$
+            Definition idDef = fCurrentStreamEventHeaderDef.lookupDefinition("id"); //$NON-NLS-1$
             if (idDef instanceof SimpleDatatypeDefinition) {
                 eventID = ((SimpleDatatypeDefinition) idDef).getIntegerValue();
             } else if (idDef != null) {
@@ -319,13 +367,13 @@ public class StreamInputPacketReader implements IDefinitionScope, AutoCloseable
              * Get the timestamp from the event header (may be overridden later
              * on)
              */
-            IntegerDefinition timestampDef = sehd.lookupInteger("timestamp"); //$NON-NLS-1$
+            IntegerDefinition timestampDef = fCurrentStreamEventHeaderDef.lookupInteger("timestamp"); //$NON-NLS-1$
             if (timestampDef != null) {
                 timestamp = calculateTimestamp(timestampDef);
             } // else timestamp remains 0
 
             /* Check for the variant v. */
-            Definition variantDef = sehd.lookupDefinition("v"); //$NON-NLS-1$
+            Definition variantDef = fCurrentStreamEventHeaderDef.lookupDefinition("v"); //$NON-NLS-1$
             if (variantDef instanceof VariantDefinition) {
 
                 /* Get the variant current field */
@@ -351,32 +399,17 @@ public class StreamInputPacketReader implements IDefinitionScope, AutoCloseable
             }
         }
 
-        /* Read the stream event context. */
-        if (fStreamEventContextDef != null) {
-            fStreamEventContextDef.read(currentBitBuffer);
-        }
-
         /* Get the right event definition using the event id. */
-        EventDefinition eventDef = fStreamInputReader.getStreamInput().getStream().getEvents().get(eventID).createDefinition(fStreamInputReader);
-        if (eventDef == null) {
+        IEventDeclaration eventDeclaration = fStreamInputReader.getStreamInput().getStream().getEvents().get(eventID);
+        if (eventDeclaration == null) {
             throw new CTFReaderException("Incorrect event id : " + eventID); //$NON-NLS-1$
         }
-
-        /* Read the event context. */
-        if (eventDef.getEventContext() != null) {
-            eventDef.getEventContext().read(currentBitBuffer);
-        }
-
-        /* Read the event fields. */
-        if (eventDef.getFields() != null) {
-            eventDef.getFields().read(currentBitBuffer);
-        }
+        EventDefinition eventDef = eventDeclaration.createDefinition(fStreamInputReader, fBitBuffer, timestamp);
 
         /*
          * Set the event timestamp using the timestamp calculated by
          * updateTimestamp.
          */
-        eventDef.setTimestamp(timestamp);
 
         if (posStart == currentBitBuffer.position()) {
             throw new CTFReaderException("Empty event not allowed, event: " + eventDef.getDeclaration().getName()); //$NON-NLS-1$
@@ -431,6 +464,32 @@ public class StreamInputPacketReader implements IDefinitionScope, AutoCloseable
 
     @Override
     public Definition lookupDefinition(String lookupPath) {
+        if (lookupPath.equals(LexicalScope.STREAM_PACKET_CONTEXT)) {
+            return fCurrentStreamPacketContextDef;
+        }
+        if (lookupPath.equals(LexicalScope.TRACE_PACKET_HEADER)) {
+            return fCurrentTracePacketHeaderDef;
+        }
         return null;
     }
+
+    /**
+     * Get stream event header
+     *
+     * @return the stream event header
+     * @since 3.0
+     */
+    public StructDefinition getCurrentStreamEventHeader() {
+        return fCurrentStreamEventHeaderDef;
+    }
+
+    /**
+     * Get the current packet event header
+     *
+     * @return the current packet event header
+     * @since 3.0
+     */
+    public StructDefinition getCurrentPacketEventHeader() {
+        return fCurrentTracePacketHeaderDef;
+    }
 }
index db3141d77815b36724410d8b6ebd00d5b3f40fbc..bcc65573d567d5d98d46b177ca02e6eef691cb7b 100644 (file)
@@ -15,7 +15,7 @@ package org.eclipse.linuxtools.ctf.core.trace;
 import java.nio.ByteOrder;
 
 import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
-import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
+import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
 import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputPacketIndexEntry;
 
 /**
@@ -111,15 +111,6 @@ public class StreamInputReader implements AutoCloseable {
         return fCurrentEvent;
     }
 
-    /**
-     * Gets the current packet context
-     *
-     * @return the current packet context (size, lost events and such)
-     */
-    public StructDefinition getCurrentPacketContext() {
-        return fPacketReader.getStreamPacketContextDef();
-    }
-
     /**
      * Gets the byte order for a trace
      *
@@ -195,6 +186,16 @@ public class StreamInputReader implements AutoCloseable {
         return fLive;
     }
 
+    /**
+     * Get the event context of the stream
+     *
+     * @return the event context declaration of the stream
+     * @since 3.0
+     */
+    public StructDeclaration getStreamEventContextDecl() {
+        return getStreamInput().getStream().getEventContextDecl();
+    }
+
     // ------------------------------------------------------------------------
     // Operations
     // ------------------------------------------------------------------------
@@ -214,7 +215,7 @@ public class StreamInputReader implements AutoCloseable {
         if (!fPacketReader.hasMoreEvents()) {
             final StreamInputPacketIndexEntry prevPacket = fPacketReader
                     .getCurrentPacket();
-            if (prevPacket != null || fLive ) {
+            if (prevPacket != null || fLive) {
                 goToNextPacket();
             }
 
index efed12738e66b9668f91a0debe709498a0f330eb..f76f055b8821c541ad4f4097e1b14a282261646a 100644 (file)
@@ -14,6 +14,9 @@ package org.eclipse.linuxtools.ctf.core.trace;
 
 import java.util.UUID;
 
+import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition;
+import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
+
 /**
  * Various utilities.
  *
@@ -21,9 +24,10 @@ import java.util.UUID;
  * @author Matthew Khouzam
  * @author Simon Marchi
  */
-public class Utils {
+public final class Utils {
 
-    private Utils() {}
+    private Utils() {
+    }
 
     // ------------------------------------------------------------------------
     // Constants
@@ -73,9 +77,9 @@ public class Utils {
      */
     public static int unsignedCompare(long left, long right) {
         /*
-         * This method assumes that the arithmetic overflow on signed
-         * integer wrap on a circular domain (modulo arithmetic in
-         * two-complement), which is the defined behavior in Java.
+         * This method assumes that the arithmetic overflow on signed integer
+         * wrap on a circular domain (modulo arithmetic in two-complement),
+         * which is the defined behavior in Java.
          *
          * This idea is to rotate the domain by the length of the negative
          * space, and then use the signed operator.
@@ -90,6 +94,31 @@ public class Utils {
         return 0;
     }
 
+    /**
+     * Gets a UUID from an array defintion
+     *
+     * @param uuidDef
+     *            the array defintions, must contain integer bytes
+     * @return the UUID
+     * @throws CTFReaderException
+     *             if the definition contains less than 16 elements
+     * @since 3.0
+     */
+    public static UUID getUUIDfromDefinition(ArrayDefinition uuidDef) throws CTFReaderException {
+        byte[] uuidArray = new byte[16];
+
+        for (int i = 0; i < uuidArray.length; i++) {
+            IntegerDefinition uuidByteDef = (IntegerDefinition) uuidDef.getElem(i);
+            if (uuidByteDef == null) {
+                throw new CTFReaderException("UUID incomplete, only " + i + " bytes available"); //$NON-NLS-1$ //$NON-NLS-2$
+            }
+            uuidArray[i] = (byte) uuidByteDef.getValue();
+        }
+
+        UUID uuid = Utils.makeUUID(uuidArray);
+        return uuid;
+    }
+
     /**
      * Creates a UUID object from an array of 16 bytes.
      *
index 5ab3e7dafb03673efdfcd932a78a3e24f0dd1fce..abd15b671e334f095aa74f0dc6b55fec15386c6b 100644 (file)
 
 package org.eclipse.linuxtools.internal.ctf.core.event;
 
-import java.nio.ByteOrder;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
 
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.linuxtools.ctf.core.CTFStrings;
 import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
 import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration;
-import org.eclipse.linuxtools.ctf.core.event.types.Encoding;
+import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
+import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope;
+import org.eclipse.linuxtools.ctf.core.event.types.Declaration;
 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
+import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
+import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
 import org.eclipse.linuxtools.ctf.core.trace.Stream;
 import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader;
 
@@ -32,6 +36,10 @@ import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader;
  */
 public class EventDeclaration implements IEventDeclaration {
 
+    @NonNull private static final String FIELDS = "fields"; //$NON-NLS-1$
+
+    @NonNull private static final String CONTEXT = "context"; //$NON-NLS-1$
+
     /** Id of lost events */
     public static final long LOST_EVENT_ID = -1L;
 
@@ -87,18 +95,24 @@ public class EventDeclaration implements IEventDeclaration {
     }
 
     @Override
-    public EventDefinition createDefinition(StreamInputReader streamInputReader) {
-        EventDefinition event = new EventDefinition(this, streamInputReader);
-
-        if (fContext != null) {
-            event.setContext(fContext.createDefinition(event, "context")); //$NON-NLS-1$
-        }
-
-        if (fFields != null) {
-            event.setFields(fFields.createDefinition(event, "fields")); //$NON-NLS-1$
-        }
-
-        return event;
+    public EventDefinition createDefinition(StreamInputReader streamInputReader, @NonNull BitBuffer input, long timestamp) throws CTFReaderException {
+        StructDeclaration streamEventContextDecl = streamInputReader.getStreamEventContextDecl();
+        StructDefinition streamEventContext = streamEventContextDecl != null ? streamEventContextDecl.createDefinition(null, LexicalScope.STREAM_EVENT_CONTEXT.toString(), input) : null;
+        StructDefinition packetContext = streamInputReader.getPacketReader().getCurrentPacketEventHeader();
+        StructDefinition eventContext = fContext != null ? fContext.createDefinition(null, CONTEXT, input) : null;
+        StructDefinition eventPayload = fFields != null ? fFields.createDefinition(null, FIELDS, input) : null;
+
+        // a bit lttng specific
+        // CTF doesn't require a timestamp,
+        // but it's passed to us
+        return new EventDefinition(
+                this,
+                streamInputReader,
+                timestamp,
+                streamEventContext,
+                eventContext,
+                packetContext,
+                eventPayload);
     }
 
     /**
@@ -109,15 +123,11 @@ public class EventDeclaration implements IEventDeclaration {
      */
     public static synchronized EventDeclaration getLostEventDeclaration() {
         EventDeclaration lostEvent = new EventDeclaration();
-        IntegerDeclaration lostEventsDeclaration = new IntegerDeclaration(32, false, 10, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 8);
-        IntegerDeclaration timestampDeclaration = new IntegerDeclaration(64, false, 10, ByteOrder.BIG_ENDIAN, Encoding.ASCII, null, 8);
-
-        lostEvent.fFields = new StructDeclaration(1);
-        lostEvent.fFields.addField(CTFStrings.LOST_EVENTS_FIELD, lostEventsDeclaration);
-        lostEvent.fFields.addField(CTFStrings.LOST_EVENTS_DURATION, timestampDeclaration);
+        String[] fieldNames = new String[] { CTFStrings.LOST_EVENTS_FIELD, CTFStrings.LOST_EVENTS_DURATION };
+        Declaration[] fieldDeclarations = new Declaration[] { IntegerDeclaration.UINT_32B_DECL, IntegerDeclaration.UINT_64B_DECL };
+        lostEvent.fFields = new StructDeclaration(fieldNames, fieldDeclarations);
         lostEvent.fId = LOST_EVENT_ID;
         lostEvent.fName = CTFStrings.LOST_EVENT_NAME;
-
         return lostEvent;
     }
 
index 8242a2d3497dd6522181c1a5bff1915d45f2fb86..3f43802442c54623b5b3b86b2b94a4060daf9629 100644 (file)
@@ -29,6 +29,7 @@ import java.util.UUID;
 
 import org.antlr.runtime.tree.CommonTree;
 import org.eclipse.core.runtime.IStatus;
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.linuxtools.ctf.core.event.CTFClock;
 import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.Encoding;
@@ -562,9 +563,9 @@ public class IOStructGen {
             final DeclarationScope parentScope, String name,
             IntegerDeclaration decl) throws ParseException {
 
-        if (decl.getByteOrder() == null) {
+        if (decl.getByteOrder() != byteOrder) {
             IntegerDeclaration newI;
-            newI = new IntegerDeclaration(decl.getLength(), decl.isSigned(),
+            newI = IntegerDeclaration.createDeclaration(decl.getLength(), decl.isSigned(),
                     decl.getBase(), byteOrder, decl.getEncoding(),
                     decl.getClock(), decl.getAlignment());
             parentScope.replaceType(name, newI);
@@ -585,9 +586,9 @@ public class IOStructGen {
 
             } else if (d instanceof IntegerDeclaration) {
                 IntegerDeclaration decl = (IntegerDeclaration) d;
-                if (decl.getByteOrder() == null) {
+                if (decl.getByteOrder() != byteOrder) {
                     IntegerDeclaration newI;
-                    newI = new IntegerDeclaration(decl.getLength(),
+                    newI = IntegerDeclaration.createDeclaration(decl.getLength(),
                             decl.isSigned(), decl.getBase(), byteOrder,
                             decl.getEncoding(), decl.getClock(),
                             decl.getAlignment());
@@ -609,7 +610,7 @@ public class IOStructGen {
             } else if (d instanceof IntegerDeclaration) {
                 IntegerDeclaration decl = (IntegerDeclaration) d;
                 IntegerDeclaration newI;
-                newI = new IntegerDeclaration(decl.getLength(),
+                newI = IntegerDeclaration.createDeclaration(decl.getLength(),
                         decl.isSigned(), decl.getBase(), byteOrder,
                         decl.getEncoding(), decl.getClock(),
                         decl.getAlignment());
@@ -1455,7 +1456,8 @@ public class IOStructGen {
         long size = 0;
         long alignment = 0;
         int base = 10;
-        String clock = null;
+        @NonNull
+        String clock = ""; //$NON-NLS-1$
 
         Encoding encoding = Encoding.NONE;
 
@@ -1514,14 +1516,16 @@ public class IOStructGen {
             }
         }
 
-        integerDeclaration = new IntegerDeclaration((int) size, signed, base,
+        integerDeclaration = IntegerDeclaration.createDeclaration((int) size, signed, base,
                 byteOrder, encoding, clock, alignment);
 
         return integerDeclaration;
     }
 
+    @NonNull
     private static String getClock(CommonTree rightNode) {
-        return rightNode.getChild(1).getChild(0).getChild(0).getText();
+        String clock = rightNode.getChild(1).getChild(0).getChild(0).getText();
+        return clock == null ? "" : clock; //$NON-NLS-1$
     }
 
     private static StringDeclaration parseString(CommonTree string)
@@ -2694,6 +2698,7 @@ public class IOStructGen {
      * @return The "encoding" value.
      * @throws ParseException
      */
+    @NonNull
     private static Encoding getEncoding(CommonTree rightNode)
             throws ParseException {
 
index a207c8fc2fa002346e71b459b9563c0555053d24..e6e4ebfd92360198266b31f50e27794bc7cf583b 100644 (file)
@@ -19,12 +19,12 @@ import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
 /**
  * The generic event structure in TMF. In its canonical form, an event has:
  * <ul>
- * <li> a parent trace
- * <li> a rank (order within the trace)
- * <li> a timestamp
- * <li> a source (reporting component)
- * <li> a type
- * <li> a content (payload)
+ * <li>a parent trace
+ * <li>a rank (order within the trace)
+ * <li>a timestamp
+ * <li>a source (reporting component)
+ * <li>a type
+ * <li>a content (payload)
  * </ul>
  * For convenience, a free-form reference field is also provided. It could be
  * used as e.g. a location marker (filename:lineno) to indicate where the event
index 6b31cbf51cfdaac7702fcce30687ca66e754efc2..0ee0abe53fd124054364851da90004d3b17f7011 100644 (file)
@@ -23,7 +23,6 @@ import java.nio.ByteOrder;
 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;
@@ -42,7 +41,7 @@ import org.junit.Test;
  * The class <code>CtfTmfEventFieldTest</code> contains tests for the class
  * <code>{@link CtfTmfEventField}</code>.
  *
- * @author ematkho
+ * @author Matthew Khouzam
  * @version 1.0
  */
 public class CtfTmfEventFieldTest {
@@ -88,8 +87,7 @@ public class CtfTmfEventFieldTest {
 
         StructDeclaration sDec = new StructDeclaration(1l);
         StringDeclaration strDec = new StringDeclaration();
-        IntegerDeclaration intDec = new IntegerDeclaration(8, false, 8,
-                ByteOrder.BIG_ENDIAN, Encoding.NONE, null, 8);
+        IntegerDeclaration intDec = IntegerDeclaration.UINT_8_DECL;
         FloatDeclaration flDec = new FloatDeclaration(8, 24,
                 ByteOrder.BIG_ENDIAN, 8);
         SequenceDeclaration seqDec = new SequenceDeclaration(LEN, intDec);
@@ -171,10 +169,8 @@ public class CtfTmfEventFieldTest {
             bb.putFloat(TEST_NUMBER);
         }
 
-        fixture = sDec.createDefinition(fixture, ROOT);
+        fixture = sDec.createDefinition(fixture, ROOT, new BitBuffer(bb));
 
-        bb.position(0);
-        fixture.read(new BitBuffer(bb));
     }
 
     /**
@@ -205,7 +201,7 @@ public class CtfTmfEventFieldTest {
     public void testParseField_int() {
         Definition fieldDef = fixture.lookupDefinition(INT);
         CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
-        assertEquals("test=02", result.toString());
+        assertEquals("test=2", result.toString());
     }
 
     /**
@@ -216,7 +212,7 @@ public class CtfTmfEventFieldTest {
     public void testParseField_array_int() {
         Definition fieldDef = fixture.lookupArray(ARRAY_INT);
         CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
-        assertEquals("test=[02, 02]", result.toString());
+        assertEquals("test=[2, 2]", result.toString());
     }
 
     /**
@@ -226,7 +222,7 @@ public class CtfTmfEventFieldTest {
     public void testParseField_sequence() {
         Definition fieldDef = fixture.lookupDefinition(SEQ);
         CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
-        assertEquals("test=[02, 02]", result.toString());
+        assertEquals("test=[2, 2]", result.toString());
     }
 
     /**
@@ -269,7 +265,7 @@ public class CtfTmfEventFieldTest {
     public void testParseField_struct() {
         Definition fieldDef = fixture.lookupDefinition(STRUCT);
         CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
-        assertEquals("test=[str=two, int=02]", result.toString());
+        assertEquals("test=[str=two, int=2]", result.toString());
     }
 
     /**
@@ -280,7 +276,7 @@ public class CtfTmfEventFieldTest {
     public void testParseField_array_struct() {
         Definition fieldDef = fixture.lookupArray(ARRAY_STRUCT);
         CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
-        assertEquals("test=[[str=two, int=02], [str=two, int=02]]", result.toString());
+        assertEquals("test=[[str=two, int=2], [str=two, int=2]]", result.toString());
     }
 
     /**
index 09caf357d516c8df6afbac97f56ae3c0a4b90615..846ed9420594e419e689e340545e6a4e9228cafc 100644 (file)
@@ -59,7 +59,9 @@ public class CtfTmfEventTest {
 
     /**
      * Perform pre-test initialization.
-     * @throws CTFReaderException error
+     *
+     * @throws CTFReaderException
+     *             error
      */
     @Before
     public void setUp() throws CTFReaderException {
@@ -218,7 +220,7 @@ public class CtfTmfEventTest {
         assertNotNull(nullEvent);
         assertEquals(-1, nullEvent.getCPU());
         assertEquals("Empty CTF event", nullEvent.getType().getName());
-        assertEquals("No stream", nullEvent.getReference());
+        assertNull(nullEvent.getReference());
         assertArrayEquals(new ITmfEventField[0], nullEvent.getContent().getFields());
         assertEquals(-1L, nullEvent.getID());
         assertEquals(-1L, nullEvent.getTimestamp().getValue());
index f08690fe06915891cb9aaafb41e9f1fa4cb35a6c..969c701fa8c6d5737900f42018e70a3e3e912025 100644 (file)
 
 package org.eclipse.linuxtools.tmf.ctf.core;
 
+import java.util.ArrayList;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Set;
 
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.linuxtools.ctf.core.event.CTFCallsite;
+import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
 import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration;
+import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
 import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
 import org.eclipse.linuxtools.tmf.core.event.ITmfCustomAttributes;
 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
@@ -43,7 +48,6 @@ public class CtfTmfEvent extends TmfEvent
     // Constants
     // ------------------------------------------------------------------------
 
-    static final String NO_STREAM = "No stream"; //$NON-NLS-1$
     private static final String EMPTY_CTF_EVENT_NAME = "Empty CTF event"; //$NON-NLS-1$
 
     // ------------------------------------------------------------------------
@@ -53,7 +57,10 @@ public class CtfTmfEvent extends TmfEvent
     private final int fSourceCPU;
     private final long fTypeId;
     private final String fEventName;
-    private final IEventDeclaration fDeclaration;
+    private final IEventDeclaration fEventDeclaration;
+    @NonNull
+    private final EventDefinition fEvent;
+    private ITmfEventField fContent;
 
     // ------------------------------------------------------------------------
     // Constructors
@@ -63,21 +70,23 @@ public class CtfTmfEvent extends TmfEvent
      * Constructor used by {@link CtfTmfEventFactory#createEvent}
      */
     CtfTmfEvent(CtfTmfTrace trace, long rank, CtfTmfTimestamp timestamp,
-            ITmfEventField content, String fileName, int cpu,
-            IEventDeclaration declaration) {
+            String fileName, int cpu, IEventDeclaration declaration, @NonNull EventDefinition eventDefinition) {
         super(trace,
                 rank,
                 timestamp,
                 String.valueOf(cpu), // Source
-                null, // Event type. We don't use TmfEvent's field here, we re-implement getType()
-                content,
+                null, // Event type. We don't use TmfEvent's field here, we
+                      // re-implement getType()
+                null, // Content handled with a lazy loaded re-implemented in
+                      // getContent()
                 fileName // Reference
         );
 
-        fDeclaration = declaration;
+        fEventDeclaration = declaration;
         fSourceCPU = cpu;
         fTypeId = declaration.getId();
         fEventName = declaration.getName();
+        fEvent = eventDefinition;
 
     }
 
@@ -96,11 +105,12 @@ public class CtfTmfEvent extends TmfEvent
                 null,
                 null,
                 new TmfEventField("", null, new CtfTmfEventField[0]), //$NON-NLS-1$
-                NO_STREAM);
+                null);
         fSourceCPU = -1;
         fTypeId = -1;
         fEventName = EMPTY_CTF_EVENT_NAME;
-        fDeclaration = null;
+        fEventDeclaration = null;
+        fEvent = EventDefinition.NULL_EVENT;
     }
 
     // ------------------------------------------------------------------------
@@ -152,10 +162,10 @@ public class CtfTmfEvent extends TmfEvent
      */
     @Override
     public Set<String> listCustomAttributes() {
-        if (fDeclaration == null) {
+        if (fEventDeclaration == null) {
             return new HashSet<>();
         }
-        return fDeclaration.getCustomAttributes();
+        return fEventDeclaration.getCustomAttributes();
     }
 
     /**
@@ -163,10 +173,10 @@ public class CtfTmfEvent extends TmfEvent
      */
     @Override
     public String getCustomAttribute(String name) {
-        if (fDeclaration == null) {
+        if (fEventDeclaration == null) {
             return null;
         }
-        return fDeclaration.getCustomAttribute(name);
+        return fEventDeclaration.getCustomAttribute(name);
     }
 
     /**
@@ -211,4 +221,41 @@ public class CtfTmfEvent extends TmfEvent
         return getCustomAttribute(CtfConstants.MODEL_URI_KEY);
     }
 
+    @Override
+    public synchronized ITmfEventField getContent() {
+        if (fContent == null) {
+            fContent = new TmfEventField(
+                    ITmfEventField.ROOT_FIELD_ID, null, parseFields(fEvent));
+        }
+        return fContent;
+    }
+
+    /**
+     * Extract the field information from the structDefinition haze-inducing
+     * mess, and put them into something ITmfEventField can cope with.
+     */
+    private static CtfTmfEventField[] parseFields(@NonNull EventDefinition eventDef) {
+        List<CtfTmfEventField> fields = new ArrayList<>();
+
+        StructDefinition structFields = eventDef.getFields();
+        if (structFields != null) {
+            if (structFields.getFieldNames() != null) {
+                for (String curFieldName : structFields.getFieldNames()) {
+                    fields.add(CtfTmfEventField.parseField(structFields.getDefinition(curFieldName), curFieldName));
+                }
+            }
+        }
+        /* Add context information as CtfTmfEventField */
+        StructDefinition structContext = eventDef.getContext();
+        if (structContext != null) {
+            for (String contextName : structContext.getFieldNames()) {
+                /* Prefix field name */
+                String curContextName = CtfConstants.CONTEXT_FIELD_PREFIX + contextName;
+                fields.add(CtfTmfEventField.parseField(structContext.getDefinition(contextName), curContextName));
+            }
+        }
+
+        return fields.toArray(new CtfTmfEventField[fields.size()]);
+    }
+
 }
index 93ba0ede7c1f80a9051bef423dc75b892e7d5cf6..e04b651bcb747654ad47ca9a9d874fcc3eaa110d 100644 (file)
 
 package org.eclipse.linuxtools.tmf.ctf.core;
 
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-
 import org.eclipse.linuxtools.ctf.core.CTFStrings;
 import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
 import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
-import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
-import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
-import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
 
@@ -38,6 +31,8 @@ import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
  */
 public final class CtfTmfEventFactory {
 
+    private static final String NO_STREAM = "No stream"; //$NON-NLS-1$
+
     /**
      * Don't let anyone instantiate this class.
      */
@@ -65,15 +60,12 @@ public final class CtfTmfEventFactory {
 
         int sourceCPU = eventDef.getCPU();
 
-        ITmfEventField content = new TmfEventField(
-                ITmfEventField.ROOT_FIELD_ID, null, parseFields(eventDef));
-
-        String reference = fileName == null ? CtfTmfEvent.NO_STREAM : fileName;
+        String reference = fileName == null ? NO_STREAM : fileName;
 
         /* Handle the special case of lost events */
         if (eventDecl.getName().equals(CTFStrings.LOST_EVENT_NAME)) {
-            Definition nbLostEventsDef = eventDef.getFields().getDefinitions().get(CTFStrings.LOST_EVENTS_FIELD);
-            Definition durationDef = eventDef.getFields().getDefinitions().get(CTFStrings.LOST_EVENTS_DURATION);
+            Definition nbLostEventsDef = eventDef.getFields().getDefinition(CTFStrings.LOST_EVENTS_FIELD);
+            Definition durationDef = eventDef.getFields().getDefinition(CTFStrings.LOST_EVENTS_DURATION);
             if (!(nbLostEventsDef instanceof IntegerDefinition) || !(durationDef instanceof IntegerDefinition)) {
                 /*
                  * One or both of these fields doesn't exist, or is not of the
@@ -89,12 +81,12 @@ public final class CtfTmfEventFactory {
 
             CtfTmfLostEvent lostEvent = new CtfTmfLostEvent(originTrace,
                     ITmfContext.UNKNOWN_RANK,
-                    content,
                     reference, // filename
                     sourceCPU,
                     eventDecl,
                     new TmfTimeRange(timestamp, timestampEnd),
-                    nbLostEvents);
+                    nbLostEvents,
+                    eventDef);
             return lostEvent;
         }
 
@@ -103,10 +95,10 @@ public final class CtfTmfEventFactory {
                 originTrace,
                 ITmfContext.UNKNOWN_RANK,
                 timestamp,
-                content,
                 reference, // filename
                 sourceCPU,
-                eventDecl);
+                eventDecl,
+                eventDef);
         return event;
     }
 
@@ -125,33 +117,5 @@ public final class CtfTmfEventFactory {
         return nullEvent;
     }
 
-    /**
-     * Extract the field information from the structDefinition haze-inducing
-     * mess, and put them into something ITmfEventField can cope with.
-     */
-    private static CtfTmfEventField[] parseFields(EventDefinition eventDef) {
-        List<CtfTmfEventField> fields = new ArrayList<>();
-
-        StructDefinition structFields = eventDef.getFields();
-        for (Map.Entry<String, Definition> entry : structFields.getDefinitions().entrySet()) {
-            String curFieldName = entry.getKey();
-            Definition curFieldDef = entry.getValue();
-            CtfTmfEventField curField = CtfTmfEventField.parseField(curFieldDef, curFieldName);
-            fields.add(curField);
-        }
-
-        /* Add context information as CtfTmfEventField */
-        StructDefinition structContext = eventDef.getContext();
-        if (structContext != null) {
-            for (Map.Entry<String, Definition> entry : structContext.getDefinitions().entrySet()) {
-                /* Prefix field name */
-                String curContextName = CtfConstants.CONTEXT_FIELD_PREFIX + entry.getKey();
-                Definition curContextDef = entry.getValue();
-                CtfTmfEventField curContext = CtfTmfEventField.parseField(curContextDef, curContextName);
-                fields.add(curContext);
-            }
-        }
 
-        return fields.toArray(new CtfTmfEventField[fields.size()]);
-    }
 }
index 1640db6e7a07e4265bc62cd432b34a644455d58a..4fa6b557ba9c4e6fd762d3996e216dd86c71fc31 100644 (file)
@@ -20,7 +20,6 @@ package org.eclipse.linuxtools.tmf.ctf.core;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
-import java.util.Map.Entry;
 
 import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
@@ -111,13 +110,13 @@ public abstract class CtfTmfEventField extends TmfEventField {
 
             } else {
                 /* Arrays of elements of any other type */
-                Definition[] definitions = arrayDef.getDefinitions();
-                CtfTmfEventField[] elements = new CtfTmfEventField[definitions.length];
+                List<Definition> definitions = arrayDef.getDefinitions();
+                CtfTmfEventField[] elements = new CtfTmfEventField[definitions.size()];
 
                 /* Parse the elements of the array. */
-                for (int i = 0; i < definitions.length; i++) {
+                for (int i = 0; i < definitions.size(); i++) {
                     CtfTmfEventField curField = CtfTmfEventField.parseField(
-                            definitions[i], fieldName + '[' + i + ']');
+                            definitions.get(i), fieldName + '[' + i + ']');
                     elements[i] = curField;
                 }
 
@@ -130,7 +129,7 @@ public abstract class CtfTmfEventField extends TmfEventField {
             if (seqDef.getLength() == 0) {
                 /* Some sequences have length = 0. Simply use an empty string */
                 field = new CTFStringField(fieldName, ""); //$NON-NLS-1$
-            } else if (seqDef.isString()) {
+            } else if (seqDecl.isString()) {
                 /* Interpret this sequence as a String */
                 field = new CTFStringField(fieldName, seqDef.toString());
             } else if (seqDecl.getElementType() instanceof IntegerDeclaration) {
@@ -149,16 +148,10 @@ public abstract class CtfTmfEventField extends TmfEventField {
         } else if (fieldDef instanceof StructDefinition) {
             StructDefinition strDef = (StructDefinition) fieldDef;
 
-            String curFieldName = null;
-            Definition curFieldDef;
-            CtfTmfEventField curField;
             List<ITmfEventField> list = new ArrayList<>();
             /* Recursively parse the fields */
-            for (Entry<String, Definition> entry : strDef.getDefinitions().entrySet()) {
-                curFieldName = entry.getKey();
-                curFieldDef = entry.getValue();
-                curField = CtfTmfEventField.parseField(curFieldDef, curFieldName);
-                list.add(curField);
+            for (String curFieldName : strDef.getFieldNames()) {
+                list.add(CtfTmfEventField.parseField(strDef.getDefinition(curFieldName), curFieldName));
             }
             field = new CTFStructField(fieldName, list.toArray(new CtfTmfEventField[list.size()]));
 
@@ -166,7 +159,7 @@ public abstract class CtfTmfEventField extends TmfEventField {
             VariantDefinition varDef = (VariantDefinition) fieldDef;
 
             String curFieldName = varDef.getCurrentFieldName();
-            Definition curFieldDef = varDef.getDefinitions().get(curFieldName);
+            Definition curFieldDef = varDef.getCurrentField();
             if (curFieldDef != null) {
                 CtfTmfEventField subField = CtfTmfEventField.parseField(curFieldDef, curFieldName);
                 field = new CTFVariantField(fieldName, subField);
index 1e4e433d686169ee8724eedc4f7eaf6159671072..c699de0aeb73db10d21794d0bb2069fd4cf86832 100644 (file)
@@ -12,8 +12,9 @@
 
 package org.eclipse.linuxtools.tmf.ctf.core;
 
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
 import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration;
-import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
 import org.eclipse.linuxtools.tmf.core.event.ITmfLostEvent;
 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
 
@@ -50,17 +51,17 @@ public class CtfTmfLostEvent extends CtfTmfEvent implements ITmfLostEvent {
      */
     CtfTmfLostEvent(CtfTmfTrace trace,
             long rank,
-            ITmfEventField content,
             String fileName,
             int cpu,
             IEventDeclaration declaration,
             TmfTimeRange timeRange,
-            long nbLost) {
+            long nbLost,
+            @NonNull EventDefinition def) {
         /*
          * Only the factory should call this method, the case to
          * (CtfTmfTimestamp) should be safe.
          */
-        super(trace, rank, (CtfTmfTimestamp) timeRange.getStartTime(), content, fileName, cpu, declaration);
+        super(trace, rank, (CtfTmfTimestamp) timeRange.getStartTime(), fileName, cpu, declaration, def);
         fTimeRange = timeRange;
         fNbLost = nbLost;
     }
This page took 0.133102 seconds and 5 git commands to generate.