blob: b9c7674fa17f6bd0a95eac86083a258b708cf8d7 [file] [log] [blame]
Christopher Wiley3a9da172016-01-29 11:10:49 -08001/*
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 Park309668e2018-07-28 16:55:44 +090017#include "aidl.h"
Jiyong Park1d2df7d2018-07-23 15:22:50 +090018#include "aidl_to_java.h"
Adam Lesinskiffa16862014-01-23 18:17:42 -080019#include "generate_java.h"
Jeongik Cha047c5ee2019-08-07 23:16:49 +090020#include "logging.h"
Jiyong Park1d2df7d2018-07-23 15:22:50 +090021#include "options.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070022
Adam Lesinskiffa16862014-01-23 18:17:42 -080023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
Andreas Gampee9c816e2018-03-14 09:05:48 -070027#include <algorithm>
28#include <unordered_set>
Jiyong Park1d2df7d2018-07-23 15:22:50 +090029#include <utility>
30#include <vector>
Andreas Gampee9c816e2018-03-14 09:05:48 -070031
Andreas Gampe7d7fa602017-11-22 10:50:03 -080032#include <android-base/stringprintf.h>
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -070033
Jiyong Park75e1a742018-07-04 12:31:23 +090034using android::base::Join;
Andreas Gampe7d7fa602017-11-22 10:50:03 -080035using android::base::StringPrintf;
Jiyong Park1d2df7d2018-07-23 15:22:50 +090036
Jiyong Park75e1a742018-07-04 12:31:23 +090037using std::string;
38using std::unique_ptr;
39using std::vector;
Andreas Gampe7d7fa602017-11-22 10:50:03 -080040
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070041namespace android {
42namespace aidl {
Christopher Wileydb154a52015-09-28 16:32:25 -070043namespace java {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070044
Adam Lesinskiffa16862014-01-23 18:17:42 -080045// =================================================
Jiyong Park2c44f072018-07-30 21:52:21 +090046class VariableFactory {
47 public:
48 using Variable = ::android::aidl::java::Variable;
Jiyong Park2c44f072018-07-30 21:52:21 +090049
50 explicit VariableFactory(const std::string& base) : base_(base), index_(0) {}
Daniel Norman716d3112019-09-10 13:11:56 -070051 std::shared_ptr<Variable> Get(const AidlTypeSpecifier& type, const AidlTypenames& typenames) {
52 auto v = std::make_shared<Variable>(JavaSignatureOf(type, typenames),
Steven Moreland48548e02019-09-18 15:10:22 -070053 StringPrintf("%s%d", base_.c_str(), index_));
Jiyong Park2c44f072018-07-30 21:52:21 +090054 vars_.push_back(v);
55 index_++;
56 return v;
57 }
58
Steven Moreland48548e02019-09-18 15:10:22 -070059 std::shared_ptr<Variable> Get(int index) { return vars_[index]; }
Jiyong Park2c44f072018-07-30 21:52:21 +090060
61 private:
Steven Moreland48548e02019-09-18 15:10:22 -070062 std::vector<std::shared_ptr<Variable>> vars_;
Jiyong Park2c44f072018-07-30 21:52:21 +090063 std::string base_;
64 int index_;
Jiyong Park2c44f072018-07-30 21:52:21 +090065};
66
67// =================================================
Christopher Wiley67502f12016-01-29 10:57:00 -080068class StubClass : public Class {
69 public:
Jeongik Chaa2080bf2019-06-18 16:44:29 +090070 StubClass(const AidlInterface* interfaceType, const Options& options);
Yi Kongde138912019-03-30 01:38:17 -070071 ~StubClass() override = default;
Adam Lesinskiffa16862014-01-23 18:17:42 -080072
Jiyong Parkd800fef2020-07-22 18:09:43 +090073 // non-copyable, non-movable
74 StubClass(const StubClass&) = delete;
75 StubClass(StubClass&&) = delete;
76 StubClass& operator=(const StubClass&) = delete;
77 StubClass& operator=(StubClass&&) = delete;
78
Steven Moreland48548e02019-09-18 15:10:22 -070079 std::shared_ptr<Variable> transact_code;
80 std::shared_ptr<Variable> transact_data;
81 std::shared_ptr<Variable> transact_reply;
82 std::shared_ptr<Variable> transact_flags;
83 std::shared_ptr<SwitchStatement> transact_switch;
84 std::shared_ptr<StatementBlock> transact_statements;
85 std::shared_ptr<SwitchStatement> code_to_method_name_switch;
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -070086
Andreas Gampe1b865af2017-11-22 11:31:47 -080087 // Where onTransact cases should be generated as separate methods.
88 bool transact_outline;
Andreas Gampee9c816e2018-03-14 09:05:48 -070089 // Specific methods that should be outlined when transact_outline is true.
90 std::unordered_set<const AidlMethod*> outline_methods;
91 // Number of all methods.
92 size_t all_method_count;
Andreas Gampe1b865af2017-11-22 11:31:47 -080093
Andreas Gampea8a66fe2017-11-22 12:17:00 -080094 // Finish generation. This will add a default case to the switch.
95 void finish();
96
Steven Moreland48548e02019-09-18 15:10:22 -070097 std::shared_ptr<Expression> get_transact_descriptor(const AidlMethod* method);
Andreas Gampe7fab0d12017-11-22 17:50:17 -080098
Christopher Wiley67502f12016-01-29 10:57:00 -080099 private:
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900100 void make_as_interface(const AidlInterface* interfaceType);
Christopher Wiley67502f12016-01-29 10:57:00 -0800101
Steven Moreland48548e02019-09-18 15:10:22 -0700102 std::shared_ptr<Variable> transact_descriptor;
Jiyong Park74595c12018-07-23 15:22:50 +0900103 const Options& options_;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800104};
105
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900106StubClass::StubClass(const AidlInterface* interfaceType, const Options& options)
Olivier Gaillard11401402018-07-05 15:01:34 +0100107 : Class(), options_(options) {
Andreas Gampe7fab0d12017-11-22 17:50:17 -0800108 transact_descriptor = nullptr;
Andreas Gampe1b865af2017-11-22 11:31:47 -0800109 transact_outline = false;
Andreas Gampee9c816e2018-03-14 09:05:48 -0700110 all_method_count = 0; // Will be set when outlining may be enabled.
Andreas Gampe7fab0d12017-11-22 17:50:17 -0800111
Christopher Wiley67502f12016-01-29 10:57:00 -0800112 this->comment = "/** Local-side IPC implementation stub class. */";
113 this->modifiers = PUBLIC | ABSTRACT | STATIC;
114 this->what = Class::CLASS;
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900115 this->type = interfaceType->GetCanonicalName() + ".Stub";
116 this->extends = "android.os.Binder";
117 this->interfaces.push_back(interfaceType->GetCanonicalName());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800118
Christopher Wiley67502f12016-01-29 10:57:00 -0800119 // ctor
Steven Moreland48548e02019-09-18 15:10:22 -0700120 auto ctor = std::make_shared<Method>();
Christopher Wiley67502f12016-01-29 10:57:00 -0800121 ctor->modifiers = PUBLIC;
122 ctor->comment =
123 "/** Construct the stub at attach it to the "
124 "interface. */";
125 ctor->name = "Stub";
Steven Moreland48548e02019-09-18 15:10:22 -0700126 ctor->statements = std::make_shared<StatementBlock>();
Steven Morelande2fcb8e2019-11-27 18:09:15 -0800127 if (interfaceType->IsVintfStability()) {
128 auto stability = std::make_shared<LiteralStatement>("this.markVintfStability();\n");
129 ctor->statements->Add(stability);
130 }
Steven Moreland48548e02019-09-18 15:10:22 -0700131 auto attach = std::make_shared<MethodCall>(
132 THIS_VALUE, "attachInterface",
133 std::vector<std::shared_ptr<Expression>>{THIS_VALUE,
134 std::make_shared<LiteralExpression>("DESCRIPTOR")});
Christopher Wiley67502f12016-01-29 10:57:00 -0800135 ctor->statements->Add(attach);
136 this->elements.push_back(ctor);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800137
Christopher Wiley67502f12016-01-29 10:57:00 -0800138 // asInterface
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900139 make_as_interface(interfaceType);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800140
Christopher Wiley67502f12016-01-29 10:57:00 -0800141 // asBinder
Steven Moreland48548e02019-09-18 15:10:22 -0700142 auto asBinder = std::make_shared<Method>();
Christopher Wiley67502f12016-01-29 10:57:00 -0800143 asBinder->modifiers = PUBLIC | OVERRIDE;
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900144 asBinder->returnType = "android.os.IBinder";
Christopher Wiley67502f12016-01-29 10:57:00 -0800145 asBinder->name = "asBinder";
Steven Moreland48548e02019-09-18 15:10:22 -0700146 asBinder->statements = std::make_shared<StatementBlock>();
147 asBinder->statements->Add(std::make_shared<ReturnStatement>(THIS_VALUE));
Christopher Wiley67502f12016-01-29 10:57:00 -0800148 this->elements.push_back(asBinder);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800149
Jiyong Park74595c12018-07-23 15:22:50 +0900150 if (options_.GenTransactionNames()) {
Olivier Gaillard83d7cd32018-07-30 16:20:57 +0100151 // getDefaultTransactionName
Steven Moreland48548e02019-09-18 15:10:22 -0700152 auto getDefaultTransactionName = std::make_shared<Method>();
Jiyong Parke0b28032019-04-10 03:08:41 +0900153 getDefaultTransactionName->comment = "/** @hide */";
Olivier Gaillard83d7cd32018-07-30 16:20:57 +0100154 getDefaultTransactionName->modifiers = PUBLIC | STATIC;
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900155 getDefaultTransactionName->returnType = "java.lang.String";
Olivier Gaillard83d7cd32018-07-30 16:20:57 +0100156 getDefaultTransactionName->name = "getDefaultTransactionName";
Steven Moreland48548e02019-09-18 15:10:22 -0700157 auto code = std::make_shared<Variable>("int", "transactionCode");
Olivier Gaillard83d7cd32018-07-30 16:20:57 +0100158 getDefaultTransactionName->parameters.push_back(code);
Steven Moreland48548e02019-09-18 15:10:22 -0700159 getDefaultTransactionName->statements = std::make_shared<StatementBlock>();
160 this->code_to_method_name_switch = std::make_shared<SwitchStatement>(code);
Olivier Gaillard83d7cd32018-07-30 16:20:57 +0100161 getDefaultTransactionName->statements->Add(this->code_to_method_name_switch);
162 this->elements.push_back(getDefaultTransactionName);
163
164 // getTransactionName
Steven Moreland48548e02019-09-18 15:10:22 -0700165 auto getTransactionName = std::make_shared<Method>();
Jiyong Parke0b28032019-04-10 03:08:41 +0900166 getTransactionName->comment = "/** @hide */";
Olivier Gaillard11401402018-07-05 15:01:34 +0100167 getTransactionName->modifiers = PUBLIC;
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900168 getTransactionName->returnType = "java.lang.String";
Olivier Gaillard11401402018-07-05 15:01:34 +0100169 getTransactionName->name = "getTransactionName";
Steven Moreland48548e02019-09-18 15:10:22 -0700170 auto code2 = std::make_shared<Variable>("int", "transactionCode");
Olivier Gaillard83d7cd32018-07-30 16:20:57 +0100171 getTransactionName->parameters.push_back(code2);
Steven Moreland48548e02019-09-18 15:10:22 -0700172 getTransactionName->statements = std::make_shared<StatementBlock>();
173 getTransactionName->statements->Add(std::make_shared<ReturnStatement>(
174 std::make_shared<MethodCall>(THIS_VALUE, "getDefaultTransactionName",
175 std::vector<std::shared_ptr<Expression>>{code2})));
Olivier Gaillard11401402018-07-05 15:01:34 +0100176 this->elements.push_back(getTransactionName);
177 }
178
Christopher Wiley67502f12016-01-29 10:57:00 -0800179 // onTransact
Steven Moreland48548e02019-09-18 15:10:22 -0700180 this->transact_code = std::make_shared<Variable>("int", "code");
181 this->transact_data = std::make_shared<Variable>("android.os.Parcel", "data");
182 this->transact_reply = std::make_shared<Variable>("android.os.Parcel", "reply");
183 this->transact_flags = std::make_shared<Variable>("int", "flags");
184 auto onTransact = std::make_shared<Method>();
Christopher Wiley67502f12016-01-29 10:57:00 -0800185 onTransact->modifiers = PUBLIC | OVERRIDE;
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900186 onTransact->returnType = "boolean";
Christopher Wiley67502f12016-01-29 10:57:00 -0800187 onTransact->name = "onTransact";
188 onTransact->parameters.push_back(this->transact_code);
189 onTransact->parameters.push_back(this->transact_data);
190 onTransact->parameters.push_back(this->transact_reply);
191 onTransact->parameters.push_back(this->transact_flags);
Steven Moreland48548e02019-09-18 15:10:22 -0700192 onTransact->statements = std::make_shared<StatementBlock>();
Andreas Gampe7fab0d12017-11-22 17:50:17 -0800193 transact_statements = onTransact->statements;
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900194 onTransact->exceptions.push_back("android.os.RemoteException");
Christopher Wiley67502f12016-01-29 10:57:00 -0800195 this->elements.push_back(onTransact);
Steven Moreland48548e02019-09-18 15:10:22 -0700196 this->transact_switch = std::make_shared<SwitchStatement>(this->transact_code);
Andreas Gampea8a66fe2017-11-22 12:17:00 -0800197}
198
199void StubClass::finish() {
Steven Moreland48548e02019-09-18 15:10:22 -0700200 auto default_case = std::make_shared<Case>();
Andreas Gampea8a66fe2017-11-22 12:17:00 -0800201
Steven Moreland48548e02019-09-18 15:10:22 -0700202 auto superCall = std::make_shared<MethodCall>(
203 SUPER_VALUE, "onTransact",
204 std::vector<std::shared_ptr<Expression>>{this->transact_code, this->transact_data,
205 this->transact_reply, this->transact_flags});
206 default_case->statements->Add(std::make_shared<ReturnStatement>(superCall));
Andreas Gampea8a66fe2017-11-22 12:17:00 -0800207 transact_switch->cases.push_back(default_case);
Andreas Gampe7fab0d12017-11-22 17:50:17 -0800208
209 transact_statements->Add(this->transact_switch);
Olivier Gaillard11401402018-07-05 15:01:34 +0100210
211 // getTransactionName
Jiyong Park74595c12018-07-23 15:22:50 +0900212 if (options_.GenTransactionNames()) {
Olivier Gaillard11401402018-07-05 15:01:34 +0100213 // Some transaction codes are common, e.g. INTERFACE_TRANSACTION or DUMP_TRANSACTION.
214 // Common transaction codes will not be resolved to a string by getTransactionName. The method
215 // will return NULL in this case.
Steven Moreland48548e02019-09-18 15:10:22 -0700216 auto code_switch_default_case = std::make_shared<Case>();
217 code_switch_default_case->statements->Add(std::make_shared<ReturnStatement>(NULL_VALUE));
Olivier Gaillard11401402018-07-05 15:01:34 +0100218 this->code_to_method_name_switch->cases.push_back(code_switch_default_case);
219 }
Andreas Gampe7fab0d12017-11-22 17:50:17 -0800220}
221
Andreas Gampee9c816e2018-03-14 09:05:48 -0700222// The the expression for the interface's descriptor to be used when
223// generating code for the given method. Null is acceptable for method
224// and stands for synthetic cases.
Steven Moreland48548e02019-09-18 15:10:22 -0700225std::shared_ptr<Expression> StubClass::get_transact_descriptor(const AidlMethod* method) {
Andreas Gampe1b865af2017-11-22 11:31:47 -0800226 if (transact_outline) {
Andreas Gampee9c816e2018-03-14 09:05:48 -0700227 if (method != nullptr) {
228 // When outlining, each outlined method needs its own literal.
229 if (outline_methods.count(method) != 0) {
Steven Moreland48548e02019-09-18 15:10:22 -0700230 return std::make_shared<LiteralExpression>("DESCRIPTOR");
Andreas Gampee9c816e2018-03-14 09:05:48 -0700231 }
232 } else {
233 // Synthetic case. A small number is assumed. Use its own descriptor
234 // if there are only synthetic cases.
235 if (outline_methods.size() == all_method_count) {
Steven Moreland48548e02019-09-18 15:10:22 -0700236 return std::make_shared<LiteralExpression>("DESCRIPTOR");
Andreas Gampee9c816e2018-03-14 09:05:48 -0700237 }
238 }
Andreas Gampe1b865af2017-11-22 11:31:47 -0800239 }
240
Andreas Gampee9c816e2018-03-14 09:05:48 -0700241 // When not outlining, store the descriptor literal into a local variable, in
242 // an effort to save const-string instructions in each switch case.
Andreas Gampe7fab0d12017-11-22 17:50:17 -0800243 if (transact_descriptor == nullptr) {
Steven Moreland48548e02019-09-18 15:10:22 -0700244 transact_descriptor = std::make_shared<Variable>("java.lang.String", "descriptor");
245 transact_statements->Add(std::make_shared<VariableDeclaration>(
246 transact_descriptor, std::make_shared<LiteralExpression>("DESCRIPTOR")));
Andreas Gampe7fab0d12017-11-22 17:50:17 -0800247 }
248 return transact_descriptor;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800249}
250
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900251void StubClass::make_as_interface(const AidlInterface* interfaceType) {
Steven Moreland48548e02019-09-18 15:10:22 -0700252 auto obj = std::make_shared<Variable>("android.os.IBinder", "obj");
Christopher Wiley67502f12016-01-29 10:57:00 -0800253
Steven Moreland48548e02019-09-18 15:10:22 -0700254 auto m = std::make_shared<Method>();
Christopher Wiley67502f12016-01-29 10:57:00 -0800255 m->comment = "/**\n * Cast an IBinder object into an ";
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900256 m->comment += interfaceType->GetCanonicalName();
Christopher Wiley67502f12016-01-29 10:57:00 -0800257 m->comment += " interface,\n";
258 m->comment += " * generating a proxy if needed.\n */";
259 m->modifiers = PUBLIC | STATIC;
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900260 m->returnType = interfaceType->GetCanonicalName();
Christopher Wiley67502f12016-01-29 10:57:00 -0800261 m->name = "asInterface";
262 m->parameters.push_back(obj);
Steven Moreland48548e02019-09-18 15:10:22 -0700263 m->statements = std::make_shared<StatementBlock>();
Christopher Wiley67502f12016-01-29 10:57:00 -0800264
Steven Moreland48548e02019-09-18 15:10:22 -0700265 auto ifstatement = std::make_shared<IfStatement>();
266 ifstatement->expression = std::make_shared<Comparison>(obj, "==", NULL_VALUE);
267 ifstatement->statements = std::make_shared<StatementBlock>();
268 ifstatement->statements->Add(std::make_shared<ReturnStatement>(NULL_VALUE));
Christopher Wiley67502f12016-01-29 10:57:00 -0800269 m->statements->Add(ifstatement);
270
271 // IInterface iin = obj.queryLocalInterface(DESCRIPTOR)
Steven Moreland48548e02019-09-18 15:10:22 -0700272 auto queryLocalInterface = std::make_shared<MethodCall>(obj, "queryLocalInterface");
273 queryLocalInterface->arguments.push_back(std::make_shared<LiteralExpression>("DESCRIPTOR"));
274 auto iin = std::make_shared<Variable>("android.os.IInterface", "iin");
275 auto iinVd = std::make_shared<VariableDeclaration>(iin, queryLocalInterface);
Christopher Wiley67502f12016-01-29 10:57:00 -0800276 m->statements->Add(iinVd);
277
278 // Ensure the instance type of the local object is as expected.
279 // One scenario where this is needed is if another package (with a
280 // different class loader) runs in the same process as the service.
281
282 // if (iin != null && iin instanceof <interfaceType>) return (<interfaceType>)
283 // iin;
Steven Moreland48548e02019-09-18 15:10:22 -0700284 auto iinNotNull = std::make_shared<Comparison>(iin, "!=", NULL_VALUE);
285 auto instOfCheck = std::make_shared<Comparison>(
286 iin, " instanceof ", std::make_shared<LiteralExpression>(interfaceType->GetCanonicalName()));
287 auto instOfStatement = std::make_shared<IfStatement>();
288 instOfStatement->expression = std::make_shared<Comparison>(iinNotNull, "&&", instOfCheck);
289 instOfStatement->statements = std::make_shared<StatementBlock>();
290 instOfStatement->statements->Add(std::make_shared<ReturnStatement>(
291 std::make_shared<Cast>(interfaceType->GetCanonicalName(), iin)));
Christopher Wiley67502f12016-01-29 10:57:00 -0800292 m->statements->Add(instOfStatement);
293
Steven Moreland48548e02019-09-18 15:10:22 -0700294 auto ne = std::make_shared<NewExpression>(interfaceType->GetCanonicalName() + ".Stub.Proxy");
Christopher Wiley67502f12016-01-29 10:57:00 -0800295 ne->arguments.push_back(obj);
Steven Moreland48548e02019-09-18 15:10:22 -0700296 m->statements->Add(std::make_shared<ReturnStatement>(ne));
Christopher Wiley67502f12016-01-29 10:57:00 -0800297
298 this->elements.push_back(m);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800299}
300
Adam Lesinskiffa16862014-01-23 18:17:42 -0800301// =================================================
Christopher Wiley67502f12016-01-29 10:57:00 -0800302class ProxyClass : public Class {
303 public:
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900304 ProxyClass(const AidlInterface* interfaceType, const Options& options);
Yi Kongde138912019-03-30 01:38:17 -0700305 ~ProxyClass() override;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800306
Steven Moreland48548e02019-09-18 15:10:22 -0700307 std::shared_ptr<Variable> mRemote;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800308};
309
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900310ProxyClass::ProxyClass(const AidlInterface* interfaceType, const Options& options) : Class() {
Christopher Wiley67502f12016-01-29 10:57:00 -0800311 this->modifiers = PRIVATE | STATIC;
312 this->what = Class::CLASS;
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900313 this->type = interfaceType->GetCanonicalName() + ".Stub.Proxy";
314 this->interfaces.push_back(interfaceType->GetCanonicalName());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800315
Christopher Wiley67502f12016-01-29 10:57:00 -0800316 // IBinder mRemote
Steven Moreland48548e02019-09-18 15:10:22 -0700317 mRemote = std::make_shared<Variable>("android.os.IBinder", "mRemote");
318 this->elements.push_back(std::make_shared<Field>(PRIVATE, mRemote));
Adam Lesinskiffa16862014-01-23 18:17:42 -0800319
Christopher Wiley67502f12016-01-29 10:57:00 -0800320 // Proxy()
Steven Moreland48548e02019-09-18 15:10:22 -0700321 auto remote = std::make_shared<Variable>("android.os.IBinder", "remote");
322 auto ctor = std::make_shared<Method>();
Christopher Wiley67502f12016-01-29 10:57:00 -0800323 ctor->name = "Proxy";
Steven Moreland48548e02019-09-18 15:10:22 -0700324 ctor->statements = std::make_shared<StatementBlock>();
Christopher Wiley67502f12016-01-29 10:57:00 -0800325 ctor->parameters.push_back(remote);
Steven Moreland48548e02019-09-18 15:10:22 -0700326 ctor->statements->Add(std::make_shared<Assignment>(mRemote, remote));
Christopher Wiley67502f12016-01-29 10:57:00 -0800327 this->elements.push_back(ctor);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800328
Jiyong Park309668e2018-07-28 16:55:44 +0900329 if (options.Version() > 0) {
330 std::ostringstream code;
331 code << "private int mCachedVersion = -1;\n";
Steven Moreland48548e02019-09-18 15:10:22 -0700332 this->elements.emplace_back(std::make_shared<LiteralClassElement>(code.str()));
Jiyong Park309668e2018-07-28 16:55:44 +0900333 }
Paul Trautrimb77048c2020-01-21 16:39:32 +0900334 if (!options.Hash().empty()) {
335 std::ostringstream code;
336 code << "private String mCachedHash = \"-1\";\n";
337 this->elements.emplace_back(std::make_shared<LiteralClassElement>(code.str()));
338 }
Jiyong Park309668e2018-07-28 16:55:44 +0900339
Christopher Wiley67502f12016-01-29 10:57:00 -0800340 // IBinder asBinder()
Steven Moreland48548e02019-09-18 15:10:22 -0700341 auto asBinder = std::make_shared<Method>();
Christopher Wiley67502f12016-01-29 10:57:00 -0800342 asBinder->modifiers = PUBLIC | OVERRIDE;
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900343 asBinder->returnType = "android.os.IBinder";
Christopher Wiley67502f12016-01-29 10:57:00 -0800344 asBinder->name = "asBinder";
Steven Moreland48548e02019-09-18 15:10:22 -0700345 asBinder->statements = std::make_shared<StatementBlock>();
346 asBinder->statements->Add(std::make_shared<ReturnStatement>(mRemote));
Christopher Wiley67502f12016-01-29 10:57:00 -0800347 this->elements.push_back(asBinder);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800348}
349
Christopher Wiley67502f12016-01-29 10:57:00 -0800350ProxyClass::~ProxyClass() {}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800351
352// =================================================
Daniel Norman716d3112019-09-10 13:11:56 -0700353static void generate_new_array(const AidlTypeSpecifier& type, const AidlTypenames& typenames,
354 std::shared_ptr<StatementBlock> addTo, std::shared_ptr<Variable> v,
355 std::shared_ptr<Variable> parcel) {
Steven Moreland48548e02019-09-18 15:10:22 -0700356 auto len = std::make_shared<Variable>("int", v->name + "_length");
357 addTo->Add(
358 std::make_shared<VariableDeclaration>(len, std::make_shared<MethodCall>(parcel, "readInt")));
359 auto lencheck = std::make_shared<IfStatement>();
360 lencheck->expression =
361 std::make_shared<Comparison>(len, "<", std::make_shared<LiteralExpression>("0"));
362 lencheck->statements->Add(std::make_shared<Assignment>(v, NULL_VALUE));
363 lencheck->elseif = std::make_shared<IfStatement>();
364 lencheck->elseif->statements->Add(std::make_shared<Assignment>(
Daniel Norman716d3112019-09-10 13:11:56 -0700365 v, std::make_shared<NewArrayExpression>(InstantiableJavaSignatureOf(type, typenames), len)));
Christopher Wiley67502f12016-01-29 10:57:00 -0800366 addTo->Add(lencheck);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800367}
368
Steven Moreland48548e02019-09-18 15:10:22 -0700369static void generate_write_to_parcel(const AidlTypeSpecifier& type,
370 std::shared_ptr<StatementBlock> addTo,
371 std::shared_ptr<Variable> v, std::shared_ptr<Variable> parcel,
372 bool is_return_value, const AidlTypenames& typenames) {
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900373 string code;
374 CodeWriterPtr writer = CodeWriter::ForString(&code);
375 CodeGeneratorContext context{
376 .writer = *(writer.get()),
377 .typenames = typenames,
378 .type = type,
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900379 .parcel = parcel->name,
Nick Desaulniers27e1ff62019-10-07 23:13:10 -0700380 .var = v->name,
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900381 .is_return_value = is_return_value,
382 };
383 WriteToParcelFor(context);
384 writer->Close();
Steven Moreland48548e02019-09-18 15:10:22 -0700385 addTo->Add(std::make_shared<LiteralStatement>(code));
Adam Lesinskiffa16862014-01-23 18:17:42 -0800386}
387
Steven Moreland693640b2018-07-19 13:46:27 -0700388static void generate_int_constant(Class* interface, const std::string& name,
389 const std::string& value) {
Jeongik Chade157cc2019-02-12 12:41:27 +0900390 auto code = StringPrintf("public static final int %s = %s;\n", name.c_str(), value.c_str());
Steven Moreland48548e02019-09-18 15:10:22 -0700391 interface->elements.push_back(std::make_shared<LiteralClassElement>(code));
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700392}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800393
Steven Moreland693640b2018-07-19 13:46:27 -0700394static void generate_string_constant(Class* interface, const std::string& name,
395 const std::string& value) {
Jeongik Chade157cc2019-02-12 12:41:27 +0900396 auto code = StringPrintf("public static final String %s = %s;\n", name.c_str(), value.c_str());
Steven Moreland48548e02019-09-18 15:10:22 -0700397 interface->elements.push_back(std::make_shared<LiteralClassElement>(code));
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800398}
399
Daniel Norman716d3112019-09-10 13:11:56 -0700400static std::shared_ptr<Method> generate_interface_method(const AidlMethod& method,
401 const AidlTypenames& typenames) {
Steven Moreland48548e02019-09-18 15:10:22 -0700402 auto decl = std::make_shared<Method>();
Christopher Wiley67502f12016-01-29 10:57:00 -0800403 decl->comment = method.GetComments();
404 decl->modifiers = PUBLIC;
Daniel Norman716d3112019-09-10 13:11:56 -0700405 decl->returnType = JavaSignatureOf(method.GetType(), typenames);
Christopher Wiley67502f12016-01-29 10:57:00 -0800406 decl->name = method.GetName();
Jiyong Parka6605ab2018-11-11 14:30:21 +0900407 decl->annotations = generate_java_annotations(method.GetType());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800408
Christopher Wiley67502f12016-01-29 10:57:00 -0800409 for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) {
Jiyong Parkbf5fd5c2020-06-05 19:48:05 +0900410 auto var = std::make_shared<Variable>(JavaSignatureOf(arg->GetType(), typenames), arg->GetName());
411 var->annotations = generate_java_annotations(arg->GetType());
412 decl->parameters.push_back(var);
Christopher Wiley67502f12016-01-29 10:57:00 -0800413 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800414
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900415 decl->exceptions.push_back("android.os.RemoteException");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800416
Andreas Gampe7d7fa602017-11-22 10:50:03 -0800417 return decl;
418}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800419
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900420static void generate_stub_code(const AidlInterface& iface, const AidlMethod& method, bool oneway,
Steven Moreland48548e02019-09-18 15:10:22 -0700421 std::shared_ptr<Variable> transact_data,
422 std::shared_ptr<Variable> transact_reply,
423 const AidlTypenames& typenames,
Devin Mooree2ccf8a2020-05-13 14:28:20 -0700424 std::shared_ptr<StatementBlock> statement_block,
Steven Moreland48548e02019-09-18 15:10:22 -0700425 std::shared_ptr<StubClass> stubClass, const Options& options) {
Devin Mooree2ccf8a2020-05-13 14:28:20 -0700426 // try and finally
427 auto tryStatement = std::make_shared<TryStatement>();
428 auto finallyStatement = std::make_shared<FinallyStatement>();
429 auto& statements = statement_block;
430
431 if (options.GenTraces()) {
432 statements->Add(tryStatement);
433 statements->Add(finallyStatement);
434 statements = tryStatement->statements;
435 tryStatement->statements->Add(std::make_shared<MethodCall>(
436 std::make_shared<LiteralExpression>("android.os.Trace"), "traceBegin",
437 std::vector<std::shared_ptr<Expression>>{
438 std::make_shared<LiteralExpression>("android.os.Trace.TRACE_TAG_AIDL"),
439 std::make_shared<StringLiteralExpression>("AIDL::java::" + iface.GetName() +
440 "::" + method.GetName() + "::server")}));
441 finallyStatement->statements->Add(std::make_shared<MethodCall>(
442 std::make_shared<LiteralExpression>("android.os.Trace"), "traceEnd",
443 std::vector<std::shared_ptr<Expression>>{
444 std::make_shared<LiteralExpression>("android.os.Trace.TRACE_TAG_AIDL")}));
445 }
446
Steven Moreland48548e02019-09-18 15:10:22 -0700447 auto realCall = std::make_shared<MethodCall>(THIS_VALUE, method.GetName());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800448
Christopher Wiley67502f12016-01-29 10:57:00 -0800449 // interface token validation is the very first thing we do
Steven Moreland48548e02019-09-18 15:10:22 -0700450 statements->Add(std::make_shared<MethodCall>(
451 transact_data, "enforceInterface",
452 std::vector<std::shared_ptr<Expression>>{stubClass->get_transact_descriptor(&method)}));
Adam Lesinskiffa16862014-01-23 18:17:42 -0800453
Christopher Wiley67502f12016-01-29 10:57:00 -0800454 // args
Christopher Wiley67502f12016-01-29 10:57:00 -0800455 VariableFactory stubArgs("_arg");
Andreas Gampe7d7fa602017-11-22 10:50:03 -0800456 {
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900457 // keep this across different args in order to create the classloader
458 // at most once.
459 bool is_classloader_created = false;
Andreas Gampe7d7fa602017-11-22 10:50:03 -0800460 for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) {
Daniel Norman716d3112019-09-10 13:11:56 -0700461 std::shared_ptr<Variable> v = stubArgs.Get(arg->GetType(), typenames);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800462
Steven Moreland48548e02019-09-18 15:10:22 -0700463 statements->Add(std::make_shared<VariableDeclaration>(v));
Adam Lesinskiffa16862014-01-23 18:17:42 -0800464
Andreas Gampe7d7fa602017-11-22 10:50:03 -0800465 if (arg->GetDirection() & AidlArgument::IN_DIR) {
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900466 string code;
467 CodeWriterPtr writer = CodeWriter::ForString(&code);
468 CodeGeneratorContext context{.writer = *(writer.get()),
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900469 .typenames = typenames,
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900470 .type = arg->GetType(),
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900471 .parcel = transact_data->name,
Nick Desaulniers27e1ff62019-10-07 23:13:10 -0700472 .var = v->name,
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900473 .is_classloader_created = &is_classloader_created};
474 CreateFromParcelFor(context);
475 writer->Close();
Steven Moreland48548e02019-09-18 15:10:22 -0700476 statements->Add(std::make_shared<LiteralStatement>(code));
Christopher Wiley67502f12016-01-29 10:57:00 -0800477 } else {
Andreas Gampe7d7fa602017-11-22 10:50:03 -0800478 if (!arg->GetType().IsArray()) {
Steven Moreland48548e02019-09-18 15:10:22 -0700479 statements->Add(std::make_shared<Assignment>(
Daniel Norman716d3112019-09-10 13:11:56 -0700480 v, std::make_shared<NewExpression>(
481 InstantiableJavaSignatureOf(arg->GetType(), typenames))));
Andreas Gampe7d7fa602017-11-22 10:50:03 -0800482 } else {
Daniel Norman716d3112019-09-10 13:11:56 -0700483 generate_new_array(arg->GetType(), typenames, statements, v, transact_data);
Andreas Gampe7d7fa602017-11-22 10:50:03 -0800484 }
Christopher Wiley67502f12016-01-29 10:57:00 -0800485 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800486
Andreas Gampe7d7fa602017-11-22 10:50:03 -0800487 realCall->arguments.push_back(v);
488 }
Christopher Wiley67502f12016-01-29 10:57:00 -0800489 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800490
Christopher Wiley67502f12016-01-29 10:57:00 -0800491 // the real call
Christopher Wiley67502f12016-01-29 10:57:00 -0800492 if (method.GetType().GetName() == "void") {
Devin Mooree2ccf8a2020-05-13 14:28:20 -0700493 statements->Add(realCall);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800494
Adam Lesinskiffa16862014-01-23 18:17:42 -0800495 if (!oneway) {
Christopher Wiley67502f12016-01-29 10:57:00 -0800496 // report that there were no exceptions
Steven Moreland48548e02019-09-18 15:10:22 -0700497 auto ex = std::make_shared<MethodCall>(transact_reply, "writeNoException");
Andreas Gampe7d7fa602017-11-22 10:50:03 -0800498 statements->Add(ex);
Christopher Wiley67502f12016-01-29 10:57:00 -0800499 }
500 } else {
Daniel Norman716d3112019-09-10 13:11:56 -0700501 auto _result =
502 std::make_shared<Variable>(JavaSignatureOf(method.GetType(), typenames), "_result");
Steven Moreland48548e02019-09-18 15:10:22 -0700503 statements->Add(std::make_shared<VariableDeclaration>(_result, realCall));
Christopher Wiley67502f12016-01-29 10:57:00 -0800504
505 if (!oneway) {
506 // report that there were no exceptions
Steven Moreland48548e02019-09-18 15:10:22 -0700507 auto ex = std::make_shared<MethodCall>(transact_reply, "writeNoException");
Andreas Gampe7d7fa602017-11-22 10:50:03 -0800508 statements->Add(ex);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800509 }
510
Christopher Wiley67502f12016-01-29 10:57:00 -0800511 // marshall the return value
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900512 generate_write_to_parcel(method.GetType(), statements, _result, transact_reply, true,
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900513 typenames);
Christopher Wiley67502f12016-01-29 10:57:00 -0800514 }
515
516 // out parameters
Andreas Gampe7d7fa602017-11-22 10:50:03 -0800517 int i = 0;
Christopher Wiley67502f12016-01-29 10:57:00 -0800518 for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) {
Steven Moreland48548e02019-09-18 15:10:22 -0700519 std::shared_ptr<Variable> v = stubArgs.Get(i++);
Christopher Wiley67502f12016-01-29 10:57:00 -0800520
521 if (arg->GetDirection() & AidlArgument::OUT_DIR) {
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900522 generate_write_to_parcel(arg->GetType(), statements, v, transact_reply, true, typenames);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800523 }
Christopher Wiley67502f12016-01-29 10:57:00 -0800524 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800525
Christopher Wiley67502f12016-01-29 10:57:00 -0800526 // return true
Steven Moreland48548e02019-09-18 15:10:22 -0700527 statements->Add(std::make_shared<ReturnStatement>(TRUE_VALUE));
Andreas Gampe7d7fa602017-11-22 10:50:03 -0800528}
Adam Lesinskiffa16862014-01-23 18:17:42 -0800529
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900530static void generate_stub_case(const AidlInterface& iface, const AidlMethod& method,
531 const std::string& transactCodeName, bool oneway,
Steven Moreland48548e02019-09-18 15:10:22 -0700532 std::shared_ptr<StubClass> stubClass, const AidlTypenames& typenames,
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900533 const Options& options) {
Steven Moreland48548e02019-09-18 15:10:22 -0700534 auto c = std::make_shared<Case>(transactCodeName);
Andreas Gampe7d7fa602017-11-22 10:50:03 -0800535
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900536 generate_stub_code(iface, method, oneway, stubClass->transact_data, stubClass->transact_reply,
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900537 typenames, c->statements, stubClass, options);
Andreas Gampe7d7fa602017-11-22 10:50:03 -0800538
539 stubClass->transact_switch->cases.push_back(c);
540}
541
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900542static void generate_stub_case_outline(const AidlInterface& iface, const AidlMethod& method,
543 const std::string& transactCodeName, bool oneway,
Steven Moreland48548e02019-09-18 15:10:22 -0700544 std::shared_ptr<StubClass> stubClass,
545 const AidlTypenames& typenames, const Options& options) {
Andreas Gampe1b865af2017-11-22 11:31:47 -0800546 std::string outline_name = "onTransact$" + method.GetName() + "$";
547 // Generate an "outlined" method with the actual code.
548 {
Steven Moreland48548e02019-09-18 15:10:22 -0700549 auto transact_data = std::make_shared<Variable>("android.os.Parcel", "data");
550 auto transact_reply = std::make_shared<Variable>("android.os.Parcel", "reply");
551 auto onTransact_case = std::make_shared<Method>();
Andreas Gampe1b865af2017-11-22 11:31:47 -0800552 onTransact_case->modifiers = PRIVATE;
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900553 onTransact_case->returnType = "boolean";
Andreas Gampe1b865af2017-11-22 11:31:47 -0800554 onTransact_case->name = outline_name;
555 onTransact_case->parameters.push_back(transact_data);
556 onTransact_case->parameters.push_back(transact_reply);
Steven Moreland48548e02019-09-18 15:10:22 -0700557 onTransact_case->statements = std::make_shared<StatementBlock>();
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900558 onTransact_case->exceptions.push_back("android.os.RemoteException");
Andreas Gampe1b865af2017-11-22 11:31:47 -0800559 stubClass->elements.push_back(onTransact_case);
560
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900561 generate_stub_code(iface, method, oneway, transact_data, transact_reply, typenames,
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900562 onTransact_case->statements, stubClass, options);
Andreas Gampe1b865af2017-11-22 11:31:47 -0800563 }
564
565 // Generate the case dispatch.
566 {
Steven Moreland48548e02019-09-18 15:10:22 -0700567 auto c = std::make_shared<Case>(transactCodeName);
Andreas Gampe1b865af2017-11-22 11:31:47 -0800568
Steven Moreland48548e02019-09-18 15:10:22 -0700569 auto helper_call =
570 std::make_shared<MethodCall>(THIS_VALUE, outline_name,
571 std::vector<std::shared_ptr<Expression>>{
572 stubClass->transact_data, stubClass->transact_reply});
573 c->statements->Add(std::make_shared<ReturnStatement>(helper_call));
Andreas Gampe1b865af2017-11-22 11:31:47 -0800574
575 stubClass->transact_switch->cases.push_back(c);
576 }
577}
578
Steven Moreland48548e02019-09-18 15:10:22 -0700579static std::shared_ptr<Method> generate_proxy_method(
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900580 const AidlInterface& iface, const AidlMethod& method, const std::string& transactCodeName,
Steven Moreland48548e02019-09-18 15:10:22 -0700581 bool oneway, std::shared_ptr<ProxyClass> proxyClass, const AidlTypenames& typenames,
582 const Options& options) {
583 auto proxy = std::make_shared<Method>();
Christopher Wiley67502f12016-01-29 10:57:00 -0800584 proxy->comment = method.GetComments();
585 proxy->modifiers = PUBLIC | OVERRIDE;
Daniel Norman716d3112019-09-10 13:11:56 -0700586 proxy->returnType = JavaSignatureOf(method.GetType(), typenames);
Christopher Wiley67502f12016-01-29 10:57:00 -0800587 proxy->name = method.GetName();
Steven Moreland48548e02019-09-18 15:10:22 -0700588 proxy->statements = std::make_shared<StatementBlock>();
Christopher Wiley67502f12016-01-29 10:57:00 -0800589 for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) {
Steven Moreland48548e02019-09-18 15:10:22 -0700590 proxy->parameters.push_back(
Daniel Norman716d3112019-09-10 13:11:56 -0700591 std::make_shared<Variable>(JavaSignatureOf(arg->GetType(), typenames), arg->GetName()));
Christopher Wiley67502f12016-01-29 10:57:00 -0800592 }
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900593 proxy->exceptions.push_back("android.os.RemoteException");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800594
Christopher Wiley67502f12016-01-29 10:57:00 -0800595 // the parcels
Steven Moreland48548e02019-09-18 15:10:22 -0700596 auto _data = std::make_shared<Variable>("android.os.Parcel", "_data");
597 proxy->statements->Add(std::make_shared<VariableDeclaration>(
598 _data, std::make_shared<MethodCall>("android.os.Parcel", "obtain")));
599 std::shared_ptr<Variable> _reply = nullptr;
Christopher Wiley67502f12016-01-29 10:57:00 -0800600 if (!oneway) {
Steven Moreland48548e02019-09-18 15:10:22 -0700601 _reply = std::make_shared<Variable>("android.os.Parcel", "_reply");
602 proxy->statements->Add(std::make_shared<VariableDeclaration>(
603 _reply, std::make_shared<MethodCall>("android.os.Parcel", "obtain")));
Christopher Wiley67502f12016-01-29 10:57:00 -0800604 }
605
606 // the return value
Steven Moreland48548e02019-09-18 15:10:22 -0700607 std::shared_ptr<Variable> _result = nullptr;
Christopher Wiley67502f12016-01-29 10:57:00 -0800608 if (method.GetType().GetName() != "void") {
Steven Moreland48548e02019-09-18 15:10:22 -0700609 _result = std::make_shared<Variable>(*proxy->returnType, "_result");
610 proxy->statements->Add(std::make_shared<VariableDeclaration>(_result));
Christopher Wiley67502f12016-01-29 10:57:00 -0800611 }
612
613 // try and finally
Steven Moreland48548e02019-09-18 15:10:22 -0700614 auto tryStatement = std::make_shared<TryStatement>();
Christopher Wiley67502f12016-01-29 10:57:00 -0800615 proxy->statements->Add(tryStatement);
Steven Moreland48548e02019-09-18 15:10:22 -0700616 auto finallyStatement = std::make_shared<FinallyStatement>();
Christopher Wiley67502f12016-01-29 10:57:00 -0800617 proxy->statements->Add(finallyStatement);
618
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900619 if (options.GenTraces()) {
Steven Moreland48548e02019-09-18 15:10:22 -0700620 tryStatement->statements->Add(std::make_shared<MethodCall>(
621 std::make_shared<LiteralExpression>("android.os.Trace"), "traceBegin",
622 std::vector<std::shared_ptr<Expression>>{
623 std::make_shared<LiteralExpression>("android.os.Trace.TRACE_TAG_AIDL"),
Devin Mooree2ccf8a2020-05-13 14:28:20 -0700624 std::make_shared<StringLiteralExpression>("AIDL::java::" + iface.GetName() +
625 "::" + method.GetName() + "::client")}));
Martijn Coenenf1b50782018-02-21 21:06:23 +0100626 }
627
Christopher Wiley67502f12016-01-29 10:57:00 -0800628 // the interface identifier token: the DESCRIPTOR constant, marshalled as a
629 // string
Steven Moreland48548e02019-09-18 15:10:22 -0700630 tryStatement->statements->Add(std::make_shared<MethodCall>(
631 _data, "writeInterfaceToken",
632 std::vector<std::shared_ptr<Expression>>{std::make_shared<LiteralExpression>("DESCRIPTOR")}));
Christopher Wiley67502f12016-01-29 10:57:00 -0800633
634 // the parameters
635 for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) {
Daniel Norman716d3112019-09-10 13:11:56 -0700636 auto v = std::make_shared<Variable>(JavaSignatureOf(arg->GetType(), typenames), arg->GetName());
Christopher Wiley67502f12016-01-29 10:57:00 -0800637 AidlArgument::Direction dir = arg->GetDirection();
638 if (dir == AidlArgument::OUT_DIR && arg->GetType().IsArray()) {
Steven Moreland48548e02019-09-18 15:10:22 -0700639 auto checklen = std::make_shared<IfStatement>();
640 checklen->expression = std::make_shared<Comparison>(v, "==", NULL_VALUE);
641 checklen->statements->Add(std::make_shared<MethodCall>(
642 _data, "writeInt",
643 std::vector<std::shared_ptr<Expression>>{std::make_shared<LiteralExpression>("-1")}));
644 checklen->elseif = std::make_shared<IfStatement>();
645 checklen->elseif->statements->Add(std::make_shared<MethodCall>(
646 _data, "writeInt",
647 std::vector<std::shared_ptr<Expression>>{std::make_shared<FieldVariable>(v, "length")}));
Christopher Wiley67502f12016-01-29 10:57:00 -0800648 tryStatement->statements->Add(checklen);
649 } else if (dir & AidlArgument::IN_DIR) {
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900650 generate_write_to_parcel(arg->GetType(), tryStatement->statements, v, _data, false,
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900651 typenames);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800652 }
Christopher Wiley67502f12016-01-29 10:57:00 -0800653 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800654
Christopher Wiley67502f12016-01-29 10:57:00 -0800655 // the transact call
Steven Moreland48548e02019-09-18 15:10:22 -0700656 auto call = std::make_shared<MethodCall>(
657 proxyClass->mRemote, "transact",
658 std::vector<std::shared_ptr<Expression>>{
659 std::make_shared<LiteralExpression>("Stub." + transactCodeName), _data,
660 _reply ? _reply : NULL_VALUE,
661 std::make_shared<LiteralExpression>(oneway ? "android.os.IBinder.FLAG_ONEWAY" : "0")});
662 auto _status = std::make_shared<Variable>("boolean", "_status");
663 tryStatement->statements->Add(std::make_shared<VariableDeclaration>(_status, call));
Jiyong Park75e1a742018-07-04 12:31:23 +0900664
665 // If the transaction returns false, which means UNKNOWN_TRANSACTION, fall
666 // back to the local method in the default impl, if set before.
667 vector<string> arg_names;
668 for (const auto& arg : method.GetArguments()) {
669 arg_names.emplace_back(arg->GetName());
670 }
671 bool has_return_type = method.GetType().GetName() != "void";
Steven Moreland48548e02019-09-18 15:10:22 -0700672 tryStatement->statements->Add(std::make_shared<LiteralStatement>(
Jiyong Park75e1a742018-07-04 12:31:23 +0900673 android::base::StringPrintf(has_return_type ? "if (!_status && getDefaultImpl() != null) {\n"
674 " return getDefaultImpl().%s(%s);\n"
675 "}\n"
676 : "if (!_status && getDefaultImpl() != null) {\n"
677 " getDefaultImpl().%s(%s);\n"
678 " return;\n"
679 "}\n",
680 method.GetName().c_str(), Join(arg_names, ", ").c_str())));
Adam Lesinskiffa16862014-01-23 18:17:42 -0800681
Christopher Wiley67502f12016-01-29 10:57:00 -0800682 // throw back exceptions.
683 if (_reply) {
Steven Moreland48548e02019-09-18 15:10:22 -0700684 auto ex = std::make_shared<MethodCall>(_reply, "readException");
Christopher Wiley67502f12016-01-29 10:57:00 -0800685 tryStatement->statements->Add(ex);
686 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800687
Christopher Wiley67502f12016-01-29 10:57:00 -0800688 // returning and cleanup
Yi Kong894d6ba2018-07-24 11:27:38 -0700689 if (_reply != nullptr) {
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900690 // keep this across return value and arguments in order to create the
691 // classloader at most once.
692 bool is_classloader_created = false;
Yi Kong894d6ba2018-07-24 11:27:38 -0700693 if (_result != nullptr) {
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900694 string code;
695 CodeWriterPtr writer = CodeWriter::ForString(&code);
696 CodeGeneratorContext context{.writer = *(writer.get()),
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900697 .typenames = typenames,
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900698 .type = method.GetType(),
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900699 .parcel = _reply->name,
Nick Desaulniers27e1ff62019-10-07 23:13:10 -0700700 .var = _result->name,
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900701 .is_classloader_created = &is_classloader_created};
702 CreateFromParcelFor(context);
703 writer->Close();
Steven Moreland48548e02019-09-18 15:10:22 -0700704 tryStatement->statements->Add(std::make_shared<LiteralStatement>(code));
Adam Lesinskiffa16862014-01-23 18:17:42 -0800705 }
Christopher Wiley67502f12016-01-29 10:57:00 -0800706
707 // the out/inout parameters
708 for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) {
Christopher Wiley67502f12016-01-29 10:57:00 -0800709 if (arg->GetDirection() & AidlArgument::OUT_DIR) {
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900710 string code;
711 CodeWriterPtr writer = CodeWriter::ForString(&code);
712 CodeGeneratorContext context{.writer = *(writer.get()),
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900713 .typenames = typenames,
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900714 .type = arg->GetType(),
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900715 .parcel = _reply->name,
Nick Desaulniers27e1ff62019-10-07 23:13:10 -0700716 .var = arg->GetName(),
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900717 .is_classloader_created = &is_classloader_created};
718 ReadFromParcelFor(context);
719 writer->Close();
Steven Moreland48548e02019-09-18 15:10:22 -0700720 tryStatement->statements->Add(std::make_shared<LiteralStatement>(code));
Christopher Wiley67502f12016-01-29 10:57:00 -0800721 }
722 }
723
Steven Moreland48548e02019-09-18 15:10:22 -0700724 finallyStatement->statements->Add(std::make_shared<MethodCall>(_reply, "recycle"));
Christopher Wiley67502f12016-01-29 10:57:00 -0800725 }
Steven Moreland48548e02019-09-18 15:10:22 -0700726 finallyStatement->statements->Add(std::make_shared<MethodCall>(_data, "recycle"));
Christopher Wiley67502f12016-01-29 10:57:00 -0800727
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900728 if (options.GenTraces()) {
Steven Moreland48548e02019-09-18 15:10:22 -0700729 finallyStatement->statements->Add(std::make_shared<MethodCall>(
730 std::make_shared<LiteralExpression>("android.os.Trace"), "traceEnd",
731 std::vector<std::shared_ptr<Expression>>{
732 std::make_shared<LiteralExpression>("android.os.Trace.TRACE_TAG_AIDL")}));
Martijn Coenenf1b50782018-02-21 21:06:23 +0100733 }
734
Yi Kong894d6ba2018-07-24 11:27:38 -0700735 if (_result != nullptr) {
Steven Moreland48548e02019-09-18 15:10:22 -0700736 proxy->statements->Add(std::make_shared<ReturnStatement>(_result));
Christopher Wiley67502f12016-01-29 10:57:00 -0800737 }
Andreas Gampe7d7fa602017-11-22 10:50:03 -0800738
739 return proxy;
740}
741
Jiyong Park74595c12018-07-23 15:22:50 +0900742static void generate_methods(const AidlInterface& iface, const AidlMethod& method, Class* interface,
Steven Moreland48548e02019-09-18 15:10:22 -0700743 std::shared_ptr<StubClass> stubClass,
744 std::shared_ptr<ProxyClass> proxyClass, int index,
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900745 const AidlTypenames& typenames, const Options& options) {
Steven Morelandacd53472018-12-14 10:17:26 -0800746 const bool oneway = method.IsOneway();
Andreas Gampe7d7fa602017-11-22 10:50:03 -0800747
748 // == the TRANSACT_ constant =============================================
749 string transactCodeName = "TRANSACTION_";
750 transactCodeName += method.GetName();
751
Steven Moreland48548e02019-09-18 15:10:22 -0700752 auto transactCode =
753 std::make_shared<Field>(STATIC | FINAL, std::make_shared<Variable>("int", transactCodeName));
Andreas Gampe7d7fa602017-11-22 10:50:03 -0800754 transactCode->value =
755 StringPrintf("(android.os.IBinder.FIRST_CALL_TRANSACTION + %d)", index);
756 stubClass->elements.push_back(transactCode);
757
Olivier Gaillard11401402018-07-05 15:01:34 +0100758 // getTransactionName
Jiyong Park74595c12018-07-23 15:22:50 +0900759 if (options.GenTransactionNames()) {
Steven Moreland48548e02019-09-18 15:10:22 -0700760 auto c = std::make_shared<Case>(transactCodeName);
761 c->statements->Add(std::make_shared<ReturnStatement>(
762 std::make_shared<StringLiteralExpression>(method.GetName())));
Olivier Gaillard11401402018-07-05 15:01:34 +0100763 stubClass->code_to_method_name_switch->cases.push_back(c);
764 }
765
Andreas Gampe7d7fa602017-11-22 10:50:03 -0800766 // == the declaration in the interface ===================================
Steven Moreland48548e02019-09-18 15:10:22 -0700767 std::shared_ptr<ClassElement> decl;
Jiyong Park309668e2018-07-28 16:55:44 +0900768 if (method.IsUserDefined()) {
Daniel Norman716d3112019-09-10 13:11:56 -0700769 decl = generate_interface_method(method, typenames);
Jiyong Park309668e2018-07-28 16:55:44 +0900770 } else {
771 if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) {
772 std::ostringstream code;
773 code << "public int " << kGetInterfaceVersion << "() "
774 << "throws android.os.RemoteException;\n";
Steven Moreland48548e02019-09-18 15:10:22 -0700775 decl = std::make_shared<LiteralClassElement>(code.str());
Jiyong Park309668e2018-07-28 16:55:44 +0900776 }
Paul Trautrimb77048c2020-01-21 16:39:32 +0900777 if (method.GetName() == kGetInterfaceHash && !options.Hash().empty()) {
778 std::ostringstream code;
779 code << "public String " << kGetInterfaceHash << "() "
780 << "throws android.os.RemoteException;\n";
781 decl = std::make_shared<LiteralClassElement>(code.str());
782 }
Jiyong Park309668e2018-07-28 16:55:44 +0900783 }
Andreas Gampe7d7fa602017-11-22 10:50:03 -0800784 interface->elements.push_back(decl);
785
786 // == the stub method ====================================================
Jiyong Park309668e2018-07-28 16:55:44 +0900787 if (method.IsUserDefined()) {
788 bool outline_stub =
789 stubClass->transact_outline && stubClass->outline_methods.count(&method) != 0;
790 if (outline_stub) {
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900791 generate_stub_case_outline(iface, method, transactCodeName, oneway, stubClass, typenames,
Jiyong Park309668e2018-07-28 16:55:44 +0900792 options);
793 } else {
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900794 generate_stub_case(iface, method, transactCodeName, oneway, stubClass, typenames, options);
Jiyong Park309668e2018-07-28 16:55:44 +0900795 }
Andreas Gampe1b865af2017-11-22 11:31:47 -0800796 } else {
Jiyong Park309668e2018-07-28 16:55:44 +0900797 if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) {
Steven Moreland48548e02019-09-18 15:10:22 -0700798 auto c = std::make_shared<Case>(transactCodeName);
Jiyong Park309668e2018-07-28 16:55:44 +0900799 std::ostringstream code;
Jiyong Park965c5b92018-11-21 13:37:15 +0900800 code << "data.enforceInterface(descriptor);\n"
801 << "reply.writeNoException();\n"
802 << "reply.writeInt(" << kGetInterfaceVersion << "());\n"
Jiyong Park309668e2018-07-28 16:55:44 +0900803 << "return true;\n";
Steven Moreland48548e02019-09-18 15:10:22 -0700804 c->statements->Add(std::make_shared<LiteralStatement>(code.str()));
Jiyong Park309668e2018-07-28 16:55:44 +0900805 stubClass->transact_switch->cases.push_back(c);
806 }
Paul Trautrimb77048c2020-01-21 16:39:32 +0900807 if (method.GetName() == kGetInterfaceHash && !options.Hash().empty()) {
808 auto c = std::make_shared<Case>(transactCodeName);
809 std::ostringstream code;
810 code << "data.enforceInterface(descriptor);\n"
811 << "reply.writeNoException();\n"
812 << "reply.writeString(" << kGetInterfaceHash << "());\n"
813 << "return true;\n";
814 c->statements->Add(std::make_shared<LiteralStatement>(code.str()));
815 stubClass->transact_switch->cases.push_back(c);
816 }
Andreas Gampe1b865af2017-11-22 11:31:47 -0800817 }
Andreas Gampe7d7fa602017-11-22 10:50:03 -0800818
819 // == the proxy method ===================================================
Steven Moreland48548e02019-09-18 15:10:22 -0700820 std::shared_ptr<ClassElement> proxy = nullptr;
Jiyong Park309668e2018-07-28 16:55:44 +0900821 if (method.IsUserDefined()) {
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900822 proxy = generate_proxy_method(iface, method, transactCodeName, oneway, proxyClass, typenames,
Steven Moreland48548e02019-09-18 15:10:22 -0700823 options);
Jiyong Park309668e2018-07-28 16:55:44 +0900824
825 } else {
826 if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) {
827 std::ostringstream code;
828 code << "@Override\n"
829 << "public int " << kGetInterfaceVersion << "()"
830 << " throws "
831 << "android.os.RemoteException {\n"
832 << " if (mCachedVersion == -1) {\n"
833 << " android.os.Parcel data = android.os.Parcel.obtain();\n"
834 << " android.os.Parcel reply = android.os.Parcel.obtain();\n"
835 << " try {\n"
Jiyong Park965c5b92018-11-21 13:37:15 +0900836 << " data.writeInterfaceToken(DESCRIPTOR);\n"
Jiyong Park6b39de42019-08-27 13:04:57 +0900837 << " boolean _status = mRemote.transact(Stub." << transactCodeName << ", "
Jiyong Park309668e2018-07-28 16:55:44 +0900838 << "data, reply, 0);\n"
Jiyong Park6b39de42019-08-27 13:04:57 +0900839 << " if (!_status) {\n"
840 << " if (getDefaultImpl() != null) {\n"
841 << " return getDefaultImpl().getInterfaceVersion();\n"
842 << " }\n"
843 << " }\n"
Jeongik Chaf1470e22019-05-20 18:45:05 +0900844 << " reply.readException();\n"
Jiyong Park309668e2018-07-28 16:55:44 +0900845 << " mCachedVersion = reply.readInt();\n"
846 << " } finally {\n"
847 << " reply.recycle();\n"
848 << " data.recycle();\n"
849 << " }\n"
850 << " }\n"
851 << " return mCachedVersion;\n"
852 << "}\n";
Steven Moreland48548e02019-09-18 15:10:22 -0700853 proxy = std::make_shared<LiteralClassElement>(code.str());
Jiyong Park309668e2018-07-28 16:55:44 +0900854 }
Paul Trautrimb77048c2020-01-21 16:39:32 +0900855 if (method.GetName() == kGetInterfaceHash && !options.Hash().empty()) {
856 std::ostringstream code;
857 code << "@Override\n"
858 << "public synchronized String " << kGetInterfaceHash << "()"
859 << " throws "
860 << "android.os.RemoteException {\n"
Jeongik Chab1428182020-05-13 19:49:30 +0900861 << " if (\"-1\".equals(mCachedHash)) {\n"
Paul Trautrimb77048c2020-01-21 16:39:32 +0900862 << " android.os.Parcel data = android.os.Parcel.obtain();\n"
863 << " android.os.Parcel reply = android.os.Parcel.obtain();\n"
864 << " try {\n"
865 << " data.writeInterfaceToken(DESCRIPTOR);\n"
866 << " boolean _status = mRemote.transact(Stub." << transactCodeName << ", "
867 << "data, reply, 0);\n"
868 << " if (!_status) {\n"
869 << " if (getDefaultImpl() != null) {\n"
870 << " return getDefaultImpl().getInterfaceHash();\n"
871 << " }\n"
872 << " }\n"
873 << " reply.readException();\n"
874 << " mCachedHash = reply.readString();\n"
875 << " } finally {\n"
876 << " reply.recycle();\n"
877 << " data.recycle();\n"
878 << " }\n"
879 << " }\n"
880 << " return mCachedHash;\n"
881 << "}\n";
882 proxy = std::make_shared<LiteralClassElement>(code.str());
883 }
Jiyong Park309668e2018-07-28 16:55:44 +0900884 }
885 if (proxy != nullptr) {
886 proxyClass->elements.push_back(proxy);
887 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800888}
889
Jiyong Park5faba3e2020-04-24 17:08:37 +0900890static void generate_interface_descriptors(const Options& options, const AidlInterface* iface,
891 Class* interface, std::shared_ptr<StubClass> stub,
Steven Moreland48548e02019-09-18 15:10:22 -0700892 std::shared_ptr<ProxyClass> proxy) {
Christopher Wiley67502f12016-01-29 10:57:00 -0800893 // the interface descriptor transaction handler
Steven Moreland48548e02019-09-18 15:10:22 -0700894 auto c = std::make_shared<Case>("INTERFACE_TRANSACTION");
895 c->statements->Add(std::make_shared<MethodCall>(
896 stub->transact_reply, "writeString",
897 std::vector<std::shared_ptr<Expression>>{stub->get_transact_descriptor(nullptr)}));
898 c->statements->Add(std::make_shared<ReturnStatement>(TRUE_VALUE));
Christopher Wiley67502f12016-01-29 10:57:00 -0800899 stub->transact_switch->cases.push_back(c);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800900
Christopher Wiley67502f12016-01-29 10:57:00 -0800901 // and the proxy-side method returning the descriptor directly
Steven Moreland48548e02019-09-18 15:10:22 -0700902 auto getDesc = std::make_shared<Method>();
Christopher Wiley67502f12016-01-29 10:57:00 -0800903 getDesc->modifiers = PUBLIC;
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900904 getDesc->returnType = "java.lang.String";
Christopher Wiley67502f12016-01-29 10:57:00 -0800905 getDesc->name = "getInterfaceDescriptor";
Steven Moreland48548e02019-09-18 15:10:22 -0700906 getDesc->statements = std::make_shared<StatementBlock>();
Christopher Wiley67502f12016-01-29 10:57:00 -0800907 getDesc->statements->Add(
Steven Moreland48548e02019-09-18 15:10:22 -0700908 std::make_shared<ReturnStatement>(std::make_shared<LiteralExpression>("DESCRIPTOR")));
Christopher Wiley67502f12016-01-29 10:57:00 -0800909 proxy->elements.push_back(getDesc);
Jiyong Park5faba3e2020-04-24 17:08:37 +0900910
911 // add the DESCRIPTOR field to the interface class
912 Class* classToAddDescriptor = interface;
913 static std::set<std::string> greylist = {
914#include "hiddenapi-greylist"
915 };
916 if (greylist.find(iface->GetCanonicalName()) != greylist.end()) {
917 // For app compatibility, we keep DESCRIPTOR to the stub class for
918 // the interfaces that are in the greylist.
919 classToAddDescriptor = stub.get();
920 }
921 auto descriptor = std::make_shared<Field>(
922 STATIC | FINAL | PUBLIC, std::make_shared<Variable>("java.lang.String", "DESCRIPTOR"));
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900923 std::string name = iface->GetDescriptor();
Jiyong Park5faba3e2020-04-24 17:08:37 +0900924 if (options.IsStructured()) {
925 // mangle the interface name at build time and demangle it at runtime, to avoid
926 // being renamed by jarjar. See b/153843174
Jiyong Park5faba3e2020-04-24 17:08:37 +0900927 std::replace(name.begin(), name.end(), '.', '$');
928 descriptor->value = "\"" + name + "\".replace('$', '.')";
929 } else {
Jiyong Park27fd7fd2020-08-27 16:25:09 +0900930 descriptor->value = "\"" + name + "\"";
Jiyong Park5faba3e2020-04-24 17:08:37 +0900931 }
932 classToAddDescriptor->elements.push_back(descriptor);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800933}
934
Andreas Gampee9c816e2018-03-14 09:05:48 -0700935// Check whether (some) methods in this interface should be "outlined," that
936// is, have specific onTransact methods for certain cases. Set up StubClass
937// metadata accordingly.
938//
939// Outlining will be enabled if the interface has more than outline_threshold
940// methods. In that case, the methods are sorted by number of arguments
941// (so that more "complex" methods come later), and the first non_outline_count
942// number of methods not outlined (are kept in the onTransact() method).
943//
944// Requirements: non_outline_count <= outline_threshold.
945static void compute_outline_methods(const AidlInterface* iface,
Steven Moreland48548e02019-09-18 15:10:22 -0700946 const std::shared_ptr<StubClass> stub, size_t outline_threshold,
Andreas Gampee9c816e2018-03-14 09:05:48 -0700947 size_t non_outline_count) {
948 CHECK_LE(non_outline_count, outline_threshold);
949 // We'll outline (create sub methods) if there are more than min_methods
950 // cases.
951 stub->transact_outline = iface->GetMethods().size() > outline_threshold;
952 if (stub->transact_outline) {
953 stub->all_method_count = iface->GetMethods().size();
954 std::vector<const AidlMethod*> methods;
955 methods.reserve(iface->GetMethods().size());
956 for (const std::unique_ptr<AidlMethod>& ptr : iface->GetMethods()) {
957 methods.push_back(ptr.get());
958 }
959
960 std::stable_sort(
961 methods.begin(),
962 methods.end(),
963 [](const AidlMethod* m1, const AidlMethod* m2) {
964 return m1->GetArguments().size() < m2->GetArguments().size();
965 });
966
967 stub->outline_methods.insert(methods.begin() + non_outline_count,
968 methods.end());
969 }
970}
971
Daniel Norman716d3112019-09-10 13:11:56 -0700972static shared_ptr<ClassElement> generate_default_impl_method(const AidlMethod& method,
973 const AidlTypenames& typenames) {
Steven Moreland48548e02019-09-18 15:10:22 -0700974 auto default_method = std::make_shared<Method>();
Jiyong Park75e1a742018-07-04 12:31:23 +0900975 default_method->comment = method.GetComments();
976 default_method->modifiers = PUBLIC | OVERRIDE;
Daniel Norman716d3112019-09-10 13:11:56 -0700977 default_method->returnType = JavaSignatureOf(method.GetType(), typenames);
Jiyong Park75e1a742018-07-04 12:31:23 +0900978 default_method->name = method.GetName();
Steven Moreland48548e02019-09-18 15:10:22 -0700979 default_method->statements = std::make_shared<StatementBlock>();
Jiyong Park75e1a742018-07-04 12:31:23 +0900980 for (const auto& arg : method.GetArguments()) {
Steven Moreland3dc29d82019-08-21 17:23:11 -0700981 default_method->parameters.push_back(
Daniel Norman716d3112019-09-10 13:11:56 -0700982 std::make_shared<Variable>(JavaSignatureOf(arg->GetType(), typenames), arg->GetName()));
Jiyong Park75e1a742018-07-04 12:31:23 +0900983 }
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900984 default_method->exceptions.push_back("android.os.RemoteException");
Jiyong Park75e1a742018-07-04 12:31:23 +0900985
986 if (method.GetType().GetName() != "void") {
Daniel Norman716d3112019-09-10 13:11:56 -0700987 const string& defaultValue = DefaultJavaValueOf(method.GetType(), typenames);
Jiyong Park75e1a742018-07-04 12:31:23 +0900988 default_method->statements->Add(
Steven Moreland48548e02019-09-18 15:10:22 -0700989 std::make_shared<LiteralStatement>(StringPrintf("return %s;\n", defaultValue.c_str())));
Jiyong Park75e1a742018-07-04 12:31:23 +0900990 }
991 return default_method;
992}
993
Steven Moreland48548e02019-09-18 15:10:22 -0700994static shared_ptr<Class> generate_default_impl_class(const AidlInterface& iface,
Daniel Norman716d3112019-09-10 13:11:56 -0700995 const AidlTypenames& typenames,
Jiyong Park309668e2018-07-28 16:55:44 +0900996 const Options& options) {
Steven Moreland48548e02019-09-18 15:10:22 -0700997 auto default_class = std::make_shared<Class>();
Jiyong Park75e1a742018-07-04 12:31:23 +0900998 default_class->comment = "/** Default implementation for " + iface.GetName() + ". */";
999 default_class->modifiers = PUBLIC | STATIC;
1000 default_class->what = Class::CLASS;
Jeongik Chaa2080bf2019-06-18 16:44:29 +09001001 default_class->type = iface.GetCanonicalName() + ".Default";
1002 default_class->interfaces.emplace_back(iface.GetCanonicalName());
Jiyong Park75e1a742018-07-04 12:31:23 +09001003
1004 for (const auto& m : iface.GetMethods()) {
Jiyong Park309668e2018-07-28 16:55:44 +09001005 if (m->IsUserDefined()) {
Daniel Norman716d3112019-09-10 13:11:56 -07001006 default_class->elements.emplace_back(generate_default_impl_method(*m.get(), typenames));
Jiyong Park309668e2018-07-28 16:55:44 +09001007 } else {
Paul Trautrimb77048c2020-01-21 16:39:32 +09001008 // These are called only when the remote side does not implement these
1009 // methods, which is normally impossible, because these methods are
1010 // automatically declared in the interface class and not implementing
1011 // them on the remote side causes a compilation error. But if the remote
1012 // side somehow managed to not implement it, that's an error and we
1013 // report the case by returning an invalid value here.
Jiyong Park309668e2018-07-28 16:55:44 +09001014 if (m->GetName() == kGetInterfaceVersion && options.Version() > 0) {
1015 std::ostringstream code;
1016 code << "@Override\n"
1017 << "public int " << kGetInterfaceVersion << "() {\n"
Paul Trautrimb77048c2020-01-21 16:39:32 +09001018 << " return 0;\n"
1019 << "}\n";
1020 default_class->elements.emplace_back(std::make_shared<LiteralClassElement>(code.str()));
1021 }
1022 if (m->GetName() == kGetInterfaceHash && !options.Hash().empty()) {
1023 std::ostringstream code;
1024 code << "@Override\n"
1025 << "public String " << kGetInterfaceHash << "() {\n"
1026 << " return \"\";\n"
Jiyong Park309668e2018-07-28 16:55:44 +09001027 << "}\n";
Steven Moreland48548e02019-09-18 15:10:22 -07001028 default_class->elements.emplace_back(std::make_shared<LiteralClassElement>(code.str()));
Jiyong Park309668e2018-07-28 16:55:44 +09001029 }
1030 }
Jiyong Park75e1a742018-07-04 12:31:23 +09001031 }
1032
1033 default_class->elements.emplace_back(
Steven Moreland48548e02019-09-18 15:10:22 -07001034 std::make_shared<LiteralClassElement>("@Override\n"
1035 "public android.os.IBinder asBinder() {\n"
1036 " return null;\n"
1037 "}\n"));
Jiyong Park75e1a742018-07-04 12:31:23 +09001038
1039 return default_class;
1040}
1041
Steven Moreland48548e02019-09-18 15:10:22 -07001042std::unique_ptr<Class> generate_binder_interface_class(const AidlInterface* iface,
1043 const AidlTypenames& typenames,
1044 const Options& options) {
Christopher Wiley67502f12016-01-29 10:57:00 -08001045 // the interface class
Steven Moreland48548e02019-09-18 15:10:22 -07001046 auto interface = std::make_unique<Class>();
Christopher Wiley67502f12016-01-29 10:57:00 -08001047 interface->comment = iface->GetComments();
1048 interface->modifiers = PUBLIC;
1049 interface->what = Class::INTERFACE;
Jeongik Chaa2080bf2019-06-18 16:44:29 +09001050 interface->type = iface->GetCanonicalName();
1051 interface->interfaces.push_back("android.os.IInterface");
Jiyong Parka6605ab2018-11-11 14:30:21 +09001052 interface->annotations = generate_java_annotations(*iface);
Adam Lesinskiffa16862014-01-23 18:17:42 -08001053
Jiyong Park309668e2018-07-28 16:55:44 +09001054 if (options.Version()) {
1055 std::ostringstream code;
1056 code << "/**\n"
1057 << " * The version of this interface that the caller is built against.\n"
1058 << " * This might be different from what {@link #getInterfaceVersion()\n"
1059 << " * getInterfaceVersion} returns as that is the version of the interface\n"
1060 << " * that the remote object is implementing.\n"
1061 << " */\n"
1062 << "public static final int VERSION = " << options.Version() << ";\n";
Steven Moreland48548e02019-09-18 15:10:22 -07001063 interface->elements.emplace_back(std::make_shared<LiteralClassElement>(code.str()));
Jiyong Park309668e2018-07-28 16:55:44 +09001064 }
Paul Trautrimb77048c2020-01-21 16:39:32 +09001065 if (!options.Hash().empty()) {
1066 std::ostringstream code;
1067 code << "public static final String HASH = \"" << options.Hash() << "\";\n";
1068 interface->elements.emplace_back(std::make_shared<LiteralClassElement>(code.str()));
1069 }
Jiyong Park309668e2018-07-28 16:55:44 +09001070
Jiyong Park75e1a742018-07-04 12:31:23 +09001071 // the default impl class
Daniel Norman716d3112019-09-10 13:11:56 -07001072 auto default_impl = generate_default_impl_class(*iface, typenames, options);
Jiyong Park309668e2018-07-28 16:55:44 +09001073 interface->elements.emplace_back(default_impl);
Jiyong Park75e1a742018-07-04 12:31:23 +09001074
Christopher Wiley67502f12016-01-29 10:57:00 -08001075 // the stub inner class
Steven Moreland48548e02019-09-18 15:10:22 -07001076 auto stub = std::make_shared<StubClass>(iface, options);
Christopher Wiley67502f12016-01-29 10:57:00 -08001077 interface->elements.push_back(stub);
Adam Lesinskiffa16862014-01-23 18:17:42 -08001078
Andreas Gampee9c816e2018-03-14 09:05:48 -07001079 compute_outline_methods(iface,
1080 stub,
1081 options.onTransact_outline_threshold_,
1082 options.onTransact_non_outline_count_);
Andreas Gampe1b865af2017-11-22 11:31:47 -08001083
Christopher Wiley67502f12016-01-29 10:57:00 -08001084 // the proxy inner class
Steven Moreland48548e02019-09-18 15:10:22 -07001085 auto proxy = std::make_shared<ProxyClass>(iface, options);
Christopher Wiley67502f12016-01-29 10:57:00 -08001086 stub->elements.push_back(proxy);
Adam Lesinskiffa16862014-01-23 18:17:42 -08001087
Christopher Wiley67502f12016-01-29 10:57:00 -08001088 // stub and proxy support for getInterfaceDescriptor()
Jiyong Park5faba3e2020-04-24 17:08:37 +09001089 generate_interface_descriptors(options, iface, interface.get(), stub, proxy);
Adam Lesinskiffa16862014-01-23 18:17:42 -08001090
Christopher Wiley67502f12016-01-29 10:57:00 -08001091 // all the declared constants of the interface
Steven Moreland693640b2018-07-19 13:46:27 -07001092 for (const auto& constant : iface->GetConstantDeclarations()) {
1093 const AidlConstantValue& value = constant->GetValue();
Jeongik Cha997281d2020-01-16 15:23:59 +09001094 auto comment = constant->GetType().GetComments();
1095 if (comment.length() != 0) {
1096 auto code = StringPrintf("%s\n", comment.c_str());
1097 interface->elements.push_back(std::make_shared<LiteralClassElement>(code));
1098 }
Jiyong Park7c3b0e42020-08-04 16:08:32 +09001099 for (const std::string& annotation : generate_java_annotations(constant->GetType())) {
1100 auto code = StringPrintf("%s\n", annotation.c_str());
1101 interface->elements.push_back(std::make_shared<LiteralClassElement>(code));
1102 }
Steven Moreland693640b2018-07-19 13:46:27 -07001103 switch (value.GetType()) {
1104 case AidlConstantValue::Type::STRING: {
Steven Moreland48548e02019-09-18 15:10:22 -07001105 generate_string_constant(interface.get(), constant->GetName(),
Steven Moreland860b1942018-08-16 14:59:28 -07001106 constant->ValueString(ConstantValueDecorator));
Steven Moreland693640b2018-07-19 13:46:27 -07001107 break;
1108 }
Will McVickerd7d18df2019-09-12 13:40:50 -07001109 case AidlConstantValue::Type::BOOLEAN: // fall-through
1110 case AidlConstantValue::Type::INT8: // fall-through
1111 case AidlConstantValue::Type::INT32: {
Steven Moreland48548e02019-09-18 15:10:22 -07001112 generate_int_constant(interface.get(), constant->GetName(),
Steven Moreland860b1942018-08-16 14:59:28 -07001113 constant->ValueString(ConstantValueDecorator));
Steven Moreland693640b2018-07-19 13:46:27 -07001114 break;
1115 }
1116 default: {
1117 LOG(FATAL) << "Unrecognized constant type: " << static_cast<int>(value.GetType());
1118 }
1119 }
Christopher Wiley67502f12016-01-29 10:57:00 -08001120 }
Casey Dahlind40e2fe2015-11-24 14:06:52 -08001121
Christopher Wiley67502f12016-01-29 10:57:00 -08001122 // all the declared methods of the interface
Andreas Gampe1b865af2017-11-22 11:31:47 -08001123
Christopher Wiley67502f12016-01-29 10:57:00 -08001124 for (const auto& item : iface->GetMethods()) {
Steven Moreland48548e02019-09-18 15:10:22 -07001125 generate_methods(*iface, *item, interface.get(), stub, proxy, item->GetId(), typenames,
1126 options);
Christopher Wiley67502f12016-01-29 10:57:00 -08001127 }
Jiyong Park75e1a742018-07-04 12:31:23 +09001128
1129 // additional static methods for the default impl set/get to the
1130 // stub class. Can't add them to the interface as the generated java files
1131 // may be compiled with Java < 1.7 where static interface method isn't
1132 // supported.
1133 // TODO(b/111417145) make this conditional depending on the Java language
1134 // version requested
Jeongik Chaa2080bf2019-06-18 16:44:29 +09001135 const string i_name = iface->GetCanonicalName();
Steven Moreland48548e02019-09-18 15:10:22 -07001136 stub->elements.emplace_back(std::make_shared<LiteralClassElement>(
Jiyong Park75e1a742018-07-04 12:31:23 +09001137 StringPrintf("public static boolean setDefaultImpl(%s impl) {\n"
Jooyung Han4a044662020-05-13 17:17:07 +09001138 " // Only one user of this interface can use this function\n"
1139 " // at a time. This is a heuristic to detect if two different\n"
1140 " // users in the same process use this function.\n"
1141 " if (Stub.Proxy.sDefaultImpl != null) {\n"
1142 " throw new IllegalStateException(\"setDefaultImpl() called twice\");\n"
1143 " }\n"
1144 " if (impl != null) {\n"
Jiyong Park75e1a742018-07-04 12:31:23 +09001145 " Stub.Proxy.sDefaultImpl = impl;\n"
1146 " return true;\n"
1147 " }\n"
1148 " return false;\n"
1149 "}\n",
1150 i_name.c_str())));
1151 stub->elements.emplace_back(
Steven Moreland48548e02019-09-18 15:10:22 -07001152 std::make_shared<LiteralClassElement>(StringPrintf("public static %s getDefaultImpl() {\n"
1153 " return Stub.Proxy.sDefaultImpl;\n"
1154 "}\n",
1155 i_name.c_str())));
Jiyong Park75e1a742018-07-04 12:31:23 +09001156
1157 // the static field is defined in the proxy class, not in the interface class
1158 // because all fields in an interface class are by default final.
Steven Moreland48548e02019-09-18 15:10:22 -07001159 proxy->elements.emplace_back(std::make_shared<LiteralClassElement>(
Jiyong Park47fb0d62018-11-17 10:12:15 +09001160 StringPrintf("public static %s sDefaultImpl;\n", i_name.c_str())));
Jiyong Park75e1a742018-07-04 12:31:23 +09001161
Andreas Gampea8a66fe2017-11-22 12:17:00 -08001162 stub->finish();
Adam Lesinskiffa16862014-01-23 18:17:42 -08001163
Christopher Wiley67502f12016-01-29 10:57:00 -08001164 return interface;
Adam Lesinskiffa16862014-01-23 18:17:42 -08001165}
1166
Christopher Wileydb154a52015-09-28 16:32:25 -07001167} // namespace java
Christopher Wileyfdeb0f42015-09-11 15:38:22 -07001168} // namespace aidl
Steven Morelandf4c64df2019-07-29 19:54:04 -07001169} // namespace android