visitImportStatement() - node.resolve() function returns null instaed of PsiClass

25 views
Skip to first unread message

Asaf Azar

unread,
Jun 23, 2021, 9:16:58 AM6/23/21
to lint-dev
Hi, 
im currently writing a lint detector which checks import statements in java files, and specifically checks that the imported class implements a certain method ("shouldFilterLog()").

Unfortunately, reslove() method always return Null value.
my question, what am i missing here? why does resolve() function doesnt works as planned? and do my check afterwards in "resloved.methods()" should work?


code:

'override fun createUastHandler(context: JavaContext): UElementHandler? {
return object : UElementHandler() {

override fun visitImportStatement(node: UImportStatement) {

val resolved: PsiElement? = node.resolve()
val importedType = node.importReference?.asSourceString()
val source = node.sourcePsi
if (!(resolved is PsiClass)) {
return
}

if (importedType != null) {
if (!importedType.contains("com.facebook.dsi.logger")) {
return
}
}

var hasRegisterFilterMethod = false
for (method in resolved.methods) {
if (method.name == "registerFilter") {
hasRegisterFilterMethod = true
break
}
}

if (!hasRegisterFilterMethod) {
context.report(
ISSUE, node as UElement, context.getLocation(node as UElement), ISSUE_MESSAGE)
}
}
}
}

Tor Norbye

unread,
Jul 14, 2021, 8:03:15 PM7/14/21
to lint-dev
I'm guessing your test files are referencing classes/APIs which are not part of the test's classpath. See https://googlesamples.github.io/android-custom-lint-rules/api-guide/unit-testing.md.html#lintcheckunittesting/librarydependenciesandstubs (as well as the tip at the end of that section; try updating to the latest version of lint (7.1.x alphas) which will do more checking and diagnostics.

-- Tor

Reply all
Reply to author
Forward
0 new messages