Fix aggregation of jar usages

This commit is contained in:
Benjamin Muskalla
2021-08-02 11:32:46 +02:00
parent 722889e881
commit 0c04c9a2c2
3 changed files with 13 additions and 13 deletions

View File

@@ -2,12 +2,8 @@ import java
private import semmle.code.java.dataflow.FlowSteps
private import semmle.code.java.dataflow.ExternalFlow
string jarName(CompilationUnit cu) {
result = cu.getParentContainer().toString().regexpCapture(".*/(.*\\.jar)/?.*", 1)
}
predicate isJavaRuntime(Callable call) {
jarName(call.getCompilationUnit()) = "rt.jar" or
call.getCompilationUnit().getParentContainer*().getStem() = "rt" and
call.getCompilationUnit().getParentContainer().toString().substring(0, 14) = "/modules/java."
}

View File

@@ -9,8 +9,6 @@ class ExternalAPI extends Callable {
not isJavaRuntime(this)
}
string jarName() { result = jarName(this.getCompilationUnit()) }
string simpleName() {
result = getDeclaringType().getSourceDeclaration() + "#" + this.getStringSignature()
}

View File

@@ -1,15 +1,21 @@
/**
* @name External libraries
* @description A list of external libraries used in the code
* @kind diagnostic
* @id java/telemetry/external-libs
*/
import java
import ExternalAPI
from ExternalAPI api
where not api.getDeclaringType() instanceof TestLibrary
// TODO [bm]: the count is not aggregated and we have the same jar with multiple usages, e.g.
// 1 protobuf-java-3.17.3.jar 373
// 2 protobuf-java-3.17.3.jar 48
select api.jarName() as jarname, count(Call c | c.getCallee() = api) as Usages order by Usages desc
from int Usages, JarFile jar
where
jar = any(ExternalAPI api).getCompilationUnit().getParentContainer*() and
Usages =
strictcount(Call c, ExternalAPI a |
c.getCallee() = a and
not c.getFile() instanceof GeneratedFile and
a.getCompilationUnit().getParentContainer*() = jar and
not a.getDeclaringType() instanceof TestLibrary
)
select jar.getFile().getStem() + "." + jar.getFile().getExtension(), Usages order by Usages desc