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 new file mode 100644 index 00000000000..dfb7e939060 --- /dev/null +++ b/java/ql/lib/change-notes/2022-09-20-CompilationUnit-getATypeInScope.md @@ -0,0 +1,4 @@ +--- +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 deleted file mode 100644 index 1ce21f6e180..00000000000 --- a/java/ql/lib/change-notes/2022-09-20-CompilationUnit-simple-name-type.md +++ /dev/null @@ -1,4 +0,0 @@ ---- -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 621750b9098..74396e7086b 100644 --- a/java/ql/lib/semmle/code/java/CompilationUnit.qll +++ b/java/ql/lib/semmle/code/java/CompilationUnit.qll @@ -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 diff --git a/java/ql/src/Advisory/Documentation/ImpossibleJavadocThrows.ql b/java/ql/src/Advisory/Documentation/ImpossibleJavadocThrows.ql index 1a5862bb9e8..2452dda4fc4 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).getATypeAvailableBySimpleName() + result = tag.getFile().(CompilationUnit).getATypeInScope() } predicate canThrow(Callable callable, Class exception) {