This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
fun `test multiple calls to method which created OkHttpClient`() { | |
val kotlinFile = kotlin( | |
""" | |
package com.brokoli.lint | |
import okhttp3.OkHttpClient | |
class MyClass { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MoreThanOneOkHttpClientDetectorTest : AndroidSdkLintDetectorTest() { | |
override fun getDetector(): Detector = MoreThanOneOkHttpClientDetector() | |
override fun getIssues(): MutableList<Issue> = mutableListOf(MoreThanOneOkHttpClientDetector.ISSUE) | |
private val okHttpClientFile = java( | |
""" | |
package okhttp3; | |
class OkHttpClient { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
NewerOlder