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?