|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package groovy.typecheckers |
| 20 | + |
| 21 | +import org.apache.groovy.lang.annotation.Incubating |
| 22 | +import org.apache.groovy.typecheckers.NullCheckingVisitor |
| 23 | +import org.codehaus.groovy.transform.stc.GroovyTypeCheckingExtensionSupport |
| 24 | + |
| 25 | +/** |
| 26 | + * A compile-time type checker that detects potential null dereferences and null-safety violations |
| 27 | + * in code annotated with {@code @Nullable}, {@code @NonNull}, and {@code @MonotonicNonNull} annotations. |
| 28 | + * <p> |
| 29 | + * This checker performs annotation-based null checking only. For additional flow-sensitive analysis |
| 30 | + * that tracks nullability through assignments and control flow (even in unannotated code), |
| 31 | + * use {@link StrictNullChecker} instead. |
| 32 | + * <p> |
| 33 | + * Supported annotations are recognized by simple name from any package: |
| 34 | + * <ul> |
| 35 | + * <li>Nullable: {@code @Nullable}, {@code @CheckForNull}, {@code @MonotonicNonNull}</li> |
| 36 | + * <li>Non-null: {@code @NonNull}, {@code @NotNull}, {@code @Nonnull}</li> |
| 37 | + * </ul> |
| 38 | + * <p> |
| 39 | + * Detected errors include: |
| 40 | + * <ul> |
| 41 | + * <li>Assigning {@code null} to a {@code @NonNull} variable</li> |
| 42 | + * <li>Passing {@code null} or a {@code @Nullable} value to a {@code @NonNull} parameter</li> |
| 43 | + * <li>Returning {@code null} or a {@code @Nullable} value from a {@code @NonNull} method</li> |
| 44 | + * <li>Dereferencing a {@code @Nullable} variable without a null check or safe navigation ({@code ?.})</li> |
| 45 | + * <li>Dereferencing the result of a {@code @Nullable}-returning method without a null check</li> |
| 46 | + * <li>Re-assigning {@code null} to a {@code @MonotonicNonNull} field after initialization</li> |
| 47 | + * </ul> |
| 48 | + * <p> |
| 49 | + * The checker recognizes null guards ({@code if (x != null)}), early exit patterns |
| 50 | + * ({@code if (x == null) return/throw}), and safe navigation ({@code ?.}). |
| 51 | + * |
| 52 | + * <pre> |
| 53 | + * {@code @TypeChecked(extensions = 'groovy.typecheckers.NullChecker')} |
| 54 | + * void process(@Nullable String input) { |
| 55 | + * // input.length() // error: potential null dereference |
| 56 | + * input?.length() // ok: safe navigation |
| 57 | + * if (input != null) { |
| 58 | + * input.length() // ok: null guard |
| 59 | + * } |
| 60 | + * } |
| 61 | + * </pre> |
| 62 | + * |
| 63 | + * Over time, the idea would be to support more cases as per: |
| 64 | + * https://checkerframework.org/manual/#nullness-checker |
| 65 | + * |
| 66 | + * @see StrictNullChecker |
| 67 | + * @see NullCheckingVisitor |
| 68 | + */ |
| 69 | +@Incubating |
| 70 | +class NullChecker extends GroovyTypeCheckingExtensionSupport.TypeCheckingDSL { |
| 71 | + |
| 72 | + @Override |
| 73 | + Object run() { |
| 74 | + NullCheckingVisitor.install(this, false) |
| 75 | + } |
| 76 | +} |
0 commit comments