This repository has been archived by the owner on Jan 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
VisibilityTestRunner.java
349 lines (299 loc) · 11.2 KB
/
VisibilityTestRunner.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
/**
* Copyright 2010 Wealthfront Inc. Licensed under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
* or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package com.kaching.platform.testing;
import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.Lists.transform;
import static com.google.common.collect.Sets.newHashSet;
import static com.google.common.io.Closeables.closeQuietly;
import static com.kaching.platform.testing.VisibilityTestRunner.Intent.PRIVATE;
import static java.lang.String.format;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static org.objectweb.asm.ClassReader.SKIP_DEBUG;
import static org.objectweb.asm.ClassReader.SKIP_FRAMES;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.util.List;
import java.util.Set;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.commons.EmptyVisitor;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.kaching.platform.testing.ParsedElements.ParsedClass;
import com.kaching.platform.testing.ParsedElements.ParsedConstructor;
import com.kaching.platform.testing.ParsedElements.ParsedField;
import com.kaching.platform.testing.ParsedElements.ParsedMethod;
/**
* Example:
* <pre>
* {@literal @}Visibilities(
* {@literal @}Check(paths = "bin", visibilities = {
* {@literal @}Visibility(value = Inject.class, intent = PRIVATE),
* {@literal @}Visibility(value = VisibleForTesting.class, intent = PRIVATE)
* }))
* {@literal @}RunWith(VisibilityTestRunner.class)
* public class VisibilityTest {
* }
* </pre>
*/
public class VisibilityTestRunner
extends AbstractDeclarativeTestRunner<VisibilityTestRunner.Visibilities> {
/**
* Top level annotation describing a visibility test.
*/
@Target(TYPE)
@Retention(RUNTIME)
public @interface Visibilities {
/**
* Lists all the checks performed by this visibility test.
*/
public Check[] value();
}
@Retention(RUNTIME)
@Target({})
public @interface Check {
public String[] paths();
public Visibility[] visibilities();
}
@Retention(RUNTIME)
@Target({})
public @interface Visibility {
/**
* An annotation used to describe a visibility, such as {@code @VisibleForTesting}.
*/
public Class<? extends Annotation> value();
/**
* The intent of the annotation. Currently, {@code PRIVATE} is the only supported intent.
*/
public Intent intent() default PRIVATE;
public String[] exceptions() default {};
}
public enum Intent {
PRIVATE, DEFAULT, PROTECTED
}
/**
* Internal use only.
*/
public VisibilityTestRunner(Class<?> clazz) {
super(clazz, Visibilities.class);
}
@Override
protected void runTest(Visibilities annotation) throws IOException {
CombinedAssertionFailedError error = new CombinedAssertionFailedError("visibility violations");
for (Check check : annotation.value()) {
List<String> paths = ImmutableList.<String> copyOf(check.paths());
for (Visibility visibility : check.visibilities()) {
checkVisibility(paths, visibility, error);
}
}
error.throwIfHasErrors();
}
private void checkVisibility(
List<String> paths,
Visibility visibility,
CombinedAssertionFailedError error) throws IOException {
new Tester(visibility, error).analyze(
concat(
transform(
transform(
paths,
new Function<String, ClassTree>() {
@Override
public ClassTree apply(String path) {
return new ClassTree(new File(path));
}
}),
new Function<ClassTree, List<File>>() {
@Override
public List<File> apply(ClassTree from) {
return from.getClassFiles();
}
})));
}
/**
* Is {@code element} visible by {@code currentClass}?
*/
@VisibleForTesting
boolean isVisible(ParsedElement element, final ParsedClass currentClass, Intent intent) {
switch (intent) {
case PRIVATE:
return element.visit(new DefaultParsedElementVisitor<Boolean>(true) {
@Override
public Boolean caseMethod(ParsedMethod element) {
String name = element.getOwner().getOwner();
String className = currentClass.getOwner();
return name.equals(className);
}
@Override
public Boolean caseField(ParsedField element) {
String name = element.getOwner().getOwner();
String className = currentClass.getOwner();
return name.equals(className);
}
@Override
public Boolean caseClass(ParsedClass element) {
String name = element.getOwner();
String className = currentClass.getOwner();
return name.equals(className);
}
});
default:
throw new UnsupportedOperationException("PRIVATE is the only supported intent");
}
}
private class Tester {
private final Class<? extends Annotation> annotationClass;
private final Intent intent;
private final Set<String> exceptions;
private final CombinedAssertionFailedError error;
private final Set<String> spuriousExceptions;
private final String annotationDescription;
private final Set<ParsedElement> annotatedElements;
Tester(Visibility visibility, CombinedAssertionFailedError error) {
this.annotationClass = visibility.value();
this.intent = visibility.intent();
this.exceptions = newHashSet(visibility.exceptions());
this.error = error;
this.spuriousExceptions = newHashSet(exceptions);
this.annotatedElements = newHashSet();
this.annotationDescription = format("L%s;", annotationClass.getName().replace(".", "/"));
}
void analyze(Iterable<File> files) throws IOException {
// find all the annotated elements
for (File file : files) {
FileInputStream in = null;
try {
in = new FileInputStream(file);
new ClassReader(in).accept(new FindAnnotatedElements(), SKIP_FRAMES | SKIP_DEBUG);
} finally {
closeQuietly(in);
}
}
// find all the references of the annotated elements and record violations
for (File file : files) {
FileInputStream in = null;
try {
in = new FileInputStream(file);
new ClassReader(in).accept(new FindIllegalCalls(), SKIP_FRAMES | SKIP_DEBUG);
} finally {
closeQuietly(in);
}
}
for (String spuriousException : spuriousExceptions) {
error.addError(format(
"%s marked as an exception for @%s but didn't occur",
spuriousException, annotationClass.getSimpleName()));
}
}
private class FindAnnotatedElements extends EmptyVisitor {
private ParsedClass currentClass;
private ParsedMethod currentMethod;
private ParsedField currentField;
private ParsedConstructor currentConstructor;
@Override
public void visit(int version, int access, String name, String signature,
String superName, String[] interfaces) {
currentClass = new ParsedClass(name);
}
@Override
public MethodVisitor visitMethod(int access, String name, String descriptor,
String signature, String[] exceptions) {
currentConstructor = null;
currentMethod = null;
currentField = null;
if (name.equals("<clinit>")) {
// interface initialization method
} else if (name.equals("<init>")) {
currentConstructor = new ParsedConstructor();
} else {
currentMethod = new ParsedMethod(currentClass, name, descriptor);
}
return this;
}
@Override
public FieldVisitor visitField(int access, String name, String descriptor,
String signature, Object value) {
currentField = new ParsedField(currentClass, name);
currentMethod = null;
currentConstructor = null;
return this;
}
@Override
public AnnotationVisitor visitAnnotation(String descriptor, boolean visible) {
if (annotationDescription.equals(descriptor)) {
if (currentField != null) {
// annotated field
annotatedElements.add(currentField);
} else if (currentMethod != null) {
// annotated method
annotatedElements.add(currentMethod);
} else if (currentConstructor != null) {
// TODO annotated constructors are not supported
} else if (currentClass != null) {
// annotated class
annotatedElements.add(currentClass);
}
}
return this;
}
}
private class FindIllegalCalls extends EmptyVisitor {
private String currentMethodName;
private ParsedClass currentClass;
@Override
public void visit(int version, int access, String name, String signature,
String superName, String[] interfaces) {
currentClass = new ParsedClass(name);
}
@Override
public MethodVisitor visitMethod(int access, String name, String descriptor,
String signature, String[] exceptions) {
currentMethodName = name;
return this;
}
@Override
public void visitMethodInsn(int opcode, String owner, String name, String descriptor) {
check(new ParsedClass(owner));
if (name.equals("<clinit>")) {
// interface initialization method
} else if (name.equals("<init>")) {
// TODO handle constructors
} else {
check(new ParsedMethod(new ParsedClass(owner), name, descriptor));
}
}
@Override
public void visitFieldInsn(int opcode, String owner, String name, String descriptor) {
check(new ParsedClass(owner));
check(new ParsedField(new ParsedClass(owner), name));
}
private void check(ParsedElement element) {
if (annotatedElements.contains(element)) {
if (!isVisible(element, currentClass, intent)) {
if (exceptions.contains(currentClass.toString())) {
spuriousExceptions.remove(currentClass.toString());
} else {
error.addError(format("%s.%s uses %s", currentClass, currentMethodName, element));
}
}
}
}
}
}
}