|
| 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 | +} |
0 commit comments