Skip to content

Commit 4ad8d74

Browse files
committed
checking suspendCoroutine v2
1 parent 078d57a commit 4ad8d74

3 files changed

Lines changed: 53 additions & 25 deletions

File tree

gradlew

100644100755
File mode changed.

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

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,65 +43,91 @@ class CheckedExceptionsDetector : Detector(), Detector.UastScanner {
4343
override fun createUastHandler(context: JavaContext) = object:UElementHandler() {
4444

4545
init {
46-
println(context.uastFile?.asRecursiveLogString())
46+
// println(context.uastFile?.asRecursiveLogString())
4747
}
4848

4949
override fun visitCallExpression(node: UCallExpression) {
5050

5151
val parrentNode = node
52-
val method = parrentNode.resolve()?:return
52+
val method = parrentNode.resolve() ?: return
5353
val uMethod = context.uastContext.getMethod(method)
5454

5555
val haveTryCatch = ArrayList<String>()
5656

57-
val tryException = findParrentByUast(parrentNode,UTryExpression::class.java)
58-
if (tryException!=null) {
57+
val tryException = findParrentByUast(parrentNode, UTryExpression::class.java)
58+
if (tryException != null) {
5959
for (catchCause in tryException.catchClauses) {
6060
haveTryCatch.add(findExceptionClassName(catchCause))
6161
}
6262
}
6363

64-
uMethod.accept(object:AbstractUastVisitor() {
64+
uMethod.accept(object : AbstractUastVisitor() {
6565
override fun visitCallExpression(node: UCallExpression): Boolean {
6666
if (node.uastParent !is UCallExpression) return super.visitCallExpression(node)
6767
val parentResolve = (node.uastParent as UCallExpression).resolve()
6868
val resolve = node.resolve()
69-
if (parentResolve?.containingClass?.qualifiedName != "kotlin.coroutines.Continuation"
70-
&& parentResolve?.containingClass?.qualifiedName != "com.thirdegg.lintrules.android.Continuation")
69+
if (parentResolve?.containingClass?.qualifiedName?.contains("Continuation") != true) {
7170
return super.visitCallExpression(node)
71+
}
7272

73-
if ((node.uastParent as UCallExpression?)?.methodName!="resumeWithException") return super.visitCallExpression(node)
73+
if ((node.uastParent as UCallExpression?)?.methodName != "resumeWithException")
74+
return super.visitCallExpression(node)
75+
76+
val clazzName = resolve?.containingClass?.qualifiedName
77+
?: return super.visitCallExpression(node)
78+
79+
if (haveTryCatch.contains(clazzName))
80+
return super.visitCallExpression(node)
81+
82+
var superClass = resolve.containingClass?.superClass
83+
while (superClass!=null) {
84+
if (haveTryCatch.contains(superClass.qualifiedName))
85+
return super.visitCallExpression(node)
86+
superClass = superClass.superClass
87+
}
7488

75-
val clazzName = resolve?.containingClass?.qualifiedName?:return super.visitCallExpression(node)
7689
context.report(ISSUE_PATTERN, parrentNode, context.getNameLocation(parrentNode),
7790
"Exception not checked: $clazzName")
7891

92+
println(clazzName)
93+
7994
return super.visitCallExpression(node)
8095
}
8196
})
8297

83-
uMethod.accept(object:AbstractUastVisitor() {
98+
uMethod.accept(object : AbstractUastVisitor() {
8499

85100
override fun visitThrowExpression(node: UThrowExpression): Boolean {
86101

87-
node.accept(object:AbstractUastVisitor() {
102+
node.accept(object : AbstractUastVisitor() {
103+
88104
override fun visitCallExpression(node: UCallExpression): Boolean {
89105
if (node is KotlinUFunctionCallExpression) {
90106
//TODO kotlin.Exception() not catch
91107
val clazz = node.resolve()
92-
val clazzName = clazz?.containingClass?.qualifiedName
93-
if (clazzName==null || haveTryCatch.contains(clazzName)) return super.visitCallExpression(node)
94-
context.report(ISSUE_PATTERN, parrentNode, context.getNameLocation(parrentNode),
95-
"Exception not checked: $clazzName")
108+
val clazzName = clazz?.containingClass?.qualifiedName?:return super.visitCallExpression(node)
109+
if (haveTryCatch.contains(clazzName)) return super.visitCallExpression(node)
110+
111+
var superClass = clazz.containingClass?.superClass
112+
while (superClass!=null) {
113+
if (haveTryCatch.contains(superClass?.qualifiedName))
114+
return super.visitCallExpression(node)
115+
superClass = superClass?.superClass
116+
}
117+
118+
context.report(ISSUE_PATTERN, parrentNode, context.getNameLocation(parrentNode),"Exception not checked: $clazzName")
96119
}
97120
return super.visitCallExpression(node)
98121
}
122+
99123
})
124+
100125
return super.visitThrowExpression(node)
101126
}
102127

103128
})
104129
}
105130

131+
106132
}
107133
}

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ object CheckedExceptionsCoroutinesClasses {
1313
callback.onResponse(Response())
1414
}
1515
16-
fun await():String = Helper.suspendCoroutine { cont:Continuation ->
16+
fun await():String = suspendCoroutine { cont ->
1717
enqueue(object: Callback {
1818
1919
override fun onResponse(response: Response) {
@@ -87,7 +87,6 @@ object CheckedExceptionsCoroutinesClasses {
8787
8888
package com.thirdegg.lintrules.android
8989
90-
import java.lang.Exception
9190
9291
class NotAuthorizedException(error: String):Exception(error)
9392
class ForbiddenException(error: String):Exception(error)
@@ -105,7 +104,11 @@ object CheckedExceptionsCoroutinesClasses {
105104
class Main {
106105
107106
fun check() {
108-
Call().await()
107+
try {
108+
Call().await()
109+
} catch (e:Exception) {
110+
e.printStackTrace()
111+
}
109112
}
110113
111114
}
@@ -130,16 +133,15 @@ object CheckedExceptionsCoroutinesClasses {
130133
}
131134
}
132135
133-
object Helper {
134-
135-
fun suspendCoroutine(block: (count:Continuation) -> Unit): String {
136-
val cont = Continuation()
137-
block(cont)
138-
return cont.result
139-
}
140136
137+
suspend inline fun suspendCoroutine(crossinline block: (Continuation) -> Unit): String {
138+
val cont = Continuation()
139+
block(cont)
140+
return cont.result
141141
}
142142
143+
144+
143145
""").indented()
144146

145147
}

0 commit comments

Comments
 (0)