diff options
Diffstat (limited to 'tools/aapt2/java/ClassDefinition.h')
-rw-r--r-- | tools/aapt2/java/ClassDefinition.h | 89 |
1 files changed, 65 insertions, 24 deletions
diff --git a/tools/aapt2/java/ClassDefinition.h b/tools/aapt2/java/ClassDefinition.h index ca76421390d6..28a3489e71a4 100644 --- a/tools/aapt2/java/ClassDefinition.h +++ b/tools/aapt2/java/ClassDefinition.h @@ -19,6 +19,8 @@ #include <ostream> #include <string> +#include <unordered_map> +#include <vector> #include "android-base/macros.h" #include "androidfw/StringPiece.h" @@ -37,10 +39,14 @@ class ClassMember { public: virtual ~ClassMember() = default; - AnnotationProcessor* GetCommentBuilder() { return &processor_; } + AnnotationProcessor* GetCommentBuilder() { + return &processor_; + } virtual bool empty() const = 0; + virtual const std::string& GetName() const = 0; + // Writes the class member to the out stream. Subclasses should derive this method // to write their own data. Call this base method from the subclass to write out // this member's comments/annotations. @@ -57,33 +63,42 @@ class PrimitiveMember : public ClassMember { PrimitiveMember(const android::StringPiece& name, const T& val) : name_(name.to_string()), val_(val) {} - bool empty() const override { return false; } + bool empty() const override { + return false; + } + + const std::string& GetName() const override { + return name_; + } void WriteToStream(const android::StringPiece& prefix, bool final, std::ostream* out) const override { ClassMember::WriteToStream(prefix, final, out); - - *out << prefix << "public static " << (final ? "final " : "") << "int " - << name_ << "=" << val_ << ";"; + *out << prefix << "public static " << (final ? "final " : "") << "int " << name_ << "=" << val_ + << ";"; } private: + DISALLOW_COPY_AND_ASSIGN(PrimitiveMember); + std::string name_; T val_; - - DISALLOW_COPY_AND_ASSIGN(PrimitiveMember); }; -/** - * Specialization for strings so they get the right type and are quoted with "". - */ +// Specialization for strings so they get the right type and are quoted with "". template <> class PrimitiveMember<std::string> : public ClassMember { public: PrimitiveMember(const android::StringPiece& name, const std::string& val) : name_(name.to_string()), val_(val) {} - bool empty() const override { return false; } + bool empty() const override { + return false; + } + + const std::string& GetName() const override { + return name_; + } void WriteToStream(const android::StringPiece& prefix, bool final, std::ostream* out) const override { @@ -94,10 +109,10 @@ class PrimitiveMember<std::string> : public ClassMember { } private: + DISALLOW_COPY_AND_ASSIGN(PrimitiveMember); + std::string name_; std::string val_; - - DISALLOW_COPY_AND_ASSIGN(PrimitiveMember); }; using IntMember = PrimitiveMember<uint32_t>; @@ -109,9 +124,17 @@ class PrimitiveArrayMember : public ClassMember { public: explicit PrimitiveArrayMember(const android::StringPiece& name) : name_(name.to_string()) {} - void AddElement(const T& val) { elements_.push_back(val); } + void AddElement(const T& val) { + elements_.push_back(val); + } - bool empty() const override { return false; } + bool empty() const override { + return false; + } + + const std::string& GetName() const override { + return name_; + } void WriteToStream(const android::StringPiece& prefix, bool final, std::ostream* out) const override { @@ -135,10 +158,10 @@ class PrimitiveArrayMember : public ClassMember { } private: + DISALLOW_COPY_AND_ASSIGN(PrimitiveArrayMember); + std::string name_; std::vector<T> elements_; - - DISALLOW_COPY_AND_ASSIGN(PrimitiveArrayMember); }; using ResourceArrayMember = PrimitiveArrayMember<ResourceId>; @@ -154,13 +177,22 @@ class MethodDefinition : public ClassMember { // formatting may be broken. void AppendStatement(const android::StringPiece& statement); + // Not quite the same as a name, but good enough. + const std::string& GetName() const override { + return signature_; + } + // Even if the method is empty, we always want to write the method signature. - bool empty() const override { return false; } + bool empty() const override { + return false; + } void WriteToStream(const android::StringPiece& prefix, bool final, std::ostream* out) const override; private: + DISALLOW_COPY_AND_ASSIGN(MethodDefinition); + std::string signature_; std::vector<std::string> statements_; }; @@ -175,21 +207,30 @@ class ClassDefinition : public ClassMember { ClassDefinition(const android::StringPiece& name, ClassQualifier qualifier, bool createIfEmpty) : name_(name.to_string()), qualifier_(qualifier), create_if_empty_(createIfEmpty) {} - void AddMember(std::unique_ptr<ClassMember> member) { - members_.push_back(std::move(member)); - } + enum class Result { + kAdded, + kOverridden, + }; + + Result AddMember(std::unique_ptr<ClassMember> member); bool empty() const override; + + const std::string& GetName() const override { + return name_; + } + void WriteToStream(const android::StringPiece& prefix, bool final, std::ostream* out) const override; private: + DISALLOW_COPY_AND_ASSIGN(ClassDefinition); + std::string name_; ClassQualifier qualifier_; bool create_if_empty_; - std::vector<std::unique_ptr<ClassMember>> members_; - - DISALLOW_COPY_AND_ASSIGN(ClassDefinition); + std::vector<std::unique_ptr<ClassMember>> ordered_members_; + std::unordered_map<android::StringPiece, size_t> indexed_members_; }; } // namespace aapt |