1 /* <lambda>null2 * Copyright (C) 2023 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.lint.UastEnvironment 20 import com.android.tools.metalava.model.ClassItem 21 import com.android.tools.metalava.model.ClassResolver 22 import com.android.tools.metalava.model.Codebase 23 import com.android.tools.metalava.model.source.SourceSet 24 import com.intellij.psi.JavaPsiFacade 25 import com.intellij.psi.search.GlobalSearchScope 26 import java.io.File 27 28 internal class PsiBasedClassResolver( 29 uastEnvironment: UastEnvironment, 30 config: Codebase.Config, 31 allowReadingComments: Boolean, 32 ) : ClassResolver { 33 private val javaPsiFacade: JavaPsiFacade 34 private val searchScope: GlobalSearchScope 35 private val classpathCodebase: PsiBasedCodebase 36 37 init { 38 // Properties used to resolve classes from the classpath 39 val project = uastEnvironment.ideaProject 40 javaPsiFacade = JavaPsiFacade.getInstance(project) 41 searchScope = GlobalSearchScope.everythingScope(project) 42 43 val assembler = 44 PsiCodebaseAssembler(uastEnvironment) { assembler -> 45 PsiBasedCodebase( 46 location = File("classpath"), 47 description = "Codebase from classpath", 48 config = config, 49 fromClasspath = true, 50 allowReadingComments = allowReadingComments, 51 assembler = assembler, 52 ) 53 } 54 assembler.initializeFromSources(SourceSet.empty(), apiPackages = null) 55 classpathCodebase = assembler.codebase 56 } 57 58 override fun resolveClass(erasedName: String): ClassItem? { 59 // If the class cannot be found on the class path then return null, otherwise create a 60 // PsiClassItem for it. 61 val psiClass = javaPsiFacade.findClass(erasedName, searchScope) ?: return null 62 return classpathCodebase.findOrCreateClass(psiClass) 63 } 64 } 65