Introduce ILexicalScope, an interface to make a scope.
May introduce performance regression, to be monitored
Change-Id: I1c0838e8190a665db8a2aaf99e974348d4f98da4
Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/44607
Reviewed-by: Hudson CI
Reviewed-by: Patrick Tasse <patrick.tasse@gmail.com>
Tested-by: Patrick Tasse <patrick.tasse@gmail.com>
package org.eclipse.tracecompass.ctf.core.tests.event;
-import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.tracecompass.ctf.core.event.EventDefinition;
-import org.eclipse.tracecompass.ctf.core.event.scope.LexicalScope;
+import org.eclipse.tracecompass.ctf.core.event.scope.ILexicalScope;
import org.eclipse.tracecompass.ctf.core.event.types.Definition;
import org.eclipse.tracecompass.ctf.core.event.types.Encoding;
import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration;
StructDeclaration pContextDec = new StructDeclaration(8);
- StructDefinition sContext = new StructDefinition(streamContextDec, null, LexicalScope.STREAM_PACKET_CONTEXT.toString(), sFieldNames, sDefs);
- StructDefinition eContext = new StructDefinition(eventContextDec, null, LexicalScope.STREAM_EVENT_CONTEXT.toString(), eFieldNames, eDefs);
- StructDefinition pContext = new StructDefinition(pContextDec, null, LexicalScope.FIELDS.toString(), Collections.EMPTY_LIST, new Definition[0]);
- StructDefinition fields = new StructDefinition(fDec, null, LexicalScope.FIELDS.toString(), fieldNames, fDefs);
+ StructDefinition sContext = new StructDefinition(streamContextDec, null, ILexicalScope.STREAM_PACKET_CONTEXT.getPath(), sFieldNames, sDefs);
+ StructDefinition eContext = new StructDefinition(eventContextDec, null, ILexicalScope.STREAM_EVENT_CONTEXT.getPath(), eFieldNames, eDefs);
+ StructDefinition pContext = new StructDefinition(pContextDec, null, ILexicalScope.FIELDS.getPath(), Collections.EMPTY_LIST, new Definition[0]);
+ StructDefinition fields = new StructDefinition(fDec, null, ILexicalScope.FIELDS.getPath(), fieldNames, fDefs);
fixture.add(new EventDefinition(eventDeclaration, null, 100, null, null, null, null));
fixture.add(new EventDefinition(eventDeclaration, null, 100, null, null, null, fields));
package org.eclipse.tracecompass.ctf.core.tests.scope;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import org.eclipse.tracecompass.ctf.core.event.scope.ILexicalScope;
import org.eclipse.tracecompass.ctf.core.event.scope.LexicalScope;
import org.junit.Test;
* Root test
*/
@Test
- public void testRoot(){
- LexicalScope scope = LexicalScope.ROOT;
+ public void testRoot() {
+ ILexicalScope scope = ILexicalScope.ROOT;
assertNotNull(scope);
}
* Test a more complex node
*/
@Test
- public void testComplexNode(){
- LexicalScope scope = LexicalScope.STREAM_EVENT_CONTEXT;
+ public void testComplexNode() {
+ ILexicalScope scope = ILexicalScope.STREAM_EVENT_CONTEXT;
assertEquals("context", scope.getName());
- assertEquals("stream.event.context", scope.toString());
+ assertEquals("stream.event.context", scope.getPath());
+ }
+
+ /**
+ * Test that getChild returns the same items for event headers
+ */
+ @Test
+ public void testEventHeaders() {
+ ILexicalScope child = ILexicalScope.ROOT.getChild("event");
+ assertNotNull(child);
+ ILexicalScope scope2 = child.getChild("header");
+ ILexicalScope scope3 = ILexicalScope.ROOT.getChild("event.header");
+ assertEquals(ILexicalScope.EVENT_HEADER, scope2);
+ assertEquals(ILexicalScope.EVENT_HEADER, scope3);
+ // they should be the same
+ assert (ILexicalScope.EVENT_HEADER == scope2);
+
+ assertNotNull(scope2);
+ ILexicalScope id = scope2.getChild("id");
+ assertNotNull(id);
+ assert (ILexicalScope.EVENT_HEADER_ID == id);
+ ILexicalScope ts = scope2.getChild("v.timestamp");
+ ILexicalScope v = scope2.getChild("v");
+ assert (ILexicalScope.EVENT_HEADER_V_TIMESTAMP == ts);
+ assert (ILexicalScope.EVENT_HEADER_V == v);
+ assertNotNull(v);
+ ILexicalScope ts2 = v.getChild("timestamp");
+ assert (ILexicalScope.EVENT_HEADER_V_TIMESTAMP == ts2);
+ assertNotNull(v);
+ id = v.getChild("id");
+ assert (ILexicalScope.EVENT_HEADER_V_ID == id);
+ assertNotNull(v);
+ ILexicalScope other = v.getChild("other");
+ assertNull(other);
+ }
+
+ /**
+ * Test that getChild returns the same items for event headers
+ */
+ @Test
+ public void testFields() {
+ ILexicalScope child = ILexicalScope.ROOT.getChild("fields");
+ assertNotNull(child);
+ ILexicalScope scope2 = child.getChild("_ret");
+ ILexicalScope scope3 = child.getChild("_tid");
+ ILexicalScope empty = child.getChild("other");
+
+ assertEquals(ILexicalScope.FIELDS_RET, scope2);
+ // they should be the same
+ assert (ILexicalScope.FIELDS_RET == scope2);
+
+ assertEquals(ILexicalScope.FIELDS_TID, scope3);
+ // they should be the same
+ assert (ILexicalScope.FIELDS_TID == scope2);
+
+ assertNull(empty);
+ }
+
+ /**
+ * Check contexts are not equals
+ */
+ @Test
+ public void testNotEquals() {
+ assertNotEquals(ILexicalScope.CONTEXT, ILexicalScope.EVENT);
+ LexicalScope context = new LexicalScope(ILexicalScope.CONTEXT, "context");
+ LexicalScope otherContext = new LexicalScope(ILexicalScope.CONTEXT, "context2");
+ assertNotEquals(context, otherContext);
+ assertNotEquals(context, null);
+ }
+
+ /**
+ * Test to strings
+ */
+ @Test
+ public void testGetPath() {
+ ILexicalScope child = ILexicalScope.ROOT.getChild("fields");
+ assertNotNull(child);
+ ILexicalScope scope2 = child.getChild("_ret");
+ assertNotNull(scope2);
+ assertEquals("fields._ret", scope2.getPath());
}
}
import org.eclipse.tracecompass.ctf.core.CTFReaderException;
import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer;
-import org.eclipse.tracecompass.ctf.core.event.scope.LexicalScope;
+import org.eclipse.tracecompass.ctf.core.event.scope.ILexicalScope;
import org.eclipse.tracecompass.ctf.core.event.types.Encoding;
import org.eclipse.tracecompass.ctf.core.event.types.EnumDeclaration;
import org.eclipse.tracecompass.ctf.core.event.types.FloatDeclaration;
bb.getByteBuffer().put(("Test").getBytes());
bb.getByteBuffer().put((byte) 0);
bb.getByteBuffer().put((byte) 0);
- StructDefinition sdef = sd.createDefinition(null, LexicalScope.PACKET_HEADER, bb);
+ StructDefinition sdef = sd.createDefinition(null, ILexicalScope.PACKET_HEADER, bb);
StreamInputPacketIndexEntry sipie = new StreamInputPacketIndexEntry(0, sdef, 128, 0);
assertNull(sipie.getTarget());
assertEquals(100, sipie.getTimestampBegin());
bb.getByteBuffer().put(("Test").getBytes());
bb.getByteBuffer().put((byte) 0);
bb.getByteBuffer().put((byte) 0);
- StructDefinition sdef = sd.createDefinition(null, LexicalScope.PACKET_HEADER, bb);
+ StructDefinition sdef = sd.createDefinition(null, ILexicalScope.PACKET_HEADER, bb);
StreamInputPacketIndexEntry sipie = new StreamInputPacketIndexEntry(0, sdef, 128, 0);
assertNull(sipie.getTarget());
assertEquals(100, sipie.getTimestampBegin());
bb.getByteBuffer().put(("Test").getBytes());
bb.getByteBuffer().put((byte) 0);
bb.getByteBuffer().put((byte) 0);
- StructDefinition sdef = sd.createDefinition(null, LexicalScope.PACKET_HEADER, bb);
+ StructDefinition sdef = sd.createDefinition(null, ILexicalScope.PACKET_HEADER, bb);
StreamInputPacketIndexEntry sipie = new StreamInputPacketIndexEntry(0, sdef, 128, 0);
assertNull(sipie.getTarget());
assertEquals(100, sipie.getTimestampBegin());
bb.getByteBuffer().put(("Test").getBytes());
bb.getByteBuffer().put((byte) 0);
bb.getByteBuffer().put((byte) 0);
- StructDefinition sdef = sd.createDefinition(null, LexicalScope.PACKET_HEADER, bb);
+ StructDefinition sdef = sd.createDefinition(null, ILexicalScope.PACKET_HEADER, bb);
StreamInputPacketIndexEntry sipie = new StreamInputPacketIndexEntry(0, sdef, 128, 0);
assertNull(sipie.getTarget());
assertEquals(Long.MIN_VALUE, sipie.getTimestampBegin());
bb.getByteBuffer().put(("Test66").getBytes());
bb.getByteBuffer().put((byte) 0);
bb.getByteBuffer().put((byte) 0);
- StructDefinition sdef = sd.createDefinition(null, LexicalScope.PACKET_HEADER, bb);
+ StructDefinition sdef = sd.createDefinition(null, ILexicalScope.PACKET_HEADER, bb);
StreamInputPacketIndexEntry sipie = new StreamInputPacketIndexEntry(0, sdef, 128, 0);
assertEquals(Long.MIN_VALUE, sipie.getTimestampBegin());
assertEquals(Long.MAX_VALUE, sipie.getTimestampEnd());
bb.getByteBuffer().putInt(0);
bb.getByteBuffer().putInt(66);
bb.getByteBuffer().putInt(300);
- StructDefinition sdef = sd.createDefinition(null, LexicalScope.PACKET_HEADER, bb);
+ StructDefinition sdef = sd.createDefinition(null, ILexicalScope.PACKET_HEADER, bb);
StreamInputPacketIndexEntry sipie = new StreamInputPacketIndexEntry(0, sdef, 128, 100);
assertEquals(Long.MIN_VALUE, sipie.getTimestampBegin());
assertEquals(Long.MAX_VALUE, sipie.getTimestampEnd());
@SuppressWarnings("null")
BitBuffer bb = new BitBuffer(ByteBuffer.allocate(128));
- StructDefinition sdef = sd.createDefinition(null, LexicalScope.PACKET_HEADER, bb);
+ StructDefinition sdef = sd.createDefinition(null, ILexicalScope.PACKET_HEADER, bb);
assertEquals(expectedResult, new StreamInputPacketIndexEntry(0, sdef, 10000, 0).toString());
}
}
\ No newline at end of file
*/
@Test
public void testGetPath() {
- String result = fixture.getScopePath().toString();
+ String result = fixture.getScopePath().getPath();
assertNotNull(result);
}
*/
@Test
public void testGetPath() {
- String result = fixture.getScopePath().toString();
+ String result = fixture.getScopePath().getPath();
assertNotNull(result);
}
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
+import org.eclipse.tracecompass.ctf.core.event.scope.ILexicalScope;
import org.eclipse.tracecompass.ctf.core.event.scope.LexicalScope;
import org.eclipse.tracecompass.ctf.core.event.types.Definition;
import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
// Getters/Setters/Predicates
// ------------------------------------------------------------------------
+ /**
+ * @since 1.0
+ */
@Override
- public LexicalScope getScopePath() {
+ public ILexicalScope getScopePath() {
String eventName = fDeclaration.getName();
if (eventName == null) {
return null;
}
- LexicalScope myScope = LexicalScope.EVENT.getChild(eventName);
+ ILexicalScope myScope = ILexicalScope.EVENT.getChild(eventName);
if (myScope == null) {
- myScope = new LexicalScope(LexicalScope.EVENT, eventName);
+ myScope = new LexicalScope(ILexicalScope.EVENT, eventName);
}
return myScope;
}
* @author Matthew Khouzam
*/
@NonNullByDefault
-public class EventHeaderScope extends LexicalScope {
+public final class EventHeaderScope extends LexicalScope {
- /**
- * Event header id string
- */
- public static final LexicalScope EVENT_HEADER_ID = new LexicalScope(EVENT_HEADER, "id"); //$NON-NLS-1$
-
- /**
- * Event header v as in variant string
- */
- public static final LexicalScope EVENT_HEADER_V = new EventHeaderVScope(EVENT_HEADER, "v"); //$NON-NLS-1$
/**
* The scope constructor
* @param name
* the name of the field
*/
- public EventHeaderScope(LexicalScope parent, String name) {
+ EventHeaderScope(ILexicalScope parent, String name) {
super(parent, name);
}
@Override
@Nullable
- public LexicalScope getChild(String name) {
+ public ILexicalScope getChild(String name) {
if (name.equals(EVENT_HEADER_ID.getName())) {
return EVENT_HEADER_ID;
}
}
@Override
- public String toString() {
+ public String getPath() {
return "event.header"; //$NON-NLS-1$
}
* @author Matthew Khouzam
*/
@NonNullByDefault
-public class EventHeaderVScope extends LexicalScope {
-
- /**
- * Packet header v id string
- */
- public static final LexicalScope PACKET_HEADER_V_ID = new LexicalScope(PACKET_HEADER, "id"); //$NON-NLS-1$
- /**
- * Packet header v timestamp string
- */
- public static final LexicalScope PACKET_HEADER_V_TIMESTAMP = new LexicalScope(PACKET_HEADER, "timestamp"); //$NON-NLS-1$
+public final class EventHeaderVScope extends LexicalScope {
/**
* The scope constructor
* @param name
* the name of the field
*/
- public EventHeaderVScope(LexicalScope parent, String name) {
+ EventHeaderVScope(ILexicalScope parent, String name) {
super(parent, name);
}
@Override
@Nullable
- public LexicalScope getChild(String name) {
- if (name.equals(PACKET_HEADER_V_TIMESTAMP.getName())) {
- return PACKET_HEADER_V_TIMESTAMP;
+ public ILexicalScope getChild(String name) {
+ if (name.equals(EVENT_HEADER_V_TIMESTAMP.getName())) {
+ return EVENT_HEADER_V_TIMESTAMP;
}
- if (name.equals(PACKET_HEADER_V_ID.getName())) {
- return PACKET_HEADER_V_ID;
+ if (name.equals(EVENT_HEADER_V_ID.getName())) {
+ return EVENT_HEADER_V_ID;
}
return super.getChild(name);
}
* @author Matthew Khouzam
*/
@NonNullByDefault
-public class FieldsScope extends LexicalScope {
-
- /**
- * ret field
- */
- public static final LexicalScope FIELDS_RET = new LexicalScope(FIELDS, "_ret"); //$NON-NLS-1$
-
- /**
- * tid field
- */
- public static final LexicalScope FIELDS_TID = new LexicalScope(FIELDS, "_tid"); //$NON-NLS-1$
+public final class FieldsScope extends LexicalScope {
/**
* The scope constructor
* @param name
* the name of the field
*/
- public FieldsScope(LexicalScope parent, String name) {
+ FieldsScope(ILexicalScope parent, String name) {
super(parent, name);
}
@Override
@Nullable
- public LexicalScope getChild(String name) {
+ public ILexicalScope getChild(String name) {
if (name.equals(FIELDS_RET.getName())) {
return FIELDS_RET;
}
* Gets the path in a C style for the scope.
*
* @return the path
+ * @since 1.0
*/
- LexicalScope getScopePath();
+ ILexicalScope getScopePath();
/**
* Looks up in this definition scope.
--- /dev/null
+/*******************************************************************************
+ * Copyright (c) 2015 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.tracecompass.ctf.core.event.scope;
+
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+
+/**
+ * @since 1.0
+ */
+@NonNullByDefault
+public interface ILexicalScope {
+ /**
+ * Empty string
+ */
+ ILexicalScope ROOT = new RootScope();
+
+ /**
+ * Trace string
+ */
+ ILexicalScope TRACE = new LexicalScope(ROOT, "trace"); //$NON-NLS-1$
+ /**
+ * Env string
+ */
+ ILexicalScope ENV = new LexicalScope(ROOT, "env"); //$NON-NLS-1$
+ /**
+ * Stream string
+ */
+ LexicalScope STREAM = new LexicalScope(ROOT, "stream"); //$NON-NLS-1$
+ /**
+ * Event string
+ */
+ LexicalScope EVENT = new LexicalScope(ROOT, "event"); //$NON-NLS-1$
+ /**
+ * Variant string
+ */
+ ILexicalScope VARIANT = new LexicalScope(ROOT, "variant"); //$NON-NLS-1$
+ /**
+ * packet string
+ */
+ LexicalScope PACKET = new LexicalScope(ROOT, "packet"); //$NON-NLS-1$
+ /**
+ * Packet header string
+ */
+ LexicalScope PACKET_HEADER = new PacketHeaderScope();
+
+ /**
+ * Packet header v id string
+ */
+ ILexicalScope EVENT_HEADER_V_ID = new LexicalScope(PACKET_HEADER, "id"); //$NON-NLS-1$
+ /**
+ * Packet header v timestamp string
+ */
+ ILexicalScope EVENT_HEADER_V_TIMESTAMP = new LexicalScope(PACKET_HEADER, "timestamp"); //$NON-NLS-1$
+
+ /**
+ * Stream packet scope
+ */
+ LexicalScope STREAM_PACKET = new LexicalScope(STREAM, "packet"); //$NON-NLS-1$
+ /**
+ * Stream Packet header string
+ */
+ ILexicalScope STREAM_PACKET_CONTEXT = new LexicalScope(STREAM_PACKET, "context"); //$NON-NLS-1$
+ /**
+ * Trace packet scope
+ */
+ LexicalScope TRACE_PACKET = new LexicalScope(TRACE, "packet"); //$NON-NLS-1$
+ /**
+ * Stream event scope
+ */
+ LexicalScope STREAM_EVENT = new LexicalScope(STREAM, "event"); //$NON-NLS-1$
+ /**
+ * Trace packet header string
+ */
+ ILexicalScope TRACE_PACKET_HEADER = new LexicalScope(TRACE_PACKET, "header"); //$NON-NLS-1$
+ /**
+ * Stream event context
+ */
+ ILexicalScope STREAM_EVENT_CONTEXT = new LexicalScope(STREAM_EVENT, "context"); //$NON-NLS-1$
+ /**
+ * Stream event header
+ */
+ ILexicalScope STREAM_EVENT_HEADER = new LexicalScope(STREAM_EVENT, "header"); //$NON-NLS-1$
+ /**
+ * Context of an event
+ */
+ LexicalScope CONTEXT = new LexicalScope(ROOT, "context"); //$NON-NLS-1$
+ /**
+ * Event Header scope
+ */
+ ILexicalScope EVENT_HEADER = new EventHeaderScope(EVENT, "header"); //$NON-NLS-1$
+
+ /**
+ * Event header id string
+ */
+ ILexicalScope EVENT_HEADER_ID = new LexicalScope(EVENT_HEADER, "id"); //$NON-NLS-1$
+
+ /**
+ * Event header v as in variant string
+ */
+ ILexicalScope EVENT_HEADER_V = new EventHeaderVScope(EVENT_HEADER, "v"); //$NON-NLS-1$
+
+ /**
+ * Fields in an event
+ */
+ ILexicalScope FIELDS = new FieldsScope(ROOT, "fields"); //$NON-NLS-1$
+
+ /**
+ * ret field
+ */
+ ILexicalScope FIELDS_RET = new LexicalScope(FIELDS, "_ret"); //$NON-NLS-1$
+
+ /**
+ * tid field
+ */
+ ILexicalScope FIELDS_TID = new LexicalScope(FIELDS, "_tid"); //$NON-NLS-1$
+
+ /**
+ * Get the name
+ *
+ * @return the name
+ */
+ String getName();
+
+ /**
+ * Gets a child of a given name
+ *
+ * @param name
+ * the child
+ * @return the scope, can be null
+ */
+ @Nullable
+ ILexicalScope getChild(String name);
+
+ // -------------------------------------------------------------------------
+ // helpers
+ // -------------------------------------------------------------------------
+
+ /**
+ * Adds a child lexical scope
+ *
+ * @param name
+ * the name of the child
+ * @param child
+ * the child
+ */
+ void addChild(String name, ILexicalScope child);
+
+ /**
+ * Get the path of the scope
+ *
+ * @return the path of the scope
+ */
+ String getPath();
+
+
+}
\ No newline at end of file
package org.eclipse.tracecompass.ctf.core.event.scope;
-import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
-
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
-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
*/
@NonNullByDefault
-public class LexicalScope implements Comparable<LexicalScope> {
- /**
- * Empty string
- */
- public static final LexicalScope ROOT = new RootScope();
-
- /**
- * Trace string
- */
- public static final LexicalScope TRACE = new LexicalScope(ROOT, "trace"); //$NON-NLS-1$
-
- /**
- * Env string
- */
- public static final LexicalScope ENV = new LexicalScope(ROOT, "env"); //$NON-NLS-1$
-
- /**
- * Stream string
- */
- public static final LexicalScope STREAM = new LexicalScope(ROOT, "stream"); //$NON-NLS-1$
-
- /**
- * Event string
- */
- public static final LexicalScope EVENT = new LexicalScope(ROOT, "event"); //$NON-NLS-1$
-
- /**
- * Variant string
- */
- public static final LexicalScope VARIANT = new LexicalScope(ROOT, "variant"); //$NON-NLS-1$
-
- /**
- * packet string
- */
- public static final LexicalScope PACKET = new LexicalScope(ROOT, "packet"); //$NON-NLS-1$
-
- /**
- * Packet header string
- */
- public static final LexicalScope PACKET_HEADER = new PacketHeaderScope();
-
- /**
- * Stream packet scope
- */
- public static final LexicalScope STREAM_PACKET = new LexicalScope(STREAM, "packet"); //$NON-NLS-1$
-
- /**
- * Stream Packet header string
- */
- public static final LexicalScope STREAM_PACKET_CONTEXT = new LexicalScope(STREAM_PACKET, "context"); //$NON-NLS-1$
-
- /**
- * Trace packet scope
- */
- public static final LexicalScope TRACE_PACKET = new LexicalScope(TRACE, "packet"); //$NON-NLS-1$
-
- /**
- * Stream event scope
- */
- public static final LexicalScope STREAM_EVENT = new LexicalScope(STREAM, "event"); //$NON-NLS-1$
-
- /**
- * Trace packet header string
- */
- public static final LexicalScope TRACE_PACKET_HEADER = new LexicalScope(TRACE_PACKET, "header"); //$NON-NLS-1$
-
- /**
- * Stream event context
- */
- public static final LexicalScope STREAM_EVENT_CONTEXT = new LexicalScope(STREAM_EVENT, "context"); //$NON-NLS-1$
-
- /**
- * Stream event header
- */
- public static final LexicalScope STREAM_EVENT_HEADER = new LexicalScope(STREAM_EVENT, "header"); //$NON-NLS-1$
-
- /**
- * Event header
- */
- public static final LexicalScope EVENT_HEADER = new EventHeaderScope(EVENT, "header"); //$NON-NLS-1$
-
- /**
- * Fields in an event
- */
- public static final LexicalScope FIELDS = new FieldsScope(ROOT, "fields"); //$NON-NLS-1$
-
- /**
- * Context of an event
- */
- public static final LexicalScope CONTEXT = new LexicalScope(ROOT, "context"); //$NON-NLS-1$
-
- /**
- * Sorted list of parent paths
- */
- public static final LexicalScope[] PARENT_PATHS = {
- ROOT,
- CONTEXT,
- FIELDS,
- PACKET_HEADER,
- STREAM_EVENT_CONTEXT,
- STREAM_EVENT_HEADER,
- STREAM_PACKET_CONTEXT,
- TRACE_PACKET_HEADER
- };
-
+public class LexicalScope implements ILexicalScope {
private int hash = 0;
private final String fName;
private final String fPath;
- private final Map<String, LexicalScope> fChildren = new ConcurrentHashMap<>();
+ private final Map<String, ILexicalScope> fChildren = new ConcurrentHashMap<>();
+
+ /**
+ * Hidden constructor for the root node only
+ *
+ * @since 1.0
+ */
+ protected LexicalScope() {
+ fPath = ""; //$NON-NLS-1$
+ fName = ""; //$NON-NLS-1$
+ }
/**
* The scope constructor
* The parent node, can be null, but shouldn't
* @param name
* the name of the field
+ * @since 1.0
*/
- public LexicalScope(@Nullable LexicalScope parent, String name) {
+ public LexicalScope(ILexicalScope parent, String name) {
fName = name;
- if (parent != null) {
- @NonNull String pathString = checkNotNull(Joiner.on('.').skipNulls().join(parent.fPath, parent.getName()));
- /*
- * if joiner return null, we get an NPE... so we won't assign fPath
- * to null
- */
- if (pathString.startsWith(".")) { //$NON-NLS-1$
- /*
- * substring throws an exception or returns a string, it won't
- * return null
- */
- pathString = checkNotNull(pathString.substring(1));
- }
- fPath = pathString;
- parent.addChild(fName, this);
- } else {
- fPath = ""; //$NON-NLS-1$
- }
+ fPath = parent.getPath().isEmpty() ? fName : parent.getPath() + '.' + fName;
+ parent.addChild(name, this);
}
/**
- * Adds a child lexical scope
- *
- * @param name
- * the name of the child
- * @param child
- * the child
+ * @since 1.0
*/
- private void addChild(String name, LexicalScope child) {
+ @Override
+ public void addChild(String name, ILexicalScope child) {
fChildren.put(name, child);
}
- /**
- * Get the name
- *
- * @return the name
- */
+ @Override
public String getName() {
return fName;
}
/**
- * Gets a child of a given name
- *
- * @param name
- * the child
- * @return the scope, can be null
+ * @since 1.0
*/
- @Nullable
- public LexicalScope getChild(String name) {
+ @Override
+ public @Nullable ILexicalScope getChild(String name) {
return fChildren.get(name);
}
+ /**
+ * @since 1.0
+ */
@Override
- public String toString() {
- return (fPath.isEmpty() ? fName : fPath + '.' + fName);
+ public String getPath() {
+ return fPath;
}
+ // for debugging purposes
@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;
+ public String toString() {
+ return getPath();
}
@Override
}
@Override
- public String toString() {
+ public String getPath() {
return "packet.header"; //$NON-NLS-1$
}
* @author Matthew Khouzam
*/
@NonNullByDefault
-public class RootScope extends LexicalScope {
+public final class RootScope extends LexicalScope {
/**
* The scope constructor
*/
public RootScope() {
- super(null, ""); //$NON-NLS-1$
+ super();
}
@Override
@Nullable
- public LexicalScope getChild(String name) {
+ public ILexicalScope getChild(String name) {
/*
* This happens ~40 % of the time
*/
- if (name.equals(EVENT_HEADER.toString())) {
+ if (name.equals(EVENT_HEADER.getPath())) {
return EVENT_HEADER;
}
/*
* This happens ~30 % of the time
*/
- if (name.equals(FIELDS.toString())) {
+ if (name.equals(FIELDS.getPath())) {
return FIELDS;
}
/*
* This happens ~30 % of the time
*/
- if (name.equals(CONTEXT.toString())) {
+ if (name.equals(CONTEXT.getPath())) {
return CONTEXT;
}
/*
* This happens ~1 % of the time
*/
- if (name.equals(PACKET_HEADER.toString())) {
+ if (name.equals(PACKET_HEADER.getPath())) {
return PACKET_HEADER;
}
return super.getChild(name);
import org.eclipse.tracecompass.ctf.core.CTFReaderException;
import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer;
import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
+import org.eclipse.tracecompass.ctf.core.event.scope.ILexicalScope;
import org.eclipse.tracecompass.ctf.core.event.scope.LexicalScope;
/**
*/
public abstract class Declaration implements IDeclaration {
+ /**
+ * @since 1.0
+ */
@Override
- public LexicalScope getPath(IDefinitionScope definitionScope, @NonNull String fieldName) {
+ public ILexicalScope getPath(IDefinitionScope definitionScope, @NonNull String fieldName) {
if (definitionScope != null) {
- final LexicalScope parentPath = definitionScope.getScopePath();
+ final ILexicalScope parentPath = definitionScope.getScopePath();
if (parentPath != null) {
- LexicalScope myScope = parentPath.getChild(fieldName);
+ ILexicalScope myScope = parentPath.getChild(fieldName);
if (myScope == null) {
myScope = new LexicalScope(parentPath, fieldName);
}
return myScope;
}
}
- LexicalScope child = LexicalScope.ROOT.getChild(fieldName);
+ ILexicalScope child = ILexicalScope.ROOT.getChild(fieldName);
if (child != null) {
return child;
}
- return new LexicalScope(LexicalScope.ROOT, fieldName);
+ return new LexicalScope(ILexicalScope.ROOT, fieldName);
}
/**
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
-import org.eclipse.tracecompass.ctf.core.event.scope.LexicalScope;
+import org.eclipse.tracecompass.ctf.core.event.scope.ILexicalScope;
/**
* A CTF definition
private final String fFieldName;
/** The complete path of this field */
- private final @NonNull LexicalScope fPath;
+ private final @NonNull ILexicalScope fPath;
private final IDefinitionScope fDefinitionScope;
*
* @param scope
* the scope
+ * @since 1.0
*/
- public Definition(@NonNull IDeclaration declaration, IDefinitionScope definitionScope, @NonNull String fieldName, @NonNull LexicalScope scope) {
+ public Definition(@NonNull IDeclaration declaration, IDefinitionScope definitionScope, @NonNull String fieldName, @NonNull ILexicalScope scope) {
fDeclaration = declaration;
fDefinitionScope = definitionScope;
fFieldName = fieldName;
return fFieldName;
}
+ /**
+ * @since 1.0
+ */
@Override
- public LexicalScope getScopePath() {
+ public ILexicalScope getScopePath() {
return fPath;
}
@Override
public String toString() {
- return fPath.toString() + '[' + Integer.toHexString(hashCode()) + ']';
+ return fPath.getPath() + '[' + Integer.toHexString(hashCode()) + ']';
}
}
import org.eclipse.tracecompass.ctf.core.CTFReaderException;
import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer;
import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
-import org.eclipse.tracecompass.ctf.core.event.scope.LexicalScope;
+import org.eclipse.tracecompass.ctf.core.event.scope.ILexicalScope;
/**
* A CTF data type declaration.
* @param fieldName
* the name of the definition
* @return the path of the definition
+ * @since 1.0
*/
- @NonNull LexicalScope getPath(IDefinitionScope definitionScope, @NonNull String fieldName);
+ @NonNull ILexicalScope getPath(IDefinitionScope definitionScope, @NonNull String fieldName);
/**
* The minimum alignment. if the field is 32 bits, the definition will pad
package org.eclipse.tracecompass.ctf.core.event.types;
import org.eclipse.jdt.annotation.NonNullByDefault;
-import org.eclipse.tracecompass.ctf.core.event.scope.LexicalScope;
+import org.eclipse.tracecompass.ctf.core.event.scope.ILexicalScope;
/**
* Interface for data definitions. A definition is when a value is given to a
* Get the complete path of this field.
*
* @return The path
+ * @since 1.0
*/
- LexicalScope getScopePath();
+ ILexicalScope getScopePath();
/**
* Get the declaration of this definition
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
-import org.eclipse.tracecompass.ctf.core.event.scope.LexicalScope;
+import org.eclipse.tracecompass.ctf.core.event.scope.ILexicalScope;
/**
* Scoped defintion. a defintion where you can lookup various datatypes
* the field name
* @param scope
* the lexical scope
+ * @since 1.0
*/
- public ScopedDefinition(StructDeclaration declaration, @Nullable IDefinitionScope definitionScope, String fieldName, LexicalScope scope) {
+ public ScopedDefinition(StructDeclaration declaration, @Nullable IDefinitionScope definitionScope, String fieldName, ILexicalScope scope) {
super(declaration, definitionScope, fieldName, scope);
}
import org.eclipse.tracecompass.ctf.core.CTFReaderException;
import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer;
import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
-import org.eclipse.tracecompass.ctf.core.event.scope.LexicalScope;
+import org.eclipse.tracecompass.ctf.core.event.scope.ILexicalScope;
/**
* A CTF structure declaration.
* @return a reference to the definition
* @throws CTFReaderException
* error in reading
+ * @since 1.0
*/
public StructDefinition createDefinition(IDefinitionScope definitionScope,
- LexicalScope fieldScope, @NonNull BitBuffer input) throws CTFReaderException {
+ ILexicalScope fieldScope, @NonNull BitBuffer input) throws CTFReaderException {
alignRead(input);
final Definition[] myFields = new Definition[fFieldMap.size()];
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
-import org.eclipse.tracecompass.ctf.core.event.scope.LexicalScope;
+import org.eclipse.tracecompass.ctf.core.event.scope.ILexicalScope;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
* the list of fields
* @param definitions
* the definitions
+ * @since 1.0
*/
public StructDefinition(@NonNull StructDeclaration declaration,
- IDefinitionScope definitionScope, @NonNull LexicalScope scope,
+ IDefinitionScope definitionScope, @NonNull ILexicalScope scope,
@NonNull String structFieldName, @NonNull Iterable<String> fieldNames, Definition[] definitions) {
super(declaration, definitionScope, structFieldName, scope);
fFieldNames = ImmutableList.copyOf(fieldNames);
fDeclarationToPopulate = fFields.get(varFieldName);
if (fDeclarationToPopulate == null) {
throw new CTFReaderException("Unknown enum selector for variant " + //$NON-NLS-1$
- definitionScope.getScopePath().toString());
+ definitionScope.getScopePath().getPath());
}
Definition fieldValue = fDeclarationToPopulate.createDefinition(definitionScope, fieldName, input);
return new VariantDefinition(this, definitionScope, varFieldName, fieldName, fieldValue);
import org.eclipse.tracecompass.ctf.core.CTFReaderException;
import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer;
import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
+import org.eclipse.tracecompass.ctf.core.event.scope.ILexicalScope;
import org.eclipse.tracecompass.ctf.core.event.scope.LexicalScope;
import org.eclipse.tracecompass.ctf.core.event.types.Definition;
import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition;
*/
@Override
public LexicalScope getScopePath() {
- return LexicalScope.STREAM;
+ return ILexicalScope.STREAM;
}
// ------------------------------------------------------------------------
private StructDefinition parseTracePacketHeader(
BitBuffer bitBuffer) throws CTFReaderException {
- StructDefinition tracePacketHeaderDef = fTracePacketHeaderDecl.createDefinition(fStream.getTrace(), LexicalScope.TRACE_PACKET_HEADER, bitBuffer);
+ StructDefinition tracePacketHeaderDef = fTracePacketHeaderDecl.createDefinition(fStream.getTrace(), ILexicalScope.TRACE_PACKET_HEADER, bitBuffer);
/*
* Check the CTF magic number
private StreamInputPacketIndexEntry parsePacketContext(long dataOffsetBits, long fileSizeBytes,
BitBuffer bitBuffer) throws CTFReaderException {
StreamInputPacketIndexEntry packetIndex;
- StructDefinition streamPacketContextDef = fStreamPacketContextDecl.createDefinition(this, LexicalScope.STREAM_PACKET_CONTEXT, bitBuffer);
+ StructDefinition streamPacketContextDef = fStreamPacketContextDecl.createDefinition(this, ILexicalScope.STREAM_PACKET_CONTEXT, bitBuffer);
packetIndex = new StreamInputPacketIndexEntry(dataOffsetBits, streamPacketContextDef, fileSizeBytes, fLostSoFar);
fLostSoFar = packetIndex.getLostEvents() + fLostSoFar;
setTimestampEnd(packetIndex.getTimestampEnd());
import org.eclipse.tracecompass.ctf.core.event.IEventDeclaration;
import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer;
import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
+import org.eclipse.tracecompass.ctf.core.event.scope.ILexicalScope;
import org.eclipse.tracecompass.ctf.core.event.scope.LexicalScope;
import org.eclipse.tracecompass.ctf.core.event.types.Definition;
import org.eclipse.tracecompass.ctf.core.event.types.ICompositeDefinition;
* out of bounds exception or such
*/
public StructDefinition getEventContextDefinition(@NonNull BitBuffer input) throws CTFReaderException {
- return fStreamEventContextDecl.createDefinition(fStreamInputReader.getStreamInput(), LexicalScope.STREAM_EVENT_CONTEXT, input);
+ return fStreamEventContextDecl.createDefinition(fStreamInputReader.getStreamInput(), ILexicalScope.STREAM_EVENT_CONTEXT, input);
}
/**
if (!(fStreamEventHeaderDecl instanceof StructDeclaration)) {
throw new IllegalStateException("Definition is not a struct definition, this is a deprecated method that doesn't work so well, stop using it."); //$NON-NLS-1$
}
- return ((StructDeclaration) fStreamEventHeaderDecl).createDefinition(this, LexicalScope.STREAM_EVENT_HEADER, input);
+ return ((StructDeclaration) fStreamEventHeaderDecl).createDefinition(this, ILexicalScope.STREAM_EVENT_HEADER, input);
}
/**
* out of bounds exception or such
*/
public StructDefinition getStreamPacketContextDefinition(@NonNull BitBuffer input) throws CTFReaderException {
- return fStreamPacketContextDecl.createDefinition(fStreamInputReader.getStreamInput(), LexicalScope.STREAM_PACKET_CONTEXT, input);
+ return fStreamPacketContextDecl.createDefinition(fStreamInputReader.getStreamInput(), ILexicalScope.STREAM_PACKET_CONTEXT, input);
}
/**
* out of bounds exception or such
*/
public StructDefinition getTracePacketHeaderDefinition(@NonNull BitBuffer input) throws CTFReaderException {
- return fTracePacketHeaderDecl.createDefinition(fStreamInputReader.getStreamInput().getStream().getTrace(), LexicalScope.TRACE_PACKET_HEADER, input);
+ return fTracePacketHeaderDecl.createDefinition(fStreamInputReader.getStreamInput().getStream().getTrace(), ILexicalScope.TRACE_PACKET_HEADER, input);
}
/**
@Override
public LexicalScope getScopePath() {
- return LexicalScope.PACKET;
+ return ILexicalScope.PACKET;
}
// ------------------------------------------------------------------------
eventID = ehd.getId();
timestamp = calculateTimestamp(ehd.getTimestamp(), ehd.getTimestampLength());
} else {
- fCurrentStreamEventHeaderDef = ((StructDeclaration) fStreamEventHeaderDecl).createDefinition(null, LexicalScope.EVENT_HEADER, currentBitBuffer);
+ fCurrentStreamEventHeaderDef = ((StructDeclaration) fStreamEventHeaderDecl).createDefinition(null, ILexicalScope.EVENT_HEADER, currentBitBuffer);
StructDefinition StructEventHeaderDef = (StructDefinition) fCurrentStreamEventHeaderDef;
/* Check for the event id. */
IDefinition idDef = StructEventHeaderDef.lookupDefinition("id"); //$NON-NLS-1$
@Override
public Definition lookupDefinition(String lookupPath) {
- if (lookupPath.equals(LexicalScope.STREAM_PACKET_CONTEXT.toString())) {
+ if (lookupPath.equals(ILexicalScope.STREAM_PACKET_CONTEXT.getPath())) {
return (Definition) fCurrentStreamPacketContextDef;
}
- if (lookupPath.equals(LexicalScope.TRACE_PACKET_HEADER.toString())) {
+ if (lookupPath.equals(ILexicalScope.TRACE_PACKET_HEADER.getPath())) {
return (Definition) fCurrentTracePacketHeaderDef;
}
return null;
import org.eclipse.tracecompass.ctf.core.event.IEventDeclaration;
import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer;
import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
-import org.eclipse.tracecompass.ctf.core.event.scope.LexicalScope;
+import org.eclipse.tracecompass.ctf.core.event.scope.ILexicalScope;
import org.eclipse.tracecompass.ctf.core.event.types.Definition;
import org.eclipse.tracecompass.ctf.core.event.types.IDefinition;
import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition;
if (fPacketHeaderDecl != null) {
/* Read the packet header */
- fPacketHeaderDef = fPacketHeaderDecl.createDefinition(this, LexicalScope.PACKET_HEADER, streamBitBuffer);
+ fPacketHeaderDef = fPacketHeaderDecl.createDefinition(this, ILexicalScope.PACKET_HEADER, streamBitBuffer);
}
} catch (IOException e) {
/* Shouldn't happen at this stage if every other check passed */
// IDefinitionScope
// ------------------------------------------------------------------------
+ /**
+ * @since 1.0
+ */
@Override
- public LexicalScope getScopePath() {
- return LexicalScope.TRACE;
+ public ILexicalScope getScopePath() {
+ return ILexicalScope.TRACE;
}
/**
*/
@Override
public Definition lookupDefinition(String lookupPath) {
- if (lookupPath.equals(LexicalScope.TRACE_PACKET_HEADER.toString())) {
+ if (lookupPath.equals(ILexicalScope.TRACE_PACKET_HEADER.getPath())) {
return fPacketHeaderDef;
}
return null;
import org.eclipse.tracecompass.ctf.core.event.EventDefinition;
import org.eclipse.tracecompass.ctf.core.event.IEventDeclaration;
import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer;
-import org.eclipse.tracecompass.ctf.core.event.scope.LexicalScope;
+import org.eclipse.tracecompass.ctf.core.event.scope.ILexicalScope;
import org.eclipse.tracecompass.ctf.core.event.types.Declaration;
import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration;
import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
@Override
public EventDefinition createDefinition(CTFStreamInputReader streamInputReader, @NonNull BitBuffer input, long timestamp) throws CTFReaderException {
StructDeclaration streamEventContextDecl = streamInputReader.getStreamEventContextDecl();
- StructDefinition streamEventContext = streamEventContextDecl != null ? streamEventContextDecl.createDefinition(fStream.getTrace(), LexicalScope.STREAM_EVENT_CONTEXT, input) : null;
+ StructDefinition streamEventContext = streamEventContextDecl != null ? streamEventContextDecl.createDefinition(fStream.getTrace(), ILexicalScope.STREAM_EVENT_CONTEXT, input) : null;
StructDefinition packetContext = streamInputReader.getPacketReader().getCurrentPacketEventHeader();
- StructDefinition eventContext = fContext != null ? fContext.createDefinition(fStream.getTrace(), LexicalScope.CONTEXT, input) : null;
- StructDefinition eventPayload = fFields != null ? fFields.createDefinition(fStream.getTrace(), LexicalScope.FIELDS, input) : null;
+ StructDefinition eventContext = fContext != null ? fContext.createDefinition(fStream.getTrace(), ILexicalScope.CONTEXT, input) : null;
+ StructDefinition eventPayload = fFields != null ? fFields.createDefinition(fStream.getTrace(), ILexicalScope.FIELDS, input) : null;
// a bit lttng specific
// CTF doesn't require a timestamp,
import java.util.List;
import org.eclipse.jdt.annotation.NonNull;
-import org.eclipse.tracecompass.ctf.core.event.scope.LexicalScope;
+import org.eclipse.tracecompass.ctf.core.event.scope.ILexicalScope;
import org.eclipse.tracecompass.ctf.core.event.types.Declaration;
import org.eclipse.tracecompass.ctf.core.event.types.Definition;
import org.eclipse.tracecompass.ctf.core.event.types.ICompositeDefinition;
* the number of bits valid in the timestamp
*/
public EventHeaderDefinition(@NonNull Declaration eventHeaderDecl, int id, long timestamp, int timestampLength) {
- super(eventHeaderDecl, null, LexicalScope.EVENT_HEADER.toString(), LexicalScope.EVENT_HEADER);
+ super(eventHeaderDecl, null, ILexicalScope.EVENT_HEADER.getPath(), ILexicalScope.EVENT_HEADER);
fId = id;
fTimestamp = timestamp;
fTimestampLength = timestampLength;