Created
April 26, 2020 11:43
-
-
Save PaulWoitaschek/7bb8fa8b4d3f4e03904c20553f425f06 to your computer and use it in GitHub Desktop.
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
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.SourceCodeScanner | |
import org.jetbrains.uast.UClass | |
class StupidLintRule : Detector(), SourceCodeScanner { | |
override fun getApplicableUastTypes() = listOf(UClass::class.java) | |
override fun createUastHandler(context: JavaContext) = ConstructorVisitor(context) | |
class ConstructorVisitor(private val context: JavaContext) : UElementHandler() { | |
override fun visitClass(node: UClass) { | |
val constructorCount = node.constructors.count() | |
if (constructorCount != 42) { | |
context.report( | |
ISSUE, | |
node, | |
context.getLocation(node.containingFile), | |
"Class doesn't have 42 constructors" | |
) | |
} | |
} | |
} | |
companion object { | |
val ISSUE = Issue.create( | |
id = "42Constructors", | |
briefDescription = "42Constructors", | |
explanation = "Every class needs 42 constructors.", | |
category = Category.CORRECTNESS, | |
priority = 6, | |
severity = Severity.FATAL, | |
androidSpecific = false, | |
implementation = Implementation(StupidLintRule::class.java, Scope.JAVA_FILE_SCOPE) | |
) | |
} | |
} | |
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
import com.android.tools.lint.checks.infrastructure.LintDetectorTest | |
import com.android.tools.lint.detector.api.Detector | |
import org.intellij.lang.annotations.Language | |
import org.junit.Test | |
import org.junit.runner.RunWith | |
import org.junit.runners.JUnit4 | |
@RunWith(JUnit4::class) | |
class StupidLintRuleTest : LintDetectorTest() { | |
override fun getIssues() = listOf(StupidLintRule.ISSUE) | |
override fun getDetector(): Detector = StupidLintRule() | |
@Test | |
fun test() { | |
@Language("kotlin") | |
val user = """ | |
package my.package | |
data class User( | |
val name: String | |
) | |
""".trimIndent() | |
lint() | |
.files(kotlin(user)) | |
.allowMissingSdk() | |
.run() | |
.expectErrorCount(1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment