用于“ when condition”检查的自定义棉绒规则

时间:2019-05-08 08:06:14

标签: android android-studio kotlin jetbrains-ide lint

我正在使用Gson的序列化适配器RuntimeTypeAdapterFactory来基于 disciminator 序列化数据。但是,如果存在unknwon标识符(来自API)并且未在客户端上定义,则TypeAdapter将为null。

在这种情况下,如果条件检查为:

when(fooType){
 is fooA -> //blaba
 is fooB -> //blaba
 //else or null is not handled
}

,并且fooTypenull,它将由于未处理null条件而崩溃。 是否可以创建自定义棉绒规则(检测器)来检查何时条件(在Java中为instanceof),如果它们实现了else或{ {1}}在Android Studio检查中检查并注入?

1 个答案:

答案 0 :(得分:1)

我确实没有编写自定义皮棉支票的经验,但我认为是这样的(尽管它不是最佳选择,并且您不应按原样使用它,因为它仅显示了想法)可能会起作用。

class WhenNullElseMissingDetector : Detector(), Detector.UastScanner {

    override fun getApplicableUastTypes(): MutableList<Class<out UElement>> =
        mutableListOf(USwitchExpression::class.java)

    override fun createUastHandler(context: JavaContext): UElementHandler = WhenNullElseMissingHandler(context)
}

class WhenNullElseMissingHandler(private val context: JavaContext) : UElementHandler() {

    override fun visitSwitchExpression(node: USwitchExpression) {
        if (node.psi?.javaClass != KtWhenExpression::class.java) {
            // returning early here, so it doesn't go false positive on java switches
            return
        }

        var hasNullClause = false
        var hasElseClause = false

        node.body.expressions.forEach { clause ->
             // checking the child's text here just for a example, you should
             // probably check if child's class is KtWhenConditionIsPattern,
             // KtWhenConditionWithExpression or LeafPsiElement
            val clauseText = clause.psi?.firstChild?.text

            if ("null" == clauseText) {
                hasNullClause = true
            } else if ("else" == clauseText) {
                hasElseClause = true
            }
        }

        if (!hasElseClause) {
            context.report(
                WHEN_NULL_OR_ELSE_MISSING, node, context.getLocation(node),
                "When expression must include an else case"
            )
        }

        if (!hasNullClause) {
            context.report(
                WHEN_NULL_OR_ELSE_MISSING, node, context.getLocation(node),
                "When expression must include a null case"
            )
        }
    }
}

当然,您必须将新的检测器添加到IssueRegistry。 假设您在一个单独的java / kotlin模块中进行了lint检查,则可能看起来像这样

package com.example.lintchecks

// omit imports

val WHEN_NULL_OR_ELSE_MISSING = Issue.create(
    "MY_ISSUE_ID",
    "A brief to show in a one line popup",
    "More detailed explanation",
    Category.CORRECTNESS,
    5,
    Severity.WARNING,
    Implementation(
        WhenNullElseMissingDetector::class.java,
        Scope.JAVA_FILE_SCOPE
    )
)

class MyIssueRegistry : IssueRegistry() {
    override val issues: List<Issue>
        get() = listOf(WHEN_NULL_OR_ELSE_MISSING)

}

库模块的build.gradle文件应具有com.android.tools.lint:lint-api:26.4.0(或与之相关的任何版本)compileOnly依赖性和类似这样的块

jar {
    manifest {
        attributes("Lint-Registry-v2": "com.example.lintchecks.MyIssueRegistry")
    }
}