Java: Rename predicate to getATypeInScope

This commit is contained in:
Marcono1234
2022-09-25 14:44:16 +02:00
parent 431aa2cb79
commit fd99ae78b3
4 changed files with 25 additions and 37 deletions

View File

@@ -0,0 +1,4 @@
---
category: feature
---
* Added the predicate `CompilationUnit.getATypeInScope()`.

View File

@@ -1,4 +0,0 @@
---
category: feature
---
* Added the predicate `CompilationUnit.getATypeAvailableBySimpleName()`.

View File

@@ -32,42 +32,30 @@ class CompilationUnit extends Element, File {
Module getModule() { cumodule(this, result) }
/**
* Gets a type which is available by its simple name in this compilation unit.
* Reasons for this can be:
* - The type is declared in this compilation unit as top-level type
* - The type is imported
* - The type is declared in the same package as this compilation unit
* - The type is declared in the package `java.lang`
* Gets a type which is available in the top-level scope of this compilation unit.
* This can be a type:
* - declared in this compilation unit as top-level type
* - imported with an `import` declaration
* - declared in the same package as this compilation unit
* - declared in the package `java.lang`
*
* This predicate not consider "shadowing", it can have types as result whose simple name is
* shadowed by another type in scope.
*/
ClassOrInterface getATypeAvailableBySimpleName() {
ClassOrInterface getATypeInScope() {
// See "Shadowing", https://docs.oracle.com/javase/specs/jls/se17/html/jls-6.html#jls-6.4.1
// Note: Currently the logic below does not consider shadowing and might have multiple results
// with the same type name
// Currently shadowing is not considered
result.(TopLevelType).getCompilationUnit() = this
or
exists(ImportStaticTypeMember importDecl |
importDecl.getCompilationUnit() = this and
result = importDecl.getATypeImport()
)
or
exists(ImportType importDecl |
importDecl.getCompilationUnit() = this and
result = importDecl.getImportedType()
)
or
exists(ImportStaticOnDemand importDecl |
importDecl.getCompilationUnit() = this and
result = importDecl.getATypeImport()
)
or
exists(ImportOnDemandFromType importDecl |
importDecl.getCompilationUnit() = this and
result = importDecl.getAnImport()
)
or
exists(ImportOnDemandFromPackage importDecl |
importDecl.getCompilationUnit() = this and
result = importDecl.getAnImport()
exists(Import importDecl | importDecl.getCompilationUnit() = this |
result =
[
importDecl.(ImportStaticTypeMember).getATypeImport(),
importDecl.(ImportType).getImportedType(),
importDecl.(ImportStaticOnDemand).getATypeImport(),
importDecl.(ImportOnDemandFromType).getAnImport(),
importDecl.(ImportOnDemandFromPackage).getAnImport(),
]
)
or
// From same package or java.lang, see https://docs.oracle.com/javase/specs/jls/se17/html/jls-7.html

View File

@@ -13,7 +13,7 @@ import java
Class getTaggedType(ThrowsTag tag) {
result.hasName(tag.getExceptionName()) and
result = tag.getFile().(CompilationUnit).getATypeAvailableBySimpleName()
result = tag.getFile().(CompilationUnit).getATypeInScope()
}
predicate canThrow(Callable callable, Class exception) {