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 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 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 { |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 26 | processor_.WriteToStream(prefix, out); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 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 | f852dd0 | 2017-08-18 19:49:58 -0700 | [diff] [blame^] | 42 | ClassDefinition::Result ClassDefinition::AddMember(std::unique_ptr<ClassMember> member) { |
| 43 | Result result = Result::kAdded; |
| 44 | auto iter = members_.find(member); |
| 45 | if (iter != members_.end()) { |
| 46 | members_.erase(iter); |
| 47 | result = Result::kOverridden; |
| 48 | } |
| 49 | members_.insert(std::move(member)); |
| 50 | return result; |
| 51 | } |
| 52 | |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 53 | bool ClassDefinition::empty() const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 54 | for (const std::unique_ptr<ClassMember>& member : members_) { |
| 55 | if (!member->empty()) { |
| 56 | return false; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 57 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 58 | } |
| 59 | return true; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 62 | void ClassDefinition::WriteToStream(const StringPiece& prefix, bool final, |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 63 | std::ostream* out) const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 64 | if (members_.empty() && !create_if_empty_) { |
| 65 | return; |
| 66 | } |
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 | ClassMember::WriteToStream(prefix, final, out); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 69 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 70 | *out << prefix << "public "; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 71 | if (qualifier_ == ClassQualifier::kStatic) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 72 | *out << "static "; |
| 73 | } |
| 74 | *out << "final class " << name_ << " {\n"; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 75 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 76 | std::string new_prefix = prefix.to_string(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 77 | new_prefix.append(kIndent); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 78 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 79 | for (const std::unique_ptr<ClassMember>& member : members_) { |
| 80 | member->WriteToStream(new_prefix, final, out); |
| 81 | *out << "\n"; |
| 82 | } |
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 | *out << prefix << "}"; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | constexpr static const char* sWarningHeader = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 88 | "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n" |
| 89 | " *\n" |
| 90 | " * This class was automatically generated by the\n" |
| 91 | " * aapt tool from the resource data it found. It\n" |
| 92 | " * should not be modified by hand.\n" |
| 93 | " */\n\n"; |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 94 | |
Adam Lesinski | 09f4d70 | 2017-08-08 10:39:55 -0700 | [diff] [blame] | 95 | bool ClassDefinition::WriteJavaFile(const ClassDefinition* def, const StringPiece& package, |
| 96 | bool final, std::ostream* out) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 97 | *out << sWarningHeader << "package " << package << ";\n\n"; |
| 98 | def->WriteToStream("", final, out); |
| 99 | return bool(*out); |
Adam Lesinski | 6cbfb1d | 2016-03-31 13:33:02 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 102 | } // namespace aapt |