blob: 0cec9ae407f59153c211f7181ac72893e2f5b826 [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
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080025void ClassMember::WriteToStream(const StringPiece& prefix, bool final, std::ostream* out) const {
26 processor_.WriteToStream(out, prefix);
27}
28
29void MethodDefinition::AppendStatement(const StringPiece& statement) {
30 statements_.push_back(statement.to_string());
31}
32
33void MethodDefinition::WriteToStream(const StringPiece& prefix, bool final,
34 std::ostream* out) const {
35 *out << prefix << signature_ << " {\n";
36 for (const auto& statement : statements_) {
37 *out << prefix << " " << statement << "\n";
38 }
39 *out << prefix << "}";
40}
41
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070042bool ClassDefinition::empty() const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043 for (const std::unique_ptr<ClassMember>& member : members_) {
44 if (!member->empty()) {
45 return false;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070046 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 }
48 return true;
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070049}
50
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051void ClassDefinition::WriteToStream(const StringPiece& prefix, bool final,
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070052 std::ostream* out) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 if (members_.empty() && !create_if_empty_) {
54 return;
55 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070056
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057 ClassMember::WriteToStream(prefix, final, out);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070058
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 *out << prefix << "public ";
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080060 if (qualifier_ == ClassQualifier::kStatic) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 *out << "static ";
62 }
63 *out << "final class " << name_ << " {\n";
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070064
Adam Lesinskid5083f62017-01-16 15:07:21 -080065 std::string new_prefix = prefix.to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 new_prefix.append(kIndent);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070067
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 for (const std::unique_ptr<ClassMember>& member : members_) {
69 member->WriteToStream(new_prefix, final, out);
70 *out << "\n";
71 }
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070072
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073 *out << prefix << "}";
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070074}
75
76constexpr static const char* sWarningHeader =
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n"
78 " *\n"
79 " * This class was automatically generated by the\n"
80 " * aapt tool from the resource data it found. It\n"
81 " * should not be modified by hand.\n"
82 " */\n\n";
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070083
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084bool ClassDefinition::WriteJavaFile(const ClassDefinition* def,
85 const StringPiece& package, bool final,
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070086 std::ostream* out) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 *out << sWarningHeader << "package " << package << ";\n\n";
88 def->WriteToStream("", final, out);
89 return bool(*out);
Adam Lesinski6cbfb1d2016-03-31 13:33:02 -070090}
91
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092} // namespace aapt