tmf: Annotate methods in ITmfEventField
authorAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Mon, 6 Jul 2015 18:57:27 +0000 (14:57 -0400)
committerAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Wed, 8 Jul 2015 15:25:34 +0000 (11:25 -0400)
Some methods in ITmfEventField can be annotated @NonNull to make
their usage easier.

getValue() and getField() should eventually be marked @Nullable,
but this will be a much larger undertaking, as most analysis
modules currently just get fields and values without even
checking them...

Change-Id: I564c6d067a62239cde5c67350ea709315bfe89f4
Signed-off-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
Reviewed-on: https://git.eclipse.org/r/51212
Reviewed-by: Hudson CI
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Tested-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-by: Patrick Tasse <patrick.tasse@gmail.com>
14 files changed:
btf/org.eclipse.tracecompass.btf.core/src/org/eclipse/tracecompass/btf/core/event/BTFPayload.java
ctf/org.eclipse.tracecompass.tmf.ctf.core.tests/src/org/eclipse/tracecompass/tmf/ctf/core/tests/event/CtfTmfEventFieldTest.java
ctf/org.eclipse.tracecompass.tmf.ctf.core/src/org/eclipse/tracecompass/tmf/ctf/core/event/CtfTmfEvent.java
ctf/org.eclipse.tracecompass.tmf.ctf.core/src/org/eclipse/tracecompass/tmf/ctf/core/event/CtfTmfEventField.java
ctf/org.eclipse.tracecompass.tmf.ctf.core/src/org/eclipse/tracecompass/tmf/ctf/core/trace/CtfTmfTrace.java
pcap/org.eclipse.tracecompass.tmf.pcap.core/src/org/eclipse/tracecompass/internal/tmf/pcap/core/util/PcapEventFactory.java
tmf/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/event/TmfEventFieldTest.java
tmf/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/event/TmfEventTest.java
tmf/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/event/TmfSyncEventStub.java
tmf/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/TmfXmlTraceStub.java
tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/event/ITmfEventField.java
tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/event/TmfEventField.java
tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/parsers/custom/CustomEventContent.java
tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/viewers/events/TmfEventPropertySource.java

index 3d62b930645400a408927b320cdfe271be740ab1..6c86f19191d681a2e8954f4b110d0273fb654d11 100644 (file)
@@ -16,6 +16,7 @@ package org.eclipse.tracecompass.btf.core.event;
 import java.util.Map;
 import java.util.Map.Entry;
 
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.tracecompass.btf.core.Messages;
 import org.eclipse.tracecompass.tmf.core.event.TmfEventField;
 
@@ -30,7 +31,7 @@ import com.google.common.collect.ImmutableMap.Builder;
 public class BTFPayload {
 
     /** Description subfield name */
-    public static final String DESCRIPTION = "description"; //$NON-NLS-1$
+    public static final @NonNull String DESCRIPTION = "description"; //$NON-NLS-1$
 
     private static final Map<String, String> EVENT_DESCRIPTIONS;
     private static final String EMPTY = ""; //$NON-NLS-1$
index 0a5100bccb2ce2e13bded07c491845f778650a73..a0f77b70a12864fa725f75c39aa7a6d8832358b2 100644 (file)
@@ -60,7 +60,7 @@ public class CtfTmfEventFieldTest {
     private static final String FLOAT = "float";
     private static final String LEN = "len";
     private static final String INT = "int";
-    private static final String NAME = "test";
+    private static final @NonNull String NAME = "test";
     private static final String STRUCT = "struct";
     private static final String VARIANT = "variant";
     private static final String ENUM = "enum";
index 334b7409efd1fa60e4219fc10a6c8e5265a59405..eee4ad93efa46c6f9dd4cc128c6b32dbab5c7a56 100644 (file)
@@ -13,6 +13,8 @@
 
 package org.eclipse.tracecompass.tmf.ctf.core.event;
 
+import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
+
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
@@ -258,7 +260,8 @@ public class CtfTmfEvent extends TmfEvent
         if (structFields != null) {
             if (structFields.getFieldNames() != null) {
                 for (String curFieldName : structFields.getFieldNames()) {
-                    fields.add(CtfTmfEventField.parseField((IDefinition) structFields.getDefinition(curFieldName), curFieldName));
+                    String fn = checkNotNull(curFieldName);
+                    fields.add(CtfTmfEventField.parseField((IDefinition) structFields.getDefinition(fn), fn));
                 }
             }
         }
index ae7fdc30d1860ea81062d591dbc36a60a744ef01..6bc6d92e10581e6035dc6f4293d88a1e574299c6 100644 (file)
 
 package org.eclipse.tracecompass.tmf.ctf.core.event;
 
+import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
+
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.tracecompass.ctf.core.event.types.AbstractArrayDefinition;
 import org.eclipse.tracecompass.ctf.core.event.types.CompoundDeclaration;
 import org.eclipse.tracecompass.ctf.core.event.types.Definition;
@@ -62,9 +65,9 @@ public abstract class CtfTmfEventField extends TmfEventField {
      * @param fields
      *            The children fields. Useful for composite fields
      */
-    protected CtfTmfEventField(String name, Object value, ITmfEventField[] fields) {
+    protected CtfTmfEventField(@NonNull String name, Object value, ITmfEventField[] fields) {
         super(/* Strip the underscore from the field name if there is one */
-                name.startsWith("_") ? name.substring(1) : name, //$NON-NLS-1$
+                name.startsWith("_") ? checkNotNull(name.substring(1)) : name, //$NON-NLS-1$
                 value,
                 fields);
     }
@@ -85,7 +88,7 @@ public abstract class CtfTmfEventField extends TmfEventField {
      */
     @Deprecated
     public static CtfTmfEventField parseField(Definition fieldDef,
-            String fieldName) {
+            @NonNull String fieldName) {
         return parseField((IDefinition) fieldDef, fieldName);
     }
 
@@ -99,7 +102,7 @@ public abstract class CtfTmfEventField extends TmfEventField {
      * @return The resulting CtfTmfEventField object
      */
     public static CtfTmfEventField parseField(IDefinition fieldDef,
-            String fieldName) {
+            @NonNull String fieldName) {
         CtfTmfEventField field = null;
 
         /* Determine the Definition type */
@@ -174,14 +177,15 @@ public abstract class CtfTmfEventField extends TmfEventField {
             List<ITmfEventField> list = new ArrayList<>();
             /* Recursively parse the fields */
             for (String curFieldName : strDef.getFieldNames()) {
-                list.add(CtfTmfEventField.parseField(strDef.getDefinition(curFieldName), curFieldName));
+                String fn = checkNotNull(curFieldName);
+                list.add(CtfTmfEventField.parseField((IDefinition) strDef.getDefinition(fn), fn));
             }
             field = new CTFStructField(fieldName, list.toArray(new CtfTmfEventField[list.size()]));
 
         } else if (fieldDef instanceof VariantDefinition) {
             VariantDefinition varDef = (VariantDefinition) fieldDef;
 
-            String curFieldName = varDef.getCurrentFieldName();
+            String curFieldName = checkNotNull(varDef.getCurrentFieldName());
             IDefinition curFieldDef = varDef.getCurrentField();
             if (curFieldDef != null) {
                 CtfTmfEventField subField = CtfTmfEventField.parseField(curFieldDef, curFieldName);
@@ -229,7 +233,7 @@ final class CTFIntegerField extends CtfTmfEventField {
      * @param signed
      *            Is the value signed or not
      */
-    CTFIntegerField(String name, long longValue, int base, boolean signed) {
+    CTFIntegerField(@NonNull String name, long longValue, int base, boolean signed) {
         super(name, longValue, null);
         fSigned = signed;
         fBase = base;
@@ -262,7 +266,7 @@ final class CTFStringField extends CtfTmfEventField {
      * @param name
      *            The name of this field
      */
-    CTFStringField(String name, String strValue) {
+    CTFStringField(@NonNull String name, String strValue) {
         super(name, strValue, null);
     }
 
@@ -294,7 +298,7 @@ final class CTFIntegerArrayField extends CtfTmfEventField {
      * @param signed
      *            Are the values in the array signed or not
      */
-    CTFIntegerArrayField(String name, long[] longValues, int base, boolean signed) {
+    CTFIntegerArrayField(@NonNull String name, long[] longValues, int base, boolean signed) {
         super(name, longValues, null);
         fBase = base;
         fSigned = signed;
@@ -336,7 +340,7 @@ final class CTFArrayField extends CtfTmfEventField {
      * @param elements
      *            The array elements of this field
      */
-    CTFArrayField(String name, CtfTmfEventField[] elements) {
+    CTFArrayField(@NonNull String name, CtfTmfEventField[] elements) {
         super(name, elements, elements);
     }
 
@@ -373,7 +377,7 @@ final class CTFFloatField extends CtfTmfEventField {
      * @param name
      *            The name of this field
      */
-    protected CTFFloatField(String name, double value) {
+    protected CTFFloatField(@NonNull String name, double value) {
         super(name, value, null);
     }
 
@@ -399,7 +403,7 @@ final class CTFEnumField extends CtfTmfEventField {
      * @param name
      *            The name of this field
      */
-    CTFEnumField(String name, CtfEnumPair enumValue) {
+    CTFEnumField(@NonNull String name, CtfEnumPair enumValue) {
         super(name, new CtfEnumPair(enumValue.getFirst(),
                 enumValue.getSecond()), null);
     }
@@ -425,7 +429,7 @@ final class CTFStructField extends CtfTmfEventField {
      * @param name
      *            The name of this field
      */
-    CTFStructField(String name, CtfTmfEventField[] fields) {
+    CTFStructField(@NonNull String name, CtfTmfEventField[] fields) {
         super(name, fields, fields);
     }
 
@@ -456,7 +460,7 @@ final class CTFVariantField extends CtfTmfEventField {
      * @param name
      *            The name of this field
      */
-    CTFVariantField(String name, CtfTmfEventField field) {
+    CTFVariantField(@NonNull String name, CtfTmfEventField field) {
         super(name, field, new CtfTmfEventField[] { field });
     }
 
index f517ff52ba3b1a7a886707b103bedf83cd5c87df..e91106441dc7b8590aeb9aca632464bfd2c35afe 100644 (file)
@@ -178,7 +178,7 @@ public class CtfTmfTrace extends TmfTrace
                         List<ITmfEventField> content = new ArrayList<>();
                         /* Should only return null the first time */
                         for (String fieldName : ied.getFields().getFieldsList()) {
-                            content.add(new TmfEventField(fieldName, null, null));
+                            content.add(new TmfEventField(checkNotNull(fieldName), null, null));
                         }
                         ITmfEventField contentTree = new TmfEventField(
                                 ITmfEventField.ROOT_FIELD_ID,
index b50522c6803d98baeff4c735ac3c53839797cd05..fd560a716897358a7842b6d5bab905098ed2c531 100644 (file)
@@ -112,7 +112,7 @@ public class PcapEventFactory {
         while (localPacket != null) {
             subfieldList.clear();
             for (Map.Entry<String, String> entry : localPacket.getFields().entrySet()) {
-                String key = entry.getKey();
+                String key = checkNotNull(entry.getKey());
                 String value = entry.getValue();
                 subfieldList.add(new TmfEventField(key, value, null));
             }
index 3f93214b6ea87284593da01973f6b877a9e07c29..10a00c8efb314e0fb0f46fcede2abc55f4a5e015 100644 (file)
@@ -25,6 +25,7 @@ import static org.junit.Assert.fail;
 
 import java.util.Collection;
 
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
 import org.eclipse.tracecompass.tmf.core.event.TmfEvent;
 import org.eclipse.tracecompass.tmf.core.event.TmfEventField;
@@ -41,8 +42,8 @@ public class TmfEventFieldTest {
     // Variables
     // ------------------------------------------------------------------------
 
-    private final String fFieldName1 = "Field-1";
-    private final String fFieldName2 = "Field-2";
+    private final @NonNull String fFieldName1 = "Field-1";
+    private final @NonNull String fFieldName2 = "Field-2";
 
     private final Object fValue1 = "Value";
     private final Object fValue2 = Integer.valueOf(10);
@@ -51,7 +52,7 @@ public class TmfEventFieldTest {
     private final TmfEventField fField2 = new TmfEventField(fFieldName2, fValue2, null);
     private final TmfEventField fField3 = new TmfEventField(fFieldName1, fValue2, null);
 
-    private final String fStructRootFieldName = "Root-S";
+    private final @NonNull String fStructRootFieldName = "Root-S";
     private final String[] fStructFieldNames = new String[] { fFieldName1, fFieldName2 };
     private final TmfEventField fStructTerminalField1 = new TmfEventField(fFieldName1, null, null);
     private final TmfEventField fStructTerminalField2 = new TmfEventField(fFieldName2, null, null);
@@ -59,7 +60,7 @@ public class TmfEventFieldTest {
     private final TmfEventField fStructRootField = new TmfEventField(fStructRootFieldName, null,
             new ITmfEventField[] { fStructTerminalField1, fStructTerminalField2 });
 
-    private final String fRootFieldName = "Root";
+    private final @NonNull String fRootFieldName = "Root";
     private final String[] fFieldNames = new String[] { fFieldName1, fFieldName2 };
     private final TmfEventField fRootField = new TmfEventField(fRootFieldName, null,
             new ITmfEventField[] { fField1, fField2 });
@@ -117,15 +118,6 @@ public class TmfEventFieldTest {
         assertArrayEquals(fFieldNames, names.toArray(new String[names.size()]));
     }
 
-    @Test
-    public void testConstructorBadArg() {
-        try {
-            new TmfEventField(null, fValue1, null);
-            fail("Invalid (null) field name");
-        } catch (final IllegalArgumentException e) {
-        }
-    }
-
     @Test
     public void testTerminalCopyConstructor() {
         final TmfEventField copy = new TmfEventField(fField1);
index 07f47b5c3140860aad486c538f24ef3ee00fa728..a1245df779b03a38a6033e866b0ee05d0ff07878 100644 (file)
@@ -49,8 +49,8 @@ public class TmfEventTest {
     private final @NonNull ITmfTrace fTrace = STUB_TRACE.getTrace();
 
     private final String fTypeId = "TestType";
-    private final String fLabel1 = "AString";
-    private final String fLabel2 = "AnInteger";
+    private final @NonNull String fLabel1 = "AString";
+    private final @NonNull String fLabel2 = "AnInteger";
     private final String[] fLabels = new String[] { fLabel1, fLabel2 };
     private final TmfEventType fType = new TmfEventType(fTypeId, TmfEventField.makeRoot(fLabels));
 
@@ -59,7 +59,7 @@ public class TmfEventTest {
     private final ITmfEventField fField1a = new TmfEventField(fLabel1, fValue1a, null);
     private final ITmfEventField fField1b = new TmfEventField(fLabel2, fValue1b, null);
     private final ITmfEventField[] fFields1 = new ITmfEventField[] { fField1a, fField1b };
-    private final String fRawContent1 = fField1a.toString() + fField1b.toString();
+    private final @NonNull String fRawContent1 = fField1a.toString() + fField1b.toString();
     private final ITmfEventField fContent1 = new TmfEventField(fRawContent1, null, fFields1);
     private final TmfTimestamp fTimestamp1 = new TmfTimestamp(12345, 2);
     private final @NonNull ITmfEvent fEvent1 = new TmfEvent(fTrace, 0, fTimestamp1, fType, fContent1);
@@ -69,7 +69,7 @@ public class TmfEventTest {
     private final ITmfEventField fField2a = new TmfEventField(fLabel1, fValue2a, null);
     private final ITmfEventField fField2b = new TmfEventField(fLabel2, fValue2b, null);
     private final ITmfEventField[] fFields2 = new ITmfEventField[] { fField2a, fField2b };
-    private final String fRawContent2 = fField2a.toString() + fField2b.toString();
+    private final @NonNull String fRawContent2 = fField2a.toString() + fField2b.toString();
     private final ITmfEventField fContent2 = new TmfEventField(fRawContent2, null, fFields2);
     private final TmfTimestamp fTimestamp2 = new TmfTimestamp(12350, 2);
     private final @NonNull ITmfEvent fEvent2 = new TmfEvent(fTrace, 1, fTimestamp2, fType, fContent2);
index 5c9c58af54c8cbd0266971ff1b3b5bee989d469c..0137ad106bda106333a29e50d7cfee9d6d6b72cb 100644 (file)
@@ -12,6 +12,7 @@
 
 package org.eclipse.tracecompass.tmf.tests.stubs.event;
 
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.tracecompass.tmf.core.event.TmfEvent;
 import org.eclipse.tracecompass.tmf.core.event.TmfEventField;
 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
@@ -25,7 +26,7 @@ import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
  */
 public class TmfSyncEventStub extends TmfEvent {
 
-    private static final String stub = "stub"; //$NON-NLS-1$
+    private static final @NonNull String stub = "stub"; //$NON-NLS-1$
 
     /**
      * Constructor
index 617985e797198eb63323a3a4a8122eb0efedd79e..8f635a68d24568191dce39dd2a3caf402ce2b7ef 100644 (file)
@@ -13,6 +13,8 @@
 
 package org.eclipse.tracecompass.tmf.tests.stubs.trace.xml;
 
+import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
+
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
@@ -272,7 +274,7 @@ public class TmfXmlTraceStub extends TmfTrace {
                     break;
                 }
             }
-            fieldsArray[i] = new TmfEventField(fields[i], val, null);
+            fieldsArray[i] = new TmfEventField(checkNotNull(fields[i]), val, null);
         }
 
         /* Generate the aspects for this trace if it is the aspects special event */
@@ -310,9 +312,6 @@ public class TmfXmlTraceStub extends TmfTrace {
         /* Add custom aspects in between */
         for (ITmfEventField field : fieldsArray) {
             String name = field.getName();
-            if (name == null) {
-                break;
-            }
             final ITmfEventAspect aspect = new TmfContentFieldAspect(name, name);
             if (name.equals(ASPECT_CPU)) {
                 builder.add(new TmfCpuAspect() {
index 97435a0d7153cef78f5c9b7bb1b86780e49bf9cb..a6a0571b85dd16d806785ad7277d8e270cbc7234 100644 (file)
@@ -46,7 +46,7 @@ public interface ITmfEventField {
     /**
      * @return the field name
      */
-    String getName();
+    @NonNull String getName();
 
     /**
      * @return the field value
@@ -64,7 +64,7 @@ public interface ITmfEventField {
      *
      * @return The subfield names (empty Collection if none)
      */
-    Collection<String> getFieldNames();
+    @NonNull Collection<String> getFieldNames();
 
     /**
      * Return the subfields. The iteration order is the same as
@@ -72,7 +72,7 @@ public interface ITmfEventField {
      *
      * @return The subfields (empty Collection if none)
      */
-    Collection<? extends ITmfEventField> getFields();
+    @NonNull Collection<? extends ITmfEventField> getFields();
 
     /**
      * Return a subfield by its path relative to this field.
index 05dbec9e76fba7eafda9a67bf1da45a7d2c30c4f..6ae877bbfbe42b78ac2900a34e3b676dbcd65498 100644 (file)
@@ -64,10 +64,7 @@ public class TmfEventField implements ITmfEventField {
      * @throws IllegalArgumentException
      *             If 'name' is null, or if 'fields' has duplicate field names.
      */
-    public TmfEventField(String name, @Nullable Object value, @Nullable ITmfEventField[] fields) {
-        if (name == null) {
-            throw new IllegalArgumentException();
-        }
+    public TmfEventField(@NonNull String name, @Nullable Object value, @Nullable ITmfEventField[] fields) {
         fName = name;
         fValue = value;
 
@@ -114,12 +111,12 @@ public class TmfEventField implements ITmfEventField {
 
     @Override
     public Collection<String> getFieldNames() {
-        return fFields.keySet();
+        return checkNotNull(fFields.keySet());
     }
 
     @Override
     public Collection<ITmfEventField> getFields() {
-        return fFields.values();
+        return checkNotNull(fFields.values());
     }
 
     @Override
@@ -150,7 +147,8 @@ public class TmfEventField implements ITmfEventField {
     public static final ITmfEventField makeRoot(final String[] labels) {
         final ITmfEventField[] fields = new ITmfEventField[labels.length];
         for (int i = 0; i < labels.length; i++) {
-            fields[i] = new TmfEventField(labels[i], null, null);
+            String label = checkNotNull(labels[i]);
+            fields[i] = new TmfEventField(label, null, null);
         }
         // Return a new root field;
         return new TmfEventField(ITmfEventField.ROOT_FIELD_ID, null, fields);
index 2a86f0100477ae69f7546d85421581f7e302d13a..61e46507d7c6c5d84196a2e037ca20668c693f37 100644 (file)
@@ -12,6 +12,7 @@
 
 package org.eclipse.tracecompass.tmf.core.parsers.custom;
 
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
 import org.eclipse.tracecompass.tmf.core.event.TmfEventField;
 
@@ -44,7 +45,7 @@ public class CustomEventContent extends TmfEventField {
      * @param fields
      *            The array of sub-fields
      */
-    public CustomEventContent(String name, Object content, ITmfEventField[] fields) {
+    public CustomEventContent(@NonNull String name, Object content, ITmfEventField[] fields) {
         super(name, content, fields);
     }
 
index e4290efdcdde06988df42af485f1e29fc0d7927b..2e88c5a80c64dd007fa6f04a16e9baac66beeb92 100644 (file)
@@ -127,7 +127,7 @@ public class TmfEventPropertySource implements IPropertySource {
         @Override
         public Object getPropertyValue(Object id) {
             ITmfEventField field = (ITmfEventField) id;
-            if (field.getFields() != null && field.getFields().size() > 0) {
+            if (!field.getFields().isEmpty()) {
                 return new ContentPropertySource(field);
             }
             return field.getFormattedValue();
This page took 0.035836 seconds and 5 git commands to generate.