/
LibraryModels.java
167 lines (147 loc) · 6.39 KB
/
LibraryModels.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Copyright (c) 2017 Uber Technologies, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.uber.nullaway;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSetMultimap;
import com.sun.tools.javac.code.Symbol;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** Provides models for library routines for the null checker. */
public interface LibraryModels {
/**
* @return map from the names of null-rejecting methods to the indexes of the arguments that
* aren't permitted to be null.
*/
ImmutableSetMultimap<MethodRef, Integer> failIfNullParameters();
/**
* @return map from the names of methods with @Nullable parameters to the indexes of the arguments
* that are @Nullable.
* <p>This is taken into account for override checks, requiring methods that override the
* methods listed here to take @Nullable parameters on the same indexes. The main use for this
* is to document which API callbacks can be passed null values.
*/
ImmutableSetMultimap<MethodRef, Integer> explicitlyNullableParameters();
/**
* @return map from the names of methods with @NonNull parameters to the indexes of the arguments
* that are @NonNull.
* <p>Note that these methods are different from the {@link #failIfNullParameters()} methods,
* in that we expect the null checker to ensure that the parameters passed to these methods
* are @NonNull. In contrast, the null checker does no such enforcement for methods in {@link
* #failIfNullParameters()}, it just learns that after the call the relevant parameters cannot
* be null.
*/
ImmutableSetMultimap<MethodRef, Integer> nonNullParameters();
/**
* @return map from the names of null-querying methods to the indexes of the arguments that are
* compared against null.
*/
ImmutableSetMultimap<MethodRef, Integer> nullImpliesTrueParameters();
/** @return set of library methods that may return null */
ImmutableSet<MethodRef> nullableReturns();
/** @return set of library methods that are assumed not to return null */
ImmutableSet<MethodRef> nonNullReturns();
/**
* representation of a method as a qualified class name + a signature for the method
*
* <p>The formatting of a method signature should match the result of calling {@link
* Symbol.MethodSymbol#toString()} on the corresponding symbol. See {@link
* com.uber.nullaway.handlers.LibraryModelsHandler.DefaultLibraryModels} for examples. Basic
* principles:
*
* <ul>
* <li>signature is a method name plus argument types, e.g., <code>foo(java.lang.Object,
* java.lang.String)</code>
* <li>constructor for class Foo looks like <code>Foo(java.lang.String)</code>
* <li>If the method has its own type parameters, they need to be declared, like <code>
* <T>checkNotNull(T)</code>
* <li>Type bounds matter for generics, e.g., <code>addAll(java.lang.Iterable<? extends
* E>)
* </code>
* </ul>
*/
final class MethodRef {
public final String enclosingClass;
/**
* we store the method name separately to enable fast comparison with MethodSymbols. See {@link
* com.uber.nullaway.handlers.LibraryModelsHandler.OptimizedLibraryModels}
*/
public final String methodName;
public final String fullMethodSig;
private MethodRef(String enclosingClass, String methodName, String fullMethodSig) {
this.enclosingClass = enclosingClass;
this.methodName = methodName;
this.fullMethodSig = fullMethodSig;
}
private static final Pattern METHOD_SIG_PATTERN = Pattern.compile("^(<.*>)?(\\w+)(\\(.*\\))$");
/**
* @param enclosingClass containing class
* @param methodSignature method signature in the appropriate format (see class docs)
* @return corresponding {@link MethodRef}
*/
public static MethodRef methodRef(String enclosingClass, String methodSignature) {
Matcher matcher = METHOD_SIG_PATTERN.matcher(methodSignature);
if (matcher.find()) {
String methodName = matcher.group(2);
if (methodName.equals(enclosingClass.substring(enclosingClass.lastIndexOf('.') + 1))) {
// constructor
methodName = "<init>";
}
return new MethodRef(enclosingClass, methodName, methodSignature);
} else {
throw new IllegalArgumentException("malformed method signature " + methodSignature);
}
}
public static MethodRef fromSymbol(Symbol.MethodSymbol symbol) {
String methodStr = symbol.toString();
return new MethodRef(
symbol.owner.getQualifiedName().toString(), symbol.name.toString(), methodStr);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
MethodRef methodRef = (MethodRef) o;
return Objects.equals(enclosingClass, methodRef.enclosingClass)
&& Objects.equals(fullMethodSig, methodRef.fullMethodSig);
}
@Override
public int hashCode() {
return Objects.hash(enclosingClass, fullMethodSig);
}
@Override
public String toString() {
return "MethodRef{"
+ "enclosingClass='"
+ enclosingClass
+ '\''
+ ", fullMethodSig='"
+ fullMethodSig
+ '\''
+ '}';
}
}
}