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