/
TypeQualifierValue.java
415 lines (360 loc) · 15.9 KB
/
TypeQualifierValue.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
* FindBugs - Find Bugs in Java programs
* Copyright (C) 2003-2008 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.jsr305;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Objects;
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.meta.TypeQualifierValidator;
import javax.annotation.meta.When;
import edu.umd.cs.findbugs.SystemProperties;
import edu.umd.cs.findbugs.ba.AnalysisContext;
import edu.umd.cs.findbugs.ba.MissingClassException;
import edu.umd.cs.findbugs.ba.XClass;
import edu.umd.cs.findbugs.ba.XMethod;
import edu.umd.cs.findbugs.classfile.CheckedAnalysisException;
import edu.umd.cs.findbugs.classfile.ClassDescriptor;
import edu.umd.cs.findbugs.classfile.DescriptorFactory;
import edu.umd.cs.findbugs.classfile.Global;
import edu.umd.cs.findbugs.classfile.IAnalysisCache;
import edu.umd.cs.findbugs.classfile.analysis.ClassData;
import edu.umd.cs.findbugs.internalAnnotations.DottedClassName;
import edu.umd.cs.findbugs.log.Profiler;
import edu.umd.cs.findbugs.util.DualKeyHashMap;
/**
* A TypeQualifierValue is a pair specifying a type qualifier annotation and a
* value. Each TypeQualifierValue is effectively a different type qualifier. For
* example, if Foo is a type qualifier annotation having an int value, then
* Foo(0), Foo(1), etc. are all different type qualifiers which must be checked
* separately.
*
* @author William Pugh
*/
public class TypeQualifierValue<A extends Annotation> {
public static final boolean DEBUG = SystemProperties.getBoolean("tqv.debug");
public static final boolean DEBUG_CLASSLOADING = SystemProperties.getBoolean("tqv.debug.classloading");
private static final ClassDescriptor EXCLUSIVE_ANNOTATION = DescriptorFactory.instance().getClassDescriptor(
javax.annotation.meta.Exclusive.class);
private static final ClassDescriptor EXHAUSTIVE_ANNOTATION = DescriptorFactory.instance().getClassDescriptor(
javax.annotation.meta.Exhaustive.class);
public final ClassDescriptor typeQualifier;
public final Class<A> typeQualifierClass;
public final A proxy;
public final @CheckForNull Object value;
private final boolean isStrict;
private final boolean isExclusive;
private final boolean isExhaustive;
private final @CheckForNull TypeQualifierValidator<A> validator;
private TypeQualifierValue(ClassDescriptor typeQualifier, @CheckForNull Object value) {
this.typeQualifier = typeQualifier;
this.value = value;
/** will be set to true if this is a strict type qualifier value */
boolean isStrict1 = false;
/** will be set to true if this is an exclusive type qualifier value */
boolean isExclusive1 = false;
/** will be set to true if this is an exhaustive type qualifier value */
boolean isExhaustive1 = false;
TypeQualifierValidator<A> validator1 = null;
Class<A> qualifierClass = null;
XClass xclass = null;
A proxy1 = null;
try {
xclass = Global.getAnalysisCache().getClassAnalysis(XClass.class, typeQualifier);
// Annotation elements appear as abstract methods in the annotation
// class (interface).
// So, if the type qualifier annotation has specified a default When
// value,
// it will appear as an abstract method called "when".
XMethod whenMethod = xclass.findMethod("when", "()Ljavax/annotation/meta/When;", false);
if (whenMethod == null) {
isStrict1 = true;
}
for (XMethod xmethod : xclass.getXMethods()) {
if ("value".equals(xmethod.getName()) && xmethod.getSignature().startsWith("()")) {
isExhaustive1 = xmethod.getAnnotation(EXHAUSTIVE_ANNOTATION) != null;
if (isExhaustive1) {
// exhaustive qualifiers are automatically exclusive
isExclusive1 = true;
} else {
// see if there is an explicit @Exclusive annotation
isExclusive1 = xmethod.getAnnotation(EXCLUSIVE_ANNOTATION) != null;
}
break;
}
}
} catch (MissingClassException e) {
AnalysisContext.currentAnalysisContext().getLookupFailureCallback().reportMissingClass(e.getClassNotFoundException());
} catch (CheckedAnalysisException e) {
AnalysisContext.logError("Error looking up annotation class " + typeQualifier.toDottedClassName(), e);
}
this.isStrict = isStrict1;
this.isExclusive = isExclusive1;
this.isExhaustive = isExhaustive1;
if (xclass != null) {
ClassDescriptor checkerName = DescriptorFactory.createClassDescriptor(typeQualifier.getClassName() + "$Checker");
if (!SystemProperties.RUNNING_AS_IDE_PLUGIN) {
/* don't do this if running in Eclipse; check below is the quick
fix for bug 3599258 (Random obscure Eclipse failures during analysis)
Also don't do this if running in IntelliJ IDEA. This causes weird issues
either (see IDEA-230268)
*/
try {
Global.getAnalysisCache().getClassAnalysis(ClassData.class, checkerName);
Class<?> c = ValidatorClassLoader.INSTANCE.loadClass(checkerName.getDottedClassName());
if (TypeQualifierValidator.class.isAssignableFrom(c)) {
@SuppressWarnings("unchecked")
Class<? extends TypeQualifierValidator<A>> validatorClass = (Class<? extends TypeQualifierValidator<A>>) c
.asSubclass(TypeQualifierValidator.class);
validator1 = getValidator(validatorClass);
qualifierClass = getQualifierClass(typeQualifier);
InvocationHandler handler = (arg0, arg1, arg2) -> {
if ("value".equals(arg1.getName())) {
return TypeQualifierValue.this.value;
}
throw new UnsupportedOperationException("Can't handle " + arg1);
};
proxy1 = qualifierClass.cast(Proxy.newProxyInstance(ValidatorClassLoader.INSTANCE,
new Class[] { qualifierClass }, handler));
}
} catch (ClassNotFoundException e) {
assert true; // ignore
} catch (CheckedAnalysisException e) {
assert true; // ignore
} catch (Exception e) {
AnalysisContext.logError("Unable to construct type qualifier checker " + checkerName, e);
} catch (Throwable e) {
AnalysisContext.logError("Unable to construct type qualifier checker " + checkerName + " due to "
+ e.getClass().getSimpleName() + ":" + e.getMessage());
}
}
}
this.validator = validator1;
this.typeQualifierClass = qualifierClass;
this.proxy = proxy1;
}
private static <A extends Annotation> TypeQualifierValidator<A> getValidator(
Class<? extends TypeQualifierValidator<A>> checkerClass)
throws InstantiationException, IllegalAccessException {
return checkerClass.newInstance();
}
@SuppressWarnings("unchecked")
private static <A> Class<A> getQualifierClass(ClassDescriptor typeQualifier) throws ClassNotFoundException {
@DottedClassName
String className = typeQualifier.getDottedClassName();
if (DEBUG_CLASSLOADING) {
System.out.println("Getting qualifier class for " + className);
}
if (className.startsWith("javax.annotation")) {
return (Class<A>) Class.forName(className);
}
try {
Global.getAnalysisCache().getClassAnalysis(ClassData.class, typeQualifier);
} catch (CheckedAnalysisException e) {
throw new ClassNotFoundException("No class data found for " + className);
}
ValidatorClassLoader validatorLoader = ValidatorClassLoader.INSTANCE;
return (Class<A>) validatorLoader.loadClass(typeQualifier.getDottedClassName());
}
static byte[] loadClassData(String name) throws CheckedAnalysisException {
ClassDescriptor d = DescriptorFactory.createClassDescriptorFromDottedClassName(name);
ClassData data = Global.getAnalysisCache().getClassAnalysis(ClassData.class, d);
return data.getData();
}
static class Data {
/**
* Cache in which constructed TypeQualifierValues are interned.
*/
DualKeyHashMap<ClassDescriptor, Object, TypeQualifierValue<?>> typeQualifierMap = new DualKeyHashMap<>();
/**
* Set of all known TypeQualifierValues.
*/
Set<TypeQualifierValue<?>> allKnownTypeQualifiers = new HashSet<>();
}
private static ThreadLocal<Data> instance = new ThreadLocal<Data>() {
@Override
protected Data initialValue() {
return new Data();
}
};
public static void clearInstance() {
instance.remove();
}
public boolean canValidate(@CheckForNull Object constantValue) {
return validator != null;
}
public When validate(@CheckForNull Object constantValue) {
if (validator == null) {
throw new IllegalStateException("No validator");
}
IAnalysisCache analysisCache = Global.getAnalysisCache();
Profiler profiler = analysisCache.getProfiler();
profiler.start(validator.getClass());
try {
return validator.forConstantValue(proxy, constantValue);
} catch (Exception e) {
AnalysisContext.logError("Error executing custom validator for " + typeQualifier + " " + constantValue, e);
return When.UNKNOWN;
} finally {
profiler.end(validator.getClass());
}
}
/**
* Given a ClassDescriptor/value pair, return the interned
* TypeQualifierValue representing that pair.
*
* @param desc
* a ClassDescriptor denoting a type qualifier annotation
* @param value
* a value
* @return an interned TypeQualifierValue object
*/
@SuppressWarnings("rawtypes")
public static @Nonnull TypeQualifierValue<?> getValue(ClassDescriptor desc, @CheckForNull Object value) {
DualKeyHashMap<ClassDescriptor, Object, TypeQualifierValue<?>> map = instance.get().typeQualifierMap;
TypeQualifierValue<?> result = map.get(desc, value);
if (result != null) {
return result;
}
result = new TypeQualifierValue(desc, value);
map.put(desc, value, result);
instance.get().allKnownTypeQualifiers.add(result);
return result;
}
@SuppressWarnings("unchecked")
public static @Nonnull <A extends Annotation> TypeQualifierValue<A> getValue(Class<A> clazz, @CheckForNull Object value) {
return (TypeQualifierValue<A>) getValue(DescriptorFactory.createClassDescriptor(clazz), value);
}
/**
* Get Collection of all known TypeQualifierValues.
*
* @return Collection of all known TypeQualifierValues
*/
public static Collection<TypeQualifierValue<?>> getAllKnownTypeQualifiers() {
return Collections.unmodifiableSet(instance.get().allKnownTypeQualifiers);
}
/**
* Get the "complementary" TypeQualifierValues for given exclusive type
* qualifier.
*
* @param tqv
* a type qualifier (which must be exclusive)
* @return Collection of complementary exclusive type qualifiers
*/
public static Collection<TypeQualifierValue<?>> getComplementaryExclusiveTypeQualifierValue(TypeQualifierValue<?> tqv) {
assert tqv.isExclusiveQualifier();
LinkedList<TypeQualifierValue<?>> result = new LinkedList<>();
for (TypeQualifierValue<?> t : instance.get().allKnownTypeQualifiers) {
//
// Any TypeQualifierValue with the same
// annotation class but a different value is a complementary
// type qualifier.
//
if (t.typeQualifier.equals(tqv.typeQualifier) && !Objects.equals(t.value, tqv.value)) {
result.add(t);
}
}
return result;
}
/**
* Determine whether or not given TypeQualifierValue has multiple variants.
* I.e., if Color is a type qualifier having values RED, GREEN, and BLUE,
* then there are 3 variants, Color(RED), Color(GREEN), and COLOR(BLUE).
*
* @param tqv
* a TypeQualifierValue
* @return true if there are multiple variants of this type qualifier, false
* otherwise
*/
public static boolean hasMultipleVariants(TypeQualifierValue<?> tqv) {
int count = 0;
for (TypeQualifierValue<?> t : instance.get().allKnownTypeQualifiers) {
if (t.typeQualifier.equals(tqv.typeQualifier)) {
count++;
}
}
return count > 1;
}
/**
* Get the ClassDescriptor which specifies the type qualifier annotation.
*
* @return ClassDescriptor which specifies the type qualifier annotation
*/
public ClassDescriptor getTypeQualifierClassDescriptor() {
return typeQualifier;
}
/**
* Return whether or not this TypeQualifierValue denotes a strict qualifier.
*
* @return true if type qualifier is strict, false otherwise
*/
public boolean isStrictQualifier() {
return isStrict;
}
/**
* Return whether or not this TypeQualifierValue denotes an exclusive
* qualifier.
*
* @return true if type qualifier is exclusive, false otherwise
*/
public boolean isExclusiveQualifier() {
return isExclusive;
}
/**
* Return whether or not this TypeQualifierValue denotes an exhaustive
* qualifier.
*
* @return true if type qualifier is exhaustive, false otherwise
*/
public boolean isExhaustiveQualifier() {
return isExhaustive;
}
@Override
public int hashCode() {
int result = typeQualifier.hashCode();
if (value != null) {
result += 37 * value.hashCode();
}
return result;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof TypeQualifierValue)) {
return false;
}
TypeQualifierValue<?> other = (TypeQualifierValue<?>) o;
return typeQualifier.equals(other.typeQualifier) && Objects.equals(value, other.value);
}
@Override
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append(typeQualifier.toString());
if (value != null) {
buf.append(':');
buf.append(value.toString());
}
return buf.toString();
}
}