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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion java/dagger/lint/DaggerKotlinIssueDetector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.

node.findAnnotation(JVM_STATIC_ANNOTATION) != null
) {
val containingClass = node.containingClass?.toUElement(UClass::class.java) ?: return
if (containingClass.isObject()) {
Expand Down