File: ClassDefinition.h

package info (click to toggle)
android-platform-frameworks-base 1%3A7.0.0%2Br33-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 137,116 kB
  • sloc: java: 704,370; cpp: 206,670; xml: 185,778; python: 2,626; ansic: 486; sh: 291; makefile: 48; sed: 19
file content (188 lines) | stat: -rw-r--r-- 5,210 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef AAPT_JAVA_CLASSDEFINITION_H
#define AAPT_JAVA_CLASSDEFINITION_H

#include "Resource.h"
#include "java/AnnotationProcessor.h"
#include "util/StringPiece.h"
#include "util/Util.h"

#include <android-base/macros.h>
#include <sstream>
#include <string>

namespace aapt {

// The number of attributes to emit per line in a Styleable array.
constexpr static size_t kAttribsPerLine = 4;
constexpr static const char* kIndent = "  ";

class ClassMember {
public:
    virtual ~ClassMember() = default;

    AnnotationProcessor* getCommentBuilder() {
        return &mProcessor;
    }

    virtual bool empty() const = 0;

    virtual void writeToStream(const StringPiece& prefix, bool final, std::ostream* out) const {
        mProcessor.writeToStream(out, prefix);
    }

private:
    AnnotationProcessor mProcessor;
};

template <typename T>
class PrimitiveMember : public ClassMember {
public:
    PrimitiveMember(const StringPiece& name, const T& val) :
            mName(name.toString()), mVal(val) {
    }

    bool empty() const override {
        return false;
    }

    void writeToStream(const StringPiece& prefix, bool final, std::ostream* out) const override {
        ClassMember::writeToStream(prefix, final, out);

        *out << prefix << "public static " << (final ? "final " : "")
             << "int " << mName << "=" << mVal << ";";
    }

private:
    std::string mName;
    T mVal;

    DISALLOW_COPY_AND_ASSIGN(PrimitiveMember);
};

/**
 * Specialization for strings so they get the right type and are quoted with "".
 */
template <>
class PrimitiveMember<std::string> : public ClassMember {
public:
    PrimitiveMember(const StringPiece& name, const std::string& val) :
            mName(name.toString()), mVal(val) {
    }

    bool empty() const override {
        return false;
    }

    void writeToStream(const StringPiece& prefix, bool final, std::ostream* out) const override {
        ClassMember::writeToStream(prefix, final, out);

        *out << prefix << "public static " << (final ? "final " : "")
             << "String " << mName << "=\"" << mVal << "\";";
    }

private:
    std::string mName;
    std::string mVal;

    DISALLOW_COPY_AND_ASSIGN(PrimitiveMember);
};

using IntMember = PrimitiveMember<uint32_t>;
using ResourceMember = PrimitiveMember<ResourceId>;
using StringMember = PrimitiveMember<std::string>;

template <typename T>
class PrimitiveArrayMember : public ClassMember {
public:
    PrimitiveArrayMember(const StringPiece& name) :
            mName(name.toString()) {
    }

    void addElement(const T& val) {
        mElements.push_back(val);
    }

    bool empty() const override {
        return false;
    }

    void writeToStream(const StringPiece& prefix, bool final, std::ostream* out) const override {
        ClassMember::writeToStream(prefix, final, out);

        *out << prefix << "public static final int[] " << mName << "={";

        const auto begin = mElements.begin();
        const auto end = mElements.end();
        for (auto current = begin; current != end; ++current) {
            if (std::distance(begin, current) % kAttribsPerLine == 0) {
                *out << "\n" << prefix << kIndent << kIndent;
            }

            *out << *current;
            if (std::distance(current, end) > 1) {
                *out << ", ";
            }
        }
        *out << "\n" << prefix << kIndent <<"};";
    }

private:
    std::string mName;
    std::vector<T> mElements;

    DISALLOW_COPY_AND_ASSIGN(PrimitiveArrayMember);
};

using ResourceArrayMember = PrimitiveArrayMember<ResourceId>;

enum class ClassQualifier {
    None,
    Static
};

class ClassDefinition : public ClassMember {
public:
    static bool writeJavaFile(const ClassDefinition* def,
                              const StringPiece& package,
                              bool final,
                              std::ostream* out);

    ClassDefinition(const StringPiece& name, ClassQualifier qualifier, bool createIfEmpty) :
            mName(name.toString()), mQualifier(qualifier), mCreateIfEmpty(createIfEmpty) {
    }

    void addMember(std::unique_ptr<ClassMember> member) {
        mMembers.push_back(std::move(member));
    }

    bool empty() const override;
    void writeToStream(const StringPiece& prefix, bool final, std::ostream* out) const override;

private:
    std::string mName;
    ClassQualifier mQualifier;
    bool mCreateIfEmpty;
    std::vector<std::unique_ptr<ClassMember>> mMembers;

    DISALLOW_COPY_AND_ASSIGN(ClassDefinition);
};

} // namespace aapt

#endif /* AAPT_JAVA_CLASSDEFINITION_H */