From 01b950f68b4119f0fa95e6810da8789b33bf7cb5 Mon Sep 17 00:00:00 2001 From: Tony Torralba Date: Tue, 4 Oct 2022 10:59:43 +0200 Subject: [PATCH] Revert "Java: Rename predicate to `getATypeInScope`" This reverts commit fd99ae78b32612d6b469430811d9a97cf30e862b. --- ...2-09-20-CompilationUnit-getATypeInScope.md | 4 -- ...-09-20-CompilationUnit-simple-name-type.md | 4 ++ .../lib/semmle/code/java/CompilationUnit.qll | 52 ++++++++++++------- .../Documentation/ImpossibleJavadocThrows.ql | 2 +- 4 files changed, 37 insertions(+), 25 deletions(-) delete mode 100644 java/ql/lib/change-notes/2022-09-20-CompilationUnit-getATypeInScope.md create mode 100644 java/ql/lib/change-notes/2022-09-20-CompilationUnit-simple-name-type.md diff --git a/java/ql/lib/change-notes/2022-09-20-CompilationUnit-getATypeInScope.md b/java/ql/lib/change-notes/2022-09-20-CompilationUnit-getATypeInScope.md deleted file mode 100644 index dfb7e939060..00000000000 --- a/java/ql/lib/change-notes/2022-09-20-CompilationUnit-getATypeInScope.md +++ /dev/null @@ -1,4 +0,0 @@ ---- -category: feature ---- -* Added the predicate `CompilationUnit.getATypeInScope()`. diff --git a/java/ql/lib/change-notes/2022-09-20-CompilationUnit-simple-name-type.md b/java/ql/lib/change-notes/2022-09-20-CompilationUnit-simple-name-type.md new file mode 100644 index 00000000000..1ce21f6e180 --- /dev/null +++ b/java/ql/lib/change-notes/2022-09-20-CompilationUnit-simple-name-type.md @@ -0,0 +1,4 @@ +--- +category: feature +--- +* Added the predicate `CompilationUnit.getATypeAvailableBySimpleName()`. diff --git a/java/ql/lib/semmle/code/java/CompilationUnit.qll b/java/ql/lib/semmle/code/java/CompilationUnit.qll index 74396e7086b..621750b9098 100644 --- a/java/ql/lib/semmle/code/java/CompilationUnit.qll +++ b/java/ql/lib/semmle/code/java/CompilationUnit.qll @@ -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 diff --git a/java/ql/src/Advisory/Documentation/ImpossibleJavadocThrows.ql b/java/ql/src/Advisory/Documentation/ImpossibleJavadocThrows.ql index 2452dda4fc4..1a5862bb9e8 100644 --- a/java/ql/src/Advisory/Documentation/ImpossibleJavadocThrows.ql +++ b/java/ql/src/Advisory/Documentation/ImpossibleJavadocThrows.ql @@ -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) {