Skip to content

Commit 960ee80

Browse files
committed
Configure nullability using a JavaCompile options extension
1 parent 657039e commit 960ee80

File tree

2 files changed

+94
-28
lines changed

2 files changed

+94
-28
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 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 io.spring.gradle.nullability;
18+
19+
import java.util.Collections;
20+
import java.util.LinkedHashMap;
21+
import java.util.Map;
22+
23+
import javax.inject.Inject;
24+
25+
import net.ltgt.gradle.errorprone.CheckSeverity;
26+
import net.ltgt.gradle.errorprone.ErrorProneOptions;
27+
import org.gradle.api.provider.Property;
28+
import org.gradle.api.tasks.compile.JavaCompile;
29+
30+
/**
31+
* Nullability configuration options for a {@link JavaCompile} task.
32+
*
33+
* @author Andy Wilkinson
34+
*/
35+
public abstract class NullabilityOptions {
36+
37+
/**
38+
* Internal use only.
39+
* @param errorProne the ErrorProne options to which the nullability options are
40+
* applied
41+
*/
42+
@Inject
43+
public NullabilityOptions(ErrorProneOptions errorProne) {
44+
errorProne.getEnabled().set(getChecking().map((checking) -> checking != Checking.DISABLED));
45+
errorProne.getDisableAllChecks().set(getChecking().map((checking) -> checking != Checking.DISABLED));
46+
errorProne.getCheckOptions().putAll(getChecking().map((checking) -> {
47+
Map<String, String> options = new LinkedHashMap<>();
48+
if (checking == Checking.MAIN) {
49+
options.put("NullAway:OnlyNullMarked", "true");
50+
options.put("NullAway:CustomContractAnnotations", "org.springframework.lang.Contract");
51+
options.put("NullAway:JSpecifyMode", "true");
52+
}
53+
return options;
54+
}));
55+
errorProne.getChecks().putAll(getChecking().map((checking) -> {
56+
if (checking == Checking.MAIN) {
57+
return Map.of("NullAway", CheckSeverity.ERROR);
58+
}
59+
return Collections.emptyMap();
60+
}));
61+
}
62+
63+
/**
64+
* Returns the type of checking to perform.
65+
* @return the type of checking
66+
*/
67+
public abstract Property<Checking> getChecking();
68+
69+
/**
70+
* The type of checking to perform for the {@link JavaCompile} task.
71+
*/
72+
public enum Checking {
73+
74+
/**
75+
* Main code nullability checking is performed.
76+
*/
77+
MAIN,
78+
79+
/**
80+
* Nullability checking is disabled.
81+
*/
82+
DISABLED
83+
84+
}
85+
86+
}

src/main/java/io/spring/gradle/nullability/NullabilityPlugin.java

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package io.spring.gradle.nullability;
1818

19-
import java.util.function.Consumer;
2019
import java.util.regex.Pattern;
2120

2221
import net.ltgt.gradle.errorprone.ErrorProneOptions;
@@ -27,6 +26,8 @@
2726
import org.gradle.api.tasks.compile.CompileOptions;
2827
import org.gradle.api.tasks.compile.JavaCompile;
2928

29+
import io.spring.gradle.nullability.NullabilityOptions.Checking;
30+
3031
/**
3132
* Gradle plugin for compile-time verification of nullability.
3233
*
@@ -58,38 +59,17 @@ private void configureDependencies(Project project, NullabilityPluginExtension n
5859

5960
private void configureJavaCompilation(Project project) {
6061
project.getTasks().withType(JavaCompile.class).configureEach((javaCompile) -> {
61-
if (compilesMainSources(javaCompile)) {
62-
configureErrorProne(javaCompile);
63-
}
64-
else {
65-
disableErrorProne(javaCompile);
66-
}
62+
CompileOptions options = javaCompile.getOptions();
63+
ErrorProneOptions errorProneOptions = ((ExtensionAware) options).getExtensions()
64+
.getByType(ErrorProneOptions.class);
65+
NullabilityOptions nullabilityOptions = ((ExtensionAware) javaCompile.getOptions()).getExtensions()
66+
.create("nullability", NullabilityOptions.class, errorProneOptions);
67+
nullabilityOptions.getChecking().set(compilesMainSources(javaCompile) ? Checking.MAIN : Checking.DISABLED);
6768
});
6869
}
6970

7071
private boolean compilesMainSources(JavaCompile compileTask) {
7172
return COMPILE_MAIN_SOURCES_TASK_NAME.matcher(compileTask.getName()).matches();
7273
}
7374

74-
private void configureErrorProne(JavaCompile javaCompile) {
75-
errorProneOptions(javaCompile, (options) -> {
76-
options.getDisableAllChecks().set(true);
77-
options.option("NullAway:OnlyNullMarked", "true");
78-
options.option("NullAway:CustomContractAnnotations", "org.springframework.lang.Contract");
79-
options.option("NullAway:JSpecifyMode", "true");
80-
options.error("NullAway");
81-
});
82-
}
83-
84-
private void disableErrorProne(JavaCompile javaCompile) {
85-
errorProneOptions(javaCompile, (errorProneOptions) -> errorProneOptions.getEnabled().set(false));
86-
}
87-
88-
private void errorProneOptions(JavaCompile compileTask, Consumer<ErrorProneOptions> optionsConsumer) {
89-
CompileOptions options = compileTask.getOptions();
90-
ErrorProneOptions errorProneOptions = ((ExtensionAware) options).getExtensions()
91-
.getByType(ErrorProneOptions.class);
92-
optionsConsumer.accept(errorProneOptions);
93-
}
94-
9575
}

0 commit comments

Comments
 (0)