Android Studio static analyzer doesn't show the error if custom lint rule worked

97 views
Skip to first unread message

Sergei Mikhailovskii

unread,
Jan 28, 2021, 3:34:24 PM1/28/21
to lint-dev
I faced the following problem: I need to implement a detector which scans method parameters and checks if two annotations (@Path and @Param) are used together. For this purpose I wrote the following detector class:

@Suppress("UnstableApiUsage")
class IssueDetector : Detector(), Detector.UastScanner {

override fun applicableAnnotations() = listOf("boringyuri.api.Path", "boringyuri.api.Param")

override fun visitAnnotationUsage(
context: JavaContext,
usage: UElement,
type: AnnotationUsageType,
annotation: UAnnotation,
qualifiedName: String,
method: PsiMethod?,
annotations: List<UAnnotation>,
allMemberAnnotations: List<UAnnotation>,
allClassAnnotations: List<UAnnotation>,
allPackageAnnotations: List<UAnnotation>
) {
val pathAnnotation = annotations.find { it.qualifiedName?.contains("boringyuri.api.Path") == true }
val paramAnnotation = annotations.find { it.qualifiedName?.contains("boringyuri.api.Param") == true }

if (pathAnnotation != null && paramAnnotation != null) {
pathAnnotation.also {
val message = "You cannot use @Path an @Param together applying to one field"
val location = context.getLocation(it)
context.report(ISSUE, it, location, message)
}
paramAnnotation.also {
val message = "You cannot use @Path an @Param together applying to one field"
val location = context.getLocation(it)
context.report(ISSUE, it, location, message)
}
}
}

companion object {
private val IMPLEMENTATION = Implementation(
IssueDetector::class.java,
Scope.JAVA_FILE_SCOPE
)

val ISSUE = Issue.create(
id = "BoringYURILintDetector",
briefDescription = "You cannot use @Path an @Param together applying to one field",
explanation = """
You cannot use @Path an @Param together applying to one field
""".trimIndent(),
category = Category.CORRECTNESS,
priority = 9,
severity = Severity.ERROR,
implementation = IMPLEMENTATION
)
}

}

As you can see, here I scan the code and report an issue if two annotations are applied. Here's my IssueRegistry class:

@Suppress("UnstableApiUsage")
class IssueRegistry : IssueRegistry() {

override val issues: List<Issue>
get() = listOf(IssueDetector.ISSUE)

override val api = CURRENT_API

}

As you can see, here's just a list of issues. When I run the ./gradlew lint the issue is found (attached the image). But Android Studio static analyzer doesn't see the issue (image 2). Why it can be caused?

Безымянный.png
Безымянный.png

Tor Norbye

unread,
Feb 23, 2021, 10:50:52 AM2/23/21
to lint-dev
I'm assuming you're using the lintCompile configuration here to have your Android code pick up lint rules compiled at the same time for a dependent Java library (the lint checks). There have been various issues reported with this in the past, sometimes because the Gradle plugin has changed the location where these intermediate jar files are stored and lint had to hardcode it -- it wasn't part of an API.  That has recently been added, so lint now reads the jars where the build system tells it they should be. I think there some other issues on the AGP side as well.

But the good news is that the AGP team is actively working on improving the lint integration now. I think in 7.0/Arctic Fox this will be much improved. (And do try the latest preview if you haven't already done that (both AGP and IDE together) to see if it works better.)

-- Tor
Reply all
Reply to author
Forward
0 new messages