Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 1 | /* |
| 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 Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 18 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 19 | #include "androidfw/StringPiece.h" |
| 20 | |
| 21 | using android::StringPiece; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 22 | |
| 23 | namespace aapt { |
| 24 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame^] | 25 | void ClassMember::WriteToStream(const StringPiece& prefix, bool final, std::ostream* out) const { |
| 26 | processor_.WriteToStream(out, prefix); |
| 27 | } |
| 28 | |
| 29 | void MethodDefinition::AppendStatement(const StringPiece& statement) { |
| 30 | statements_.push_back(statement.to_string()); |
| 31 | } |
| 32 | |
| 33 | void 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 Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 42 | bool ClassDefinition::empty() const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 43 | for (const std::unique_ptr<ClassMember>& member : members_) { |
| 44 | if (!member->empty()) { |
| 45 | return false; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 46 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 47 | } |
| 48 | return true; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 51 | void ClassDefinition::WriteToStream(const StringPiece& prefix, bool final, |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 52 | std::ostream* out) const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 53 | if (members_.empty() && !create_if_empty_) { |
| 54 | return; |
| 55 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 56 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 57 | ClassMember::WriteToStream(prefix, final, out); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 58 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 59 | *out << prefix << "public "; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame^] | 60 | if (qualifier_ == ClassQualifier::kStatic) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 61 | *out << "static "; |
| 62 | } |
| 63 | *out << "final class " << name_ << " {\n"; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 64 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 65 | std::string new_prefix = prefix.to_string(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 66 | new_prefix.append(kIndent); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 67 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 68 | for (const std::unique_ptr<ClassMember>& member : members_) { |
| 69 | member->WriteToStream(new_prefix, final, out); |
| 70 | *out << "\n"; |
| 71 | } |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 72 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 73 | *out << prefix << "}"; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | constexpr static const char* sWarningHeader = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 77 | "/* 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 Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 83 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 84 | bool ClassDefinition::WriteJavaFile(const ClassDefinition* def, |
| 85 | const StringPiece& package, bool final, |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 86 | std::ostream* out) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 87 | *out << sWarningHeader << "package " << package << ";\n\n"; |
| 88 | def->WriteToStream("", final, out); |
| 89 | return bool(*out); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 92 | } // namespace aapt |