如何从依赖项中获得实现的方法调用列表
例如,依赖组id: "com.google.protobuf">
方法调用由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 call
查询控制台链接