Fix #KT-12047 (Kotlin Lint: "Missing @JavascriptInterface on methods" does not report anything)

(cherry picked from commit 61e8e01)
(cherry picked from commit dbc54e2)
This commit is contained in:
Yan Zhulanow
2016-05-17 20:40:12 +03:00
parent d1447faf2b
commit 4ee19cce93
3 changed files with 21 additions and 1 deletions
@@ -96,7 +96,7 @@ public class JavaScriptInterfaceDetector extends Detector implements UastScanner
}
UExpression first = node.getValueArguments().get(0);
UElement resolved = node.resolve(context);
UElement resolved = UastUtils.resolveIfCan(first, context);
if (resolved instanceof UVariable) {
// We're passing in a variable to the addJavaScriptInterface method;
// the variable may be of a more generic type than the actual
@@ -16,9 +16,15 @@
package org.jetbrains.kotlin.uast
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.psi.KtObjectLiteralExpression
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperClassOrAny
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.uast.UElement
import org.jetbrains.uast.UObjectLiteralExpression
import org.jetbrains.uast.UType
import org.jetbrains.uast.psi.PsiElementBacked
class KotlinUObjectLiteralExpression(
@@ -26,4 +32,10 @@ class KotlinUObjectLiteralExpression(
override val parent: UElement
) : UObjectLiteralExpression, PsiElementBacked, KotlinUElementWithType {
override val declaration by lz { KotlinUClass(psi.objectDeclaration, this, true) }
override fun getExpressionType(): UType? {
val obj = psi.objectDeclaration
val bindingContext = obj.analyze(BodyResolveMode.PARTIAL)
val descriptor = bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, obj] as? ClassDescriptor ?: return null
return KotlinConverter.convert(descriptor.getSuperClassOrAny().defaultType, psi.project, null)
}
}
@@ -12,6 +12,10 @@ class JavaScriptTestK {
webview.addJavascriptInterface(InheritsFromAnnotated(), "myobj")
webview.addJavascriptInterface(NonAnnotatedObject(), "myobj")
webview.addJavascriptInterface(null, "nothing")
webview.addJavascriptInterface(object : Any() { @JavascriptInterface fun method() {} }, "nothing")
webview.addJavascriptInterface(JavascriptFace(), "nothing")
var o: Any = NonAnnotatedObject()
webview.addJavascriptInterface(o, "myobj")
o = InheritsFromAnnotated()
@@ -55,4 +59,8 @@ class JavaScriptTestK {
override fun test2() {}
}
}
class JavascriptFace {
fun method() {}
}