1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.tools.metalava.model.psi 18 19 import com.android.tools.metalava.model.ClassItem 20 import com.android.tools.metalava.model.TypeParameterItem 21 import com.android.tools.metalava.model.psi.ClassType.TYPE_PARAMETER 22 import com.intellij.psi.PsiTypeParameter 23 import org.jetbrains.kotlin.asJava.elements.KtLightTypeParameter 24 25 class PsiTypeParameterItem( 26 codebase: PsiBasedCodebase, 27 psiClass: PsiTypeParameter, 28 name: String, 29 modifiers: PsiModifierItem 30 31 ) : PsiClassItem( 32 codebase = codebase, 33 psiClass = psiClass, 34 name = name, 35 fullName = name, 36 qualifiedName = name, 37 hasImplicitDefaultConstructor = false, 38 classType = TYPE_PARAMETER, 39 modifiers = modifiers, 40 documentation = "" 41 ), TypeParameterItem { boundsnull42 override fun bounds(): List<ClassItem> = bounds 43 44 override fun isReified(): Boolean { 45 return element is KtLightTypeParameter && element.kotlinOrigin.text.startsWith("reified") 46 } 47 48 private lateinit var bounds: List<ClassItem> 49 finishInitializationnull50 override fun finishInitialization() { 51 super.finishInitialization() 52 53 val refs = psiClass.extendsList?.referencedTypes 54 bounds = if (refs != null && refs.isNotEmpty()) { 55 // Omit java.lang.Object since PSI will turn "T extends Comparable" to "T extends Object & Comparable" 56 // and this just makes comparisons harder; *everything* extends Object. 57 refs.mapNotNull { PsiTypeItem.create(codebase, it).asClass() }.filter { !it.isJavaLangObject() } 58 } else { 59 emptyList() 60 } 61 } 62 63 companion object { createnull64 fun create(codebase: PsiBasedCodebase, psiClass: PsiTypeParameter): PsiTypeParameterItem { 65 val simpleName = psiClass.name!! 66 val modifiers = modifiers(codebase, psiClass, "") 67 68 val item = PsiTypeParameterItem( 69 codebase = codebase, 70 psiClass = psiClass, 71 name = simpleName, 72 modifiers = modifiers 73 ) 74 item.modifiers.setOwner(item) 75 item.initialize(emptyList(), emptyList(), emptyList(), emptyList(), emptyList()) 76 return item 77 } 78 } 79 }