Christopher Wiley | 3a9da17 | 2016-01-29 11:10:49 -0800 | [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 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 17 | #include "generate_java.h" |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 18 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 22 | #include <memory> |
| 23 | #include <sstream> |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 24 | |
Christopher Wiley | 3a9da17 | 2016-01-29 11:10:49 -0800 | [diff] [blame] | 25 | #include <android-base/stringprintf.h> |
| 26 | |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 27 | #include "aidl_to_java.h" |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 28 | #include "code_writer.h" |
Christopher Wiley | 775fa1f | 2015-09-22 15:00:12 -0700 | [diff] [blame] | 29 | #include "type_java.h" |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 30 | |
Christopher Wiley | f76b59a | 2016-01-29 11:32:11 -0800 | [diff] [blame] | 31 | using std::unique_ptr; |
Christopher Wiley | db154a5 | 2015-09-28 16:32:25 -0700 | [diff] [blame] | 32 | using ::android::aidl::java::Variable; |
Christopher Wiley | 3a9da17 | 2016-01-29 11:10:49 -0800 | [diff] [blame] | 33 | using std::string; |
Christopher Wiley | db154a5 | 2015-09-28 16:32:25 -0700 | [diff] [blame] | 34 | |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 35 | namespace android { |
| 36 | namespace aidl { |
Christopher Wiley | db154a5 | 2015-09-28 16:32:25 -0700 | [diff] [blame] | 37 | namespace java { |
| 38 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 39 | bool generate_java_interface(const string& filename, const string& original_src, |
| 40 | const AidlInterface* iface, JavaTypeNamespace* types, |
| 41 | const IoDelegate& io_delegate, const Options& options) { |
Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 42 | Class* cl = generate_binder_interface_class(iface, types, options); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 43 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 44 | Document* document = |
| 45 | new Document("" /* no comment */, iface->GetPackage(), original_src, unique_ptr<Class>(cl)); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 46 | |
Christopher Wiley | 3a9da17 | 2016-01-29 11:10:49 -0800 | [diff] [blame] | 47 | CodeWriterPtr code_writer = io_delegate.GetCodeWriter(filename); |
| 48 | document->Write(code_writer.get()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 49 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 50 | return true; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 53 | bool generate_java_parcel(const std::string& filename, const std::string& original_src, |
| 54 | const AidlStructuredParcelable* parcel, JavaTypeNamespace* types, |
| 55 | const IoDelegate& io_delegate, const Options& options) { |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 56 | Class* cl = generate_parcel_class(parcel, types, options); |
| 57 | |
| 58 | Document* document = |
| 59 | new Document("" /* no comment */, parcel->GetPackage(), original_src, unique_ptr<Class>(cl)); |
| 60 | |
| 61 | CodeWriterPtr code_writer = io_delegate.GetCodeWriter(filename); |
| 62 | document->Write(code_writer.get()); |
| 63 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 64 | return true; |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Steven Moreland | 2a9a7d6 | 2019-02-05 16:11:54 -0800 | [diff] [blame^] | 67 | bool generate_java_parcel_declaration(const std::string& filename, const IoDelegate& io_delegate) { |
| 68 | CodeWriterPtr code_writer = io_delegate.GetCodeWriter(filename); |
| 69 | *code_writer |
| 70 | << "// This file is intentionally left blank as placeholder for parcel declaration.\n"; |
| 71 | |
| 72 | return true; |
| 73 | } |
| 74 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 75 | bool generate_java(const std::string& filename, const std::string& original_src, |
| 76 | const AidlDefinedType* defined_type, JavaTypeNamespace* types, |
| 77 | const IoDelegate& io_delegate, const Options& options) { |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 78 | const AidlStructuredParcelable* parcelable = defined_type->AsStructuredParcelable(); |
| 79 | if (parcelable != nullptr) { |
| 80 | return generate_java_parcel(filename, original_src, parcelable, types, io_delegate, options); |
| 81 | } |
| 82 | |
Steven Moreland | 2a9a7d6 | 2019-02-05 16:11:54 -0800 | [diff] [blame^] | 83 | const AidlParcelable* parcelable_decl = defined_type->AsParcelable(); |
| 84 | if (parcelable_decl != nullptr) { |
| 85 | return generate_java_parcel_declaration(filename, io_delegate); |
| 86 | } |
| 87 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 88 | const AidlInterface* interface = defined_type->AsInterface(); |
| 89 | if (interface != nullptr) { |
| 90 | return generate_java_interface(filename, original_src, interface, types, io_delegate, options); |
| 91 | } |
| 92 | |
| 93 | CHECK(false) << "Unrecognized type sent for cpp generation."; |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | android::aidl::java::Class* generate_parcel_class(const AidlStructuredParcelable* parcel, |
| 98 | java::JavaTypeNamespace* types, |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 99 | const Options& /*options*/) { |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 100 | const ParcelType* parcelType = parcel->GetLanguageType<ParcelType>(); |
| 101 | |
| 102 | Class* parcel_class = new Class; |
| 103 | parcel_class->comment = parcel->GetComments(); |
| 104 | parcel_class->modifiers = PUBLIC; |
| 105 | parcel_class->what = Class::CLASS; |
| 106 | parcel_class->type = parcelType; |
Jiyong Park | b77f7a5 | 2018-09-13 21:32:38 +0900 | [diff] [blame] | 107 | parcel_class->interfaces.push_back(types->ParcelableInterfaceType()); |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 108 | parcel_class->annotations = generate_java_annotations(*parcel); |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 109 | |
| 110 | for (const auto& variable : parcel->GetFields()) { |
| 111 | const Type* type = variable->GetType().GetLanguageType<Type>(); |
Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 112 | |
| 113 | std::ostringstream out; |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 114 | out << variable->GetType().GetComments() << "\n"; |
| 115 | for (const auto& a : generate_java_annotations(variable->GetType())) { |
| 116 | out << a << "\n"; |
| 117 | } |
Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 118 | out << "public " << type->JavaType() << (variable->GetType().IsArray() ? "[]" : "") << " " |
| 119 | << variable->GetName(); |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 120 | if (variable->GetDefaultValue()) { |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 121 | out << " = " << variable->ValueString(AidlConstantValueDecorator); |
Steven Moreland | 9ea10e3 | 2018-07-19 15:26:09 -0700 | [diff] [blame] | 122 | } |
| 123 | out << ";\n"; |
| 124 | parcel_class->elements.push_back(new LiteralClassElement(out.str())); |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | std::ostringstream out; |
| 128 | out << "public static final android.os.Parcelable.Creator<" << parcel->GetName() << "> CREATOR = " |
| 129 | << "new android.os.Parcelable.Creator<" << parcel->GetName() << ">() {\n"; |
Jiyong Park | b77f7a5 | 2018-09-13 21:32:38 +0900 | [diff] [blame] | 130 | out << " @Override\n"; |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 131 | out << " public " << parcel->GetName() |
| 132 | << " createFromParcel(android.os.Parcel _aidl_source) {\n"; |
| 133 | out << " " << parcel->GetName() << " _aidl_out = new " << parcel->GetName() << "();\n"; |
| 134 | out << " _aidl_out.readFromParcel(_aidl_source);\n"; |
| 135 | out << " return _aidl_out;\n"; |
| 136 | out << " }\n"; |
Jiyong Park | b77f7a5 | 2018-09-13 21:32:38 +0900 | [diff] [blame] | 137 | out << " @Override\n"; |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 138 | out << " public " << parcel->GetName() << "[] newArray(int _aidl_size) {\n"; |
| 139 | out << " return new " << parcel->GetName() << "[_aidl_size];\n"; |
| 140 | out << " }\n"; |
| 141 | out << "};\n"; |
| 142 | parcel_class->elements.push_back(new LiteralClassElement(out.str())); |
| 143 | |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 144 | Variable* flag_variable = new Variable(new Type(types, "int", 0, false), "_aidl_flag"); |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 145 | Variable* parcel_variable = |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 146 | new Variable(new Type(types, "android.os.Parcel", 0, false), "_aidl_parcel"); |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 147 | |
| 148 | Method* write_method = new Method; |
Jeongik Cha | a2ada0c | 2018-11-17 15:11:45 +0900 | [diff] [blame] | 149 | write_method->modifiers = PUBLIC | OVERRIDE | FINAL; |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 150 | write_method->returnType = new Type(types, "void", 0, false); |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 151 | write_method->name = "writeToParcel"; |
| 152 | write_method->parameters.push_back(parcel_variable); |
| 153 | write_method->parameters.push_back(flag_variable); |
| 154 | write_method->statements = new StatementBlock(); |
Jeongik Cha | 95eba57 | 2018-11-22 09:14:52 +0900 | [diff] [blame] | 155 | |
| 156 | out.str(""); |
| 157 | out << "int _aidl_start_pos = _aidl_parcel.dataPosition();\n" |
| 158 | << "_aidl_parcel.writeInt(0);\n"; |
| 159 | write_method->statements->Add(new LiteralStatement(out.str())); |
| 160 | |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 161 | for (const auto& field : parcel->GetFields()) { |
| 162 | string code; |
| 163 | CodeWriterPtr writer = CodeWriter::ForString(&code); |
| 164 | CodeGeneratorContext context{ |
| 165 | .writer = *(writer.get()), |
| 166 | .typenames = types->typenames_, |
| 167 | .type = field->GetType(), |
| 168 | .var = field->GetName(), |
| 169 | .parcel = parcel_variable->name, |
| 170 | .is_return_value = false, |
| 171 | }; |
| 172 | WriteToParcelFor(context); |
| 173 | writer->Close(); |
| 174 | write_method->statements->Add(new LiteralStatement(code)); |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 175 | } |
Jeongik Cha | 95eba57 | 2018-11-22 09:14:52 +0900 | [diff] [blame] | 176 | |
| 177 | out.str(""); |
| 178 | out << "int _aidl_end_pos = _aidl_parcel.dataPosition();\n" |
| 179 | << "_aidl_parcel.setDataPosition(_aidl_start_pos);\n" |
| 180 | << "_aidl_parcel.writeInt(_aidl_end_pos - _aidl_start_pos);\n" |
| 181 | << "_aidl_parcel.setDataPosition(_aidl_end_pos);\n"; |
| 182 | |
| 183 | write_method->statements->Add(new LiteralStatement(out.str())); |
| 184 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 185 | parcel_class->elements.push_back(write_method); |
| 186 | |
| 187 | Method* read_method = new Method; |
Jeongik Cha | a2ada0c | 2018-11-17 15:11:45 +0900 | [diff] [blame] | 188 | read_method->modifiers = PUBLIC | FINAL; |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 189 | read_method->returnType = new Type(types, "void", 0, false); |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 190 | read_method->name = "readFromParcel"; |
| 191 | read_method->parameters.push_back(parcel_variable); |
| 192 | read_method->statements = new StatementBlock(); |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 193 | |
Jeongik Cha | 95eba57 | 2018-11-22 09:14:52 +0900 | [diff] [blame] | 194 | out.str(""); |
| 195 | out << "int _aidl_start_pos = _aidl_parcel.dataPosition();\n" |
| 196 | << "int _aidl_parcelable_size = _aidl_parcel.readInt();\n" |
| 197 | << "if (_aidl_parcelable_size < 0) return;\n" |
| 198 | << "try {\n"; |
| 199 | |
| 200 | read_method->statements->Add(new LiteralStatement(out.str())); |
| 201 | |
| 202 | out.str(""); |
| 203 | out << " if (_aidl_parcel.dataPosition() - _aidl_start_pos >= _aidl_parcelable_size) return;\n"; |
| 204 | |
George Burgess IV | 8efe21a | 2018-12-05 16:40:40 -0800 | [diff] [blame] | 205 | LiteralStatement* sizeCheck = nullptr; |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 206 | // keep this across different fields in order to create the classloader |
| 207 | // at most once. |
| 208 | bool is_classloader_created = false; |
| 209 | for (const auto& field : parcel->GetFields()) { |
| 210 | string code; |
| 211 | CodeWriterPtr writer = CodeWriter::ForString(&code); |
| 212 | CodeGeneratorContext context{ |
| 213 | .writer = *(writer.get()), |
| 214 | .typenames = types->typenames_, |
| 215 | .type = field->GetType(), |
| 216 | .var = field->GetName(), |
| 217 | .parcel = parcel_variable->name, |
| 218 | .is_classloader_created = &is_classloader_created, |
| 219 | }; |
Jeongik Cha | 95eba57 | 2018-11-22 09:14:52 +0900 | [diff] [blame] | 220 | context.writer.Indent(); |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 221 | CreateFromParcelFor(context); |
| 222 | writer->Close(); |
| 223 | read_method->statements->Add(new LiteralStatement(code)); |
George Burgess IV | 8efe21a | 2018-12-05 16:40:40 -0800 | [diff] [blame] | 224 | if (!sizeCheck) sizeCheck = new LiteralStatement(out.str()); |
Jeongik Cha | 95eba57 | 2018-11-22 09:14:52 +0900 | [diff] [blame] | 225 | read_method->statements->Add(sizeCheck); |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 226 | } |
Jeongik Cha | 95eba57 | 2018-11-22 09:14:52 +0900 | [diff] [blame] | 227 | |
| 228 | out.str(""); |
| 229 | out << "} finally {\n" |
| 230 | << " _aidl_parcel.setDataPosition(_aidl_start_pos + _aidl_parcelable_size);\n" |
| 231 | << "}\n"; |
| 232 | |
| 233 | read_method->statements->Add(new LiteralStatement(out.str())); |
| 234 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 235 | parcel_class->elements.push_back(read_method); |
| 236 | |
Jiyong Park | b77f7a5 | 2018-09-13 21:32:38 +0900 | [diff] [blame] | 237 | Method* describe_contents_method = new Method; |
| 238 | describe_contents_method->modifiers = PUBLIC | OVERRIDE; |
| 239 | describe_contents_method->returnType = types->IntType(); |
| 240 | describe_contents_method->name = "describeContents"; |
| 241 | describe_contents_method->statements = new StatementBlock(); |
Jeongik Cha | 372a8c8 | 2018-12-12 16:31:11 +0900 | [diff] [blame] | 242 | describe_contents_method->statements->Add(new LiteralStatement("return 0;\n")); |
Jiyong Park | b77f7a5 | 2018-09-13 21:32:38 +0900 | [diff] [blame] | 243 | parcel_class->elements.push_back(describe_contents_method); |
| 244 | |
Steven Moreland | 5557f1c | 2018-07-02 13:50:23 -0700 | [diff] [blame] | 245 | return parcel_class; |
| 246 | } |
| 247 | |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 248 | std::vector<std::string> generate_java_annotations(const AidlAnnotatable& a) { |
| 249 | std::vector<std::string> result; |
| 250 | if (a.IsUnsupportedAppUsage()) { |
| 251 | result.emplace_back("@android.annotation.UnsupportedAppUsage"); |
| 252 | } |
Jeongik Cha | 698e6af | 2018-12-12 14:39:55 +0900 | [diff] [blame] | 253 | if (a.IsSystemApi()) { |
| 254 | result.emplace_back("@android.annotation.SystemApi"); |
| 255 | } |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 256 | return result; |
| 257 | } |
| 258 | |
Christopher Wiley | db154a5 | 2015-09-28 16:32:25 -0700 | [diff] [blame] | 259 | } // namespace java |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 260 | } // namespace android |
| 261 | } // namespace aidl |