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" |
| 18 | #include "util/StringPiece.h" |
| 19 | |
| 20 | #include <ostream> |
| 21 | |
| 22 | namespace aapt { |
| 23 | |
| 24 | bool ClassDefinition::empty() const { |
| 25 | for (const std::unique_ptr<ClassMember>& member : mMembers) { |
| 26 | if (!member->empty()) { |
| 27 | return false; |
| 28 | } |
| 29 | } |
| 30 | return true; |
| 31 | } |
| 32 | |
| 33 | void ClassDefinition::writeToStream(const StringPiece& prefix, bool final, |
| 34 | std::ostream* out) const { |
| 35 | if (mMembers.empty() && !mCreateIfEmpty) { |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | ClassMember::writeToStream(prefix, final, out); |
| 40 | |
| 41 | *out << prefix << "public "; |
| 42 | if (mQualifier == ClassQualifier::Static) { |
| 43 | *out << "static "; |
| 44 | } |
| 45 | *out << "final class " << mName << " {\n"; |
| 46 | |
| 47 | std::string newPrefix = prefix.toString(); |
| 48 | newPrefix.append(kIndent); |
| 49 | |
| 50 | for (const std::unique_ptr<ClassMember>& member : mMembers) { |
| 51 | member->writeToStream(newPrefix, final, out); |
| 52 | *out << "\n"; |
| 53 | } |
| 54 | |
| 55 | *out << prefix << "}"; |
| 56 | } |
| 57 | |
| 58 | constexpr static const char* sWarningHeader = |
| 59 | "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n" |
| 60 | " *\n" |
| 61 | " * This class was automatically generated by the\n" |
| 62 | " * aapt tool from the resource data it found. It\n" |
| 63 | " * should not be modified by hand.\n" |
| 64 | " */\n\n"; |
| 65 | |
| 66 | bool ClassDefinition::writeJavaFile(const ClassDefinition* def, |
| 67 | const StringPiece& package, |
| 68 | bool final, |
| 69 | std::ostream* out) { |
| 70 | *out << sWarningHeader << "package " << package << ";\n\n"; |
| 71 | def->writeToStream("", final, out); |
| 72 | return bool(*out); |
| 73 | } |
| 74 | |
| 75 | } // namespace aapt |