Revert "Java: Rename predicate to getATypeInScope"

This reverts commit fd99ae78b3.
This commit is contained in:
Tony Torralba
2022-10-04 10:59:43 +02:00
parent df29e05b9f
commit 01b950f68b
4 changed files with 37 additions and 25 deletions

View File

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

View File

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

View File

@@ -32,30 +32,42 @@ class CompilationUnit extends Element, File {
Module getModule() { cumodule(this, result) }
/**
* 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.
* 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`
*/
ClassOrInterface getATypeInScope() {
ClassOrInterface getATypeAvailableBySimpleName() {
// See "Shadowing", https://docs.oracle.com/javase/specs/jls/se17/html/jls-6.html#jls-6.4.1
// Currently shadowing is not considered
// Note: Currently the logic below does not consider shadowing and might have multiple results
// with the same type name
result.(TopLevelType).getCompilationUnit() = this
or
exists(Import importDecl | importDecl.getCompilationUnit() = this |
result =
[
importDecl.(ImportStaticTypeMember).getATypeImport(),
importDecl.(ImportType).getImportedType(),
importDecl.(ImportStaticOnDemand).getATypeImport(),
importDecl.(ImportOnDemandFromType).getAnImport(),
importDecl.(ImportOnDemandFromPackage).getAnImport(),
]
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()
)
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).getATypeInScope()
result = tag.getFile().(CompilationUnit).getATypeAvailableBySimpleName()
}
predicate canThrow(Callable callable, Class exception) {