Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save guilhermekrz/7d466902ef2e797fe54895284f9db489 to your computer and use it in GitHub Desktop.
Save guilhermekrz/7d466902ef2e797fe54895284f9db489 to your computer and use it in GitHub Desktop.
override fun isCallGraphRequired(): Boolean {
return true
}
// Another call graph example: https://groups.google.com/forum/#!searchin/lint-dev/caller|sort:date/lint-dev/wFvCZOt4wZ8/g5punP99BAAJ
override fun analyzeCallGraph(context: Context, callGraph: CallGraphResult) {
val contextualCallGraph = callGraph.callGraph.buildContextualCallGraph(callGraph.receiverEval)
val okHttpClientNodes = contextualCallGraph.contextualNodes.stream().filter { contextualNode ->
val element = contextualNode.node.target.element
if(element is UMethod) {
element.containingClass?.qualifiedName == OKHTTP_CLIENT && element.name == "OkHttpClient"
} else {
false
}
}.collect(Collectors.toList())
if(okHttpClientNodes.size == 0) {
// OkHttpClient not used in this project
return
}
if(okHttpClientNodes.size != 1) {
val parser = context.client.getUastParser(context.project)
val node = okHttpClientNodes.first()
val location = parser.createLocation(node.node.target.element)
reportUsage(context, location)
return
}
backwardSearch(context, callGraph.callGraph, contextualCallGraph, okHttpClientNodes.first())
}
private fun backwardSearch(context: Context, callGraph: CallGraph, contextualCallGraph: ContextualCallGraph, node: ContextualNode) {
val callers = contextualCallGraph.inEdges(node)
if(callers.isEmpty()) {
// We are done
return
}
if(callers.size == 1) {
backwardSearch(context, callGraph, contextualCallGraph, callers.first().contextualNode)
return
}
val parser = context.client.getUastParser(context.project)
callers.map { it.contextualNode }.forEach { contextualNode ->
val location = parser.createLocation(contextualNode.node.target.element)
reportUsage(context, location)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment