/
NullnessAnnotation.java
100 lines (87 loc) · 3.83 KB
/
NullnessAnnotation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* FindBugs - Find Bugs in Java programs
* Copyright (C) 2005, University of Maryland
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package edu.umd.cs.findbugs.ba;
import javax.annotation.CheckForNull;
import javax.annotation.ParametersAreNonnullByDefault;
import edu.umd.cs.findbugs.internalAnnotations.DottedClassName;
import edu.umd.cs.findbugs.util.ClassName;
/**
* Nullness annotation detector.
*
* @author pugh
* @author Kosta Zaikin
*/
@ParametersAreNonnullByDefault
public class NullnessAnnotation extends AnnotationEnumeration<NullnessAnnotation> {
public final static NullnessAnnotation CHECK_FOR_NULL = new NullnessAnnotation("CheckForNull", 3) {
@Override
boolean match(@DottedClassName String className) {
return "android.support.annotation.Nullable".equals(className)
|| "androidx.annotation.Nullable".equals(className)
|| "com.google.common.base.Nullable".equals(className)
|| "org.apache.avro.reflect.Nullable".equals(className)
|| "org.eclipse.jdt.annotation.Nullable".equals(className)
|| "org.jetbrains.annotations.Nullable".equals(className)
|| "org.checkerframework.checker.nullness.qual.Nullable".equals(className)
|| "org.checkerframework.checker.nullness.compatqual.NullableDecl".equals(className)
|| className.endsWith("PossiblyNull")
|| super.match(className);
}
};
public final static NullnessAnnotation NONNULL = new NullnessAnnotation("NonNull", 1) {
@Override
boolean match(@DottedClassName String className) {
// Unfortunately there are mixed case Nonnull and NonNull annotations (JSR305, FB and JDT)
return "org.jetbrains.annotations.NotNull".equals(className)
|| className.endsWith("Nonnull")
|| super.match(className);
}
};
public final static NullnessAnnotation NULLABLE = new NullnessAnnotation("Nullable", 2);
public final static NullnessAnnotation UNKNOWN_NULLNESS = new NullnessAnnotation("UnknownNullness", 0);
private final static NullnessAnnotation[] myValues = { UNKNOWN_NULLNESS, NONNULL, NULLABLE, CHECK_FOR_NULL };
public static class Parser {
@CheckForNull
public static NullnessAnnotation parse(@DottedClassName String className) {
className = ClassName.toDottedClassName(className);
if (UNKNOWN_NULLNESS.match(className)) {
return UNKNOWN_NULLNESS;
}
if (NONNULL.match(className)) {
return NONNULL;
}
if (CHECK_FOR_NULL.match(className)) {
return CHECK_FOR_NULL;
}
if (NULLABLE.match(className)) {
return NULLABLE;
}
return null;
}
}
boolean match(@DottedClassName String className) {
return className.endsWith(name);
}
public static NullnessAnnotation[] values() {
return myValues.clone();
}
private NullnessAnnotation(String s, int i) {
super(s, i);
}
}