Last active
February 26, 2021 20:24
-
-
Save AdamMc331/9179c4471ad703ea96530e5267746e1b to your computer and use it in GitHub Desktop.
A custom lint check for verifying any classes using the `@Json` annotation must also use the `@JsonClass` annotation.
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
package // ... | |
import com.android.tools.lint.client.api.UElementHandler | |
import com.android.tools.lint.detector.api.Category | |
import com.android.tools.lint.detector.api.Detector | |
import com.android.tools.lint.detector.api.Implementation | |
import com.android.tools.lint.detector.api.Issue | |
import com.android.tools.lint.detector.api.JavaContext | |
import com.android.tools.lint.detector.api.Scope | |
import com.android.tools.lint.detector.api.Severity | |
import com.android.tools.lint.detector.api.TextFormat | |
import org.jetbrains.uast.UClass | |
import org.jetbrains.uast.UElement | |
import org.jetbrains.uast.kotlin.KotlinUClass | |
@Suppress("UnstableApiUsage") | |
class MissingJsonClassAnnotationDetector : Detector(), Detector.UastScanner { | |
override fun getApplicableUastTypes(): List<Class<out UElement>> { | |
return listOf(UClass::class.java) | |
} | |
override fun createUastHandler(context: JavaContext): UElementHandler { | |
return JsonClassElementHandler(context) | |
} | |
private class JsonClassElementHandler(private val context: JavaContext) : UElementHandler() { | |
override fun visitClass(node: UClass) { | |
if (node is KotlinUClass) { | |
val hasJsonFields = doesClassHaveJsonFieldAnnotation(node) | |
if (hasJsonFields) { | |
val hasJsonClassAnnotation = | |
node.findAnnotation("com.squareup.moshi.JsonClass") != null | |
if (!hasJsonClassAnnotation) { | |
context.report( | |
issue = ISSUE_MISSING_JSON_CLASS_ANNOTATION, | |
location = context.getLocation(node.javaPsi), | |
message = ISSUE_MISSING_JSON_CLASS_ANNOTATION.getExplanation(TextFormat.TEXT), | |
) | |
} | |
} | |
} | |
} | |
private fun doesClassHaveJsonFieldAnnotation(kotlinUClass: KotlinUClass): Boolean { | |
return kotlinUClass | |
.fields | |
.any { field -> | |
field.findAnnotation("com.squareup.moshi.Json") != null | |
} | |
} | |
} | |
companion object { | |
internal val ISSUE_MISSING_JSON_CLASS_ANNOTATION = Issue.create( | |
id = "MissingJsonClassAnnotation", | |
briefDescription = "Classes that use Moshi parsing should have the JsonClass annotation.", | |
explanation = "The JsonClass annotation is required to use Moshi Code-Gen.", | |
category = Category.CORRECTNESS, | |
severity = Severity.ERROR, | |
implementation = Implementation( | |
MissingJsonClassAnnotationDetector::class.java, | |
Scope.JAVA_FILE_SCOPE, | |
), | |
priority = 10 | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment