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 | |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 17 | #include "aidl.h" |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 18 | #include "aidl_to_java.h" |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 19 | #include "generate_java.h" |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 20 | #include "options.h" |
| 21 | #include "type_java.h" |
Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 22 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | |
Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 27 | #include <algorithm> |
| 28 | #include <unordered_set> |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 29 | #include <utility> |
| 30 | #include <vector> |
Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 31 | |
Elliott Hughes | 0a62067 | 2015-12-04 13:53:18 -0800 | [diff] [blame] | 32 | #include <android-base/macros.h> |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 33 | #include <android-base/stringprintf.h> |
Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 34 | |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 35 | using android::base::Join; |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 36 | using android::base::StringPrintf; |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 37 | |
| 38 | using std::pair; |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 39 | using std::string; |
| 40 | using std::unique_ptr; |
| 41 | using std::vector; |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 42 | |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 43 | namespace android { |
| 44 | namespace aidl { |
Christopher Wiley | db154a5 | 2015-09-28 16:32:25 -0700 | [diff] [blame] | 45 | namespace java { |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 46 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 47 | // ================================================= |
Jiyong Park | 2c44f07 | 2018-07-30 21:52:21 +0900 | [diff] [blame] | 48 | class VariableFactory { |
| 49 | public: |
| 50 | using Variable = ::android::aidl::java::Variable; |
| 51 | using Type = ::android::aidl::java::Type; |
| 52 | |
| 53 | explicit VariableFactory(const std::string& base) : base_(base), index_(0) {} |
| 54 | Variable* Get(const Type* type) { |
| 55 | Variable* v = new Variable(type, StringPrintf("%s%d", base_.c_str(), index_)); |
| 56 | vars_.push_back(v); |
| 57 | index_++; |
| 58 | return v; |
| 59 | } |
| 60 | |
| 61 | Variable* Get(int index) { return vars_[index]; } |
| 62 | |
| 63 | private: |
| 64 | std::vector<Variable*> vars_; |
| 65 | std::string base_; |
| 66 | int index_; |
| 67 | |
| 68 | DISALLOW_COPY_AND_ASSIGN(VariableFactory); |
| 69 | }; |
| 70 | |
| 71 | // ================================================= |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 72 | class StubClass : public Class { |
| 73 | public: |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 74 | StubClass(const Type* type, const InterfaceType* interfaceType, JavaTypeNamespace* types, |
| 75 | const Options& options); |
Christopher Wiley | 3a9da17 | 2016-01-29 11:10:49 -0800 | [diff] [blame] | 76 | virtual ~StubClass() = default; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 77 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 78 | Variable* transact_code; |
| 79 | Variable* transact_data; |
| 80 | Variable* transact_reply; |
| 81 | Variable* transact_flags; |
| 82 | SwitchStatement* transact_switch; |
Andreas Gampe | 7fab0d1 | 2017-11-22 17:50:17 -0800 | [diff] [blame] | 83 | StatementBlock* transact_statements; |
Olivier Gaillard | 1140140 | 2018-07-05 15:01:34 +0100 | [diff] [blame] | 84 | SwitchStatement* code_to_method_name_switch; |
Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 85 | |
Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 86 | // Where onTransact cases should be generated as separate methods. |
| 87 | bool transact_outline; |
Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 88 | // Specific methods that should be outlined when transact_outline is true. |
| 89 | std::unordered_set<const AidlMethod*> outline_methods; |
| 90 | // Number of all methods. |
| 91 | size_t all_method_count; |
Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 92 | |
Andreas Gampe | a8a66fe | 2017-11-22 12:17:00 -0800 | [diff] [blame] | 93 | // Finish generation. This will add a default case to the switch. |
| 94 | void finish(); |
| 95 | |
Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 96 | Expression* get_transact_descriptor(const JavaTypeNamespace* types, |
| 97 | const AidlMethod* method); |
Andreas Gampe | 7fab0d1 | 2017-11-22 17:50:17 -0800 | [diff] [blame] | 98 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 99 | private: |
| 100 | void make_as_interface(const InterfaceType* interfaceType, |
| 101 | JavaTypeNamespace* types); |
| 102 | |
Andreas Gampe | 7fab0d1 | 2017-11-22 17:50:17 -0800 | [diff] [blame] | 103 | Variable* transact_descriptor; |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 104 | const Options& options_; |
Andreas Gampe | 7fab0d1 | 2017-11-22 17:50:17 -0800 | [diff] [blame] | 105 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 106 | DISALLOW_COPY_AND_ASSIGN(StubClass); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 107 | }; |
| 108 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 109 | StubClass::StubClass(const Type* type, const InterfaceType* interfaceType, JavaTypeNamespace* types, |
| 110 | const Options& options) |
Olivier Gaillard | 1140140 | 2018-07-05 15:01:34 +0100 | [diff] [blame] | 111 | : Class(), options_(options) { |
Andreas Gampe | 7fab0d1 | 2017-11-22 17:50:17 -0800 | [diff] [blame] | 112 | transact_descriptor = nullptr; |
Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 113 | transact_outline = false; |
Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 114 | all_method_count = 0; // Will be set when outlining may be enabled. |
Andreas Gampe | 7fab0d1 | 2017-11-22 17:50:17 -0800 | [diff] [blame] | 115 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 116 | this->comment = "/** Local-side IPC implementation stub class. */"; |
| 117 | this->modifiers = PUBLIC | ABSTRACT | STATIC; |
| 118 | this->what = Class::CLASS; |
| 119 | this->type = type; |
| 120 | this->extends = types->BinderNativeType(); |
| 121 | this->interfaces.push_back(interfaceType); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 122 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 123 | // descriptor |
| 124 | Field* descriptor = |
| 125 | new Field(STATIC | FINAL | PRIVATE, |
| 126 | new Variable(types->StringType(), "DESCRIPTOR")); |
Christopher Wiley | d21bfee | 2016-01-29 15:11:38 -0800 | [diff] [blame] | 127 | descriptor->value = "\"" + interfaceType->JavaType() + "\""; |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 128 | this->elements.push_back(descriptor); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 129 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 130 | // ctor |
| 131 | Method* ctor = new Method; |
| 132 | ctor->modifiers = PUBLIC; |
| 133 | ctor->comment = |
| 134 | "/** Construct the stub at attach it to the " |
| 135 | "interface. */"; |
| 136 | ctor->name = "Stub"; |
| 137 | ctor->statements = new StatementBlock; |
| 138 | MethodCall* attach = |
| 139 | new MethodCall(THIS_VALUE, "attachInterface", 2, THIS_VALUE, |
| 140 | new LiteralExpression("DESCRIPTOR")); |
| 141 | ctor->statements->Add(attach); |
| 142 | this->elements.push_back(ctor); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 143 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 144 | // asInterface |
| 145 | make_as_interface(interfaceType, types); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 146 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 147 | // asBinder |
| 148 | Method* asBinder = new Method; |
| 149 | asBinder->modifiers = PUBLIC | OVERRIDE; |
| 150 | asBinder->returnType = types->IBinderType(); |
| 151 | asBinder->name = "asBinder"; |
| 152 | asBinder->statements = new StatementBlock; |
| 153 | asBinder->statements->Add(new ReturnStatement(THIS_VALUE)); |
| 154 | this->elements.push_back(asBinder); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 155 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 156 | if (options_.GenTransactionNames()) { |
Olivier Gaillard | 83d7cd3 | 2018-07-30 16:20:57 +0100 | [diff] [blame] | 157 | // getDefaultTransactionName |
| 158 | Method* getDefaultTransactionName = new Method; |
| 159 | getDefaultTransactionName->modifiers = PUBLIC | STATIC; |
| 160 | getDefaultTransactionName->returnType = types->StringType(); |
| 161 | getDefaultTransactionName->name = "getDefaultTransactionName"; |
| 162 | Variable* code = new Variable(types->IntType(), "transactionCode"); |
| 163 | getDefaultTransactionName->parameters.push_back(code); |
| 164 | getDefaultTransactionName->statements = new StatementBlock; |
| 165 | this->code_to_method_name_switch = new SwitchStatement(code); |
| 166 | getDefaultTransactionName->statements->Add(this->code_to_method_name_switch); |
| 167 | this->elements.push_back(getDefaultTransactionName); |
| 168 | |
| 169 | // getTransactionName |
Olivier Gaillard | 1140140 | 2018-07-05 15:01:34 +0100 | [diff] [blame] | 170 | Method* getTransactionName = new Method; |
| 171 | getTransactionName->modifiers = PUBLIC; |
| 172 | getTransactionName->returnType = types->StringType(); |
| 173 | getTransactionName->name = "getTransactionName"; |
Olivier Gaillard | 83d7cd3 | 2018-07-30 16:20:57 +0100 | [diff] [blame] | 174 | Variable* code2 = new Variable(types->IntType(), "transactionCode"); |
| 175 | getTransactionName->parameters.push_back(code2); |
Olivier Gaillard | 1140140 | 2018-07-05 15:01:34 +0100 | [diff] [blame] | 176 | getTransactionName->statements = new StatementBlock; |
Olivier Gaillard | 83d7cd3 | 2018-07-30 16:20:57 +0100 | [diff] [blame] | 177 | getTransactionName->statements->Add( |
| 178 | new ReturnStatement(new MethodCall(THIS_VALUE, "getDefaultTransactionName", 1, code2))); |
Olivier Gaillard | 1140140 | 2018-07-05 15:01:34 +0100 | [diff] [blame] | 179 | this->elements.push_back(getTransactionName); |
| 180 | } |
| 181 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 182 | // onTransact |
| 183 | this->transact_code = new Variable(types->IntType(), "code"); |
| 184 | this->transact_data = new Variable(types->ParcelType(), "data"); |
| 185 | this->transact_reply = new Variable(types->ParcelType(), "reply"); |
| 186 | this->transact_flags = new Variable(types->IntType(), "flags"); |
| 187 | Method* onTransact = new Method; |
| 188 | onTransact->modifiers = PUBLIC | OVERRIDE; |
| 189 | onTransact->returnType = types->BoolType(); |
| 190 | onTransact->name = "onTransact"; |
| 191 | onTransact->parameters.push_back(this->transact_code); |
| 192 | onTransact->parameters.push_back(this->transact_data); |
| 193 | onTransact->parameters.push_back(this->transact_reply); |
| 194 | onTransact->parameters.push_back(this->transact_flags); |
| 195 | onTransact->statements = new StatementBlock; |
Andreas Gampe | 7fab0d1 | 2017-11-22 17:50:17 -0800 | [diff] [blame] | 196 | transact_statements = onTransact->statements; |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 197 | onTransact->exceptions.push_back(types->RemoteExceptionType()); |
| 198 | this->elements.push_back(onTransact); |
| 199 | this->transact_switch = new SwitchStatement(this->transact_code); |
Andreas Gampe | a8a66fe | 2017-11-22 12:17:00 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void StubClass::finish() { |
| 203 | Case* default_case = new Case; |
| 204 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 205 | MethodCall* superCall = new MethodCall( |
Andreas Gampe | a8a66fe | 2017-11-22 12:17:00 -0800 | [diff] [blame] | 206 | SUPER_VALUE, "onTransact", 4, this->transact_code, this->transact_data, |
| 207 | this->transact_reply, this->transact_flags); |
| 208 | default_case->statements->Add(new ReturnStatement(superCall)); |
| 209 | transact_switch->cases.push_back(default_case); |
Andreas Gampe | 7fab0d1 | 2017-11-22 17:50:17 -0800 | [diff] [blame] | 210 | |
| 211 | transact_statements->Add(this->transact_switch); |
Olivier Gaillard | 1140140 | 2018-07-05 15:01:34 +0100 | [diff] [blame] | 212 | |
| 213 | // getTransactionName |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 214 | if (options_.GenTransactionNames()) { |
Olivier Gaillard | 1140140 | 2018-07-05 15:01:34 +0100 | [diff] [blame] | 215 | // Some transaction codes are common, e.g. INTERFACE_TRANSACTION or DUMP_TRANSACTION. |
| 216 | // Common transaction codes will not be resolved to a string by getTransactionName. The method |
| 217 | // will return NULL in this case. |
| 218 | Case* code_switch_default_case = new Case; |
| 219 | code_switch_default_case->statements->Add(new ReturnStatement(NULL_VALUE)); |
| 220 | this->code_to_method_name_switch->cases.push_back(code_switch_default_case); |
| 221 | } |
Andreas Gampe | 7fab0d1 | 2017-11-22 17:50:17 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 224 | // The the expression for the interface's descriptor to be used when |
| 225 | // generating code for the given method. Null is acceptable for method |
| 226 | // and stands for synthetic cases. |
| 227 | Expression* StubClass::get_transact_descriptor(const JavaTypeNamespace* types, |
| 228 | const AidlMethod* method) { |
Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 229 | if (transact_outline) { |
Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 230 | if (method != nullptr) { |
| 231 | // When outlining, each outlined method needs its own literal. |
| 232 | if (outline_methods.count(method) != 0) { |
| 233 | return new LiteralExpression("DESCRIPTOR"); |
| 234 | } |
| 235 | } else { |
| 236 | // Synthetic case. A small number is assumed. Use its own descriptor |
| 237 | // if there are only synthetic cases. |
| 238 | if (outline_methods.size() == all_method_count) { |
| 239 | return new LiteralExpression("DESCRIPTOR"); |
| 240 | } |
| 241 | } |
Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 244 | // When not outlining, store the descriptor literal into a local variable, in |
| 245 | // an effort to save const-string instructions in each switch case. |
Andreas Gampe | 7fab0d1 | 2017-11-22 17:50:17 -0800 | [diff] [blame] | 246 | if (transact_descriptor == nullptr) { |
| 247 | transact_descriptor = new Variable(types->StringType(), "descriptor"); |
| 248 | transact_statements->Add( |
| 249 | new VariableDeclaration(transact_descriptor, |
| 250 | new LiteralExpression("DESCRIPTOR"))); |
| 251 | } |
| 252 | return transact_descriptor; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 253 | } |
| 254 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 255 | void StubClass::make_as_interface(const InterfaceType* interfaceType, |
| 256 | JavaTypeNamespace* types) { |
| 257 | Variable* obj = new Variable(types->IBinderType(), "obj"); |
| 258 | |
| 259 | Method* m = new Method; |
| 260 | m->comment = "/**\n * Cast an IBinder object into an "; |
Christopher Wiley | d21bfee | 2016-01-29 15:11:38 -0800 | [diff] [blame] | 261 | m->comment += interfaceType->JavaType(); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 262 | m->comment += " interface,\n"; |
| 263 | m->comment += " * generating a proxy if needed.\n */"; |
| 264 | m->modifiers = PUBLIC | STATIC; |
| 265 | m->returnType = interfaceType; |
| 266 | m->name = "asInterface"; |
| 267 | m->parameters.push_back(obj); |
| 268 | m->statements = new StatementBlock; |
| 269 | |
| 270 | IfStatement* ifstatement = new IfStatement(); |
| 271 | ifstatement->expression = new Comparison(obj, "==", NULL_VALUE); |
| 272 | ifstatement->statements = new StatementBlock; |
| 273 | ifstatement->statements->Add(new ReturnStatement(NULL_VALUE)); |
| 274 | m->statements->Add(ifstatement); |
| 275 | |
| 276 | // IInterface iin = obj.queryLocalInterface(DESCRIPTOR) |
| 277 | MethodCall* queryLocalInterface = new MethodCall(obj, "queryLocalInterface"); |
| 278 | queryLocalInterface->arguments.push_back(new LiteralExpression("DESCRIPTOR")); |
| 279 | IInterfaceType* iinType = new IInterfaceType(types); |
| 280 | Variable* iin = new Variable(iinType, "iin"); |
| 281 | VariableDeclaration* iinVd = |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 282 | new VariableDeclaration(iin, queryLocalInterface, nullptr); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 283 | m->statements->Add(iinVd); |
| 284 | |
| 285 | // Ensure the instance type of the local object is as expected. |
| 286 | // One scenario where this is needed is if another package (with a |
| 287 | // different class loader) runs in the same process as the service. |
| 288 | |
| 289 | // if (iin != null && iin instanceof <interfaceType>) return (<interfaceType>) |
| 290 | // iin; |
| 291 | Comparison* iinNotNull = new Comparison(iin, "!=", NULL_VALUE); |
| 292 | Comparison* instOfCheck = |
| 293 | new Comparison(iin, " instanceof ", |
Christopher Wiley | d21bfee | 2016-01-29 15:11:38 -0800 | [diff] [blame] | 294 | new LiteralExpression(interfaceType->JavaType())); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 295 | IfStatement* instOfStatement = new IfStatement(); |
| 296 | instOfStatement->expression = new Comparison(iinNotNull, "&&", instOfCheck); |
| 297 | instOfStatement->statements = new StatementBlock; |
| 298 | instOfStatement->statements->Add( |
| 299 | new ReturnStatement(new Cast(interfaceType, iin))); |
| 300 | m->statements->Add(instOfStatement); |
| 301 | |
| 302 | NewExpression* ne = new NewExpression(interfaceType->GetProxy()); |
| 303 | ne->arguments.push_back(obj); |
| 304 | m->statements->Add(new ReturnStatement(ne)); |
| 305 | |
| 306 | this->elements.push_back(m); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 309 | // ================================================= |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 310 | class ProxyClass : public Class { |
| 311 | public: |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 312 | ProxyClass(const JavaTypeNamespace* types, const Type* type, const InterfaceType* interfaceType, |
| 313 | const Options& options); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 314 | virtual ~ProxyClass(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 315 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 316 | Variable* mRemote; |
| 317 | bool mOneWay; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 318 | }; |
| 319 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 320 | ProxyClass::ProxyClass(const JavaTypeNamespace* types, const Type* type, |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 321 | const InterfaceType* interfaceType, const Options& options) |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 322 | : Class() { |
| 323 | this->modifiers = PRIVATE | STATIC; |
| 324 | this->what = Class::CLASS; |
| 325 | this->type = type; |
| 326 | this->interfaces.push_back(interfaceType); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 327 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 328 | mOneWay = interfaceType->OneWay(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 329 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 330 | // IBinder mRemote |
| 331 | mRemote = new Variable(types->IBinderType(), "mRemote"); |
| 332 | this->elements.push_back(new Field(PRIVATE, mRemote)); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 333 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 334 | // Proxy() |
| 335 | Variable* remote = new Variable(types->IBinderType(), "remote"); |
| 336 | Method* ctor = new Method; |
| 337 | ctor->name = "Proxy"; |
| 338 | ctor->statements = new StatementBlock; |
| 339 | ctor->parameters.push_back(remote); |
| 340 | ctor->statements->Add(new Assignment(mRemote, remote)); |
| 341 | this->elements.push_back(ctor); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 342 | |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 343 | if (options.Version() > 0) { |
| 344 | std::ostringstream code; |
| 345 | code << "private int mCachedVersion = -1;\n"; |
| 346 | this->elements.emplace_back(new LiteralClassElement(code.str())); |
| 347 | } |
| 348 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 349 | // IBinder asBinder() |
| 350 | Method* asBinder = new Method; |
| 351 | asBinder->modifiers = PUBLIC | OVERRIDE; |
| 352 | asBinder->returnType = types->IBinderType(); |
| 353 | asBinder->name = "asBinder"; |
| 354 | asBinder->statements = new StatementBlock; |
| 355 | asBinder->statements->Add(new ReturnStatement(mRemote)); |
| 356 | this->elements.push_back(asBinder); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 357 | } |
| 358 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 359 | ProxyClass::~ProxyClass() {} |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 360 | |
| 361 | // ================================================= |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 362 | static void generate_new_array(const Type* t, StatementBlock* addTo, |
| 363 | Variable* v, Variable* parcel, |
| 364 | JavaTypeNamespace* types) { |
| 365 | Variable* len = new Variable(types->IntType(), v->name + "_length"); |
| 366 | addTo->Add(new VariableDeclaration(len, new MethodCall(parcel, "readInt"))); |
| 367 | IfStatement* lencheck = new IfStatement(); |
| 368 | lencheck->expression = new Comparison(len, "<", new LiteralExpression("0")); |
| 369 | lencheck->statements->Add(new Assignment(v, NULL_VALUE)); |
| 370 | lencheck->elseif = new IfStatement(); |
| 371 | lencheck->elseif->statements->Add( |
| 372 | new Assignment(v, new NewArrayExpression(t, len))); |
| 373 | addTo->Add(lencheck); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 376 | static void generate_write_to_parcel(const AidlTypeSpecifier& type, StatementBlock* addTo, |
| 377 | Variable* v, Variable* parcel, bool is_return_value, |
| 378 | const AidlTypenames& typenames) { |
| 379 | string code; |
| 380 | CodeWriterPtr writer = CodeWriter::ForString(&code); |
| 381 | CodeGeneratorContext context{ |
| 382 | .writer = *(writer.get()), |
| 383 | .typenames = typenames, |
| 384 | .type = type, |
| 385 | .var = v->name, |
| 386 | .parcel = parcel->name, |
| 387 | .is_return_value = is_return_value, |
| 388 | }; |
| 389 | WriteToParcelFor(context); |
| 390 | writer->Close(); |
| 391 | addTo->Add(new LiteralStatement(code)); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 392 | } |
| 393 | |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 394 | static void generate_int_constant(Class* interface, const std::string& name, |
| 395 | const std::string& value) { |
| 396 | IntConstant* decl = new IntConstant(name, value); |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 397 | interface->elements.push_back(decl); |
| 398 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 399 | |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 400 | static void generate_string_constant(Class* interface, const std::string& name, |
| 401 | const std::string& value) { |
| 402 | StringConstant* decl = new StringConstant(name, value); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 403 | interface->elements.push_back(decl); |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 404 | } |
| 405 | |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 406 | static std::unique_ptr<Method> generate_interface_method( |
| 407 | const AidlMethod& method, JavaTypeNamespace* types) { |
| 408 | std::unique_ptr<Method> decl(new Method); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 409 | decl->comment = method.GetComments(); |
| 410 | decl->modifiers = PUBLIC; |
| 411 | decl->returnType = method.GetType().GetLanguageType<Type>(); |
| 412 | decl->returnTypeDimension = method.GetType().IsArray() ? 1 : 0; |
| 413 | decl->name = method.GetName(); |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame^] | 414 | decl->annotations = generate_java_annotations(method.GetType()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 415 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 416 | for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) { |
| 417 | decl->parameters.push_back( |
| 418 | new Variable(arg->GetType().GetLanguageType<Type>(), arg->GetName(), |
| 419 | arg->GetType().IsArray() ? 1 : 0)); |
| 420 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 421 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 422 | decl->exceptions.push_back(types->RemoteExceptionType()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 423 | |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 424 | return decl; |
| 425 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 426 | |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 427 | static void generate_stub_code(const AidlInterface& iface, const AidlMethod& method, bool oneway, |
| 428 | Variable* transact_data, Variable* transact_reply, |
| 429 | JavaTypeNamespace* types, StatementBlock* statements, |
| 430 | StubClass* stubClass, const Options& options) { |
Martijn Coenen | f1b5078 | 2018-02-21 21:06:23 +0100 | [diff] [blame] | 431 | TryStatement* tryStatement = nullptr; |
| 432 | FinallyStatement* finallyStatement = nullptr; |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 433 | MethodCall* realCall = new MethodCall(THIS_VALUE, method.GetName()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 434 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 435 | // interface token validation is the very first thing we do |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 436 | statements->Add(new MethodCall(transact_data, |
| 437 | "enforceInterface", 1, |
Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 438 | stubClass->get_transact_descriptor(types, |
| 439 | &method))); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 440 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 441 | // args |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 442 | VariableFactory stubArgs("_arg"); |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 443 | { |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 444 | // keep this across different args in order to create the classloader |
| 445 | // at most once. |
| 446 | bool is_classloader_created = false; |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 447 | for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) { |
| 448 | const Type* t = arg->GetType().GetLanguageType<Type>(); |
| 449 | Variable* v = stubArgs.Get(t); |
| 450 | v->dimension = arg->GetType().IsArray() ? 1 : 0; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 451 | |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 452 | statements->Add(new VariableDeclaration(v)); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 453 | |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 454 | if (arg->GetDirection() & AidlArgument::IN_DIR) { |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 455 | string code; |
| 456 | CodeWriterPtr writer = CodeWriter::ForString(&code); |
| 457 | CodeGeneratorContext context{.writer = *(writer.get()), |
| 458 | .typenames = types->typenames_, |
| 459 | .type = arg->GetType(), |
| 460 | .var = v->name, |
| 461 | .parcel = transact_data->name, |
| 462 | .is_classloader_created = &is_classloader_created}; |
| 463 | CreateFromParcelFor(context); |
| 464 | writer->Close(); |
| 465 | statements->Add(new LiteralStatement(code)); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 466 | } else { |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 467 | if (!arg->GetType().IsArray()) { |
| 468 | statements->Add(new Assignment(v, new NewExpression(v->type))); |
| 469 | } else { |
| 470 | generate_new_array(v->type, |
| 471 | statements, |
| 472 | v, |
| 473 | transact_data, |
| 474 | types); |
| 475 | } |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 476 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 477 | |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 478 | realCall->arguments.push_back(v); |
| 479 | } |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 480 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 481 | |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 482 | if (options.GenTraces()) { |
Martijn Coenen | f1b5078 | 2018-02-21 21:06:23 +0100 | [diff] [blame] | 483 | // try and finally, but only when generating trace code |
| 484 | tryStatement = new TryStatement(); |
| 485 | finallyStatement = new FinallyStatement(); |
| 486 | |
| 487 | tryStatement->statements->Add(new MethodCall( |
| 488 | new LiteralExpression("android.os.Trace"), "traceBegin", 2, |
| 489 | new LiteralExpression("android.os.Trace.TRACE_TAG_AIDL"), |
| 490 | new StringLiteralExpression(iface.GetName() + "::" |
| 491 | + method.GetName() + "::server"))); |
| 492 | |
| 493 | finallyStatement->statements->Add(new MethodCall( |
| 494 | new LiteralExpression("android.os.Trace"), "traceEnd", 1, |
| 495 | new LiteralExpression("android.os.Trace.TRACE_TAG_AIDL"))); |
| 496 | } |
| 497 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 498 | // the real call |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 499 | if (method.GetType().GetName() == "void") { |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 500 | if (options.GenTraces()) { |
Martijn Coenen | f1b5078 | 2018-02-21 21:06:23 +0100 | [diff] [blame] | 501 | statements->Add(tryStatement); |
| 502 | tryStatement->statements->Add(realCall); |
| 503 | statements->Add(finallyStatement); |
| 504 | } else { |
| 505 | statements->Add(realCall); |
| 506 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 507 | |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 508 | if (!oneway) { |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 509 | // report that there were no exceptions |
| 510 | MethodCall* ex = |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 511 | new MethodCall(transact_reply, "writeNoException", 0); |
| 512 | statements->Add(ex); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 513 | } |
| 514 | } else { |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 515 | Variable* _result = |
| 516 | new Variable(method.GetType().GetLanguageType<Type>(), |
| 517 | "_result", |
| 518 | method.GetType().IsArray() ? 1 : 0); |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 519 | if (options.GenTraces()) { |
Martijn Coenen | f1b5078 | 2018-02-21 21:06:23 +0100 | [diff] [blame] | 520 | statements->Add(new VariableDeclaration(_result)); |
| 521 | statements->Add(tryStatement); |
| 522 | tryStatement->statements->Add(new Assignment(_result, realCall)); |
| 523 | statements->Add(finallyStatement); |
| 524 | } else { |
| 525 | statements->Add(new VariableDeclaration(_result, realCall)); |
| 526 | } |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 527 | |
| 528 | if (!oneway) { |
| 529 | // report that there were no exceptions |
| 530 | MethodCall* ex = |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 531 | new MethodCall(transact_reply, "writeNoException", 0); |
| 532 | statements->Add(ex); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 533 | } |
| 534 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 535 | // marshall the return value |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 536 | generate_write_to_parcel(method.GetType(), statements, _result, transact_reply, true, |
| 537 | types->typenames_); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | // out parameters |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 541 | int i = 0; |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 542 | for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) { |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 543 | Variable* v = stubArgs.Get(i++); |
| 544 | |
| 545 | if (arg->GetDirection() & AidlArgument::OUT_DIR) { |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 546 | generate_write_to_parcel(arg->GetType(), statements, v, transact_reply, true, |
| 547 | types->typenames_); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 548 | } |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 549 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 550 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 551 | // return true |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 552 | statements->Add(new ReturnStatement(TRUE_VALUE)); |
| 553 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 554 | |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 555 | static void generate_stub_case(const AidlInterface& iface, const AidlMethod& method, |
| 556 | const std::string& transactCodeName, bool oneway, |
| 557 | StubClass* stubClass, JavaTypeNamespace* types, |
| 558 | const Options& options) { |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 559 | Case* c = new Case(transactCodeName); |
| 560 | |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 561 | generate_stub_code(iface, method, oneway, stubClass->transact_data, stubClass->transact_reply, |
| 562 | types, c->statements, stubClass, options); |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 563 | |
| 564 | stubClass->transact_switch->cases.push_back(c); |
| 565 | } |
| 566 | |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 567 | static void generate_stub_case_outline(const AidlInterface& iface, const AidlMethod& method, |
| 568 | const std::string& transactCodeName, bool oneway, |
| 569 | StubClass* stubClass, JavaTypeNamespace* types, |
| 570 | const Options& options) { |
Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 571 | std::string outline_name = "onTransact$" + method.GetName() + "$"; |
| 572 | // Generate an "outlined" method with the actual code. |
| 573 | { |
| 574 | Variable* transact_data = new Variable(types->ParcelType(), "data"); |
| 575 | Variable* transact_reply = new Variable(types->ParcelType(), "reply"); |
| 576 | Method* onTransact_case = new Method; |
| 577 | onTransact_case->modifiers = PRIVATE; |
| 578 | onTransact_case->returnType = types->BoolType(); |
| 579 | onTransact_case->name = outline_name; |
| 580 | onTransact_case->parameters.push_back(transact_data); |
| 581 | onTransact_case->parameters.push_back(transact_reply); |
| 582 | onTransact_case->statements = new StatementBlock; |
| 583 | onTransact_case->exceptions.push_back(types->RemoteExceptionType()); |
| 584 | stubClass->elements.push_back(onTransact_case); |
| 585 | |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 586 | generate_stub_code(iface, method, oneway, transact_data, transact_reply, types, |
| 587 | onTransact_case->statements, stubClass, options); |
Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | // Generate the case dispatch. |
| 591 | { |
| 592 | Case* c = new Case(transactCodeName); |
| 593 | |
| 594 | MethodCall* helper_call = new MethodCall(THIS_VALUE, |
| 595 | outline_name, |
| 596 | 2, |
| 597 | stubClass->transact_data, |
| 598 | stubClass->transact_reply); |
| 599 | c->statements->Add(new ReturnStatement(helper_call)); |
| 600 | |
| 601 | stubClass->transact_switch->cases.push_back(c); |
| 602 | } |
| 603 | } |
| 604 | |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 605 | static std::unique_ptr<Method> generate_proxy_method( |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 606 | const AidlInterface& iface, const AidlMethod& method, const std::string& transactCodeName, |
| 607 | bool oneway, ProxyClass* proxyClass, JavaTypeNamespace* types, const Options& options) { |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 608 | std::unique_ptr<Method> proxy(new Method); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 609 | proxy->comment = method.GetComments(); |
| 610 | proxy->modifiers = PUBLIC | OVERRIDE; |
| 611 | proxy->returnType = method.GetType().GetLanguageType<Type>(); |
| 612 | proxy->returnTypeDimension = method.GetType().IsArray() ? 1 : 0; |
| 613 | proxy->name = method.GetName(); |
| 614 | proxy->statements = new StatementBlock; |
| 615 | for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) { |
| 616 | proxy->parameters.push_back( |
| 617 | new Variable(arg->GetType().GetLanguageType<Type>(), arg->GetName(), |
| 618 | arg->GetType().IsArray() ? 1 : 0)); |
| 619 | } |
| 620 | proxy->exceptions.push_back(types->RemoteExceptionType()); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 621 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 622 | // the parcels |
| 623 | Variable* _data = new Variable(types->ParcelType(), "_data"); |
| 624 | proxy->statements->Add(new VariableDeclaration( |
| 625 | _data, new MethodCall(types->ParcelType(), "obtain"))); |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 626 | Variable* _reply = nullptr; |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 627 | if (!oneway) { |
| 628 | _reply = new Variable(types->ParcelType(), "_reply"); |
| 629 | proxy->statements->Add(new VariableDeclaration( |
| 630 | _reply, new MethodCall(types->ParcelType(), "obtain"))); |
| 631 | } |
| 632 | |
| 633 | // the return value |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 634 | Variable* _result = nullptr; |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 635 | if (method.GetType().GetName() != "void") { |
| 636 | _result = new Variable(proxy->returnType, "_result", |
| 637 | method.GetType().IsArray() ? 1 : 0); |
| 638 | proxy->statements->Add(new VariableDeclaration(_result)); |
| 639 | } |
| 640 | |
| 641 | // try and finally |
| 642 | TryStatement* tryStatement = new TryStatement(); |
| 643 | proxy->statements->Add(tryStatement); |
| 644 | FinallyStatement* finallyStatement = new FinallyStatement(); |
| 645 | proxy->statements->Add(finallyStatement); |
| 646 | |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 647 | if (options.GenTraces()) { |
Martijn Coenen | f1b5078 | 2018-02-21 21:06:23 +0100 | [diff] [blame] | 648 | tryStatement->statements->Add(new MethodCall( |
| 649 | new LiteralExpression("android.os.Trace"), "traceBegin", 2, |
| 650 | new LiteralExpression("android.os.Trace.TRACE_TAG_AIDL"), |
| 651 | new StringLiteralExpression(iface.GetName() + "::" + |
| 652 | method.GetName() + "::client"))); |
| 653 | } |
| 654 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 655 | // the interface identifier token: the DESCRIPTOR constant, marshalled as a |
| 656 | // string |
| 657 | tryStatement->statements->Add(new MethodCall( |
| 658 | _data, "writeInterfaceToken", 1, new LiteralExpression("DESCRIPTOR"))); |
| 659 | |
| 660 | // the parameters |
| 661 | for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) { |
| 662 | const Type* t = arg->GetType().GetLanguageType<Type>(); |
| 663 | Variable* v = |
| 664 | new Variable(t, arg->GetName(), arg->GetType().IsArray() ? 1 : 0); |
| 665 | AidlArgument::Direction dir = arg->GetDirection(); |
| 666 | if (dir == AidlArgument::OUT_DIR && arg->GetType().IsArray()) { |
| 667 | IfStatement* checklen = new IfStatement(); |
| 668 | checklen->expression = new Comparison(v, "==", NULL_VALUE); |
| 669 | checklen->statements->Add( |
| 670 | new MethodCall(_data, "writeInt", 1, new LiteralExpression("-1"))); |
| 671 | checklen->elseif = new IfStatement(); |
| 672 | checklen->elseif->statements->Add( |
| 673 | new MethodCall(_data, "writeInt", 1, new FieldVariable(v, "length"))); |
| 674 | tryStatement->statements->Add(checklen); |
| 675 | } else if (dir & AidlArgument::IN_DIR) { |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 676 | generate_write_to_parcel(arg->GetType(), tryStatement->statements, v, _data, false, |
| 677 | types->typenames_); |
Christopher Wiley | 5f84852 | 2016-03-01 16:29:41 -0800 | [diff] [blame] | 678 | } else { |
| 679 | delete v; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 680 | } |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 681 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 682 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 683 | // the transact call |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 684 | unique_ptr<MethodCall> call(new MethodCall( |
| 685 | proxyClass->mRemote, "transact", 4, new LiteralExpression("Stub." + transactCodeName), _data, |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 686 | _reply ? _reply : NULL_VALUE, |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 687 | new LiteralExpression(oneway ? "android.os.IBinder.FLAG_ONEWAY" : "0"))); |
| 688 | unique_ptr<Variable> _status(new Variable(types->BoolType(), "_status")); |
| 689 | tryStatement->statements->Add(new VariableDeclaration(_status.release(), call.release())); |
| 690 | |
| 691 | // If the transaction returns false, which means UNKNOWN_TRANSACTION, fall |
| 692 | // back to the local method in the default impl, if set before. |
| 693 | vector<string> arg_names; |
| 694 | for (const auto& arg : method.GetArguments()) { |
| 695 | arg_names.emplace_back(arg->GetName()); |
| 696 | } |
| 697 | bool has_return_type = method.GetType().GetName() != "void"; |
| 698 | tryStatement->statements->Add(new LiteralStatement( |
| 699 | android::base::StringPrintf(has_return_type ? "if (!_status && getDefaultImpl() != null) {\n" |
| 700 | " return getDefaultImpl().%s(%s);\n" |
| 701 | "}\n" |
| 702 | : "if (!_status && getDefaultImpl() != null) {\n" |
| 703 | " getDefaultImpl().%s(%s);\n" |
| 704 | " return;\n" |
| 705 | "}\n", |
| 706 | method.GetName().c_str(), Join(arg_names, ", ").c_str()))); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 707 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 708 | // throw back exceptions. |
| 709 | if (_reply) { |
| 710 | MethodCall* ex = new MethodCall(_reply, "readException", 0); |
| 711 | tryStatement->statements->Add(ex); |
| 712 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 713 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 714 | // returning and cleanup |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 715 | if (_reply != nullptr) { |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 716 | // keep this across return value and arguments in order to create the |
| 717 | // classloader at most once. |
| 718 | bool is_classloader_created = false; |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 719 | if (_result != nullptr) { |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 720 | string code; |
| 721 | CodeWriterPtr writer = CodeWriter::ForString(&code); |
| 722 | CodeGeneratorContext context{.writer = *(writer.get()), |
| 723 | .typenames = types->typenames_, |
| 724 | .type = method.GetType(), |
| 725 | .var = _result->name, |
| 726 | .parcel = _reply->name, |
| 727 | .is_classloader_created = &is_classloader_created}; |
| 728 | CreateFromParcelFor(context); |
| 729 | writer->Close(); |
| 730 | tryStatement->statements->Add(new LiteralStatement(code)); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 731 | } |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 732 | |
| 733 | // the out/inout parameters |
| 734 | for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) { |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 735 | if (arg->GetDirection() & AidlArgument::OUT_DIR) { |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 736 | string code; |
| 737 | CodeWriterPtr writer = CodeWriter::ForString(&code); |
| 738 | CodeGeneratorContext context{.writer = *(writer.get()), |
| 739 | .typenames = types->typenames_, |
| 740 | .type = arg->GetType(), |
| 741 | .var = arg->GetName(), |
| 742 | .parcel = _reply->name, |
| 743 | .is_classloader_created = &is_classloader_created}; |
| 744 | ReadFromParcelFor(context); |
| 745 | writer->Close(); |
| 746 | tryStatement->statements->Add(new LiteralStatement(code)); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | |
| 750 | finallyStatement->statements->Add(new MethodCall(_reply, "recycle")); |
| 751 | } |
| 752 | finallyStatement->statements->Add(new MethodCall(_data, "recycle")); |
| 753 | |
Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 754 | if (options.GenTraces()) { |
Martijn Coenen | f1b5078 | 2018-02-21 21:06:23 +0100 | [diff] [blame] | 755 | finallyStatement->statements->Add(new MethodCall( |
| 756 | new LiteralExpression("android.os.Trace"), "traceEnd", 1, |
| 757 | new LiteralExpression("android.os.Trace.TRACE_TAG_AIDL"))); |
| 758 | } |
| 759 | |
Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 760 | if (_result != nullptr) { |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 761 | proxy->statements->Add(new ReturnStatement(_result)); |
| 762 | } |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 763 | |
| 764 | return proxy; |
| 765 | } |
| 766 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 767 | static void generate_methods(const AidlInterface& iface, const AidlMethod& method, Class* interface, |
| 768 | StubClass* stubClass, ProxyClass* proxyClass, int index, |
| 769 | JavaTypeNamespace* types, const Options& options) { |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 770 | const bool oneway = proxyClass->mOneWay || method.IsOneway(); |
| 771 | |
| 772 | // == the TRANSACT_ constant ============================================= |
| 773 | string transactCodeName = "TRANSACTION_"; |
| 774 | transactCodeName += method.GetName(); |
| 775 | |
| 776 | Field* transactCode = new Field( |
| 777 | STATIC | FINAL, new Variable(types->IntType(), transactCodeName)); |
| 778 | transactCode->value = |
| 779 | StringPrintf("(android.os.IBinder.FIRST_CALL_TRANSACTION + %d)", index); |
| 780 | stubClass->elements.push_back(transactCode); |
| 781 | |
Olivier Gaillard | 1140140 | 2018-07-05 15:01:34 +0100 | [diff] [blame] | 782 | // getTransactionName |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 783 | if (options.GenTransactionNames()) { |
Olivier Gaillard | 1140140 | 2018-07-05 15:01:34 +0100 | [diff] [blame] | 784 | Case* c = new Case(transactCodeName); |
| 785 | c->statements->Add(new ReturnStatement(new StringLiteralExpression(method.GetName()))); |
| 786 | stubClass->code_to_method_name_switch->cases.push_back(c); |
| 787 | } |
| 788 | |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 789 | // == the declaration in the interface =================================== |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 790 | ClassElement* decl; |
| 791 | if (method.IsUserDefined()) { |
| 792 | decl = generate_interface_method(method, types).release(); |
| 793 | } else { |
| 794 | if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) { |
| 795 | std::ostringstream code; |
| 796 | code << "public int " << kGetInterfaceVersion << "() " |
| 797 | << "throws android.os.RemoteException;\n"; |
| 798 | decl = new LiteralClassElement(code.str()); |
| 799 | } |
| 800 | } |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 801 | interface->elements.push_back(decl); |
| 802 | |
| 803 | // == the stub method ==================================================== |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 804 | if (method.IsUserDefined()) { |
| 805 | bool outline_stub = |
| 806 | stubClass->transact_outline && stubClass->outline_methods.count(&method) != 0; |
| 807 | if (outline_stub) { |
| 808 | generate_stub_case_outline(iface, method, transactCodeName, oneway, stubClass, types, |
| 809 | options); |
| 810 | } else { |
| 811 | generate_stub_case(iface, method, transactCodeName, oneway, stubClass, types, options); |
| 812 | } |
Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 813 | } else { |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 814 | if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) { |
| 815 | Case* c = new Case(transactCodeName); |
| 816 | std::ostringstream code; |
| 817 | code << "reply.writeInt(" << kGetInterfaceVersion << "());\n" |
| 818 | << "return true;\n"; |
| 819 | c->statements->Add(new LiteralStatement(code.str())); |
| 820 | stubClass->transact_switch->cases.push_back(c); |
| 821 | } |
Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 822 | } |
Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 823 | |
| 824 | // == the proxy method =================================================== |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 825 | ClassElement* proxy = nullptr; |
| 826 | if (method.IsUserDefined()) { |
| 827 | proxy = |
| 828 | generate_proxy_method(iface, method, transactCodeName, oneway, proxyClass, types, options) |
| 829 | .release(); |
| 830 | |
| 831 | } else { |
| 832 | if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) { |
| 833 | std::ostringstream code; |
| 834 | code << "@Override\n" |
| 835 | << "public int " << kGetInterfaceVersion << "()" |
| 836 | << " throws " |
| 837 | << "android.os.RemoteException {\n" |
| 838 | << " if (mCachedVersion == -1) {\n" |
| 839 | << " android.os.Parcel data = android.os.Parcel.obtain();\n" |
| 840 | << " android.os.Parcel reply = android.os.Parcel.obtain();\n" |
| 841 | << " try {\n" |
| 842 | << " mRemote.transact(Stub." << transactCodeName << ", " |
| 843 | << "data, reply, 0);\n" |
| 844 | << " mCachedVersion = reply.readInt();\n" |
| 845 | << " } finally {\n" |
| 846 | << " reply.recycle();\n" |
| 847 | << " data.recycle();\n" |
| 848 | << " }\n" |
| 849 | << " }\n" |
| 850 | << " return mCachedVersion;\n" |
| 851 | << "}\n"; |
| 852 | proxy = new LiteralClassElement(code.str()); |
| 853 | } |
| 854 | } |
| 855 | if (proxy != nullptr) { |
| 856 | proxyClass->elements.push_back(proxy); |
| 857 | } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 858 | } |
| 859 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 860 | static void generate_interface_descriptors(StubClass* stub, ProxyClass* proxy, |
| 861 | const JavaTypeNamespace* types) { |
| 862 | // the interface descriptor transaction handler |
| 863 | Case* c = new Case("INTERFACE_TRANSACTION"); |
| 864 | c->statements->Add(new MethodCall(stub->transact_reply, "writeString", 1, |
Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 865 | stub->get_transact_descriptor(types, |
| 866 | nullptr))); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 867 | c->statements->Add(new ReturnStatement(TRUE_VALUE)); |
| 868 | stub->transact_switch->cases.push_back(c); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 869 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 870 | // and the proxy-side method returning the descriptor directly |
| 871 | Method* getDesc = new Method; |
| 872 | getDesc->modifiers = PUBLIC; |
| 873 | getDesc->returnType = types->StringType(); |
| 874 | getDesc->returnTypeDimension = 0; |
| 875 | getDesc->name = "getInterfaceDescriptor"; |
| 876 | getDesc->statements = new StatementBlock; |
| 877 | getDesc->statements->Add( |
| 878 | new ReturnStatement(new LiteralExpression("DESCRIPTOR"))); |
| 879 | proxy->elements.push_back(getDesc); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 880 | } |
| 881 | |
Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 882 | // Check whether (some) methods in this interface should be "outlined," that |
| 883 | // is, have specific onTransact methods for certain cases. Set up StubClass |
| 884 | // metadata accordingly. |
| 885 | // |
| 886 | // Outlining will be enabled if the interface has more than outline_threshold |
| 887 | // methods. In that case, the methods are sorted by number of arguments |
| 888 | // (so that more "complex" methods come later), and the first non_outline_count |
| 889 | // number of methods not outlined (are kept in the onTransact() method). |
| 890 | // |
| 891 | // Requirements: non_outline_count <= outline_threshold. |
| 892 | static void compute_outline_methods(const AidlInterface* iface, |
| 893 | StubClass* stub, |
| 894 | size_t outline_threshold, |
| 895 | size_t non_outline_count) { |
| 896 | CHECK_LE(non_outline_count, outline_threshold); |
| 897 | // We'll outline (create sub methods) if there are more than min_methods |
| 898 | // cases. |
| 899 | stub->transact_outline = iface->GetMethods().size() > outline_threshold; |
| 900 | if (stub->transact_outline) { |
| 901 | stub->all_method_count = iface->GetMethods().size(); |
| 902 | std::vector<const AidlMethod*> methods; |
| 903 | methods.reserve(iface->GetMethods().size()); |
| 904 | for (const std::unique_ptr<AidlMethod>& ptr : iface->GetMethods()) { |
| 905 | methods.push_back(ptr.get()); |
| 906 | } |
| 907 | |
| 908 | std::stable_sort( |
| 909 | methods.begin(), |
| 910 | methods.end(), |
| 911 | [](const AidlMethod* m1, const AidlMethod* m2) { |
| 912 | return m1->GetArguments().size() < m2->GetArguments().size(); |
| 913 | }); |
| 914 | |
| 915 | stub->outline_methods.insert(methods.begin() + non_outline_count, |
| 916 | methods.end()); |
| 917 | } |
| 918 | } |
| 919 | |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 920 | static unique_ptr<ClassElement> generate_default_impl_method(const AidlMethod& method) { |
| 921 | unique_ptr<Method> default_method(new Method); |
| 922 | default_method->comment = method.GetComments(); |
| 923 | default_method->modifiers = PUBLIC | OVERRIDE; |
| 924 | default_method->returnType = method.GetType().GetLanguageType<Type>(); |
| 925 | default_method->returnTypeDimension = method.GetType().IsArray() ? 1 : 0; |
| 926 | default_method->name = method.GetName(); |
| 927 | default_method->statements = new StatementBlock; |
| 928 | for (const auto& arg : method.GetArguments()) { |
| 929 | default_method->parameters.push_back(new Variable( |
| 930 | arg->GetType().GetLanguageType<Type>(), arg->GetName(), arg->GetType().IsArray() ? 1 : 0)); |
| 931 | } |
| 932 | default_method->exceptions.push_back( |
| 933 | method.GetType().GetLanguageType<Type>()->GetTypeNamespace()->RemoteExceptionType()); |
| 934 | |
| 935 | if (method.GetType().GetName() != "void") { |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 936 | const string& defaultValue = DefaultJavaValueOf(method.GetType()); |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 937 | default_method->statements->Add( |
| 938 | new LiteralStatement(StringPrintf("return %s;\n", defaultValue.c_str()))); |
| 939 | } |
| 940 | return default_method; |
| 941 | } |
| 942 | |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 943 | static unique_ptr<Class> generate_default_impl_class(const AidlInterface& iface, |
| 944 | const Options& options) { |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 945 | unique_ptr<Class> default_class(new Class); |
| 946 | default_class->comment = "/** Default implementation for " + iface.GetName() + ". */"; |
| 947 | default_class->modifiers = PUBLIC | STATIC; |
| 948 | default_class->what = Class::CLASS; |
| 949 | default_class->type = iface.GetLanguageType<InterfaceType>()->GetDefaultImpl(); |
| 950 | default_class->interfaces.emplace_back(iface.GetLanguageType<InterfaceType>()); |
| 951 | |
| 952 | for (const auto& m : iface.GetMethods()) { |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 953 | if (m->IsUserDefined()) { |
| 954 | default_class->elements.emplace_back(generate_default_impl_method(*(m.get())).release()); |
| 955 | } else { |
| 956 | if (m->GetName() == kGetInterfaceVersion && options.Version() > 0) { |
Jiyong Park | a2f9ca3 | 2018-08-01 21:04:11 +0900 | [diff] [blame] | 957 | // This is called only when the remote side is not implementing this |
| 958 | // method, which is impossible in normal case, because this method is |
| 959 | // automatically declared in the interface class and not implementing |
| 960 | // it in the remote side is causing compilation error. But if the remote |
| 961 | // side somehow managed to not implement it, that's an error and we |
| 962 | // report the case by returning -1 here. |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 963 | std::ostringstream code; |
| 964 | code << "@Override\n" |
| 965 | << "public int " << kGetInterfaceVersion << "() {\n" |
Jiyong Park | a2f9ca3 | 2018-08-01 21:04:11 +0900 | [diff] [blame] | 966 | << " return -1;\n" |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 967 | << "}\n"; |
| 968 | default_class->elements.emplace_back(new LiteralClassElement(code.str())); |
| 969 | } |
| 970 | } |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | default_class->elements.emplace_back( |
| 974 | new LiteralClassElement("@Override\n" |
| 975 | "public android.os.IBinder asBinder() {\n" |
| 976 | " return null;\n" |
| 977 | "}\n")); |
| 978 | |
| 979 | return default_class; |
| 980 | } |
| 981 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 982 | Class* generate_binder_interface_class(const AidlInterface* iface, JavaTypeNamespace* types, |
| 983 | const Options& options) { |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 984 | const InterfaceType* interfaceType = iface->GetLanguageType<InterfaceType>(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 985 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 986 | // the interface class |
| 987 | Class* interface = new Class; |
| 988 | interface->comment = iface->GetComments(); |
| 989 | interface->modifiers = PUBLIC; |
| 990 | interface->what = Class::INTERFACE; |
| 991 | interface->type = interfaceType; |
| 992 | interface->interfaces.push_back(types->IInterfaceType()); |
Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame^] | 993 | interface->annotations = generate_java_annotations(*iface); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 994 | |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 995 | if (options.Version()) { |
| 996 | std::ostringstream code; |
| 997 | code << "/**\n" |
| 998 | << " * The version of this interface that the caller is built against.\n" |
| 999 | << " * This might be different from what {@link #getInterfaceVersion()\n" |
| 1000 | << " * getInterfaceVersion} returns as that is the version of the interface\n" |
| 1001 | << " * that the remote object is implementing.\n" |
| 1002 | << " */\n" |
| 1003 | << "public static final int VERSION = " << options.Version() << ";\n"; |
| 1004 | interface->elements.emplace_back(new LiteralClassElement(code.str())); |
| 1005 | } |
| 1006 | |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 1007 | // the default impl class |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1008 | Class* default_impl = generate_default_impl_class(*iface, options).release(); |
| 1009 | interface->elements.emplace_back(default_impl); |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 1010 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1011 | // the stub inner class |
| 1012 | StubClass* stub = |
Olivier Gaillard | 1140140 | 2018-07-05 15:01:34 +0100 | [diff] [blame] | 1013 | new StubClass(interfaceType->GetStub(), interfaceType, types, options); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1014 | interface->elements.push_back(stub); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1015 | |
Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 1016 | compute_outline_methods(iface, |
| 1017 | stub, |
| 1018 | options.onTransact_outline_threshold_, |
| 1019 | options.onTransact_non_outline_count_); |
Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 1020 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1021 | // the proxy inner class |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 1022 | ProxyClass* proxy = new ProxyClass(types, interfaceType->GetProxy(), interfaceType, options); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1023 | stub->elements.push_back(proxy); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1024 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1025 | // stub and proxy support for getInterfaceDescriptor() |
| 1026 | generate_interface_descriptors(stub, proxy, types); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1027 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1028 | // all the declared constants of the interface |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 1029 | for (const auto& constant : iface->GetConstantDeclarations()) { |
| 1030 | const AidlConstantValue& value = constant->GetValue(); |
| 1031 | |
| 1032 | switch (value.GetType()) { |
| 1033 | case AidlConstantValue::Type::STRING: { |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 1034 | generate_string_constant(interface, constant->GetName(), |
| 1035 | constant->ValueString(ConstantValueDecorator)); |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 1036 | break; |
| 1037 | } |
Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 1038 | case AidlConstantValue::Type::INTEGRAL: |
| 1039 | case AidlConstantValue::Type::HEXIDECIMAL: { |
Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 1040 | generate_int_constant(interface, constant->GetName(), |
| 1041 | constant->ValueString(ConstantValueDecorator)); |
Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 1042 | break; |
| 1043 | } |
| 1044 | default: { |
| 1045 | LOG(FATAL) << "Unrecognized constant type: " << static_cast<int>(value.GetType()); |
| 1046 | } |
| 1047 | } |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1048 | } |
Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 1049 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1050 | // all the declared methods of the interface |
Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 1051 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1052 | for (const auto& item : iface->GetMethods()) { |
Martijn Coenen | f1b5078 | 2018-02-21 21:06:23 +0100 | [diff] [blame] | 1053 | generate_methods(*iface, |
| 1054 | *item, |
Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 1055 | interface, |
| 1056 | stub, |
| 1057 | proxy, |
| 1058 | item->GetId(), |
Olivier Gaillard | 1140140 | 2018-07-05 15:01:34 +0100 | [diff] [blame] | 1059 | types, |
| 1060 | options); |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1061 | } |
Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 1062 | |
| 1063 | // additional static methods for the default impl set/get to the |
| 1064 | // stub class. Can't add them to the interface as the generated java files |
| 1065 | // may be compiled with Java < 1.7 where static interface method isn't |
| 1066 | // supported. |
| 1067 | // TODO(b/111417145) make this conditional depending on the Java language |
| 1068 | // version requested |
| 1069 | const string i_name = interfaceType->JavaType(); |
| 1070 | stub->elements.emplace_back(new LiteralClassElement( |
| 1071 | StringPrintf("public static boolean setDefaultImpl(%s impl) {\n" |
| 1072 | " if (Stub.Proxy.sDefaultImpl == null && impl != null) {\n" |
| 1073 | " Stub.Proxy.sDefaultImpl = impl;\n" |
| 1074 | " return true;\n" |
| 1075 | " }\n" |
| 1076 | " return false;\n" |
| 1077 | "}\n", |
| 1078 | i_name.c_str()))); |
| 1079 | stub->elements.emplace_back( |
| 1080 | new LiteralClassElement(StringPrintf("public static %s getDefaultImpl() {\n" |
| 1081 | " return Stub.Proxy.sDefaultImpl;\n" |
| 1082 | "}\n", |
| 1083 | i_name.c_str()))); |
| 1084 | |
| 1085 | // the static field is defined in the proxy class, not in the interface class |
| 1086 | // because all fields in an interface class are by default final. |
| 1087 | proxy->elements.emplace_back(new LiteralClassElement( |
| 1088 | StringPrintf("public static %s sDefaultImpl = null;\n", i_name.c_str()))); |
| 1089 | |
Andreas Gampe | a8a66fe | 2017-11-22 12:17:00 -0800 | [diff] [blame] | 1090 | stub->finish(); |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1091 | |
Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1092 | return interface; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1093 | } |
| 1094 | |
Christopher Wiley | db154a5 | 2015-09-28 16:32:25 -0700 | [diff] [blame] | 1095 | } // namespace java |
Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 1096 | } // namespace android |
| 1097 | } // namespace aidl |