Skip to content

Commit 2654011

Browse files
committed
Factor out common code for referencing an external type
1 parent 8781366 commit 2654011

File tree

2 files changed

+28
-70
lines changed

2 files changed

+28
-70
lines changed

java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt

Lines changed: 17 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,11 +1903,7 @@ open class KotlinFileExtractor(
19031903
tw.writeCallableBinding(id, methodLabel)
19041904
}
19051905

1906-
private val defaultConstructorMarkerClass by lazy {
1907-
val result = pluginContext.referenceClass(FqName("kotlin.jvm.internal.DefaultConstructorMarker"))?.owner
1908-
result?.let { extractExternalClassLater(it) }
1909-
result
1910-
}
1906+
private val defaultConstructorMarkerClass by lazy { referenceExternalClass("kotlin.jvm.internal.DefaultConstructorMarker") }
19111907

19121908
private val defaultConstructorMarkerType by lazy {
19131909
defaultConstructorMarkerClass?.typeWith()
@@ -2269,11 +2265,7 @@ open class KotlinFileExtractor(
22692265

22702266
private fun findFunction(cls: IrClass, name: String): IrFunction? = cls.declarations.findSubType<IrFunction> { it.name.asString() == name }
22712267

2272-
val jvmIntrinsicsClass by lazy {
2273-
val result = pluginContext.referenceClass(FqName("kotlin.jvm.internal.Intrinsics"))?.owner
2274-
result?.let { extractExternalClassLater(it) }
2275-
result
2276-
}
2268+
val jvmIntrinsicsClass by lazy { referenceExternalClass("kotlin.jvm.internal.Intrinsics") }
22772269

22782270
private fun findJdkIntrinsicOrWarn(name: String, warnAgainstElement: IrElement): IrFunction? {
22792271
val result = jvmIntrinsicsClass?.let { findFunction(it, name) }
@@ -2319,11 +2311,7 @@ open class KotlinFileExtractor(
23192311
return prop
23202312
}
23212313

2322-
val javaLangString by lazy {
2323-
val result = pluginContext.referenceClass(FqName("java.lang.String"))?.owner
2324-
result?.let { extractExternalClassLater(it) }
2325-
result
2326-
}
2314+
val javaLangString by lazy { referenceExternalClass("java.lang.String") }
23272315

23282316
val stringValueOfObjectMethod by lazy {
23292317
val result = javaLangString?.declarations?.findSubType<IrFunction> {
@@ -2347,11 +2335,7 @@ open class KotlinFileExtractor(
23472335
result
23482336
}
23492337

2350-
val kotlinNoWhenBranchMatchedExn by lazy {
2351-
val result = pluginContext.referenceClass(FqName("kotlin.NoWhenBranchMatchedException"))?.owner
2352-
result?.let { extractExternalClassLater(it) }
2353-
result
2354-
}
2338+
val kotlinNoWhenBranchMatchedExn by lazy {referenceExternalClass("kotlin.NoWhenBranchMatchedException") }
23552339

23562340
val kotlinNoWhenBranchMatchedConstructor by lazy {
23572341
val result = kotlinNoWhenBranchMatchedExn?.declarations?.findSubType<IrConstructor> {
@@ -2363,11 +2347,7 @@ open class KotlinFileExtractor(
23632347
result
23642348
}
23652349

2366-
val javaUtilArrays by lazy {
2367-
val result = pluginContext.referenceClass(FqName("java.util.Arrays"))?.owner
2368-
result?.let { extractExternalClassLater(it) }
2369-
result
2370-
}
2350+
val javaUtilArrays by lazy { referenceExternalClass("java.util.Arrays") }
23712351

23722352
private fun isFunction(target: IrFunction, pkgName: String, classNameLogged: String, classNamePredicate: (String) -> Boolean, vararg fNames: String, isNullable: Boolean? = false) =
23732353
fNames.any { isFunction(target, pkgName, classNameLogged, classNamePredicate, it, isNullable) }
@@ -4633,6 +4613,8 @@ open class KotlinFileExtractor(
46334613
}
46344614
}
46354615

4616+
private val propertyRefType by lazy { referenceExternalClass("kotlin.jvm.internal.PropertyReference")?.typeWith() }
4617+
46364618
private fun extractPropertyReference(
46374619
exprKind: String,
46384620
propertyReferenceExpr: IrCallableReference<out IrSymbol>,
@@ -4696,8 +4678,7 @@ open class KotlinFileExtractor(
46964678

46974679
val declarationParent = peekDeclStackAsDeclarationParent(propertyReferenceExpr) ?: return
46984680
// The base class could be `Any`. `PropertyReference` is used to keep symmetry with function references.
4699-
val baseClass = pluginContext.referenceClass(FqName("kotlin.jvm.internal.PropertyReference"))?.owner?.typeWith()
4700-
?: pluginContext.irBuiltIns.anyType
4681+
val baseClass = propertyRefType ?: pluginContext.irBuiltIns.anyType
47014682

47024683
val classId = extractGeneratedClass(ids, listOf(baseClass, kPropertyType), locId, propertyReferenceExpr, declarationParent)
47034684

@@ -4791,6 +4772,8 @@ open class KotlinFileExtractor(
47914772
}
47924773
}
47934774

4775+
private val functionRefType by lazy { referenceExternalClass("kotlin.jvm.internal.FunctionReference")?.typeWith() }
4776+
47944777
private fun extractFunctionReference(
47954778
functionReferenceExpr: IrFunctionReference,
47964779
parent: StmtExprParent,
@@ -4905,8 +4888,7 @@ open class KotlinFileExtractor(
49054888
} else {
49064889
val declarationParent = peekDeclStackAsDeclarationParent(functionReferenceExpr) ?: return
49074890
// `FunctionReference` base class is required, because that's implementing `KFunction`.
4908-
val baseClass = pluginContext.referenceClass(FqName("kotlin.jvm.internal.FunctionReference"))?.owner?.typeWith()
4909-
?: pluginContext.irBuiltIns.anyType
4891+
val baseClass = functionRefType ?: pluginContext.irBuiltIns.anyType
49104892

49114893
val classId = extractGeneratedClass(ids, listOf(baseClass, fnInterfaceType), locId, functionReferenceExpr, declarationParent, null, { it.valueParameters.size == 1 }) {
49124894
// The argument to FunctionReference's constructor is the function arity.
@@ -4952,34 +4934,14 @@ open class KotlinFileExtractor(
49524934
}
49534935
}
49544936

4955-
private fun getFunctionalInterfaceType(functionNTypeArguments: List<IrType>): IrSimpleType? {
4956-
if (functionNTypeArguments.size > BuiltInFunctionArity.BIG_ARITY) {
4957-
val funName = "kotlin.jvm.functions.FunctionN"
4958-
val theFun = pluginContext.referenceClass(FqName(funName))
4959-
if (theFun == null) {
4960-
logger.warn("Cannot find $funName for getFunctionalInterfaceType")
4961-
return null
4962-
} else {
4963-
return theFun.typeWith(functionNTypeArguments.last())
4964-
}
4965-
} else {
4966-
return functionN(pluginContext)(functionNTypeArguments.size - 1).typeWith(functionNTypeArguments)
4967-
}
4968-
}
4937+
private fun getFunctionalInterfaceType(functionNTypeArguments: List<IrType>) =
4938+
getFunctionalInterfaceTypeWithTypeArgs(functionNTypeArguments.map { makeTypeProjection(it, Variance.INVARIANT) })
49694939

4970-
private fun getFunctionalInterfaceTypeWithTypeArgs(functionNTypeArguments: List<IrTypeArgument>): IrSimpleType? =
4971-
if (functionNTypeArguments.size > BuiltInFunctionArity.BIG_ARITY) {
4972-
val funName = "kotlin.jvm.functions.FunctionN"
4973-
val theFun = pluginContext.referenceClass(FqName(funName))
4974-
if (theFun == null) {
4975-
logger.warn("Cannot find $funName for getFunctionalInterfaceTypeWithTypeArgs")
4976-
null
4977-
} else {
4978-
theFun.typeWithArguments(listOf(functionNTypeArguments.last()))
4979-
}
4980-
} else {
4940+
private fun getFunctionalInterfaceTypeWithTypeArgs(functionNTypeArguments: List<IrTypeArgument>) =
4941+
if (functionNTypeArguments.size > BuiltInFunctionArity.BIG_ARITY)
4942+
referenceExternalClass("kotlin.jvm.functions.FunctionN")?.symbol?.typeWithArguments(listOf(functionNTypeArguments.last()))
4943+
else
49814944
functionN(pluginContext)(functionNTypeArguments.size - 1).symbol.typeWithArguments(functionNTypeArguments)
4982-
}
49834945

49844946
private data class FunctionLabels(
49854947
val methodId: Label<DbMethod>,

java/kotlin-extractor/src/main/kotlin/KotlinUsesExtractor.kt

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,15 @@ open class KotlinUsesExtractor(
4040
val pluginContext: IrPluginContext,
4141
val globalExtensionState: KotlinExtractorGlobalState
4242
) {
43-
val javaLangObject by lazy {
44-
val result = pluginContext.referenceClass(FqName("java.lang.Object"))?.owner
45-
result?.let { extractExternalClassLater(it) }
46-
result
47-
}
43+
fun referenceExternalClass(name: String) =
44+
pluginContext.referenceClass(FqName(name))?.owner.also {
45+
if (it == null)
46+
logger.warn("Unable to resolve external class $name")
47+
else
48+
extractExternalClassLater(it)
49+
}
50+
51+
val javaLangObject by lazy { referenceExternalClass("java.lang.Object") }
4852

4953
val javaLangObjectType by lazy {
5054
javaLangObject?.typeWith()
@@ -885,11 +889,7 @@ open class KotlinUsesExtractor(
885889
else -> null
886890
}
887891

888-
val javaUtilCollection by lazy {
889-
val result = pluginContext.referenceClass(FqName("java.util.Collection"))?.owner
890-
result?.let { extractExternalClassLater(it) }
891-
result
892-
}
892+
val javaUtilCollection by lazy { referenceExternalClass("java.util.Collection") }
893893

894894
val wildcardCollectionType by lazy {
895895
javaUtilCollection?.let {
@@ -1152,11 +1152,7 @@ open class KotlinUsesExtractor(
11521152
return "@\"$prefix;{$parentId}.$name($paramTypeIds){$returnTypeId}${typeArgSuffix}\""
11531153
}
11541154

1155-
val javaLangClass by lazy {
1156-
val result = pluginContext.referenceClass(FqName("java.lang.Class"))?.owner
1157-
result?.let { extractExternalClassLater(it) }
1158-
result
1159-
}
1155+
val javaLangClass by lazy { referenceExternalClass("java.lang.Class") }
11601156

11611157
fun kClassToJavaClass(t: IrType): IrType {
11621158
when(t) {

0 commit comments

Comments
 (0)