如何获得实现来自依赖项的方法调用列表
例如,依赖组id:"com.google.protobuf“
发布于 2022-07-19 13:44:08
方法调用由CodeQL类MethodAccess建模。如果还想包含构造函数调用,则可以使用CodeQL超类Call。Java引用表达式(例如MyClass::doSomething)分别建模为MemberRefExpr,因此您需要单独处理它们,以防您也考虑它们。
匹配方法调用的最简单方法是检查包名。例如,Protobuf类位于包com.google.protobuf或子包中。以下查询查找对它们的调用:
import java
from MethodAccess call
where
call.getMethod()
.getCompilationUnit()
.getPackage()
// Check if name starts with "com.google.protobuf"
.getName().matches("com.google.protobuf%")
select call使用Maven组和工件ID要复杂一些,而且可能也不那么可靠,如果构建不使用Maven,它可能无法工作。Maven构件是由CodeQL类MavenRepoJar建模的;该类位于一个单独的模块中,需要一个import。
import java
import semmle.code.xml.MavenPom
from MethodAccess call, MavenRepoJar repoJar
where
call.getMethod()
// Get the source declaration to prevent any issues with generic methods
.getSourceDeclaration()
.getCompilationUnit()
// Match the Maven artifact which contains the class
// Uses a transitive closure (`+`) to apply the predicate one or more times
// see https://codeql.github.com/docs/ql-language-reference/recursion/#transitive-closures
.getParentContainer+() = repoJar
and repoJar.getGroupId() = "com.google.protobuf"
select callhttps://stackoverflow.com/questions/73004184
复制相似问题