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;
}
+ 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
// ------------------------------------------------------------------------
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);
+ }
}
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;
public final class TmfTraceUtils {
private static final int MAX_NB_BINARY_BYTES = 2048;
+ private static final Set<ITmfEventAspect<?>> EXTRA_ASPECTS = new HashSet<>();
private 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
*/
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);
}
/**