mirror of
https://github.com/github/codeql.git
synced 2025-12-22 11:46:32 +01:00
Kotlin: annotation properties should be java.lang.Class not KClass
As documented at https://kotlinlang.org/docs/annotations.html#constructors, annotation properties of type KClass get rewritten when targeting the JVM.
This commit is contained in:
@@ -628,14 +628,16 @@ open class KotlinFileExtractor(
|
||||
private fun extractValueParameter(vp: IrValueParameter, parent: Label<out DbCallable>, idx: Int, typeSubstitution: TypeSubstitution?, parentSourceDeclaration: Label<out DbCallable>, classTypeArgsIncludingOuterClasses: List<IrTypeArgument>?, extractTypeAccess: Boolean, locOverride: Label<DbLocation>? = null): TypeResults {
|
||||
with("value parameter", vp) {
|
||||
val location = locOverride ?: getLocation(vp, classTypeArgsIncludingOuterClasses)
|
||||
val maybeErasedType = (vp.parent as? IrFunction)?.let {
|
||||
val maybeAlteredType = (vp.parent as? IrFunction)?.let {
|
||||
if (overridesCollectionsMethodWithAlteredParameterTypes(it))
|
||||
eraseCollectionsMethodParameterType(vp.type, it.name.asString(), idx)
|
||||
else if ((vp.parent as? IrConstructor)?.parentClassOrNull?.kind == ClassKind.ANNOTATION_CLASS)
|
||||
kClassToJavaClass(vp.type)
|
||||
else
|
||||
null
|
||||
} ?: vp.type
|
||||
val javaType = (vp.parent as? IrFunction)?.let { getJavaCallable(it)?.let { jCallable -> getJavaValueParameterType(jCallable, idx) } }
|
||||
val typeWithWildcards = addJavaLoweringWildcards(maybeErasedType, !hasWildcardSuppressionAnnotation(vp), javaType)
|
||||
val typeWithWildcards = addJavaLoweringWildcards(maybeAlteredType, !hasWildcardSuppressionAnnotation(vp), javaType)
|
||||
val substitutedType = typeSubstitution?.let { it(typeWithWildcards, TypeContext.OTHER, pluginContext) } ?: typeWithWildcards
|
||||
val id = useValueParameter(vp, parent)
|
||||
if (extractTypeAccess) {
|
||||
@@ -734,14 +736,17 @@ open class KotlinFileExtractor(
|
||||
val initializer: IrExpressionBody?
|
||||
val lhsType: TypeResults?
|
||||
val vId: Label<out DbVariable>?
|
||||
val isAnnotationClassField: Boolean
|
||||
if (f is IrField) {
|
||||
static = f.isStatic
|
||||
initializer = f.initializer
|
||||
lhsType = useType(f.type)
|
||||
isAnnotationClassField = isAnnotationClassField(f)
|
||||
lhsType = useType(if (isAnnotationClassField) kClassToJavaClass(f.type) else f.type)
|
||||
vId = useField(f)
|
||||
} else if (f is IrEnumEntry) {
|
||||
static = true
|
||||
initializer = f.initializerExpression
|
||||
isAnnotationClassField = false
|
||||
lhsType = getEnumEntryType(f)
|
||||
if (lhsType == null) {
|
||||
return
|
||||
@@ -762,7 +767,7 @@ open class KotlinFileExtractor(
|
||||
tw.writeStmts_exprstmt(stmtId, blockAndFunctionId.first, idx++, blockAndFunctionId.second)
|
||||
tw.writeHasLocation(stmtId, declLocId)
|
||||
val assignmentId = tw.getFreshIdLabel<DbAssignexpr>()
|
||||
val type = useType(expr.type)
|
||||
val type = useType(if (isAnnotationClassField) kClassToJavaClass(expr.type) else expr.type)
|
||||
tw.writeExprs_assignexpr(assignmentId, type.javaResult.id, stmtId, 0)
|
||||
tw.writeExprsKotlinType(assignmentId, type.kotlinResult.id)
|
||||
tw.writeHasLocation(assignmentId, declLocId)
|
||||
@@ -936,7 +941,8 @@ open class KotlinFileExtractor(
|
||||
with("field", f) {
|
||||
DeclarationStackAdjuster(f).use {
|
||||
val fNameSuffix = getExtensionReceiverType(f)?.let { it.classFqName?.asString()?.replace(".", "$$") } ?: ""
|
||||
return extractField(useField(f), "${f.name.asString()}$fNameSuffix", f.type, parentId, tw.getLocation(f), f.visibility, f, isExternalDeclaration(f), f.isFinal)
|
||||
val extractType = if (isAnnotationClassField(f)) kClassToJavaClass(f.type) else f.type
|
||||
return extractField(useField(f), "${f.name.asString()}$fNameSuffix", extractType, parentId, tw.getLocation(f), f.visibility, f, isExternalDeclaration(f), f.isFinal)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2917,14 +2923,17 @@ open class KotlinFileExtractor(
|
||||
if (owner is IrValueParameter && owner.index == -1 && !owner.isExtensionReceiver()) {
|
||||
extractThisAccess(e, exprParent, callable)
|
||||
} else {
|
||||
extractVariableAccess(useValueDeclaration(owner), e.type, tw.getLocation(e), exprParent.parent, exprParent.idx, callable, exprParent.enclosingStmt)
|
||||
val isAnnotationClassParameter = ((owner as? IrValueParameter)?.parent as? IrConstructor)?.parentClassOrNull?.kind == ClassKind.ANNOTATION_CLASS
|
||||
val extractType = if (isAnnotationClassParameter) kClassToJavaClass(e.type) else e.type
|
||||
extractVariableAccess(useValueDeclaration(owner), extractType, tw.getLocation(e), exprParent.parent, exprParent.idx, callable, exprParent.enclosingStmt)
|
||||
}
|
||||
}
|
||||
is IrGetField -> {
|
||||
val exprParent = parent.expr(e, callable)
|
||||
val owner = tryReplaceAndroidSyntheticField(e.symbol.owner)
|
||||
val locId = tw.getLocation(e)
|
||||
extractVariableAccess(useField(owner), e.type, locId, exprParent.parent, exprParent.idx, callable, exprParent.enclosingStmt).also { id ->
|
||||
val fieldType = if (isAnnotationClassField(owner)) kClassToJavaClass(e.type) else e.type
|
||||
extractVariableAccess(useField(owner), fieldType, locId, exprParent.parent, exprParent.idx, callable, exprParent.enclosingStmt).also { id ->
|
||||
val receiver = e.receiver
|
||||
if (receiver != null) {
|
||||
extractExpressionExpr(receiver, callable, id, -1, exprParent.enclosingStmt)
|
||||
|
||||
@@ -1103,7 +1103,50 @@ open class KotlinUsesExtractor(
|
||||
return "@\"$prefix;{$parentId}.$name($paramTypeIds){$returnTypeId}${typeArgSuffix}\""
|
||||
}
|
||||
|
||||
val javaLangClass by lazy {
|
||||
val result = pluginContext.referenceClass(FqName("java.lang.Class"))?.owner
|
||||
result?.let { extractExternalClassLater(it) }
|
||||
result
|
||||
}
|
||||
|
||||
fun kClassToJavaClass(t: IrType): IrType {
|
||||
when(t) {
|
||||
is IrSimpleType -> {
|
||||
if (t.classifier == pluginContext.irBuiltIns.kClassClass) {
|
||||
javaLangClass?.let { jlc ->
|
||||
return jlc.symbol.typeWithArguments(t.arguments)
|
||||
}
|
||||
} else if (t.isArray() || t.isNullableArray()) {
|
||||
val elementType = t.getArrayElementType(pluginContext.irBuiltIns)
|
||||
val replacedElementType = kClassToJavaClass(elementType)
|
||||
if (replacedElementType !== elementType) {
|
||||
val newArg = makeTypeProjection(replacedElementType, (t.arguments[0] as IrTypeProjection).variance)
|
||||
return t.classOrNull!!.typeWithArguments(listOf(newArg)).codeQlWithHasQuestionMark(t.isNullableArray())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
fun isAnnotationClassField(f: IrField) =
|
||||
f.correspondingPropertySymbol?.let {
|
||||
isAnnotationClassProperty(it)
|
||||
} ?: false
|
||||
|
||||
fun isAnnotationClassProperty(p: IrPropertySymbol) =
|
||||
p.owner.parentClassOrNull?.kind == ClassKind.ANNOTATION_CLASS
|
||||
|
||||
fun getAdjustedReturnType(f: IrFunction) : IrType {
|
||||
// Replace annotation val accessor types as needed:
|
||||
(f as? IrSimpleFunction)?.correspondingPropertySymbol?.let {
|
||||
if (isAnnotationClassProperty(it) && f == it.owner.getter) {
|
||||
val replaced = kClassToJavaClass(f.returnType)
|
||||
if (replaced != f.returnType)
|
||||
return replaced
|
||||
}
|
||||
}
|
||||
|
||||
// The return type of `java.util.concurrent.ConcurrentHashMap<K,V>.keySet/0` is defined as `Set<K>` in the stubs inside the Android SDK.
|
||||
// This does not match the Java SDK return type: `ConcurrentHashMap.KeySetView<K,V>`, so it's adjusted here.
|
||||
// This is a deliberate change in the Android SDK: https://github.com/AndroidSDKSources/android-sdk-sources-for-api-level-31/blob/2c56b25f619575bea12f9c5520ed2259620084ac/java/util/concurrent/ConcurrentHashMap.java#L1244-L1249
|
||||
|
||||
Reference in New Issue
Block a user