Skip to content

Commit 27a71b2

Browse files
authored
Merge pull request #3985 from graphql-java/enforce-jspecify-and-no-other-nullability-libs
Add ArchUnit rule to ban javax & JetBrains nullability annotations
2 parents 30d642e + 08c8bd3 commit 27a71b2

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ shadowJar {
176176
bnd('''
177177
-exportcontents: graphql.*
178178
-removeheaders: Private-Package
179-
Import-Package: !android.os.*,!com.google.*,!org.checkerframework.*,!javax.annotation.*,!graphql.com.google.*,!org.antlr.*,!graphql.org.antlr.*,!sun.misc.*,org.jspecify.annotations;resolution:=optional,*
179+
Import-Package: !android.os.*,!com.google.*,!org.checkerframework.*,!graphql.com.google.*,!org.antlr.*,!graphql.org.antlr.*,!sun.misc.*,org.jspecify.annotations;resolution:=optional,*
180180
''')
181181
}
182182

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package graphql
2+
3+
import com.tngtech.archunit.core.domain.JavaClasses
4+
import com.tngtech.archunit.core.importer.ClassFileImporter
5+
import com.tngtech.archunit.core.importer.ImportOption
6+
import com.tngtech.archunit.lang.ArchRule
7+
import com.tngtech.archunit.lang.EvaluationResult
8+
import com.tngtech.archunit.lang.syntax.ArchRuleDefinition
9+
import spock.lang.Specification
10+
11+
class NullabilityAnnotationUsageTest extends Specification {
12+
13+
private static final JavaClasses importedClasses = new ClassFileImporter()
14+
.withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS)
15+
.importPackages("graphql")
16+
17+
def "should only use JSpecify nullability annotations"() {
18+
given:
19+
ArchRule dependencyRule = ArchRuleDefinition.noClasses()
20+
.should()
21+
.dependOnClassesThat()
22+
.resideInAnyPackage(
23+
"javax.annotation",
24+
"org.jetbrains.annotations"
25+
)
26+
.because("We are using JSpecify nullability annotations only. Please change to use JSpecify.")
27+
28+
when:
29+
EvaluationResult result = dependencyRule.evaluate(importedClasses)
30+
31+
then:
32+
if (result.hasViolation()) {
33+
println "We are using JSpecify nullability annotations only. Please change the following to use JSpecify instead:"
34+
result.getFailureReport().getDetails().each { violation ->
35+
println "- ${violation}"
36+
}
37+
}
38+
!result.hasViolation()
39+
}
40+
}

0 commit comments

Comments
 (0)