-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathStartInConstructor.ql
More file actions
45 lines (41 loc) · 1.33 KB
/
StartInConstructor.ql
File metadata and controls
45 lines (41 loc) · 1.33 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
/**
* @name Start of thread in constructor
* @description Starting a thread within a constructor may cause the thread to start before
* any subclass constructor has completed its initialization, causing unexpected
* results.
* @kind problem
* @problem.severity warning
* @precision medium
* @id java/thread-start-in-constructor
* @tags quality
* reliability
* correctness
* concurrency
*/
import java
private predicate hasASubclass(RefType t) {
exists(RefType sub | sub != t | sub.getAnAncestor() = t)
}
/**
* Holds if this type is either `final` or
* `private` and without subtypes.
*/
private predicate cannotBeExtended(RefType t) {
t.isFinal()
or
// If the class is private, all possible subclasses are known.
t.isPrivate() and
not hasASubclass(t)
or
// If the class only has private constructors, all possible subclasses are known.
forex(Constructor c | c.getDeclaringType() = t | c.isPrivate()) and
not hasASubclass(t)
}
from MethodCall m, Constructor c, Class clazz
where
m.getMethod().getDeclaringType().hasQualifiedName("java.lang", "Thread") and
m.getMethod().getName() = "start" and
m.getEnclosingCallable() = c and
c.getDeclaringType() = clazz and
not cannotBeExtended(clazz)
select m, "Class $@ starts a thread in its constructor.", clazz, clazz.getName()