diff options
| author | 2022-05-25 21:19:03 +0000 | |
|---|---|---|
| committer | 2022-05-25 21:36:32 +0000 | |
| commit | f92570de6f735fd547eeffee2f8a7e262248a2c3 (patch) | |
| tree | 85656aed44cbb11288df061d931615030a0f0682 | |
| parent | 5cadf305922e081f80cb4e28f6529dafd9ee6520 (diff) | |
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
| -rw-r--r-- | tools/codegen/src/com/android/codegen/Generators.kt | 14 | ||||
| -rwxr-xr-x | tools/codegen/src/com/android/codegen/Main.kt | 3 |
2 files changed, 10 insertions, 7 deletions
diff --git a/tools/codegen/src/com/android/codegen/Generators.kt b/tools/codegen/src/com/android/codegen/Generators.kt index 5fc800b09ee9..685733386cae 100644 --- a/tools/codegen/src/com/android/codegen/Generators.kt +++ b/tools/codegen/src/com/android/codegen/Generators.kt @@ -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}" @@ -949,4 +951,4 @@ fun ClassPrinter.generateMetadata(file: File) { +"" +"@Deprecated" +"private void __metadata() {}\n" -}
\ No newline at end of file +} diff --git a/tools/codegen/src/com/android/codegen/Main.kt b/tools/codegen/src/com/android/codegen/Main.kt index 4b508d022991..bcc6230fd53f 100755 --- a/tools/codegen/src/com/android/codegen/Main.kt +++ b/tools/codegen/src/com/android/codegen/Main.kt @@ -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") @@ -133,4 +134,4 @@ private fun handleUpdateFlag(cliArgs: Array<String>, sourceLines: List<String>): System.exit(0) } return cliArgs - "--update-only" -}
\ No newline at end of file +} |