Skip to content

Commit bba1412

Browse files
committed
refactor: introduce Nullable annotation
1 parent 98aeaab commit bba1412

File tree

14 files changed

+74
-5
lines changed

14 files changed

+74
-5
lines changed

docs/src/content/docs/architecture/goals.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ provides detailed information on how to use the library.
6161
FastCSV is designed to be maintainable to ensure its longevity, continued development and support.
6262
Checkout our [FAQ](/faq/) for more information on how to get help and how to contribute.
6363

64-
[Spotbugs](https://spotbugs.github.io), [PMD](https://pmd.github.io) and
64+
[Spotbugs](https://spotbugs.github.io),
65+
[PMD](https://pmd.github.io),
66+
[Error Prone](https://errorprone.info),
67+
[NullAway](https://github.com/uber/NullAway) and
6568
[Checkstyle](https://checkstyle.sourceforge.io) are used to ensure the quality of the code.
6669

6770
## Non-Goals

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ gummy-bears = { module = "com.toasttab.android:gummy-bears-api-34", version = "0
66
spotbugs = { module = "com.github.spotbugs.snom:spotbugs-gradle-plugin", version = "6.1.11" }
77
jackson-dataformat-yaml = { module = "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml", version = "2.18.3" }
88
errorprone = { module = "com.google.errorprone:error_prone_core", version = "2.38.0" }
9+
nullaway = { module = "com.uber.nullaway:nullaway", version = "0.12.7" }
910

1011
[plugins]
1112
jmh = { id = "me.champeau.jmh", version = "0.7.3" }

lib/build.gradle.kts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@file:Suppress("StringLiteralDuplication")
22
import net.ltgt.gradle.errorprone.errorprone
3+
import org.gradle.kotlin.dsl.errorprone
34

45
plugins {
56
id("fastcsv.java-conventions")
@@ -27,12 +28,19 @@ java {
2728
tasks.withType<JavaCompile>().configureEach {
2829
if (name == "compileJmhJava") {
2930
options.errorprone.isEnabled.set(false)
31+
} else if (name == "compileTestJava" || name == "compileIntTestJava") {
32+
options.errorprone.disable("NullAway")
33+
} else {
34+
options.errorprone {
35+
option("NullAway:AnnotatedPackages", "de.siegmar.fastcsv")
36+
}
3037
}
3138
}
3239

3340
tasks.compileJava {
3441
options.release.set(17)
3542
options.compilerArgs.addAll(listOf("-Xlint:all", "-Werror"))
43+
options.errorprone.error("NullAway")
3644
}
3745

3846
// enable parameter names for tests with @ParameterizedTest
@@ -68,6 +76,7 @@ configurations[intTest.runtimeOnlyConfigurationName].extendsFrom(configurations.
6876

6977
dependencies {
7078
errorprone(libs.errorprone)
79+
errorprone(libs.nullaway)
7180

7281
commonImplementation(libs.assertj.core)
7382

lib/src/main/java/de/siegmar/fastcsv/reader/AbstractBaseCsvCallbackHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
public abstract class AbstractBaseCsvCallbackHandler<T> extends CsvCallbackHandler<T> {
99

1010
private long startingLineNumber;
11-
private RecordType recordType;
11+
private RecordType recordType = RecordType.DATA;
1212
private int fieldCount;
1313

1414
/// Constructs a new instance.

lib/src/main/java/de/siegmar/fastcsv/reader/AbstractInternalCsvCallbackHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public abstract sealed class AbstractInternalCsvCallbackHandler<T> extends CsvCa
3939
protected int fieldIdx;
4040

4141
/// The type of the current record.
42-
protected RecordType recordType;
42+
protected RecordType recordType = RecordType.DATA;
4343

4444
/// Constructs a new instance with the given configuration.
4545
///

lib/src/main/java/de/siegmar/fastcsv/reader/BomInputStreamReader.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66
import java.io.Reader;
77
import java.nio.charset.Charset;
88

9+
import de.siegmar.fastcsv.util.Nullable;
10+
911
/// InputStreamReader that is capable of detecting and handling BOM headers.
1012
final class BomInputStreamReader extends Reader {
1113

1214
private final InputStream inputStream;
1315
private final Charset defaultCharset;
16+
17+
@Nullable
1418
private Reader reader;
1519

1620
BomInputStreamReader(final InputStream inputStream, final Charset defaultCharset) {

lib/src/main/java/de/siegmar/fastcsv/reader/CsvCallbackHandler.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package de.siegmar.fastcsv.reader;
22

3+
import de.siegmar.fastcsv.util.Nullable;
4+
35
/// This class defines the methods that are called during the CSV reading process.
46
///
57
/// Implementations highly affect the behavior of the [CsvReader]. With great power comes great responsibility.
@@ -91,6 +93,7 @@ protected CsvCallbackHandler() {
9193
/// Called at the end of each CSV record to build the actual record representation.
9294
///
9395
/// @return the record or `null` if the record should be ignored/skipped as it is consumed by the callback handler.
96+
@Nullable
9497
protected abstract T buildRecord();
9598

9699
/// Called at the end of the CSV reading process.

lib/src/main/java/de/siegmar/fastcsv/reader/CsvParser.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.io.Closeable;
44
import java.io.IOException;
55

6+
import de.siegmar.fastcsv.util.Nullable;
7+
68
sealed interface CsvParser extends Closeable permits StrictCsvParser, RelaxedCsvParser {
79

810
/// Parses the next record from the stream and passes it to the callback handler.
@@ -11,6 +13,7 @@ sealed interface CsvParser extends Closeable permits StrictCsvParser, RelaxedCsv
1113
boolean parse() throws IOException;
1214

1315
/// {@return the next line from the stream without consuming it.}
16+
@Nullable
1417
String peekLine() throws IOException;
1518

1619
/// Skips a line in the stream after skipping the specified number of characters.

lib/src/main/java/de/siegmar/fastcsv/reader/CsvReader.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.stream.Stream;
2020
import java.util.stream.StreamSupport;
2121

22+
import de.siegmar.fastcsv.util.Nullable;
2223
import de.siegmar.fastcsv.util.Preconditions;
2324

2425
/// This is the main class for reading CSV data.
@@ -210,6 +211,7 @@ public Stream<T> stream() {
210211
});
211212
}
212213

214+
@Nullable
213215
private T fetchRecord() throws IOException {
214216
while (csvParser.parse()) {
215217
final T csvRecord = processRecord();
@@ -224,6 +226,7 @@ private T fetchRecord() throws IOException {
224226
return null;
225227
}
226228

229+
@Nullable
227230
@SuppressWarnings("checkstyle:ReturnCount")
228231
private T processRecord() {
229232
final T csvRecord = callbackHandler.buildRecord();
@@ -275,6 +278,7 @@ public String toString() {
275278
.toString();
276279
}
277280

281+
@Nullable
278282
@SuppressWarnings({"checkstyle:IllegalCatch", "PMD.AvoidCatchingThrowable"})
279283
private T fetch() {
280284
try {
@@ -304,6 +308,7 @@ public boolean tryAdvance(final Consumer<? super T> action) {
304308
return false;
305309
}
306310

311+
@Nullable
307312
@Override
308313
public Spliterator<T> trySplit() {
309314
return null;
@@ -323,7 +328,9 @@ public int characteristics() {
323328

324329
private class CsvRecordIterator implements CloseableIterator<T> {
325330

331+
@Nullable
326332
private T fetchedRecord;
333+
327334
private boolean fetched;
328335

329336
@Override

lib/src/main/java/de/siegmar/fastcsv/reader/IndexedCsvReader.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.concurrent.locks.Lock;
1919
import java.util.concurrent.locks.ReentrantLock;
2020

21+
import de.siegmar.fastcsv.util.Nullable;
2122
import de.siegmar.fastcsv.util.Preconditions;
2223
import de.siegmar.fastcsv.util.Util;
2324

@@ -64,7 +65,8 @@ public final class IndexedCsvReader<T> implements Closeable {
6465
final boolean allowExtraCharsAfterClosingQuote,
6566
final int maxBufferSize,
6667
final int pageSize,
67-
final CsvCallbackHandler<T> csvRecordHandler, final CsvIndex csvIndex,
68+
final CsvCallbackHandler<T> csvRecordHandler,
69+
@Nullable final CsvIndex csvIndex,
6870
final StatusListener statusListener)
6971
throws IOException {
7072

@@ -278,9 +280,15 @@ public static final class IndexedCsvReaderBuilder {
278280
private CommentStrategy commentStrategy = CommentStrategy.NONE;
279281
private char commentCharacter = '#';
280282
private boolean allowExtraCharsAfterClosingQuote;
283+
284+
@Nullable
281285
private StatusListener statusListener;
286+
282287
private int pageSize = DEFAULT_PAGE_SIZE;
288+
289+
@Nullable
283290
private CsvIndex csvIndex;
291+
284292
private int maxBufferSize = DEFAULT_MAX_BUFFER_SIZE;
285293

286294
private IndexedCsvReaderBuilder() {

0 commit comments

Comments
 (0)