blob: f1f1f925480c0e63dbd74ff47333b7f358fdece9 [file] [log] [blame]
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "java/ClassDefinition.h"
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070018
Adam Lesinskice5e56e2016-10-21 17:56:45 -070019#include "util/StringPiece.h"
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070020
21namespace aapt {
22
23bool ClassDefinition::empty() const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070024 for (const std::unique_ptr<ClassMember>& member : members_) {
25 if (!member->empty()) {
26 return false;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070027 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070028 }
29 return true;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070030}
31
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032void ClassDefinition::WriteToStream(const StringPiece& prefix, bool final,
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070033 std::ostream* out) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034 if (members_.empty() && !create_if_empty_) {
35 return;
36 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070037
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038 ClassMember::WriteToStream(prefix, final, out);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070039
Adam Lesinskice5e56e2016-10-21 17:56:45 -070040 *out << prefix << "public ";
41 if (qualifier_ == ClassQualifier::Static) {
42 *out << "static ";
43 }
44 *out << "final class " << name_ << " {\n";
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070045
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 std::string new_prefix = prefix.ToString();
47 new_prefix.append(kIndent);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070048
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 for (const std::unique_ptr<ClassMember>& member : members_) {
50 member->WriteToStream(new_prefix, final, out);
51 *out << "\n";
52 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070053
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 *out << prefix << "}";
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070055}
56
57constexpr static const char* sWarningHeader =
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n"
59 " *\n"
60 " * This class was automatically generated by the\n"
61 " * aapt tool from the resource data it found. It\n"
62 " * should not be modified by hand.\n"
63 " */\n\n";
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070064
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065bool ClassDefinition::WriteJavaFile(const ClassDefinition* def,
66 const StringPiece& package, bool final,
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070067 std::ostream* out) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 *out << sWarningHeader << "package " << package << ";\n\n";
69 def->WriteToStream("", final, out);
70 return bool(*out);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070071}
72
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073} // namespace aapt