-
Notifications
You must be signed in to change notification settings - Fork 348
/
SearchIndexAnnotatedTypeFactory.java
240 lines (212 loc) · 8.54 KB
/
SearchIndexAnnotatedTypeFactory.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package org.checkerframework.checker.index.searchindex;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.util.Elements;
import org.checkerframework.checker.index.qual.NegativeIndexFor;
import org.checkerframework.checker.index.qual.SearchIndexBottom;
import org.checkerframework.checker.index.qual.SearchIndexFor;
import org.checkerframework.checker.index.qual.SearchIndexUnknown;
import org.checkerframework.common.basetype.BaseAnnotatedTypeFactory;
import org.checkerframework.common.basetype.BaseTypeChecker;
import org.checkerframework.common.value.ValueAnnotatedTypeFactory;
import org.checkerframework.common.value.ValueChecker;
import org.checkerframework.framework.type.ElementQualifierHierarchy;
import org.checkerframework.framework.type.QualifierHierarchy;
import org.checkerframework.javacutil.AnnotationBuilder;
import org.checkerframework.javacutil.AnnotationUtils;
import org.checkerframework.javacutil.TreeUtils;
import org.checkerframework.javacutil.TypeSystemError;
/**
* The Search Index Checker is used to help type the results of calls to the JDK's binary search
* methods. It is part of the Index Checker.
*/
public class SearchIndexAnnotatedTypeFactory extends BaseAnnotatedTypeFactory {
/** The @{@link SearchIndexUnknown} annotation. */
public final AnnotationMirror UNKNOWN =
AnnotationBuilder.fromClass(elements, SearchIndexUnknown.class);
/** The @{@link SearchIndexBottom} annotation. */
public final AnnotationMirror BOTTOM =
AnnotationBuilder.fromClass(elements, SearchIndexBottom.class);
/** The NegativeIndexFor.value field/element. */
protected final ExecutableElement negativeIndexForValueElement =
TreeUtils.getMethod(NegativeIndexFor.class, "value", 0, processingEnv);
/** The SearchIndexFor.value field/element. */
protected final ExecutableElement searchIndexForValueElement =
TreeUtils.getMethod(SearchIndexFor.class, "value", 0, processingEnv);
/**
* Create a new SearchIndexAnnotatedTypeFactory.
*
* @param checker the type-checker associated with this
*/
public SearchIndexAnnotatedTypeFactory(BaseTypeChecker checker) {
super(checker);
this.postInit();
}
/**
* Provides a way to query the Constant Value Checker, which computes the values of expressions
* known at compile time (constant propagation and folding).
*/
ValueAnnotatedTypeFactory getValueAnnotatedTypeFactory() {
return getTypeFactoryOfSubchecker(ValueChecker.class);
}
@Override
protected Set<Class<? extends Annotation>> createSupportedTypeQualifiers() {
return new LinkedHashSet<>(
Arrays.asList(
SearchIndexFor.class,
SearchIndexBottom.class,
SearchIndexUnknown.class,
NegativeIndexFor.class));
}
@Override
protected QualifierHierarchy createQualifierHierarchy() {
return new SearchIndexQualifierHierarchy(this.getSupportedTypeQualifiers(), elements);
}
/**
* Returns the {@code value} field/element of the given annotation.
*
* @param am a @NegativeIndexFor or @SearchIndexFor annotation
* @return the {@code value} field/element of the given annotation
*/
private List<String> getValueElement(AnnotationMirror am) {
if (areSameByClass(am, NegativeIndexFor.class)) {
return AnnotationUtils.getElementValueArray(am, negativeIndexForValueElement, String.class);
} else if (areSameByClass(am, SearchIndexFor.class)) {
return AnnotationUtils.getElementValueArray(am, searchIndexForValueElement, String.class);
} else {
throw new TypeSystemError("indexForValue(%s)", am);
}
}
/** SearchIndexQualifierHierarchy. */
private final class SearchIndexQualifierHierarchy extends ElementQualifierHierarchy {
/**
* Creates a SearchIndexQualifierHierarchy from the given classes.
*
* @param qualifierClasses classes of annotations that are the qualifiers
* @param elements element utils
*/
public SearchIndexQualifierHierarchy(
Set<Class<? extends Annotation>> qualifierClasses, Elements elements) {
super(qualifierClasses, elements);
}
@Override
public AnnotationMirror greatestLowerBound(AnnotationMirror a1, AnnotationMirror a2) {
if (AnnotationUtils.areSame(a1, UNKNOWN)) {
return a2;
}
if (AnnotationUtils.areSame(a2, UNKNOWN)) {
return a1;
}
if (AnnotationUtils.areSame(a1, BOTTOM)) {
return a1;
}
if (AnnotationUtils.areSame(a2, BOTTOM)) {
return a2;
}
if (isSubtype(a1, a2)) {
return a1;
}
if (isSubtype(a2, a1)) {
return a2;
}
// If neither is a subtype of the other, then create an
// annotation that combines their values.
// Each annotation is either NegativeIndexFor or SearchIndexFor.
Set<String> combinedArrays = new HashSet<>(getValueElement(a1));
combinedArrays.addAll(getValueElement(a2));
// NegativeIndexFor <: SearchIndexFor.
if (areSameByClass(a1, NegativeIndexFor.class)
|| areSameByClass(a2, NegativeIndexFor.class)) {
return createNegativeIndexFor(Arrays.asList(combinedArrays.toArray(new String[0])));
} else {
return createSearchIndexFor(Arrays.asList(combinedArrays.toArray(new String[0])));
}
}
@Override
public AnnotationMirror leastUpperBound(AnnotationMirror a1, AnnotationMirror a2) {
if (AnnotationUtils.areSame(a1, UNKNOWN)) {
return a1;
}
if (AnnotationUtils.areSame(a2, UNKNOWN)) {
return a2;
}
if (AnnotationUtils.areSame(a1, BOTTOM)) {
return a2;
}
if (AnnotationUtils.areSame(a2, BOTTOM)) {
return a1;
}
if (isSubtype(a1, a2)) {
return a2;
}
if (isSubtype(a2, a1)) {
return a1;
}
// If neither is a subtype of the other, then create an
// annotation that includes only their overlapping values.
// Each annotation is either NegativeIndexFor or SearchIndexFor.
List<String> arrayIntersection = getValueElement(a1);
arrayIntersection.retainAll(getValueElement(a2)); // intersection
if (arrayIntersection.isEmpty()) {
return UNKNOWN;
}
if (areSameByClass(a1, SearchIndexFor.class) || areSameByClass(a2, SearchIndexFor.class)) {
return createSearchIndexFor(arrayIntersection);
} else {
return createNegativeIndexFor(arrayIntersection);
}
}
@Override
public boolean isSubtype(AnnotationMirror subAnno, AnnotationMirror superAnno) {
if (areSameByClass(superAnno, SearchIndexUnknown.class)) {
return true;
}
if (areSameByClass(subAnno, SearchIndexBottom.class)) {
return true;
}
if (areSameByClass(subAnno, SearchIndexUnknown.class)) {
return false;
}
if (areSameByClass(superAnno, SearchIndexBottom.class)) {
return false;
}
// Each annotation is either NegativeIndexFor or SearchIndexFor.
List<String> superArrays = getValueElement(superAnno);
List<String> subArrays = getValueElement(subAnno);
// Subtyping requires:
// * subtype is NegativeIndexFor or supertype is SearchIndexFor
// * subtype's arrays are a superset of supertype's arrays
return ((areSameByClass(subAnno, NegativeIndexFor.class)
|| areSameByClass(superAnno, SearchIndexFor.class))
&& subArrays.containsAll(superArrays));
}
}
/** Create a new {@code @NegativeIndexFor} annotation with the given arrays as its arguments. */
AnnotationMirror createNegativeIndexFor(List<String> arrays) {
if (arrays.isEmpty()) {
return UNKNOWN;
}
arrays = new ArrayList<>(new TreeSet<>(arrays)); // remove duplicates and sort
AnnotationBuilder builder = new AnnotationBuilder(processingEnv, NegativeIndexFor.class);
builder.setValue("value", arrays);
return builder.build();
}
/** Create a new {@code @SearchIndexFor} annotation with the given arrays as its arguments. */
AnnotationMirror createSearchIndexFor(List<String> arrays) {
if (arrays.isEmpty()) {
return UNKNOWN;
}
arrays = new ArrayList<>(new TreeSet<>(arrays)); // remove duplicates and sort
AnnotationBuilder builder = new AnnotationBuilder(processingEnv, SearchIndexFor.class);
builder.setValue("value", arrays);
return builder.build();
}
}