Skip to content

Instantly share code, notes, and snippets.

@cmathew
cmathew / WarnLocalDateConversion.kt
Created September 12, 2023 15:25
Android Lint code to remind developers that DateTime.toLocalDate is a lossy operation
class WarnLocalDateConversion : Detector(), SourceCodeScanner {
override fun getApplicableMethodNames(): List<String> = listOf("toLocalDateTime", "toLocalDate")
override fun visitMethodCall(context: JavaContext, node: UCallExpression, method: PsiMethod) {
val receiverClassName = (node.receiverType as PsiClassReferenceType).reference.qualifiedName
if (receiverClassName == "org.joda.time.DateTime") {
context.report(
WARN_LOCAL_DATE_CONVERSION,
node,
context.getLocation(node),
@cmathew
cmathew / AndroidLintJodaExample.kt
Created September 12, 2023 17:19
Example of providing a stubbed dependency to an Android Lint unit test
// Define Joda-Time stub
val JODA_STUB = TestFiles.kotlin(
"""
package org.joda.time
class DateTime(
private val moment: Int,
private val zone: String
) {
companion object {