blob: 53d6ea1a79c57ed148619d1f156e6d9133e8acdd [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 Lesinskid5083f62017-01-16 15:07:21 -080019#include "androidfw/StringPiece.h"
20
21using android::StringPiece;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070022
23namespace aapt {
24
25bool ClassDefinition::empty() const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026 for (const std::unique_ptr<ClassMember>& member : members_) {
27 if (!member->empty()) {
28 return false;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070029 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070030 }
31 return true;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070032}
33
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034void ClassDefinition::WriteToStream(const StringPiece& prefix, bool final,
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070035 std::ostream* out) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070036 if (members_.empty() && !create_if_empty_) {
37 return;
38 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070039
Adam Lesinskice5e56e2016-10-21 17:56:45 -070040 ClassMember::WriteToStream(prefix, final, out);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070041
Adam Lesinskice5e56e2016-10-21 17:56:45 -070042 *out << prefix << "public ";
43 if (qualifier_ == ClassQualifier::Static) {
44 *out << "static ";
45 }
46 *out << "final class " << name_ << " {\n";
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070047
Adam Lesinskid5083f62017-01-16 15:07:21 -080048 std::string new_prefix = prefix.to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 new_prefix.append(kIndent);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070050
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 for (const std::unique_ptr<ClassMember>& member : members_) {
52 member->WriteToStream(new_prefix, final, out);
53 *out << "\n";
54 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070055
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 *out << prefix << "}";
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070057}
58
59constexpr static const char* sWarningHeader =
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060 "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n"
61 " *\n"
62 " * This class was automatically generated by the\n"
63 " * aapt tool from the resource data it found. It\n"
64 " * should not be modified by hand.\n"
65 " */\n\n";
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070066
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067bool ClassDefinition::WriteJavaFile(const ClassDefinition* def,
68 const StringPiece& package, bool final,
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070069 std::ostream* out) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 *out << sWarningHeader << "package " << package << ";\n\n";
71 def->WriteToStream("", final, out);
72 return bool(*out);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070073}
74
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075} // namespace aapt