Skip to content

Commit ce4975b

Browse files
committed
Java: Add ModuleImportDeclaration QL class
1 parent 18596cb commit ce4975b

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

java/ql/lib/semmle/code/java/Import.qll

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module;
66

77
import semmle.code.Location
88
import CompilationUnit
9+
import Modules
910

1011
/** A common super-class for all kinds of Java import declarations. */
1112
class Import extends Element, @import {
@@ -154,3 +155,42 @@ class ImportStaticTypeMember extends Import {
154155

155156
override string getAPrimaryQlClass() { result = "ImportStaticTypeMember" }
156157
}
158+
159+
/**
160+
* A module import declaration, which imports an entire module.
161+
*
162+
* For example, `import module java.base;`.
163+
*/
164+
class ModuleImportDeclaration extends Element, @moduleimportdeclaration {
165+
/** Gets the compilation unit in which this module import declaration occurs. */
166+
override CompilationUnit getCompilationUnit() { result = this.getFile() }
167+
168+
/** Holds if this module import declaration occurs in source code. */
169+
override predicate fromSource() { any() }
170+
171+
/** Gets the name of the imported module. */
172+
string getModuleName() { moduleImportDeclarations(this, _, result) }
173+
174+
/** Gets the imported module. */
175+
Module getModule() { result.getName() = this.getModuleName() }
176+
177+
/** Gets an exported package from the imported module. */
178+
Package getAnImportedPackage() {
179+
exists(ExportsDirective exports |
180+
exports = this.getModule().getADirective() and
181+
result = exports.getExportedPackage()
182+
)
183+
}
184+
185+
/** Gets a type that is imported from the module. */
186+
RefType getAnImportedType() {
187+
exists(Package pkg |
188+
pkg = this.getAnImportedPackage() and
189+
result.getPackage() = pkg
190+
)
191+
}
192+
193+
override string toString() { result = "import module " + this.getModuleName() }
194+
195+
override string getAPrimaryQlClass() { result = "ModuleImportDeclaration" }
196+
}

0 commit comments

Comments
 (0)