-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
category: feature | ||
--- | ||
* Added the predicate `CompilationUnit.getATypeInScope()`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question re: throwing things by an interface type There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doh right, I had |
||
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 | ||
|
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. | |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.