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