Skip to content

Commit cadbb6e

Browse files
committed
Fixed throws superclass check
1 parent c1aef59 commit cadbb6e

3 files changed

Lines changed: 24 additions & 4 deletions

File tree

lint-rules-android/src/main/java/com/thirdegg/lintrules/android/CheckedExceptionsDetector.kt

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import com.android.tools.lint.detector.api.Category.Companion.CORRECTNESS
44
import com.android.tools.lint.detector.api.Severity.WARNING
55
import com.android.tools.lint.client.api.UElementHandler
66
import com.android.tools.lint.detector.api.*
7+
import com.intellij.psi.PsiType
78
import org.jetbrains.uast.*
89
import org.jetbrains.uast.kotlin.KotlinUFunctionCallExpression
910
import java.util.*
1011
import org.jetbrains.uast.visitor.AbstractUastVisitor
1112
import kotlin.collections.ArrayList
13+
import kotlin.collections.HashSet
1214

1315

1416
val ISSUE_PATTERN = Issue.create("CheckedExceptions",
@@ -58,6 +60,15 @@ class CheckedExceptionsDetector : Detector(), Detector.UastScanner {
5860
return namedExpressions
5961
}
6062

63+
fun findRecursiveExtentionsInClass(superTypes: Array<PsiType>):HashSet<String> {
64+
val classes = HashSet<String>()
65+
superTypes.forEach {
66+
classes.addAll(findRecursiveExtentionsInClass(it.superTypes))
67+
classes.add(it.canonicalText)
68+
}
69+
return classes
70+
}
71+
6172
override fun createUastHandler(context: JavaContext) = object:UElementHandler() {
6273

6374
init {
@@ -100,13 +111,21 @@ class CheckedExceptionsDetector : Detector(), Detector.UastScanner {
100111

101112
override fun visitClassLiteralExpression(node: UClassLiteralExpression): Boolean {
102113

114+
val arrayList = ArrayList<String>()
115+
103116
val clazzName = node.type?.canonicalText?:return super.visitClassLiteralExpression(node)
104117

118+
arrayList.add(clazzName)
119+
105120
if (haveTryCatch.contains(clazzName))
106121
return super.visitClassLiteralExpression(node)
107122

108-
node.type?.superTypes?.forEach {
109-
if (haveTryCatch.contains(it.canonicalText))
123+
if (node.type?.superTypes!=null) {
124+
arrayList.addAll(findRecursiveExtentionsInClass(node.type?.superTypes!!))
125+
}
126+
127+
for (clazz in arrayList) {
128+
if (haveTryCatch.contains(clazz))
110129
return super.visitClassLiteralExpression(node)
111130
}
112131

lint-rules-android/src/test/java/com/thirdegg/lintrules/android/CheckedExceptionsUnitTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class CheckedExceptionsUnitTest {
7474
ThrowsInterfaceKotlin
7575
).run()
7676
.expect("""
77-
src/com/thirdegg/lintrules/android/ThrowsClassJava.kt:24: Warning: Exception not checked: com.thirdegg.lintrules.android.ThrowsTwoException [CheckedExceptions]
77+
src/com/thirdegg/lintrules/android/ThrowsClassJava.kt:24: Warning: Unhandled exception: com.thirdegg.lintrules.android.ThrowsTwoException [CheckedExceptions]
7878
test()
7979
~~~~
8080
0 errors, 1 warnings

lint-rules-android/src/test/java/com/thirdegg/lintrules/android/checkedexceptions/ThrowsTestClasses.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ object ThrowsTestClasses {
3030
3131
class ThrowsException:Exception("ThrowsException")
3232
class ThrowsTwoException:Exception("ThrowsException")
33+
class ThrowsThreeException:ThrowsTwoException()
3334
3435
""").indented()
3536

@@ -49,7 +50,7 @@ object ThrowsTestClasses {
4950
ThrowsInterfaceKotlin().tryTwo()
5051
}
5152
52-
@Throws(ThrowsException::class,ThrowsTwoException::class)
53+
@Throws(ThrowsException::class,ThrowsTwoException::class,ThrowsThreeException::class)
5354
fun test() {
5455
tryTwo()
5556
tryThree()

0 commit comments

Comments
 (0)