Skip to content

Commit 4fbeccd

Browse files
committed
Error Prone / NullAway support for JSpecify - added Kotlin
1 parent 3f676ac commit 4fbeccd

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

build.gradle

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
2+
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
3+
import net.ltgt.gradle.errorprone.CheckSeverity
14
import java.text.SimpleDateFormat
25

36
plugins {
@@ -12,6 +15,9 @@ plugins {
1215
id 'com.github.ben-manes.versions' version '0.51.0'
1316
id "me.champeau.jmh" version "0.7.3"
1417
id "net.ltgt.errorprone" version '4.2.0'
18+
19+
// Kotlin just for tests - not
20+
id 'org.jetbrains.kotlin.jvm' version '2.1.21'
1521
}
1622

1723
java {
@@ -20,6 +26,19 @@ java {
2026
}
2127
}
2228

29+
kotlin {
30+
compilerOptions {
31+
apiVersion = KotlinVersion.KOTLIN_2_0
32+
languageVersion = KotlinVersion.KOTLIN_2_0
33+
jvmTarget = JvmTarget.JVM_11
34+
javaParameters = true
35+
freeCompilerArgs = [
36+
'-Xemit-jvm-type-annotations',
37+
'-Xjspecify-annotations=strict',
38+
]
39+
}
40+
}
41+
2342
def getDevelopmentVersion() {
2443
def output = new StringBuilder()
2544
def error = new StringBuilder()
@@ -79,9 +98,10 @@ dependencies {
7998

8099
errorprone 'com.uber.nullaway:nullaway:0.12.6'
81100
errorprone 'com.google.errorprone:error_prone_core:2.37.0'
82-
}
83101

84-
import net.ltgt.gradle.errorprone.CheckSeverity
102+
// just tests
103+
testCompileOnly 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
104+
}
85105

86106
tasks.withType(JavaCompile) {
87107
options.release = 11

src/main/java/org/dataloader/DataLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
*/
6969
@PublicApi
7070
@NullMarked
71-
public class DataLoader<K, V> {
71+
public class DataLoader<K, V extends @Nullable Object> {
7272

7373
private final @Nullable String name;
7474
private final DataLoaderHelper<K, V> helper;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.dataloader
2+
3+
import org.junit.jupiter.api.Test
4+
import java.util.concurrent.CompletableFuture
5+
6+
/**
7+
* Some Kotlin code to prove that are JSpecify annotations work here
8+
* as expected in Kotlin land. We don't intend to ue Kotlin in our tests
9+
* or to deliver Kotlin code in the java
10+
*/
11+
class KotlinExamples {
12+
13+
@Test
14+
fun `basic kotlin test of non nullable value types`() {
15+
val dataLoader: DataLoader<String, String> = DataLoaderFactory.newDataLoader { keys -> CompletableFuture.completedFuture(keys.toList()) }
16+
17+
val cfA = dataLoader.load("A")
18+
val cfB = dataLoader.load("B")
19+
20+
dataLoader.dispatch()
21+
22+
cfA.join().equals("A")
23+
cfB.join().equals("B")
24+
}
25+
26+
@Test
27+
fun `basic kotlin test of nullable value types`() {
28+
val dataLoader: DataLoader<String, String?> = DataLoaderFactory.newDataLoader { keys -> CompletableFuture.completedFuture(keys.toList()) }
29+
30+
val cfA = dataLoader.load("A")
31+
val cfB = dataLoader.load("B")
32+
33+
dataLoader.dispatch()
34+
35+
cfA.join().equals("A")
36+
cfB.join().equals("B")
37+
}
38+
39+
}

0 commit comments

Comments
 (0)