tmf: Add possibility to add global aspects
authorGeneviève Bastien <gbastien+lttng@versatic.net>
Thu, 11 May 2017 20:50:09 +0000 (16:50 -0400)
committerGenevieve Bastien <gbastien+lttng@versatic.net>
Fri, 12 May 2017 14:33:10 +0000 (10:33 -0400)
Those aspects will be searched if no aspect in the trace gives results.

Change-Id: I89de44132492c6f480be1bb028407424ef42b19d
Signed-off-by: Geneviève Bastien <gbastien+lttng@versatic.net>
Reviewed-on: https://git.eclipse.org/r/96899
Reviewed-by: Hudson CI
Reviewed-by: Jean-Christian Kouame <jean-christian.kouame@ericsson.com>
Tested-by: Jean-Christian Kouame <jean-christian.kouame@ericsson.com>
tmf/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/trace/TmfTraceUtilsTest.java
tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/trace/TmfTraceUtils.java

index 69ee1098eb63a59fc80e49b5852081662380ea97..cf89ffd4e43960d8ae07ecc1fb980afb756262de 100644 (file)
@@ -14,12 +14,14 @@ package org.eclipse.tracecompass.tmf.core.tests.trace;
 
 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 static org.junit.Assert.fail;
 
 import java.util.Collection;
 
 import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
 import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
@@ -80,6 +82,27 @@ public class TmfTraceUtilsTest {
 
     }
 
+    private static class TestEventAspect implements ITmfEventAspect<Integer> {
+
+        public static final Integer RESOLVED_VALUE = 2;
+
+        @Override
+        public @NonNull String getName() {
+            return "test";
+        }
+
+        @Override
+        public @NonNull String getHelpText() {
+            return "test";
+        }
+
+        @Override
+        public @Nullable Integer resolve(@NonNull ITmfEvent event) {
+            return RESOLVED_VALUE;
+        }
+
+    }
+
     // ------------------------------------------------------------------------
     // Housekeeping
     // ------------------------------------------------------------------------
@@ -157,9 +180,33 @@ public class TmfTraceUtilsTest {
         assertNotNull(event);
 
         /* Make sure the CPU aspect returns the expected value */
-        Object cpuObj = TmfTraceUtils.resolveEventAspectOfClassForEvent(trace,  TmfCpuAspect.class, event);
+        Object cpuObj = TmfTraceUtils.resolveEventAspectOfClassForEvent(trace, TmfCpuAspect.class, event);
         assertNotNull(cpuObj);
         assertEquals(1, cpuObj);
 
     }
+
+    /**
+     * Test the {@link TmfTraceUtils#registerEventAspect(ITmfEventAspect)} method
+     */
+    @Test
+    public void testAdditionalAspects() {
+        TmfTrace trace = fTrace;
+
+        assertNotNull(trace);
+
+        ITmfContext context = trace.seekEvent(0L);
+        ITmfEvent event = trace.getNext(context);
+        assertNotNull(event);
+
+        Object obj = TmfTraceUtils.resolveEventAspectOfClassForEvent(trace, TestEventAspect.class, event);
+        assertNull(obj);
+
+        // Register the aspect
+        TmfTraceUtils.registerEventAspect(new TestEventAspect());
+        // See that the aspect is resolved now
+        obj = TmfTraceUtils.resolveEventAspectOfClassForEvent(trace, TestEventAspect.class, event);
+        assertNotNull(obj);
+        assertEquals(TestEventAspect.RESOLVED_VALUE, obj);
+    }
 }
index a76e671d728410b18e6086ad35bdf8bfbe675679..948611986faa70afe424b392ddcf596a06863206 100644 (file)
@@ -19,8 +19,10 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Optional;
+import java.util.Set;
 import java.util.function.Predicate;
 
 import org.eclipse.core.runtime.IProgressMonitor;
@@ -46,6 +48,7 @@ import com.google.common.collect.Lists;
 public final class TmfTraceUtils {
 
     private static final int MAX_NB_BINARY_BYTES = 2048;
+    private static final Set<ITmfEventAspect<?>> EXTRA_ASPECTS = new HashSet<>();
 
     private TmfTraceUtils() {
     }
@@ -74,6 +77,21 @@ public final class TmfTraceUtils {
         return null;
     }
 
+    /**
+     * Registers an extra event aspect that may apply to all traces, not
+     * directly linked to a trace type. These can be used to retrieve values
+     * using the
+     * {@link #resolveEventAspectOfClassForEvent(ITmfTrace, Class, ITmfEvent)}
+     * method, but will not appear where trace aspects are being displayed.
+     *
+     * @param aspect
+     *            The event aspect to register
+     * @since 2.4
+     */
+    public static void registerEventAspect(ITmfEventAspect<?> aspect) {
+        EXTRA_ASPECTS.add(aspect);
+    }
+
     /**
      * Return the analysis modules that are of a given class. The modules will be
      * cast to the requested class. If the trace has children, the childrens modules
@@ -120,11 +138,21 @@ public final class TmfTraceUtils {
      */
     public static <T extends ITmfEventAspect<?>> @Nullable Object resolveEventAspectOfClassForEvent(
             ITmfTrace trace, Class<T> aspectClass, ITmfEvent event) {
-            return StreamUtils.getStream(trace.getEventAspects())
-                    .filter(aspect -> aspectClass.isAssignableFrom(aspect.getClass()))
-                    .map(aspect -> aspect.resolve(event))
-                    .filter(obj -> obj != null)
-                    .findFirst().orElse(null);
+        // First look in the trace aspects
+        Object value = StreamUtils.getStream(trace.getEventAspects())
+                .filter(aspect -> aspectClass.isAssignableFrom(aspect.getClass()))
+                .map(aspect -> aspect.resolve(event))
+                .filter(obj -> obj != null)
+                .findFirst().orElse(null);
+        if (value != null) {
+            return value;
+        }
+        // If the value is not found, look at the global aspects
+        return EXTRA_ASPECTS.stream()
+                .filter(aspect -> aspectClass.isAssignableFrom(aspect.getClass()))
+                .map(aspect -> aspect.resolve(event))
+                .filter(obj -> obj != null)
+                .findFirst().orElse(null);
     }
 
     /**
This page took 0.028041 seconds and 5 git commands to generate.