Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Lint crash with AGP 8.1.0 #4023

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mr-thierry
Copy link

A crash occurred in the project I'm working on when upgrading to AGP 8.1.0. See #3980.

I was able to reproduce the issue with a simple project. See https://github.com/mr-thierry/dagger_lint_crash

The cause of the crash is that in DaggerKotlinIssueDetector, node.hasAnnotation(JVM_STATIC_ANNOTATION) can return true, but node.findAnnotation(JVM_STATIC_ANNOTATION) can still return null. On AGP 8.0.0, findAnnotation wouldn't return null. I'm not sure what has changed in AGP that caused this.

@google-cla
Copy link

google-cla bot commented Aug 21, 2023

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@@ -200,7 +200,7 @@ class DaggerKotlinIssueDetector : Detector(), SourceCodeScanner {
override fun visitMethod(node: UMethod) {
if (!node.isConstructor &&
node.hasAnnotation(PROVIDES_ANNOTATION) &&
node.hasAnnotation(JVM_STATIC_ANNOTATION)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that UMethod doesn't override hasAnnotation and delegates it to the underlying javaPsi (of PsiMethod). So, having node.hasAnnotation(...) == true means the underlying light-class method has @JvmStatic that comes from the source PSI.

Changing like this will avoid the crash below, but cause false negatives: a static (e.g. (companion) object functions) with @JvmStatic won't be caught.


The right solution is to change:

val annotation = node.findAnnotation(JVM_STATIC_ANNOTATION)!!

to

val annotation = node.findAnnotation(JVM_STATIC_ANNOTATION)
  ?: node.javaPsi.modifierList.findAnnotation(JVM_STATIC_ANNOTATION)

where the first line still searches for UAnnotation for @JvmStatic in UMethod, and as a fall back for already-static method, the second line searches for the LC modeling of @JvmStatic in PsiMethod.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants