aboutsummaryrefslogtreecommitdiffstats
path: root/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc
diff options
context:
space:
mode:
authorMax Cai <maxtroy@google.com>2013-10-09 13:36:23 +0100
committerMax Cai <maxtroy@google.com>2013-12-10 16:46:22 +0000
commit5cc242074f189837b38e7768b57ccfb0bca258df (patch)
tree186c81cc5282eb6ae8056ad478692e3a24188e33 /src/google/protobuf/compiler/javanano/javanano_primitive_field.cc
parentcea499acf68b35921b956785c26c0e6f18c241c1 (diff)
downloadexternal_protobuf-5cc242074f189837b38e7768b57ccfb0bca258df.zip
external_protobuf-5cc242074f189837b38e7768b57ccfb0bca258df.tar.gz
external_protobuf-5cc242074f189837b38e7768b57ccfb0bca258df.tar.bz2
Avoid class initializers to help ProGuard.
Class initializers prevent ProGuard from inlining any methods because it thinks the class initializer may have side effects. This is true for static methods, but instance methods can still be inlined, because to have an instance you will have touched the class and any class initializers would have run. But ProGuard only starts inlining instance methods of classes with class initializers from v4.11b6, and Android uses v4.4 now. This change tries to avoid the class initializers as much as possible, by delaying the initialization of the empty array and some fields' saved defaults until when they're needed. However, if the message hosts any extensions, they must be public static final and therefore introducing the class initializer. In that case we won't bother with lazy initialization. Change-Id: I00d8296f6eb0023112b93ee135cdb28dbd52b0b8
Diffstat (limited to 'src/google/protobuf/compiler/javanano/javanano_primitive_field.cc')
-rw-r--r--src/google/protobuf/compiler/javanano/javanano_primitive_field.cc94
1 files changed, 68 insertions, 26 deletions
diff --git a/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc b/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc
index c0717e6..3428f69 100644
--- a/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc
+++ b/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc
@@ -251,35 +251,41 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, const Params param
} else {
(*variables)["type"] = PrimitiveTypeName(GetJavaType(descriptor));
}
- (*variables)["default"] = DefaultValue(params, descriptor);
- (*variables)["default_constant"] = FieldDefaultConstantName(descriptor);
- // For C++-string types (string and bytes), we might need to have
- // the generated code do the unicode decoding (see comments in
- // InternalNano.java for gory details.). We would like to do this
- // once into a "private static final" field and re-use that from
+ // Deals with defaults. For C++-string types (string and bytes),
+ // we might need to have the generated code do the unicode decoding
+ // (see comments in InternalNano.java for gory details.). We would
+ // like to do this once into a static field and re-use that from
// then on.
if (descriptor->cpp_type() == FieldDescriptor::CPPTYPE_STRING &&
!descriptor->default_value_string().empty() &&
!params.use_reference_types_for_primitives()) {
- string default_value;
if (descriptor->type() == FieldDescriptor::TYPE_BYTES) {
- default_value = strings::Substitute(
+ (*variables)["default"] = DefaultValue(params, descriptor);
+ (*variables)["default_constant"] = FieldDefaultConstantName(descriptor);
+ (*variables)["default_constant_value"] = strings::Substitute(
"com.google.protobuf.nano.InternalNano.bytesDefaultValue(\"$0\")",
CEscape(descriptor->default_value_string()));
- (*variables)["default_copy_if_needed"] = (*variables)["default"] + ".clone()";
+ (*variables)["default_copy_if_needed"] =
+ (*variables)["default"] + ".clone()";
+ } else if (AllAscii(descriptor->default_value_string())) {
+ // All chars are ASCII. In this case directly referencing a
+ // CEscape()'d string literal works fine.
+ (*variables)["default"] =
+ "\"" + CEscape(descriptor->default_value_string()) + "\"";
+ (*variables)["default_copy_if_needed"] = (*variables)["default"];
} else {
- if (AllAscii(descriptor->default_value_string())) {
- // All chars are ASCII. In this case CEscape() works fine.
- default_value = "\"" + CEscape(descriptor->default_value_string()) + "\"";
- } else {
- default_value = strings::Substitute(
- "com.google.protobuf.nano.InternalNano.stringDefaultValue(\"$0\")",
- CEscape(descriptor->default_value_string()));
- }
+ // Strings where some chars are non-ASCII. We need to save the
+ // default value.
+ (*variables)["default"] = DefaultValue(params, descriptor);
+ (*variables)["default_constant"] = FieldDefaultConstantName(descriptor);
+ (*variables)["default_constant_value"] = strings::Substitute(
+ "com.google.protobuf.nano.InternalNano.stringDefaultValue(\"$0\")",
+ CEscape(descriptor->default_value_string()));
(*variables)["default_copy_if_needed"] = (*variables)["default"];
}
- (*variables)["default_constant_value"] = default_value;
} else {
+ // Non-string, non-bytes field. Defaults are literals.
+ (*variables)["default"] = DefaultValue(params, descriptor);
(*variables)["default_copy_if_needed"] = (*variables)["default"];
}
(*variables)["boxed_type"] = BoxedPrimitiveTypeName(GetJavaType(descriptor));
@@ -306,12 +312,29 @@ PrimitiveFieldGenerator(const FieldDescriptor* descriptor, const Params& params)
PrimitiveFieldGenerator::~PrimitiveFieldGenerator() {}
+bool PrimitiveFieldGenerator::SavedDefaultNeeded() const {
+ return variables_.find("default_constant") != variables_.end();
+}
+
+void PrimitiveFieldGenerator::GenerateInitSavedDefaultCode(io::Printer* printer) const {
+ if (variables_.find("default_constant") != variables_.end()) {
+ printer->Print(variables_,
+ "$default_constant$ = $default_constant_value$;\n");
+ }
+}
+
void PrimitiveFieldGenerator::
-GenerateMembers(io::Printer* printer) const {
- if (variables_.find("default_constant_value") != variables_.end()) {
+GenerateMembers(io::Printer* printer, bool lazy_init) const {
+ if (variables_.find("default_constant") != variables_.end()) {
// Those primitive types that need a saved default.
- printer->Print(variables_,
- "private static final $type$ $default_constant$ = $default_constant_value$;\n");
+ if (lazy_init) {
+ printer->Print(variables_,
+ "private static $type$ $default_constant$;\n");
+ } else {
+ printer->Print(variables_,
+ "private static final $type$ $default_constant$ =\n"
+ " $default_constant_value$;\n");
+ }
}
printer->Print(variables_,
@@ -514,11 +537,30 @@ AccessorPrimitiveFieldGenerator(const FieldDescriptor* descriptor,
AccessorPrimitiveFieldGenerator::~AccessorPrimitiveFieldGenerator() {}
+bool AccessorPrimitiveFieldGenerator::SavedDefaultNeeded() const {
+ return variables_.find("default_constant") != variables_.end();
+}
+
void AccessorPrimitiveFieldGenerator::
-GenerateMembers(io::Printer* printer) const {
- if (variables_.find("default_constant_value") != variables_.end()) {
+GenerateInitSavedDefaultCode(io::Printer* printer) const {
+ if (variables_.find("default_constant") != variables_.end()) {
printer->Print(variables_,
- "private static final $type$ $default_constant$ = $default_constant_value$;\n");
+ "$default_constant$ = $default_constant_value$;\n");
+ }
+}
+
+void AccessorPrimitiveFieldGenerator::
+GenerateMembers(io::Printer* printer, bool lazy_init) const {
+ if (variables_.find("default_constant") != variables_.end()) {
+ // Those primitive types that need a saved default.
+ if (lazy_init) {
+ printer->Print(variables_,
+ "private static $type$ $default_constant$;\n");
+ } else {
+ printer->Print(variables_,
+ "private static final $type$ $default_constant$ =\n"
+ " $default_constant_value$;\n");
+ }
}
printer->Print(variables_,
"private $type$ $name$_;\n"
@@ -671,7 +713,7 @@ RepeatedPrimitiveFieldGenerator(const FieldDescriptor* descriptor, const Params&
RepeatedPrimitiveFieldGenerator::~RepeatedPrimitiveFieldGenerator() {}
void RepeatedPrimitiveFieldGenerator::
-GenerateMembers(io::Printer* printer) const {
+GenerateMembers(io::Printer* printer, bool /*unused init_defaults*/) const {
printer->Print(variables_,
"public $type$[] $name$;\n");
}