Skip to content

Commit

Permalink
Merge pull request #916 from ZacSweers/z/workaroundGetPackage
Browse files Browse the repository at this point in the history
Work around FIR missing getPackage()
  • Loading branch information
ZacSweers authored Mar 12, 2024
2 parents 9562c2a + c450c1c commit fbb31b8
Showing 1 changed file with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,30 @@ import com.squareup.anvil.compiler.internal.reference.toTopLevelFunctionReferenc
import com.squareup.anvil.compiler.internal.reference.toTopLevelPropertyReference
import com.squareup.anvil.compiler.internal.requireFqName
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptorVisitor
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
import org.jetbrains.kotlin.descriptors.PackageViewDescriptor
import org.jetbrains.kotlin.descriptors.TypeAliasDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.findTypeAliasAcrossModuleDependencies
import org.jetbrains.kotlin.descriptors.resolveClassByFqName
import org.jetbrains.kotlin.incremental.components.LookupLocation
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtFunction
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
import org.jetbrains.kotlin.resolve.descriptorUtil.classId
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
import org.jetbrains.kotlin.resolve.scopes.MemberScope

public class RealAnvilModuleDescriptor private constructor(
delegate: ModuleDescriptor,
private val delegate: ModuleDescriptor,
) : AnvilModuleDescriptor, ModuleDescriptor by delegate {

private val ktFileToClassReferenceMap = mutableMapOf<String, List<Psi>>()
Expand Down Expand Up @@ -148,6 +155,17 @@ public class RealAnvilModuleDescriptor private constructor(
return psiClassReference() ?: descriptorClassReference()
}

override fun getPackage(fqName: FqName): PackageViewDescriptor {
return try {
delegate.getPackage(fqName)
} catch (ignored: IllegalStateException) {
// In K2 FIR, ModuleDescriptor.getPackage() throws an exception if no fragments are
// found in it. We expect there to sometimes be no fragments found though, so we can just
// swallow this exception and return an empty view.
EmptyPackageViewDescriptor(fqName, this)
}
}

private val KtFile.identifier: String
get() = packageFqName.asString() + name

Expand Down Expand Up @@ -176,6 +194,38 @@ public class RealAnvilModuleDescriptor private constructor(
ClassReferenceCacheKey(fqNameSafe, DESCRIPTOR)
}
}

private class EmptyPackageViewDescriptor(
override val fqName: FqName,
override val module: ModuleDescriptor,
) : PackageViewDescriptor {
override val annotations: Annotations = Annotations.EMPTY
override val fragments: List<PackageFragmentDescriptor> = emptyList()
override val memberScope: MemberScope = MemberScope.Empty

override fun <R : Any?, D : Any?> accept(
visitor: DeclarationDescriptorVisitor<R, D>?,
data: D,
): R {
throw NotImplementedError()
}

override fun acceptVoid(visitor: DeclarationDescriptorVisitor<Void, Void>?) {
throw NotImplementedError()
}

override fun getContainingDeclaration(): PackageViewDescriptor? {
throw NotImplementedError()
}

override fun getName(): Name {
throw NotImplementedError()
}

override fun getOriginal(): DeclarationDescriptor {
throw NotImplementedError()
}
}
}

private fun KtFile.classesAndInnerClasses(): List<KtClassOrObject> {
Expand Down

0 comments on commit fbb31b8

Please sign in to comment.