Skip to content

Commit 3f676ac

Browse files
committed
Error Prone / NullAway support for JSpecify
1 parent cabda40 commit 3f676ac

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

build.gradle

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ plugins {
1111
id 'io.github.gradle-nexus.publish-plugin' version '1.0.0'
1212
id 'com.github.ben-manes.versions' version '0.51.0'
1313
id "me.champeau.jmh" version "0.7.3"
14+
id "net.ltgt.errorprone" version '4.2.0'
1415
}
1516

1617
java {
1718
toolchain {
18-
languageVersion = JavaLanguageVersion.of(11)
19+
languageVersion = JavaLanguageVersion.of(17)
1920
}
2021
}
2122

@@ -75,8 +76,34 @@ dependencies {
7576
// this is needed for the idea jmh plugin to work correctly
7677
jmh 'org.openjdk.jmh:jmh-core:1.37'
7778
jmh 'org.openjdk.jmh:jmh-generator-annprocess:1.37'
79+
80+
errorprone 'com.uber.nullaway:nullaway:0.12.6'
81+
errorprone 'com.google.errorprone:error_prone_core:2.37.0'
82+
}
83+
84+
import net.ltgt.gradle.errorprone.CheckSeverity
85+
86+
tasks.withType(JavaCompile) {
87+
options.release = 11
88+
options.errorprone {
89+
disableAllChecks = true
90+
check("NullAway", CheckSeverity.ERROR)
91+
//
92+
// end state has us with this config turned on - eg all classes
93+
//
94+
//option("NullAway:AnnotatedPackages", "org.dataloader")
95+
option("NullAway:OnlyNullMarked", "true")
96+
option("NullAway:JSpecifyMode", "true")
97+
}
98+
// Include to disable NullAway on test code
99+
if (name.toLowerCase().contains("test")) {
100+
options.errorprone {
101+
disable("NullAway")
102+
}
103+
}
78104
}
79105

106+
80107
task sourcesJar(type: Jar) {
81108
dependsOn classes
82109
archiveClassifier.set('sources')

src/main/java/org/dataloader/BatchLoaderEnvironment.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
@NullMarked
2020
public class BatchLoaderEnvironment {
2121

22-
private final Object context;
22+
private final @Nullable Object context;
2323
private final Map<Object, Object> keyContexts;
2424
private final List<Object> keyContextsList;
2525

26-
private BatchLoaderEnvironment(Object context, List<Object> keyContextsList, Map<Object, Object> keyContexts) {
26+
private BatchLoaderEnvironment(@Nullable Object context, List<Object> keyContextsList, Map<Object, Object> keyContexts) {
2727
this.context = context;
2828
this.keyContexts = keyContexts;
2929
this.keyContextsList = keyContextsList;
@@ -33,7 +33,6 @@ private BatchLoaderEnvironment(Object context, List<Object> keyContextsList, Map
3333
* Returns the overall context object provided by {@link org.dataloader.BatchLoaderContextProvider}
3434
*
3535
* @param <T> the type you would like the object to be
36-
*
3736
* @return a context object or null if there isn't one
3837
*/
3938
@SuppressWarnings("unchecked")
@@ -68,7 +67,7 @@ public static Builder newBatchLoaderEnvironment() {
6867
}
6968

7069
public static class Builder {
71-
private Object context;
70+
private @Nullable Object context;
7271
private Map<Object, Object> keyContexts = Collections.emptyMap();
7372
private List<Object> keyContextsList = Collections.emptyList();
7473

src/main/java/org/dataloader/DataLoaderRegistry.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.dataloader;
22

33
import org.dataloader.annotations.PublicApi;
4+
import org.dataloader.impl.Assertions;
45
import org.dataloader.instrumentation.ChainedDataLoaderInstrumentation;
56
import org.dataloader.instrumentation.DataLoaderInstrumentation;
67
import org.dataloader.instrumentation.DataLoaderInstrumentationHelper;
@@ -14,6 +15,7 @@
1415
import java.util.LinkedHashMap;
1516
import java.util.List;
1617
import java.util.Map;
18+
import java.util.Objects;
1719
import java.util.Set;
1820
import java.util.concurrent.ConcurrentHashMap;
1921
import java.util.function.Function;
@@ -142,7 +144,7 @@ private static DataLoaderOptions setInInstrumentation(DataLoaderOptions options,
142144
*/
143145
public DataLoaderRegistry register(DataLoader<?, ?> dataLoader) {
144146
String name = dataLoader.getName();
145-
assertState(name != null, () -> "The DataLoader must have a non null name");
147+
Objects.requireNonNull(name, "The DataLoader must have a non null name");
146148
dataLoaders.put(name, nameAndInstrumentDL(name, instrumentation, dataLoader));
147149
return this;
148150
}
@@ -176,7 +178,7 @@ public DataLoaderRegistry register(String key, DataLoader<?, ?> dataLoader) {
176178
*/
177179
public <K, V> DataLoader<K, V> registerAndGet(String key, DataLoader<?, ?> dataLoader) {
178180
dataLoaders.put(key, nameAndInstrumentDL(key, instrumentation, dataLoader));
179-
return getDataLoader(key);
181+
return Objects.requireNonNull(getDataLoader(key));
180182
}
181183

182184
/**
@@ -251,10 +253,10 @@ public DataLoaderRegistry unregister(String key) {
251253
* @param key the key of the data loader
252254
* @param <K> the type of keys
253255
* @param <V> the type of values
254-
* @return a data loader or null if its not present
256+
* @return a data loader or null if it's not present
255257
*/
256258
@SuppressWarnings("unchecked")
257-
public <K, V> DataLoader<K, V> getDataLoader(String key) {
259+
public <K, V> @Nullable DataLoader<K, V> getDataLoader(String key) {
258260
return (DataLoader<K, V>) dataLoaders.get(key);
259261
}
260262

src/test/java/org/dataloader/DataLoaderRegistryTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void registration_works() {
6363
try {
6464
registry.register(dlUnnamed);
6565
Assertions.fail("Should have thrown an exception");
66-
} catch (DataLoaderAssertionException ignored) {
66+
} catch (NullPointerException ignored) {
6767
}
6868
}
6969

0 commit comments

Comments
 (0)