Skip to content

Commit ca27f95

Browse files
committed
Replace uses of !! operator
1 parent 73df316 commit ca27f95

File tree

1 file changed

+52
-33
lines changed

1 file changed

+52
-33
lines changed

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

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -512,18 +512,24 @@ open class KotlinFileExtractor(
512512
annotationClass: IrClass,
513513
containerClass: IrClass,
514514
entries: List<IrConstructorCall>,
515-
): IrConstructorCall {
515+
): IrConstructorCall? {
516516
val annotationType = annotationClass.typeWith()
517-
return IrConstructorCallImpl.fromSymbolOwner(containerClass.defaultType, containerClass.primaryConstructor!!.symbol).apply {
518-
putValueArgument(
519-
0,
520-
IrVarargImpl(
521-
UNDEFINED_OFFSET, UNDEFINED_OFFSET,
522-
pluginContext.irBuiltIns.arrayClass.typeWith(annotationType),
523-
annotationType,
524-
entries
517+
val containerConstructor = containerClass.primaryConstructor
518+
if (containerConstructor == null) {
519+
logger.warnElement("Expected container class to have a primary constructor", containerClass)
520+
return null
521+
} else {
522+
return IrConstructorCallImpl.fromSymbolOwner(containerClass.defaultType, containerConstructor.symbol).apply {
523+
putValueArgument(
524+
0,
525+
IrVarargImpl(
526+
UNDEFINED_OFFSET, UNDEFINED_OFFSET,
527+
pluginContext.irBuiltIns.arrayClass.typeWith(annotationType),
528+
annotationType,
529+
entries
530+
)
525531
)
526-
)
532+
}
527533
}
528534
}
529535

@@ -560,11 +566,15 @@ open class KotlinFileExtractor(
560566
.filterIsInstance<IrProperty>()
561567
.first { it.name == param.name }
562568
val v = constructorCall.getValueArgument(i) ?: param.defaultValue?.expression
563-
val getter = prop.getter!!
564-
val getterId = useFunction<DbMethod>(getter)
565-
val exprId = extractAnnotationValueExpression(v, id, i, "{${getterId}}", getter.returnType)
566-
if (exprId != null) {
567-
tw.writeAnnotValue(id, getterId, exprId)
569+
val getter = prop.getter
570+
if (getter == null) {
571+
logger.warnElement("Expected annotation property to define a getter", prop)
572+
} else {
573+
val getterId = useFunction<DbMethod>(getter)
574+
val exprId = extractAnnotationValueExpression(v, id, i, "{${getterId}}", getter.returnType)
575+
if (exprId != null) {
576+
tw.writeAnnotValue(id, getterId, exprId)
577+
}
568578
}
569579
}
570580
return id
@@ -599,22 +609,26 @@ open class KotlinFileExtractor(
599609
// Use the context type (i.e., the type the annotation expects, not the actual type of the array)
600610
// because the Java extractor fills in array types using the same technique. These should only
601611
// differ for generic annotations.
602-
val type = useType(kClassToJavaClass(contextType!!))
603-
tw.writeExprs_arrayinit(arrayId, type.javaResult.id, parent, idx)
604-
tw.writeExprsKotlinType(arrayId, type.kotlinResult.id)
605-
tw.writeHasLocation(arrayId, tw.getLocation(v))
606-
607-
v.elements.forEachIndexed { index, irVarargElement -> run {
608-
val argExpr = when (irVarargElement) {
609-
is IrExpression -> irVarargElement
610-
is IrSpreadElement -> irVarargElement.expression
611-
else -> {
612-
logger.errorElement("Unrecognised IrVarargElement: " + irVarargElement.javaClass, irVarargElement)
613-
null
612+
if (contextType == null) {
613+
logger.warnElement("Expected an annotation array to have an enclosing context", v)
614+
} else {
615+
val type = useType(kClassToJavaClass(contextType))
616+
tw.writeExprs_arrayinit(arrayId, type.javaResult.id, parent, idx)
617+
tw.writeExprsKotlinType(arrayId, type.kotlinResult.id)
618+
tw.writeHasLocation(arrayId, tw.getLocation(v))
619+
620+
v.elements.forEachIndexed { index, irVarargElement -> run {
621+
val argExpr = when (irVarargElement) {
622+
is IrExpression -> irVarargElement
623+
is IrSpreadElement -> irVarargElement.expression
624+
else -> {
625+
logger.errorElement("Unrecognised IrVarargElement: " + irVarargElement.javaClass, irVarargElement)
626+
null
627+
}
614628
}
615-
}
616-
extractAnnotationValueExpression(argExpr, arrayId, index, "child;$index", null)
617-
} }
629+
extractAnnotationValueExpression(argExpr, arrayId, index, "child;$index", null)
630+
} }
631+
}
618632
}
619633
}
620634
// is IrErrorExpression
@@ -900,9 +914,14 @@ open class KotlinFileExtractor(
900914
c.declarations
901915
.filterIsInstance<IrProperty>()
902916
.map {
903-
val getter = it.getter!!
904-
val label = extractFunction(getter, id, extractBody = false, extractMethodAndParameterTypeAccesses = true, extractAnnotations = true, null, listOf())
905-
tw.writeIsAnnotElem(label!!.cast<DbMethod>())
917+
val getter = it.getter
918+
if (getter == null) {
919+
logger.warnElement("Expected an annotation property to have a getter", it)
920+
} else {
921+
extractFunction(getter, id, extractBody = false, extractMethodAndParameterTypeAccesses = true, extractAnnotations = true, null, listOf())?.also { functionLabel ->
922+
tw.writeIsAnnotElem(functionLabel.cast())
923+
}
924+
}
906925
}
907926
} else {
908927
c.declarations.forEach { extractDeclaration(it, extractPrivateMembers = extractPrivateMembers, extractFunctionBodies = extractFunctionBodies, extractAnnotations = true) }

0 commit comments

Comments
 (0)