-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathImport.qll
More file actions
188 lines (147 loc) · 6.24 KB
/
Import.qll
File metadata and controls
188 lines (147 loc) · 6.24 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
/**
* Provides classes and predicates for working with Java imports.
*/
overlay[local?]
module;
import semmle.code.Location
import CompilationUnit
/** A common super-class for all kinds of Java import declarations. */
class Import extends Element, @import {
/** Gets the compilation unit in which this import declaration occurs. */
override CompilationUnit getCompilationUnit() { result = this.getFile() }
/** Holds if this import declaration occurs in source code. */
override predicate fromSource() { any() }
/*abstract*/ override string toString() { result = "import" }
}
/**
* A single-type-import declaration.
*
* For example, `import java.util.Set;`.
*/
class ImportType extends Import {
ImportType() { imports(this, _, _, 1) }
/** Gets the imported type. */
ClassOrInterface getImportedType() { imports(this, result, _, _) }
override string toString() {
result = "import " + pragma[only_bind_out](this.getImportedType()).toString()
}
override string getAPrimaryQlClass() { result = "ImportType" }
}
/**
* A type-import-on-demand declaration that allows all accessible
* nested types of a named type to be imported as needed.
*
* For example, `import java.util.Map.*;` imports
* the nested type `java.util.Map.Entry` from the type
* `java.util.Map`.
*/
class ImportOnDemandFromType extends Import {
ImportOnDemandFromType() { imports(this, _, _, 2) }
/** Gets the type from which accessible nested types are imported. */
ClassOrInterface getTypeHoldingImport() { imports(this, result, _, _) }
/** Gets an imported type. */
NestedType getAnImport() { result.getEnclosingType() = this.getTypeHoldingImport() }
override string toString() {
result = "import " + pragma[only_bind_out](this.getTypeHoldingImport()).toString() + ".*"
}
override string getAPrimaryQlClass() { result = "ImportOnDemandFromType" }
}
/**
* A type-import-on-demand declaration that allows all accessible
* types of a named package to be imported as needed.
*
* For example, `import java.util.*;`.
*/
class ImportOnDemandFromPackage extends Import {
ImportOnDemandFromPackage() { imports(this, _, _, 3) }
/** Gets the package from which accessible types are imported. */
Package getPackageHoldingImport() { imports(this, result, _, _) }
/** Gets an imported type. */
RefType getAnImport() { result.getPackage() = this.getPackageHoldingImport() }
/** Gets a printable representation of this import declaration. */
override string toString() {
result = "import " + pragma[only_bind_out](this.getPackageHoldingImport()).toString() + ".*"
}
override string getAPrimaryQlClass() { result = "ImportOnDemandFromPackage" }
}
/**
* A static-import-on-demand declaration, which allows all accessible
* static members of a named type to be imported as needed.
*
* For example, `import static java.lang.System.*;`.
*/
class ImportStaticOnDemand extends Import {
ImportStaticOnDemand() { imports(this, _, _, 4) }
/** Gets the type from which accessible static members are imported. */
ClassOrInterface getTypeHoldingImport() { imports(this, result, _, _) }
/** Gets an imported type. */
NestedType getATypeImport() { result.getEnclosingType() = this.getTypeHoldingImport() }
/** Gets an imported method. */
Method getAMethodImport() { result.getDeclaringType() = this.getTypeHoldingImport() }
/** Gets an imported field. */
Field getAFieldImport() { result.getDeclaringType() = this.getTypeHoldingImport() }
/** Gets a printable representation of this import declaration. */
override string toString() {
result = "import static " + pragma[only_bind_out](this.getTypeHoldingImport()).toString() + ".*"
}
override string getAPrimaryQlClass() { result = "ImportStaticOnDemand" }
}
/**
* A single-static-import declaration, which imports all accessible
* static members with a given simple name from a type.
*
* For example, `import static java.util.Collections.sort;`
* imports all the static methods named `sort` from the
* class `java.util.Collections`.
*/
class ImportStaticTypeMember extends Import {
ImportStaticTypeMember() { imports(this, _, _, 5) }
/** Gets the type from which static members with a given name are imported. */
ClassOrInterface getTypeHoldingImport() { imports(this, result, _, _) }
/** Gets the name of the imported member(s). */
override string getName() { imports(this, _, result, _) }
/** Gets an imported member. */
Member getAMemberImport() {
this.getTypeHoldingImport().getAMember() = result and
result.getName() = this.getName() and
result.isStatic()
}
/** Gets an imported type. */
NestedType getATypeImport() { result = this.getAMemberImport() }
/** Gets an imported method. */
Method getAMethodImport() { result = this.getAMemberImport() }
/** Gets an imported field. */
Field getAFieldImport() { result = this.getAMemberImport() }
/** Gets a printable representation of this import declaration. */
override string toString() {
result =
"import static " + pragma[only_bind_out](this.getTypeHoldingImport()).toString() + "." +
this.getName()
}
override string getAPrimaryQlClass() { result = "ImportStaticTypeMember" }
}
/**
* A module import declaration, which imports an entire module.
*
* For example, `import module java.base;` makes all packages exported
* by the `java.base` module available, and through those packages,
* the types they declare become accessible.
*/
class ModuleImportDeclaration extends Import {
ModuleImportDeclaration() { imports(this, _, _, 6) }
/** Gets the name of the imported module. */
string getModuleName() { imports(this, _, result, _) }
/** Gets the imported module. */
Module getModule() { result.getName() = this.getModuleName() }
/** Gets an exported package from the imported module. */
Package getAnImportedPackage() {
exists(ExportsDirective exports |
exports = this.getModule().getADirective() and
result = exports.getExportedPackage()
)
}
/** Gets a type from a package that is accessible through this module import. */
RefType getAnImportedType() { result.getPackage() = this.getAnImportedPackage() }
override string toString() { result = "import module " + this.getModuleName() }
override string getAPrimaryQlClass() { result = "ModuleImportDeclaration" }
}