Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f92570de authored by Keith Mok's avatar Keith Mok
Browse files

codegen: add primitive arrays type

Primitive Arrays Parcel can handle null objects directly.
There is no need to use a flag in parcel for special handling
This can generate more efficient code for variables declared as
nullable.

Bug: 233795798
Test: codegen
$ANDROID_BUILD_TOP/packages/services/Car/car-lib/src/android/car/vms/VmsProviderInfo.java

Change-Id: I09a1fcd70d6692b51c5796073001b2c67ad04979
parent 5cadf305
Loading
Loading
Loading
Loading
+8 −6
Original line number Diff line number Diff line
@@ -393,7 +393,7 @@ private fun ClassPrinter.generateBuilderBuild() {
fun ClassPrinter.generateParcelable() {
    val booleanFields = fields.filter { it.Type == "boolean" }
    val objectFields = fields.filter { it.Type !in PRIMITIVE_TYPES }
    val nullableFields = objectFields.filter { it.mayBeNull }
    val nullableFields = objectFields.filter { it.mayBeNull && it.Type !in PRIMITIVE_ARRAY_TYPES }
    val nonBooleanFields = fields - booleanFields


@@ -457,7 +457,7 @@ fun ClassPrinter.generateParcelable() {
                    hasAnnotation("@$DataClassEnum") ->
                        +"dest.writeInt($internalGetter == null ? -1 : $internalGetter.ordinal());"
                    else -> {
                        if (mayBeNull) !"if ($internalGetter != null) "
                        if (mayBeNull && Type !in PRIMITIVE_ARRAY_TYPES) !"if ($internalGetter != null) "
                        var args = internalGetter
                        if (ParcelMethodsSuffix.startsWith("Parcelable")
                                || ParcelMethodsSuffix.startsWith("TypedObject")
@@ -529,7 +529,7 @@ fun ClassPrinter.generateParcelable() {
                    if (passContainer) {
                        methodArgs.add(_name)
                        !"$Type $_name = "
                        if (mayBeNull) {
                        if (mayBeNull && Type !in PRIMITIVE_ARRAY_TYPES) {
                            +"null;"
                            !"if ((flg & $fieldBit) != 0) {"
                            pushIndent()
@@ -539,7 +539,9 @@ fun ClassPrinter.generateParcelable() {
                        +"$containerInitExpr;"
                    } else {
                        !"$Type $_name = "
                        if (mayBeNull) !"(flg & $fieldBit) == 0 ? null : "
                        if (mayBeNull && Type !in PRIMITIVE_ARRAY_TYPES) {
                            !"(flg & $fieldBit) == 0 ? null : "
                        }
                        if (ParcelMethodsSuffix == "StrongInterface") {
                            !"$FieldClass.Stub.asInterface("
                        } else if (Type !in PRIMITIVE_TYPES + "String" + "Bundle" &&
@@ -578,7 +580,7 @@ fun ClassPrinter.generateParcelable() {
                    +";"

                    // Cleanup if passContainer
                    if (passContainer && mayBeNull) {
                    if (passContainer && mayBeNull && Type !in PRIMITIVE_ARRAY_TYPES) {
                        popIndent()
                        rmEmptyLine()
                        +"\n}"
+2 −1
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ const val GENERATED_END = "// End of generated code"
const val INDENT_SINGLE = "    "

val PRIMITIVE_TYPES = listOf("byte", "short", "int", "long", "char", "float", "double", "boolean")
val PRIMITIVE_ARRAY_TYPES = listOf("byte[]", "short[]", "int[]", "long[]", "char[]", "float[]", "double[]", "boolean[]")
val BOXED_PRIMITIVE_TYPES = PRIMITIVE_TYPES.map { it.capitalize() } - "Int" + "Integer" - "Char" + "Character"

val BUILTIN_SPECIAL_PARCELLINGS = listOf("Pattern")