ApiUsageUastVisitor: make `isClassReferenceInConstructorInvocation` accept `UReferenc...
authorNicolay Mitropolsky <nicolay.mitropolsky@jetbrains.com>
Fri, 5 Aug 2022 15:14:35 +0000 (17:14 +0200)
committerintellij-monorepo-bot <intellij-monorepo-bot-no-reply@jetbrains.com>
Mon, 8 Aug 2022 15:19:34 +0000 (15:19 +0000)
GitOrigin-RevId: e22e36ccfd21226a7e995fd33d7c15e9dee29786

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

index bc18f7acef1dd8daa375d01e61176241cdde8142..bf1971d80740b4d47461acbc130ed2363a3382b2 100644 (file)
@@ -44,7 +44,7 @@ open class ApiUsageUastVisitor(private val apiUsageProcessor: ApiUsageProcessor)
     }
     val resolved = node.resolve()
     if (resolved is PsiMethod) {
-      if (isClassReferenceInConstructorInvocation(node) || isClassReferenceInKotlinSuperClassConstructor(node)) {
+      if (isClassReferenceInConstructorInvocation(node)) {
         /*
           Suppose a code:
           ```
@@ -333,40 +333,6 @@ open class ApiUsageUastVisitor(private val apiUsageProcessor: ApiUsageProcessor)
     }
   }
 
-  /**
-   * UAST for Kotlin generates UAST tree with "UnknownKotlinExpression (CONSTRUCTOR_CALLEE)" for the following expressions:
-   * 1) an object literal expression: `object : BaseClass() { ... }`
-   * 2) a super class constructor invocation `class Derived : BaseClass(42) { ... }`
-   *
-   *
-   * ```
-   * UObjectLiteralExpression
-   *     UnknownKotlinExpression (CONSTRUCTOR_CALLEE)
-   *         UTypeReferenceExpression (BaseClass)
-   *             USimpleNameReferenceExpression (BaseClass)
-   * ```
-   *
-   * and
-   *
-   * ```
-   * UCallExpression (kind = CONSTRUCTOR_CALL)
-   *     UnknownKotlinExpression (CONSTRUCTOR_CALLEE)
-   *         UTypeReferenceExpression (BaseClass)
-   *             USimpleNameReferenceExpression (BaseClass)
-   * ```
-   *
-   * This method checks if the given simple reference points to the `BaseClass` part,
-   * which is treated by Kotlin UAST as a reference to `BaseClass'` constructor, not to the `BaseClass` itself.
-   */
-  private fun isClassReferenceInKotlinSuperClassConstructor(expression: USimpleNameReferenceExpression): Boolean {
-    val parent1 = expression.uastParent
-    val parent2 = parent1?.uastParent
-    val parent3 = parent2?.uastParent
-    return parent1 is UTypeReferenceExpression
-           && parent2 != null && parent2.asLogString().contains("CONSTRUCTOR_CALLEE")
-           && (parent3 is UObjectLiteralExpression || parent3 is UCallExpression && parent3.kind == UastCallKind.CONSTRUCTOR_CALL)
-  }
-
   private fun isSelectorOfQualifiedReference(expression: USimpleNameReferenceExpression): Boolean {
     val qualifiedReference = expression.uastParent as? UQualifiedReferenceExpression ?: return false
     return haveSameSourceElement(expression, qualifiedReference.selector)
@@ -377,25 +343,25 @@ open class ApiUsageUastVisitor(private val apiUsageProcessor: ApiUsageProcessor)
     return callExpression.kind == UastCallKind.NEW_ARRAY_WITH_DIMENSIONS
   }
 
-  private fun isSuperOrThisCall(simpleReference: USimpleNameReferenceExpression): Boolean {
+  private fun isSuperOrThisCall(simpleReference: UReferenceExpression): Boolean {
     val callExpression = simpleReference.uastParent as? UCallExpression ?: return false
     return callExpression.kind == UastCallKind.CONSTRUCTOR_CALL &&
            (callExpression.methodIdentifier?.name == "super" || callExpression.methodIdentifier?.name == "this")
   }
 
-  private fun isClassReferenceInConstructorInvocation(simpleReference: USimpleNameReferenceExpression): Boolean {
-    if (isSuperOrThisCall(simpleReference)) {
+  private fun isClassReferenceInConstructorInvocation(reference: UReferenceExpression): Boolean {
+    if (isSuperOrThisCall(reference)) {
       return false
     }
-    val callExpression = simpleReference.uastParent as? UCallExpression ?: return false
+    val callExpression = reference.uastParent as? UCallExpression ?: return false
     if (callExpression.kind != UastCallKind.CONSTRUCTOR_CALL) {
       return false
     }
     val classReferenceNameElement = callExpression.classReference?.referenceNameElement
     if (classReferenceNameElement != null) {
-      return haveSameSourceElement(classReferenceNameElement, simpleReference.referenceNameElement)
+      return haveSameSourceElement(classReferenceNameElement, reference.referenceNameElement)
     }
-    return callExpression.resolve()?.name == simpleReference.resolvedName
+    return callExpression.resolve()?.name == reference.resolvedName
   }
 
   private fun isMethodReferenceOfCallExpression(expression: USimpleNameReferenceExpression): Boolean {