Skip to content

Instantly share code, notes, and snippets.

View guilhermekrz's full-sized avatar

Guilherme Krzisch guilhermekrz

View GitHub Profile
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) {
@Test
fun `test multiple calls to method which created OkHttpClient`() {
val kotlinFile = kotlin(
"""
package com.brokoli.lint
import okhttp3.OkHttpClient
class MyClass {
private const val OKHTTP_CLIENT = "okhttp3.OkHttpClient"
private val okHttpClientConstructors = mutableSetOf<CallContextLocation>()
data class CallContextLocation(val context: JavaContext, val location: Location)
override fun getApplicableUastTypes(): List<Class<out UElement>>? {
return listOf(UCallExpression::class.java)
}
@Test
fun `two calls to OkHttpClient constructor in different files`() {
val javaFile = java(
"""
package com.brokoli.lint;
import okhttp3.OkHttpClient;
class MyClass {
public void method1() {
class MoreThanOneOkHttpClientDetectorTest : AndroidSdkLintDetectorTest() {
override fun getDetector(): Detector = MoreThanOneOkHttpClientDetector()
override fun getIssues(): MutableList<Issue> = mutableListOf(MoreThanOneOkHttpClientDetector.ISSUE)
private val okHttpClientFile = java(
"""
package okhttp3;
class OkHttpClient {
class MoreThanOneOkHttpClientDetector : Detector(), SourceCodeScanner {
private fun reportUsage(context: JavaContext, location: Location) {
context.report(
issue = ISSUE,
location = location,
message = "You should only create one OkHttpClient instance"
)
}
UFile (package = com.brokoli.lint)
UClass (name = MyClass)
UAnnotationMethod (name = methodWith6Parameters)
UParameter (name = first)
UAnnotation (fqName = org.jetbrains.annotations.NotNull)
UParameter (name = second)
UAnnotation (fqName = org.jetbrains.annotations.NotNull)
UParameter (name = third)
UAnnotation (fqName = org.jetbrains.annotations.NotNull)
UParameter (name = fourth)
override fun getApplicableUastTypes(): List<Class<out UElement>>? {
return listOf(UMethod::class.java)
}
override fun createUastHandler(context: JavaContext): UElementHandler? {
return MethodHandler(context)
}
inner class MethodHandler(private val context: JavaContext) : UElementHandler() {
@Test
fun `kotlin method with 6 parameters`() {
val kotlinFile = kotlin(
"""
package com.brokoli.lint;
class MyClass {
fun methodWith6Parameters(first: Boolean, second: String, third: Int, fourth: Long, fifth: Char, sixth: Boolean) {
override fun getDetector(): Detector = TooManyParametersDetector()
override fun getIssues(): MutableList<Issue> = mutableListOf(TooManyParametersDetector.ISSUE)
@Test
fun `java method with 0 parameters`() {
val javaFile = java(
"""
package com.brokoli.lint;