Skip to content

Java: Add CompilationUnit.getATypeInScope() #10498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: feature
---
* Added the predicate `CompilationUnit.getATypeInScope()`.
33 changes: 33 additions & 0 deletions java/ql/lib/semmle/code/java/CompilationUnit.qll
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,38 @@ 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.
*/
ClassOrInterface getATypeInScope() {
// See "Shadowing", https://docs.oracle.com/javase/specs/jls/se17/html/jls-6.html#jls-6.4.1
// Currently shadowing is not considered
result.(TopLevelType).getCompilationUnit() = this
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant w.r.t. the same-package test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. Currently this structure matches (roughly?) the precedence order of shadowing, but I could drop this nonetheless if you want.

or
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
result.(TopLevelType).getPackage() = this.getPackage()
or
result.(TopLevelType).getPackage().hasName("java.lang")
}

override string getAPrimaryQlClass() { result = "CompilationUnit" }
}
16 changes: 7 additions & 9 deletions java/ql/src/Advisory/Documentation/ImpossibleJavadocThrows.ql
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,20 @@

import java

RefType getTaggedType(ThrowsTag tag) {
ClassOrInterface getTaggedType(ThrowsTag tag) {
result.hasName(tag.getExceptionName()) and
exists(ImportType i | i.getFile() = tag.getFile() | i.getImportedType() = result)
result = tag.getFile().(CompilationUnit).getATypeInScope()
}

predicate canThrow(Callable callable, RefType exception) {
exists(string uncheckedException |
uncheckedException = "RuntimeException" or uncheckedException = "Error"
|
exception.getAnAncestor().hasQualifiedName("java.lang", uncheckedException)
)
predicate canThrow(Callable callable, Class exception) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question re: throwing things by an interface type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All exception types are classes though, so you cannot declare an interface type to be thrown, can you?
(neither in Kotlin, right?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doh right, I had Exception in mind as an interface. Never mind.

exception instanceof UncheckedThrowableType
or
callable.getAnException().getType().getADescendant() = exception
}

from ThrowsTag throwsTag, RefType thrownType, Callable docMethod
// Uses ClassOrInterface as type for thrownType to also cover case where erroneously an interface
// type is declared as thrown exception
from ThrowsTag throwsTag, ClassOrInterface thrownType, Callable docMethod
where
getTaggedType(throwsTag) = thrownType and
docMethod.getDoc().getJavadoc().getAChild*() = throwsTag and
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
| ImpossibleJavadocThrows.java:9:5:9:12 | @throws | Javadoc for bad1 claims to throw InterruptedException but this is impossible. |
| ImpossibleJavadocThrows.java:16:5:16:15 | @exception | Javadoc for bad2 claims to throw Exception but this is impossible. |
| ImpossibleJavadocThrows.java:23:5:23:12 | @throws | Javadoc for bad3 claims to throw Runnable but this is impossible. |
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ public void bad1() {
public void bad2() {
}

/**
*
* @throws Runnable
*/
public void bad3() {
}

/**
*
* @throws InterruptedException
Expand Down