aboutsummaryrefslogtreecommitdiffstats
path: root/src/google/protobuf/compiler/javanano/javanano_field.h
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_field.h
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_field.h')
-rw-r--r--src/google/protobuf/compiler/javanano/javanano_field.h20
1 files changed, 16 insertions, 4 deletions
diff --git a/src/google/protobuf/compiler/javanano/javanano_field.h b/src/google/protobuf/compiler/javanano/javanano_field.h
index 14b6489..61fc621 100644
--- a/src/google/protobuf/compiler/javanano/javanano_field.h
+++ b/src/google/protobuf/compiler/javanano/javanano_field.h
@@ -53,11 +53,23 @@ namespace javanano {
class FieldGenerator {
public:
- //FieldGenerator() {}
FieldGenerator(const Params& params) : params_(params) {}
virtual ~FieldGenerator();
- virtual void GenerateMembers(io::Printer* printer) const = 0;
+ virtual bool SavedDefaultNeeded() const;
+ virtual void GenerateInitSavedDefaultCode(io::Printer* printer) const;
+
+ // Generates code for Java fields and methods supporting this field.
+ // If this field needs a saved default (SavedDefaultNeeded() is true),
+ // then @lazy_init controls how the static field for that default value
+ // and its initialization code should be generated. If @lazy_init is
+ // true, the static field is not declared final and the initialization
+ // code is generated only when GenerateInitSavedDefaultCode is called;
+ // otherwise, the static field is declared final and initialized inline.
+ // GenerateInitSavedDefaultCode will not be called in the latter case.
+ virtual void GenerateMembers(
+ io::Printer* printer, bool lazy_init) const = 0;
+
virtual void GenerateClearCode(io::Printer* printer) const = 0;
virtual void GenerateMergingCode(io::Printer* printer) const = 0;
@@ -84,14 +96,14 @@ class FieldGeneratorMap {
~FieldGeneratorMap();
const FieldGenerator& get(const FieldDescriptor* field) const;
- const FieldGenerator& get_extension(int index) const;
int total_bits() const { return total_bits_; }
+ bool saved_defaults_needed() const { return saved_defaults_needed_; }
private:
const Descriptor* descriptor_;
scoped_array<scoped_ptr<FieldGenerator> > field_generators_;
- scoped_array<scoped_ptr<FieldGenerator> > extension_generators_;
int total_bits_;
+ bool saved_defaults_needed_;
static FieldGenerator* MakeGenerator(const FieldDescriptor* field,
const Params &params, int* next_has_bit_index);