ApiUsageUastVisitor: make `visitQualifiedReferenceExpression` call `processClassRefer...
authorNicolay Mitropolsky <nicolay.mitropolsky@jetbrains.com>
Mon, 8 Aug 2022 10:32:00 +0000 (12:32 +0200)
committerintellij-monorepo-bot <intellij-monorepo-bot-no-reply@jetbrains.com>
Mon, 8 Aug 2022 15:19:37 +0000 (15:19 +0000)
GitOrigin-RevId: 27d5a7beeb19fdda4306d7b5b02d05c63b838f55

java/java-analysis-impl/src/com/intellij/codeInspection/apiUsage/ApiUsageUastVisitor.kt

index bf1971d80740b4d47461acbc130ed2363a3382b2..224323047cb796c3b15f37dbfbf3b34e0afad3e8 100644 (file)
@@ -43,37 +43,40 @@ open class ApiUsageUastVisitor(private val apiUsageProcessor: ApiUsageProcessor)
       return true
     }
     val resolved = node.resolve()
-    if (resolved is PsiMethod) {
-      if (isClassReferenceInConstructorInvocation(node)) {
-        /*
+    if (processClassReferenceInConstructorInvocation(node, resolved)) return true
+    if (resolved is PsiModifierListOwner) {
+      apiUsageProcessor.processReference(node, resolved, null)
+      return true
+    }
+    return true
+  }
+
+  private fun processClassReferenceInConstructorInvocation(node: UReferenceExpression, resolved: PsiElement?): Boolean {
+    if (resolved is PsiMethod && isClassReferenceInConstructorInvocation(node)) {
+      /*
           Suppose a code:
           ```
              object : SomeClass(42) { }
-
+  
              or
-
+  
              new SomeClass(42)
           ```
           with USimpleNameReferenceExpression pointing to `SomeClass`.
-
+  
           We want ApiUsageProcessor to notice two events: 1) reference to `SomeClass` and 2) reference to `SomeClass(int)` constructor.
-
+  
           But Kotlin UAST resolves this simple reference to the PSI constructor of the class SomeClass.
           So we resolve it manually to the class because the constructor will be handled separately
           in "visitObjectLiteralExpression" or "visitCallExpression".
         */
-        val resolvedClass = resolved.containingClass
-        if (resolvedClass != null) {
-          apiUsageProcessor.processReference(node, resolvedClass, null)
-        }
-        return true
+      val resolvedClass = resolved.containingClass
+      if (resolvedClass != null) {
+        apiUsageProcessor.processReference(node, resolvedClass, null)
       }
-    }
-    if (resolved is PsiModifierListOwner) {
-      apiUsageProcessor.processReference(node, resolved, null)
       return true
     }
-    return true
+    return false
   }
 
   override fun visitQualifiedReferenceExpression(node: UQualifiedReferenceExpression): Boolean {
@@ -89,6 +92,7 @@ open class ApiUsageUastVisitor(private val apiUsageProcessor: ApiUsageProcessor)
     if (resolved == null) {
       resolved = node.selector.tryResolve()
     }
+    if (processClassReferenceInConstructorInvocation(node, resolved)) return true
     if (resolved is PsiModifierListOwner) {
       apiUsageProcessor.processReference(node.selector, resolved, node.receiver)
     }