-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathStubs.qll
More file actions
563 lines (510 loc) · 16.6 KB
/
Stubs.qll
File metadata and controls
563 lines (510 loc) · 16.6 KB
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/**
* Generates java stubs for use in test code.
*
* Extend the abstract class `GeneratedDeclaration` with the declarations that should be generated.
* This will generate stubs for all the required dependencies as well.
*/
import java
/** Holds if `id` is a valid Java identifier. */
bindingset[id]
private predicate isValidIdentifier(string id) { id.regexpMatch("[\\w_$]+") }
/** A type that should be in the generated code. */
abstract private class GeneratedType extends ClassOrInterface {
GeneratedType() {
not this instanceof AnonymousClass and
not this.isLocal() and
not this.getPackage() instanceof ExcludedPackage and
isValidIdentifier(this.getName())
}
private string stubKeyword() {
this instanceof Interface and
(if this instanceof AnnotationType then result = "@interface" else result = "interface")
or
this instanceof Class and
(if this instanceof EnumType then result = "enum" else result = "class")
}
private string stubAbstractModifier() {
if this.(Class).isAbstract() then result = "abstract " else result = ""
}
private string stubStaticModifier() {
if this.(NestedType).isStatic() then result = "static " else result = ""
}
private string stubAccessibilityModifier() {
if this.isPublic() then result = "public " else result = ""
}
private string stubAnnotations() {
if exists(this.(AnnotationType).getAnAnnotation())
then
result =
concat(Annotation an |
this.(AnnotationType).getAnAnnotation() = an
|
stubAnnotation(an), "\n" order by an.getType().getQualifiedName()
) + "\n"
else result = ""
}
/** Gets the entire Java stub code for this type. */
final string getStub() {
result =
this.stubAnnotations() + this.stubAbstractModifier() + this.stubStaticModifier() +
this.stubAccessibilityModifier() + this.stubKeyword() + " " + this.getName() +
stubGenericArguments(this, true) + this.stubBaseTypesString() + "\n{\n" + this.stubMembers()
+ "}"
}
private RefType getAnInterestingBaseType() {
result = this.getASupertype() and
not result instanceof TypeObject and
not this instanceof EnumType and
// generic types have their source declaration (the corresponding raw type) as a supertype of themselves
result.getSourceDeclaration() != this
}
private string stubBaseTypesString() {
if exists(this.getAnInterestingBaseType())
then
exists(string cls, string interface, string int_kw | result = cls + int_kw + interface |
(
if exists(this.getAnInterestingBaseType().(Class))
then cls = " extends " + stubTypeName(this.getAnInterestingBaseType().(Class))
else cls = ""
) and
(
if
exists(this.getAnInterestingBaseType().(Interface)) and
not this instanceof AnnotationType
then (
(if this instanceof Class then int_kw = " implements " else int_kw = " extends ") and
interface = concat(stubTypeName(this.getAnInterestingBaseType().(Interface)), ", ")
) else (
int_kw = "" and interface = ""
)
)
)
else result = ""
}
language[monotonicAggregates]
private string stubMembers() {
result =
stubEnumConstants(this) + stubFakeConstructor(this) +
concat(Member m | m = this.getAGeneratedMember() | stubMember(m))
}
private Member getAGeneratedMember() {
(
not result instanceof NestedType and
result.getDeclaringType() = this
or
exists(NestedType nt | result = nt |
nt = nt.getSourceDeclaration() and
nt.getEnclosingType().getSourceDeclaration() = this
)
) and
not result.isPrivate() and
not result.isPackageProtected() and
not result instanceof StaticInitializer and
not result instanceof InstanceInitializer and
isValidIdentifier(result.getName())
}
final Type getAGeneratedType() {
result = this.getAnInterestingBaseType() or
result = this.getAGeneratedMember().(Callable).getReturnType() or
result = this.getAGeneratedMember().(Callable).getAParameter().getType() or
result = this.getAGeneratedMember().(Field).getType() or
result = this.getAGeneratedMember().(NestedType) or
result = this.(AnnotationType).getAnAnnotation().getType() or
result = this.(AnnotationType).getAnAnnotation().getValue(_).getType() or
result = this.(AnnotationType).getAnAnnotation().getAnArrayValue(_).getType()
}
}
/**
* A declaration that should be generated.
* This is extended in client code to identify the actual
* declarations that should be generated.
*/
abstract class GeneratedDeclaration extends Element { }
private class IndirectType extends GeneratedType {
IndirectType() {
this.getASubtype() instanceof GeneratedType
or
this.(GenericType).getAParameterizedType() instanceof GeneratedType
or
exists(GeneratedType t |
this = getAContainedType(t.getAGeneratedType()).(RefType).getSourceDeclaration()
)
or
this.getSourceDeclaration() instanceof GeneratedType
or
this = any(GeneratedType t).getSourceDeclaration()
or
exists(GeneratedDeclaration decl |
decl.(Member).getDeclaringType().getSourceDeclaration() = this
)
or
this.(NestedType).getEnclosingType() instanceof GeneratedType
or
exists(NestedType nt | nt instanceof GeneratedType and this = nt.getEnclosingType())
}
}
private class RootGeneratedType extends GeneratedType {
RootGeneratedType() { this = any(GeneratedDeclaration decl).(RefType).getSourceDeclaration() }
}
private Type getAContainedType(Type t) {
result = t
or
result = getAContainedType(t.(ParameterizedType).getATypeArgument())
or
result = getAContainedType(t.(Array).getElementType())
or
result = getAContainedType(t.(BoundedType).getATypeBound().getType())
}
/**
* Specify packages to exclude.
* Do not generate any types from these packages.
*/
abstract class ExcludedPackage extends Package { }
/** Exclude types from the standard library. */
private class DefaultLibs extends ExcludedPackage {
DefaultLibs() { this.getName().matches(["java.%", "jdk.%", "sun.%"]) }
}
private string stubAccessibility(Member m) {
if m.getDeclaringType() instanceof Interface
then result = ""
else
if m.isPublic()
then result = "public "
else
if m.isProtected()
then result = "protected "
else
if m.isPrivate()
then result = "private "
else
if m.isPackageProtected()
then result = ""
else result = "unknown-accessibility"
}
private string stubModifiers(Member m) {
result = stubAccessibility(m) + stubStaticOrFinal(m) + stubAbstractOrDefault(m)
}
private string stubStaticOrFinal(Member m) {
if m.(Modifiable).isStatic()
then result = "static "
else
if m.(Modifiable).isFinal()
then result = "final "
else result = ""
}
private string stubAbstractOrDefault(Member m) {
if m.getDeclaringType() instanceof Interface
then if m.isDefault() then result = "default " else result = ""
else
if m.isAbstract()
then result = "abstract "
else result = ""
}
private string stubTypeName(Type t) {
if t instanceof PrimitiveType
then result = t.getName()
else
if t instanceof VoidType
then result = "void"
else
if t instanceof TypeVariable
then result = t.getName()
else
if t instanceof Wildcard
then result = "?" + stubTypeBound(t)
else
if t instanceof Array
then result = stubTypeName(t.(Array).getComponentType()) + "[]"
else
if t instanceof ClassOrInterface
then
result =
stubQualifier(t) + t.(RefType).getSourceDeclaration().getName() +
stubGenericArguments(t, false)
else result = "<error>"
}
language[monotonicAggregates]
private string stubTypeBound(BoundedType t) {
if not exists(t.getATypeBound())
then result = ""
else
exists(string kw, string bounds | result = kw + bounds |
(if t.(Wildcard).hasLowerBound() then kw = " super " else kw = " extends ") and
bounds =
concat(TypeBound b |
b = t.getATypeBound()
|
stubTypeName(b.getType()), " & " order by b.getPosition()
)
)
}
private string maybeStubTypeBound(BoundedType t, boolean typeVarBounds) {
typeVarBounds = true and
result = stubTypeBound(t)
or
typeVarBounds = false and
result = ""
}
private string stubQualifier(RefType t) {
if t instanceof NestedType
then
exists(RefType et | et = t.(NestedType).getEnclosingType().getSourceDeclaration() |
result = stubQualifier(et) + et.getName() + "."
)
else
if needsPackageName(t)
then result = t.getPackage().getName() + "."
else result = ""
}
pragma[nomagic]
private predicate needsPackageNameHelper(RefType t, GeneratedTopLevel top, string name) {
t.getSourceDeclaration() =
pragma[only_bind_out]([getAReferencedType(top), top].getSourceDeclaration()) and
name = t.getName()
}
pragma[nomagic]
private predicate describesMultipleTypes(GeneratedTopLevel top, string name) {
2 <= strictcount(RefType t | needsPackageNameHelper(t, top, name))
}
/**
* Holds if `t` may clash with another type of the same name, so should be referred to using the fully qualified name
*/
private predicate needsPackageName(RefType t) {
exists(GeneratedTopLevel top, string name |
needsPackageNameHelper(t, top, name) and
describesMultipleTypes(top, name)
)
}
language[monotonicAggregates]
private string stubGenericArguments(RefType t, boolean typeVarBounds) {
typeVarBounds = [true, false] and
if t instanceof GenericType
then
result =
"<" +
concat(int n, TypeVariable tv |
tv = t.(GenericType).getTypeParameter(n)
|
tv.getName() + maybeStubTypeBound(tv, typeVarBounds), ", " order by n
) + ">"
else
if t instanceof ParameterizedType
then
result =
"<" +
concat(int n, Type tpar |
tpar = t.(ParameterizedType).getTypeArgument(n)
|
stubTypeName(tpar), ", " order by n
) + ">"
else result = ""
}
private string stubGenericCallableParams(Callable m) {
if m instanceof GenericCallable
then
result =
"<" +
concat(int n, TypeVariable param |
param = m.(GenericCallable).getTypeParameter(n)
|
param.getName() + stubTypeBound(param), ", " order by n
) + "> "
else result = ""
}
private string stubImplementation(Callable c) {
if c.isAbstract()
then result = ";"
else
if c instanceof Constructor or c.getReturnType() instanceof VoidType
then result = "{}"
else result = "{ return " + stubDefaultValue(c.getReturnType()) + "; }"
}
private string stubDefaultValue(Type t) {
if t instanceof RefType
then result = "null"
else
if t instanceof CharacterType
then result = "'0'"
else
if t instanceof BooleanType
then result = "false"
else
if t instanceof NumericType
then result = "0"
else result = "<error>"
}
private string stubParameters(Callable c) {
result =
concat(int i, Parameter param |
param = c.getParameter(i)
|
stubParameter(param), ", " order by i
)
}
private string stubParameter(Parameter p) {
exists(Type t, string suff | result = stubTypeName(t) + suff + " " + p.getName() |
if p.isVarargs()
then (
t = p.getType().(Array).getComponentType() and
suff = "..."
) else (
t = p.getType() and suff = ""
)
)
}
private string stubEnumConstants(RefType t) {
if t instanceof EnumType
then
exists(EnumType et | et = t |
result =
" " + concat(EnumConstant c | c = et.getAnEnumConstant() | c.getName(), ", ") + ";\n"
)
else result = ""
}
// Holds if the member is to be excluded from stubMember
private predicate excludedMember(Member m) {
m instanceof EnumConstant
or
m.(Method).getDeclaringType() instanceof EnumType and
m.hasName(["values", "valueOf"]) and
m.isStatic()
or
exists(Parameter p |
p = m.(Method).getAParameter() and
p.getType().fromSource() and
not p.getType().(RefType).isPublic()
)
}
private string stubMember(Member m) {
if excludedMember(m)
then result = ""
else (
result =
" " + stubModifiers(m) + stubGenericCallableParams(m) +
stubTypeName(m.(Method).getReturnType()) + " " + m.getName() + "(" + stubParameters(m) + ")"
+ stubImplementation(m) + "\n"
or
m instanceof Constructor and
result =
" " + stubModifiers(m) + stubGenericCallableParams(m) + m.getName() + "(" +
stubParameters(m) + ")" + stubImplementation(m) + "\n"
or
result =
" " + stubModifiers(m) + stubTypeName(m.(Field).getType()) + " " + m.getName() + " = " +
stubDefaultValue(m.(Field).getType()) + ";\n"
or
result = indent(m.(NestedType).(GeneratedType).getStub())
)
}
language[monotonicAggregates]
private string stubAnnotation(Annotation a) {
if exists(a.getValue(_))
then
result =
"@" + a.getType().getName() + "(" +
concat(string name, Expr value |
value = a.getValue(name)
|
name + "=" + stubAnnotationValue(value), ","
) + ")"
else result = "@" + a.getType().getName()
}
language[monotonicAggregates]
private string stubAnnotationValue(Expr value) {
result = value.(FieldAccess).getField().getQualifiedName()
or
(
value instanceof Literal or
value instanceof CompileTimeConstantExpr
) and
if value instanceof StringLiteral
then result = "\"\""
else result = stubDefaultValue(value.getType())
or
result = stubAnnotation(value)
or
result = value.(TypeLiteral).getReferencedType().getName() + ".class"
or
value instanceof ArrayInit and
result =
"{" +
concat(int i, Expr arrayElement |
i >= 0 and
arrayElement = value.(ArrayInit).getInit(i)
|
stubAnnotationValue(arrayElement), "," order by i
) + "}"
}
bindingset[s]
private string indent(string s) { result = " " + s.replaceAll("\n", "\n ") + "\n" }
// If a class's superclass doesn't have a no-arg constructor, then it won't compile when its constructor's bodies are stubbed
// So we synthesise no-arg constructors for each generated type that doesn't have one.
private string stubFakeConstructor(RefType t) {
if not t instanceof Class
then result = ""
else
exists(string mod |
// this won't conflict with any existing private constructors, since we don't generate stubs for any private members.
if t instanceof EnumType then mod = " private " else mod = " protected "
|
if hasNoArgConstructor(t) then result = "" else result = mod + t.getName() + "() {}\n"
)
}
private predicate hasNoArgConstructor(Class t) {
exists(Constructor c | c.getDeclaringType() = t |
c.getNumberOfParameters() = 0 and
not c.isPrivate()
)
}
private RefType getAReferencedType(RefType t) {
result = t.(GeneratedType).getAGeneratedType()
or
result =
getAReferencedType(any(NestedType nt |
nt.getEnclosingType().getSourceDeclaration() = t.getSourceDeclaration()
))
or
exists(RefType t1 | t1 = getAReferencedType(t) |
result = t1.(NestedType).getEnclosingType()
or
result = t1.getSourceDeclaration()
or
result = t1.(ParameterizedType).getATypeArgument()
or
result = t1.(Array).getComponentType()
or
result = t1.(BoundedType).getATypeBound().getType()
)
}
/** A top level type whose file should be stubbed */
class GeneratedTopLevel extends TopLevelType instanceof GeneratedType {
GeneratedTopLevel() { this = this.(ClassOrInterface).getSourceDeclaration() }
private TopLevelType getAnImportedType() {
result = getAReferencedType(this).getSourceDeclaration() and
not needsPackageName(result) // use the fully qualified name rather than importing it if it may cause name clashes
}
private string stubAnImport() {
exists(ClassOrInterface t, string pkg, string name |
t = this.getAnImportedType() and
t.hasQualifiedName(pkg, name) and
t != this and
pkg != "java.lang"
|
result = "import " + pkg + "." + name + ";\n"
)
}
private string stubImports() { result = concat(this.stubAnImport()) + "\n" }
private string stubPackage() {
if this.getPackage().getName() != ""
then result = "package " + this.getPackage().getName() + ";\n\n"
else result = ""
}
private string stubComment() {
result =
"// Generated automatically from " + this.getQualifiedName() + " for testing purposes\n\n"
}
/** Creates a full stub for the file containing this type. */
string stubFile() {
result = this.stubComment() + this.stubPackage() + this.stubImports() + super.getStub() + "\n"
}
}