Skip to content

Commit fda3948

Browse files
author
Benjamin Muskalla
committed
Turn external API query into diagnostics query
* Expose (partial) CSV model for the API * Rework and simplify predicates
1 parent 8595ae7 commit fda3948

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

java/ql/src/Telemetry/ExternalAPI.qll

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
1-
import java
2-
import APIUsage
3-
private import experimental.semmle.code.java.Logging
1+
private import java
2+
private import APIUsage
3+
private import semmle.code.java.dataflow.ExternalFlow
44

55
class ExternalAPI extends Callable {
66
ExternalAPI() { not this.fromSource() }
77

8-
string simpleName() {
9-
result = getDeclaringType().getSourceDeclaration() + "#" + this.getStringSignature()
8+
predicate isTestLibrary() { getDeclaringType() instanceof TestLibrary }
9+
10+
predicate isInteresting() {
11+
getNumberOfParameters() > 0 and
12+
not (
13+
getReturnType() instanceof VoidType or
14+
getReturnType() instanceof PrimitiveType or
15+
getReturnType() instanceof BoxedType
16+
)
17+
}
18+
19+
string asCSV(ExternalAPI api) {
20+
result =
21+
api.getDeclaringType().getPackage() + ";?;" + api.getDeclaringType().getSourceDeclaration() +
22+
";" + api.getName() + ";" + paramsString(api)
1023
}
1124
}
1225

13-
class TestLibrary extends RefType {
26+
private class TestLibrary extends RefType {
1427
TestLibrary() {
1528
getPackage()
1629
.getName()

java/ql/src/Telemetry/ExternalAPIUsages.ql

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,23 @@
22
* @name Usage of APIs coming from external libraries
33
* @description A list of 3rd party APIs used in the codebase. Excludes test and generated code.
44
* @id java/telemetry/external-api
5+
* @kind diagnostic
56
*/
67

78
import java
9+
import APIUsage
810
import ExternalAPI
911
import semmle.code.java.GeneratedFiles
1012

13+
// TODO [bm]: decide whether to drop the order by or
14+
// turn Usage into string for diagnostic kind
15+
// https://github.slack.com/archives/C01JJP3EF8E/p1627910071013000
1116
from ExternalAPI api
1217
where
13-
not api.getDeclaringType() instanceof TestLibrary and
14-
isInterestingAPI(api)
15-
select api.simpleName() as API,
18+
not api.isTestLibrary() and
19+
api.isInteresting()
20+
select api.asCSV(api) as csv,
1621
count(Call c |
1722
c.getCallee() = api and
1823
not c.getFile() instanceof GeneratedFile
19-
) as Usages, supportKind(api) as Kind, api.getReturnType() as ReturnType,
20-
api.getDeclaringType().getPackage() as Package order by Usages desc
24+
) as Usages, supportKind(api) as Kind order by Usages desc

java/ql/src/Telemetry/ExternalLibraryUsage.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ where
1616
c.getCallee() = a and
1717
not c.getFile() instanceof GeneratedFile and
1818
a.getCompilationUnit().getParentContainer*() = jar and
19-
not a.getDeclaringType() instanceof TestLibrary
19+
not a.isTestLibrary()
2020
)
2121
select jar.getFile().getStem() + "." + jar.getFile().getExtension(), Usages order by Usages desc

0 commit comments

Comments
 (0)