Skip to content

Commit

Permalink
ctf: Replace NonNullUtils#checkNotNull with Objects#requireNonNull
Browse files Browse the repository at this point in the history
Follow Java standard and remove a dependency to o.e.t.tmf.common.core

[Changed] Use standard nullchecks in CTF parser

Change-Id: Ib1b65deebbac0de899b966c03639adf4cb988251
Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/c/tracecompass/org.eclipse.tracecompass/+/205779
Tested-by: Trace Compass Bot <tracecompass-bot@eclipse.org>
Tested-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
Reviewed-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
  • Loading branch information
MatthewKhouzam committed Dec 13, 2023
1 parent d55f1f6 commit 5890f93
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package org.eclipse.tracecompass.ctf.core.tests.trace;

import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
import static java.util.Objects.requireNonNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -176,7 +176,7 @@ public CTFTraceWriterTest (String name, long startTime, long endTime, int nbEven
public void testKernelTrace() {
try {
CTFTrace trace = CtfTestTraceUtils.getTrace(CtfTestTrace.KERNEL);
CTFTraceWriter ctfWriter = new CTFTraceWriter(checkNotNull(trace));
CTFTraceWriter ctfWriter = new CTFTraceWriter(requireNonNull(trace));
String traceName = createTraceName(fName);
ctfWriter.copyPackets(fStartTime, fEndTime, traceName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import java.util.Map;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.tracecompass.ctf.core.trace.ICTFPacketDescriptor;
import org.eclipse.tracecompass.ctf.core.event.types.ICompositeDefinition;
import org.eclipse.tracecompass.ctf.core.trace.ICTFPacketDescriptor;

/**
* Internal implementation to test packet index
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package org.eclipse.tracecompass.ctf.core.event.types;

import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
import static java.util.Objects.requireNonNull;

import java.math.BigInteger;
import java.nio.ByteOrder;
Expand Down Expand Up @@ -503,7 +503,7 @@ public BigInteger getMaxValue() {
* For a given N significant bits, compute the maximal value which is (1
* << N) - 1.
*/
return checkNotNull(BigInteger.ONE.shiftLeft(significantBits).subtract(BigInteger.ONE));
return requireNonNull(BigInteger.ONE.shiftLeft(significantBits).subtract(BigInteger.ONE));
}

/**
Expand All @@ -513,7 +513,7 @@ public BigInteger getMaxValue() {
*/
public BigInteger getMinValue() {
if (!fSigned) {
return checkNotNull(BigInteger.ZERO);
return requireNonNull(BigInteger.ZERO);
}

/*
Expand All @@ -525,7 +525,7 @@ public BigInteger getMinValue() {
* For a given N significant bits, compute the minimal value which is -
* (1 << N).
*/
return checkNotNull(BigInteger.ONE.shiftLeft(significantBits).negate());
return requireNonNull(BigInteger.ONE.shiftLeft(significantBits).negate());
}

private long read(BitBuffer input) throws CTFException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package org.eclipse.tracecompass.ctf.core.trace;

import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
import static java.util.Objects.requireNonNull;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -72,7 +72,7 @@ public CTFStreamOutputWriter(@NonNull CTFStreamInput streamInput, @NonNull File
Path outFilePath = FileSystems.getDefault().getPath(file.getAbsolutePath(), inFileName);

try {
fOutFile = checkNotNull(Files.createFile(outFilePath).toFile());
fOutFile = requireNonNull(Files.createFile(outFilePath).toFile());
} catch (IOException e) {
throw new CTFIOException("Output file can't be created: " + outFilePath, e); //$NON-NLS-1$
}
Expand Down Expand Up @@ -102,7 +102,7 @@ public void copyPackets(long startTime, long endTime) throws CTFException {
throw new CTFIOException("StreamInput is null. Can't copy packets"); //$NON-NLS-1$
}

try (FileChannel fc = checkNotNull(FileChannel.open(fOutFile.toPath(), StandardOpenOption.WRITE));
try (FileChannel fc = requireNonNull(FileChannel.open(fOutFile.toPath(), StandardOpenOption.WRITE));
FileChannel source = FileChannel.open(streamInput.getFile().toPath(), StandardOpenOption.READ);) {
StreamInputPacketIndex index = streamInput.getIndex();
int count = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

package org.eclipse.tracecompass.ctf.core.trace;

import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
import static java.util.Objects.requireNonNull;

import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -116,7 +116,7 @@ public CTFTraceReader(CTFTrace trace) throws CTFException {
*/
fStartTime = 0;
if (hasMoreEvents()) {
fStartTime = fTrace.timestampCyclesToNanos(checkNotNull(getTopStream().getCurrentEvent()).getTimestamp());
fStartTime = fTrace.timestampCyclesToNanos(requireNonNull(getTopStream().getCurrentEvent()).getTimestamp());
setEndTime(fStartTime);
}
}
Expand Down Expand Up @@ -216,7 +216,7 @@ private void createStreamInputReaders() throws CTFException {
/*
* Create a reader and add it to the group.
*/
fStreamInputReaders.add(new CTFStreamInputReader(checkNotNull(streamInput)));
fStreamInputReaders.add(new CTFStreamInputReader(requireNonNull(streamInput)));
}
}

Expand Down Expand Up @@ -251,9 +251,9 @@ public void update() throws CTFException {
* Create a reader to check if it already exists in the list. If it doesn't, add
* it.
*/
try (CTFStreamInputReader streamInputReader = new CTFStreamInputReader(checkNotNull(streamInput))) {
try (CTFStreamInputReader streamInputReader = new CTFStreamInputReader(requireNonNull(streamInput))) {
if (!fStreamInputReaders.contains(streamInputReader)) {
CTFStreamInputReader streamInputReaderToAdd = new CTFStreamInputReader(checkNotNull(streamInput));
CTFStreamInputReader streamInputReaderToAdd = new CTFStreamInputReader(requireNonNull(streamInput));
streamInputReaderToAdd.readNextEvent();
fStreamInputReaders.add(streamInputReaderToAdd);
readers.add(streamInputReaderToAdd);
Expand Down Expand Up @@ -373,7 +373,7 @@ public boolean advance() throws CTFException {
* We're in OK, there's a guaranteed top#getCurrentEvent() unless another thread
* does something bad.
*/
IEventDefinition currentEvent = checkNotNull(top.getCurrentEvent());
IEventDefinition currentEvent = Objects.requireNonNull(top.getCurrentEvent(),()->{return String.format("Stream %s at offset %l reported an event was read, but the event is null.", top.getFilename(), top.getCurrentPacketReader().getCurrentPacket().getOffsetBytes());}); //$NON-NLS-1$
final long topEnd = fTrace.timestampCyclesToNanos(currentEvent.getTimestamp());
setEndTime(Math.max(topEnd, getEndTime()));
fEventCountPerTraceFile[top.getName()]++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package org.eclipse.tracecompass.ctf.core.trace;

import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
import static java.util.Objects.requireNonNull;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -104,7 +104,7 @@ public void copyPackets(long startTime, long endTime, String newTracePath) throw
for (ICTFStream stream : trace.getStreams()) {
Set<CTFStreamInput> inputs = stream.getStreamInputs();
for (CTFStreamInput s : inputs) {
CTFStreamOutputWriter streamOutputwriter = new CTFStreamOutputWriter(checkNotNull(s), out);
CTFStreamOutputWriter streamOutputwriter = new CTFStreamOutputWriter(requireNonNull(s), out);
streamOutputwriter.copyPackets(adjustedStart, adjustedEnd);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.eclipse.tracecompass.ctf.core.trace;

import static java.util.Objects.requireNonNull;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -45,7 +47,6 @@
import org.antlr.runtime.tree.RewriteCardinalityException;
import org.apache.commons.io.IOUtils;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tracecompass.common.core.NonNullUtils;
import org.eclipse.tracecompass.ctf.core.CTFException;
import org.eclipse.tracecompass.ctf.parser.CTFLexer;
import org.eclipse.tracecompass.ctf.parser.CTFParser;
Expand Down Expand Up @@ -215,7 +216,7 @@ public void parseJsonFile() throws CTFException {
try (InputStream is = new FileInputStream(metadataFile)) {
String json = IOUtils.toString(is, "UTF-8"); //$NON-NLS-1$
ICTFMetadataNode tree = parseJsonToTree(json);
fTreeParser = new IOStructGen(tree, NonNullUtils.checkNotNull(fTrace));
fTreeParser = new IOStructGen(tree, requireNonNull(fTrace));
fTreeParser.generate();

} catch (FileNotFoundException e) {
Expand Down Expand Up @@ -295,7 +296,7 @@ public void checkCTFVersion() throws CTFException {
@Nullable ICTFMetadataNode metadata;
metadata = gson.fromJson(json, JsonPreambleMetadataNode.class);

TraceDeclarationParser.INSTANCE.parse(metadata, new TraceDeclarationParser.Param(fTrace, NonNullUtils.checkNotNull(fTrace.getScope())));
TraceDeclarationParser.INSTANCE.parse(metadata, new TraceDeclarationParser.Param(fTrace, requireNonNull(fTrace.getScope())));

} catch (IOException | ParseException e) {
throw new CTFIOException(e);
Expand Down Expand Up @@ -380,7 +381,7 @@ public void parseText(String data) throws CTFException {

private void readMetaDataText(Reader metadataTextInput) throws IOException, RecognitionException, ParseException {
ICTFMetadataNode tree = createAST(metadataTextInput);
fTreeParser = new IOStructGen(tree, NonNullUtils.checkNotNull(fTrace));
fTreeParser = new IOStructGen(tree, requireNonNull(fTrace));
/* Generate IO structures (declarations) */
fTreeParser.generate();
/* store locally in case of concurrent modification */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@

package org.eclipse.tracecompass.internal.ctf.core.event.metadata;

import static java.util.Objects.requireNonNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.tracecompass.common.core.NonNullUtils;
import org.eclipse.tracecompass.ctf.core.event.CTFCallsite;
import org.eclipse.tracecompass.ctf.core.event.CTFClock;
import org.eclipse.tracecompass.ctf.core.event.metadata.DeclarationScope;
Expand Down Expand Up @@ -87,7 +88,7 @@ public class IOStructGen {
public IOStructGen(ICTFMetadataNode tree, @NonNull CTFTrace trace) {
fTrace = trace;
fTree = tree;
fRoot = NonNullUtils.checkNotNull(trace.getScope());
fRoot = requireNonNull(trace.getScope());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

package org.eclipse.tracecompass.internal.ctf.core.event.types;

import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
import static java.util.Objects.requireNonNull;

import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -135,7 +135,7 @@ public AbstractArrayDefinition createDefinition(
Builder<@NonNull Definition> definitions = new ImmutableList.Builder<>();
for (int i = 0; i < length; i++) {
/* We should not have inserted any null values */
String elemName = checkNotNull(paths.get(i));
String elemName = requireNonNull(paths.get(i));
definitions.add(fElemType.createDefinition(definitionScope, elemName, input));
}
List<@NonNull Definition> list = definitions.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@

package org.eclipse.tracecompass.internal.ctf.core.trace;

import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.tracecompass.ctf.core.trace.ICTFPacketDescriptor;
Expand Down Expand Up @@ -79,7 +78,7 @@ public boolean isEmpty() {
*/
public void appendAll(Collection<ICTFPacketDescriptor> preParsedIndex) {
for (ICTFPacketDescriptor sipie : preParsedIndex) {
append(checkNotNull(sipie));
append(Objects.requireNonNull(sipie));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@

package org.eclipse.tracecompass.internal.ctf.core.trace;

import static java.util.Objects.requireNonNull;

import java.util.AbstractMap;
import java.util.Collections;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.tracecompass.common.core.NonNullUtils;
import org.eclipse.tracecompass.ctf.core.CTFStrings;
import org.eclipse.tracecompass.ctf.core.event.types.Declaration;
import org.eclipse.tracecompass.ctf.core.event.types.EnumDefinition;
Expand Down Expand Up @@ -234,8 +235,8 @@ public StreamInputPacketIndexEntry(ICTFPacketDescriptor entryToAdd, long newTime
} else if (id instanceof EnumDefinition) {
final EnumDefinition enumDec = (EnumDefinition) id;
attributeBuilder.put(fieldName, new AbstractMap.SimpleImmutableEntry<>(
NonNullUtils.checkNotNull(enumDec.getStringValue()),
NonNullUtils.checkNotNull(enumDec.getIntegerValue())));
requireNonNull(enumDec.getStringValue()),
requireNonNull(enumDec.getIntegerValue())));
} else if (id instanceof StringDefinition) {
attributeBuilder.put(fieldName, ((StringDefinition) id).getValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package org.eclipse.tracecompass.internal.ctf.core.trace;

import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
import static java.util.Objects.requireNonNull;

import java.io.Serializable;
import java.util.Comparator;
Expand Down Expand Up @@ -48,8 +48,8 @@ public class StreamInputReaderTimestampComparator implements
*/
@Override
public int compare(CTFStreamInputReader a, CTFStreamInputReader b) {
IEventDefinition eventA = checkNotNull(a.getCurrentEvent());
IEventDefinition eventB = checkNotNull(b.getCurrentEvent());
IEventDefinition eventA = requireNonNull(a.getCurrentEvent());
IEventDefinition eventB = requireNonNull(b.getCurrentEvent());

long ta = eventA.getTimestamp();
long tb = eventB.getTimestamp();
Expand Down

0 comments on commit 5890f93

Please sign in to comment.