|
| 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 java.util.function.Consumer; |
| 20 | +import java.util.regex.Pattern; |
| 21 | + |
| 22 | +import net.ltgt.gradle.errorprone.ErrorProneOptions; |
| 23 | +import net.ltgt.gradle.errorprone.ErrorPronePlugin; |
| 24 | +import org.gradle.api.Plugin; |
| 25 | +import org.gradle.api.Project; |
| 26 | +import org.gradle.api.artifacts.DependencySet; |
| 27 | +import org.gradle.api.plugins.ExtensionAware; |
| 28 | +import org.gradle.api.plugins.JavaPlugin; |
| 29 | +import org.gradle.api.tasks.compile.CompileOptions; |
| 30 | +import org.gradle.api.tasks.compile.JavaCompile; |
| 31 | + |
| 32 | +/** |
| 33 | + * {@link Plugin} for enforcing Nullability checks on the source code. |
| 34 | + * |
| 35 | + * @author Brian Clozel |
| 36 | + */ |
| 37 | +public class NullabilityPlugin implements Plugin<Project> { |
| 38 | + |
| 39 | + private static final Pattern COMPILE_MAIN_SOURCES_TASK_NAME = Pattern.compile("compile(\\d+)?Java"); |
| 40 | + |
| 41 | + @Override |
| 42 | + public void apply(Project project) { |
| 43 | + project.getPlugins().apply(ErrorPronePlugin.class); |
| 44 | + DependencySet errorproneConfig = project.getConfigurations().getByName("errorprone").getDependencies(); |
| 45 | + errorproneConfig.add(project.getDependencies().create("com.uber.nullaway:nullaway:0.12.6")); |
| 46 | + errorproneConfig.add(project.getDependencies().create("com.google.errorprone:error_prone_core:2.37.0")); |
| 47 | + |
| 48 | + project.getTasks() |
| 49 | + .withType(JavaCompile.class) |
| 50 | + .configureEach((javaCompile) -> { |
| 51 | + if (compilesMainSources(javaCompile)) { |
| 52 | + doWithErrorProneOptions(javaCompile, (errorProneOptions) -> { |
| 53 | + errorProneOptions.getDisableAllChecks().set(true); |
| 54 | + errorProneOptions.option("NullAway:OnlyNullMarked", "true"); |
| 55 | + errorProneOptions.option("NullAway:CustomContractAnnotations", "org.springframework.lang.Contract"); |
| 56 | + errorProneOptions.option("NullAway:JSpecifyMode", "true"); |
| 57 | + errorProneOptions.error("NullAway"); |
| 58 | + }); |
| 59 | + } |
| 60 | + else { |
| 61 | + doWithErrorProneOptions(javaCompile, (errorProneOptions) -> { |
| 62 | + errorProneOptions.getEnabled().set(false); |
| 63 | + }); |
| 64 | + } |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + private boolean compilesMainSources(JavaCompile compileTask) { |
| 69 | + return COMPILE_MAIN_SOURCES_TASK_NAME.matcher(compileTask.getName()).matches(); |
| 70 | + } |
| 71 | + |
| 72 | + private void doWithErrorProneOptions(JavaCompile compileTask, Consumer<ErrorProneOptions> optionsConsumer) { |
| 73 | + CompileOptions options = compileTask.getOptions(); |
| 74 | + ErrorProneOptions errorProneOptions = ((ExtensionAware) options).getExtensions().getByType(ErrorProneOptions.class); |
| 75 | + optionsConsumer.accept(errorProneOptions); |
| 76 | + } |
| 77 | + |
| 78 | +} |
0 commit comments