Skip to content

Commit 546099c

Browse files
committed
Configure Nullabillity checks with NullAway
1 parent 3ad4d3d commit 546099c

File tree

6 files changed

+69
-4
lines changed

6 files changed

+69
-4
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ ext {
1010
subprojects {
1111
apply plugin: 'org.springframework.graphql.conventions'
1212
apply plugin: 'org.springframework.graphql.architecture'
13+
apply plugin: 'org.springframework.graphql.nullability'
1314
group = 'org.springframework.graphql'
1415

1516
ext.javadocLinks = [

buildSrc/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ dependencies {
2424
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
2525
implementation("org.jetbrains.kotlin:kotlin-compiler-embeddable:${kotlinVersion}")
2626
implementation("io.spring.javaformat:spring-javaformat-gradle-plugin:${javaFormatVersion}")
27+
implementation("net.ltgt.errorprone:net.ltgt.errorprone.gradle.plugin:${errorProneVersion}")
2728
implementation "com.tngtech.archunit:archunit:1.4.0"
2829
}
2930

@@ -41,6 +42,10 @@ gradlePlugin {
4142
id = "org.springframework.graphql.architecture"
4243
implementationClass = "org.springframework.graphql.build.architecture.ArchitecturePlugin"
4344
}
45+
nullability {
46+
id = "org.springframework.graphql.nullability"
47+
implementationClass = "org.springframework.graphql.build.nullability.NullabilityPlugin"
48+
}
4449
}
4550
}
4651

buildSrc/gradle.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
javaFormatVersion=0.0.43
1+
javaFormatVersion=0.0.43
2+
errorProneVersion=4.2.0

buildSrc/src/main/java/org/springframework/graphql/build/architecture/ArchitectureCheck.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@
4545
import org.gradle.api.tasks.TaskAction;
4646

4747
import static org.springframework.graphql.build.architecture.ArchitectureRules.allPackagesShouldBeFreeOfTangles;
48+
import static org.springframework.graphql.build.architecture.ArchitectureRules.classShouldNotUseSpringNullAnnotations;
4849
import static org.springframework.graphql.build.architecture.ArchitectureRules.classesShouldNotImportForbiddenTypes;
4950
import static org.springframework.graphql.build.architecture.ArchitectureRules.javaClassesShouldNotImportKotlinAnnotations;
5051
import static org.springframework.graphql.build.architecture.ArchitectureRules.noClassesShouldCallStringToLowerCaseWithoutLocale;
5152
import static org.springframework.graphql.build.architecture.ArchitectureRules.noClassesShouldCallStringToUpperCaseWithoutLocale;
53+
import static org.springframework.graphql.build.architecture.ArchitectureRules.packageInfoShouldBeNullMarked;
5254

5355
/**
5456
* {@link Task} that checks for architecture problems.
@@ -68,7 +70,9 @@ public ArchitectureCheck() {
6870
javaClassesShouldNotImportKotlinAnnotations(),
6971
allPackagesShouldBeFreeOfTangles(),
7072
noClassesShouldCallStringToLowerCaseWithoutLocale(),
71-
noClassesShouldCallStringToUpperCaseWithoutLocale());
73+
noClassesShouldCallStringToUpperCaseWithoutLocale(),
74+
packageInfoShouldBeNullMarked(),
75+
classShouldNotUseSpringNullAnnotations());
7276
getRuleDescriptions().set(getRules().map((rules) -> rules.stream().map(ArchRule::getDescription).toList()));
7377
}
7478

buildSrc/src/main/java/org/springframework/graphql/build/architecture/ArchitectureRules.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ static ArchRule packageInfoShouldBeNullMarked() {
5555
static ArchRule classShouldNotUseSpringNullAnnotations() {
5656
return ArchRuleDefinition.noClasses()
5757
.should().dependOnClassesThat()
58-
.haveFullyQualifiedName("org.springframework.lang.NonNull")
58+
.haveFullyQualifiedName("org.jspecify.annotations.NonNull")
5959
.orShould().dependOnClassesThat()
60-
.haveFullyQualifiedName("org.springframework.lang.Nullable");
60+
.haveFullyQualifiedName("org.jspecify.annotations.Nullable");
6161
}
6262

6363

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2020-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.graphql.build.nullability;
18+
19+
import net.ltgt.gradle.errorprone.ErrorProneOptions;
20+
import net.ltgt.gradle.errorprone.ErrorPronePlugin;
21+
import org.gradle.api.Plugin;
22+
import org.gradle.api.Project;
23+
import org.gradle.api.artifacts.DependencySet;
24+
import org.gradle.api.plugins.ExtensionAware;
25+
import org.gradle.api.tasks.compile.CompileOptions;
26+
import org.gradle.api.tasks.compile.JavaCompile;
27+
28+
/**
29+
* {@link Plugin} for enforcing Nullability checks on the source code.
30+
*
31+
* @author Brian Clozel
32+
*/
33+
public class NullabilityPlugin implements Plugin<Project> {
34+
35+
@Override
36+
public void apply(Project project) {
37+
new ErrorPronePlugin(project.getProviders()).apply(project);
38+
DependencySet errorproneConfig = project.getConfigurations().getByName("errorprone").getDependencies();
39+
errorproneConfig.add(project.getDependencies().create("com.uber.nullaway:nullaway:0.12.6"));
40+
errorproneConfig.add(project.getDependencies().create("com.google.errorprone:error_prone_core:2.37.0"));
41+
42+
project.getTasks().withType(JavaCompile.class).configureEach((javaCompile) -> {
43+
CompileOptions options = javaCompile.getOptions();
44+
ErrorProneOptions errorProneOptions = ((ExtensionAware) options).getExtensions().getByType(ErrorProneOptions.class);
45+
errorProneOptions.getDisableAllChecks().set(true);
46+
errorProneOptions.option("NullAway:OnlyNullMarked", "true");
47+
errorProneOptions.option("NullAway:CustomContractAnnotations", "org.springframework.lang.Contract");
48+
errorProneOptions.option("NullAway:JSpecifyMode", "true");
49+
errorProneOptions.error("NullAway");
50+
});
51+
52+
}
53+
54+
}

0 commit comments

Comments
 (0)