blob: daff4807bcd4a4d7e62db62c6dcdb40114fb1bd9 [file] [log] [blame]
Adam Lesinskiffa16862014-01-23 18:17:42 -08001#include "generate_java.h"
Christopher Wileyf690be52015-09-14 15:19:10 -07002
Adam Lesinskiffa16862014-01-23 18:17:42 -08003#include <string.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -07008#include <base/macros.h>
9
Christopher Wiley775fa1f2015-09-22 15:00:12 -070010#include "type_java.h"
Christopher Wileyf690be52015-09-14 15:19:10 -070011
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070012namespace android {
13namespace aidl {
Christopher Wileydb154a52015-09-28 16:32:25 -070014namespace java {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070015
Adam Lesinskiffa16862014-01-23 18:17:42 -080016// =================================================
17class StubClass : public Class
18{
19public:
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -070020 StubClass(const Type* type, const Type* interfaceType,
21 JavaTypeNamespace* types);
Adam Lesinskiffa16862014-01-23 18:17:42 -080022 virtual ~StubClass();
23
24 Variable* transact_code;
25 Variable* transact_data;
26 Variable* transact_reply;
27 Variable* transact_flags;
28 SwitchStatement* transact_switch;
29private:
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -070030 void make_as_interface(const Type* interfaceType, JavaTypeNamespace* types);
31
32 DISALLOW_COPY_AND_ASSIGN(StubClass);
Adam Lesinskiffa16862014-01-23 18:17:42 -080033};
34
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -070035StubClass::StubClass(const Type* type, const Type* interfaceType,
36 JavaTypeNamespace* types)
Adam Lesinskiffa16862014-01-23 18:17:42 -080037 :Class()
38{
39 this->comment = "/** Local-side IPC implementation stub class. */";
40 this->modifiers = PUBLIC | ABSTRACT | STATIC;
41 this->what = Class::CLASS;
42 this->type = type;
Christopher Wiley60b02ae2015-09-23 16:35:18 -070043 this->extends = types->BinderNativeType();
Adam Lesinskiffa16862014-01-23 18:17:42 -080044 this->interfaces.push_back(interfaceType);
45
46 // descriptor
47 Field* descriptor = new Field(STATIC | FINAL | PRIVATE,
Christopher Wiley60b02ae2015-09-23 16:35:18 -070048 new Variable(types->StringType(), "DESCRIPTOR"));
Adam Lesinskiffa16862014-01-23 18:17:42 -080049 descriptor->value = "\"" + interfaceType->QualifiedName() + "\"";
50 this->elements.push_back(descriptor);
51
52 // ctor
53 Method* ctor = new Method;
54 ctor->modifiers = PUBLIC;
55 ctor->comment = "/** Construct the stub at attach it to the "
56 "interface. */";
57 ctor->name = "Stub";
58 ctor->statements = new StatementBlock;
59 MethodCall* attach = new MethodCall(THIS_VALUE, "attachInterface",
60 2, THIS_VALUE, new LiteralExpression("DESCRIPTOR"));
61 ctor->statements->Add(attach);
62 this->elements.push_back(ctor);
63
64 // asInterface
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -070065 make_as_interface(interfaceType, types);
Adam Lesinskiffa16862014-01-23 18:17:42 -080066
67 // asBinder
68 Method* asBinder = new Method;
69 asBinder->modifiers = PUBLIC | OVERRIDE;
Christopher Wiley60b02ae2015-09-23 16:35:18 -070070 asBinder->returnType = types->IBinderType();
Adam Lesinskiffa16862014-01-23 18:17:42 -080071 asBinder->name = "asBinder";
72 asBinder->statements = new StatementBlock;
73 asBinder->statements->Add(new ReturnStatement(THIS_VALUE));
74 this->elements.push_back(asBinder);
75
76 // onTransact
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -070077 this->transact_code = new Variable(types->IntType(), "code");
Christopher Wiley60b02ae2015-09-23 16:35:18 -070078 this->transact_data = new Variable(types->ParcelType(), "data");
79 this->transact_reply = new Variable(types->ParcelType(), "reply");
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -070080 this->transact_flags = new Variable(types->IntType(), "flags");
Adam Lesinskiffa16862014-01-23 18:17:42 -080081 Method* onTransact = new Method;
82 onTransact->modifiers = PUBLIC | OVERRIDE;
Christopher Wiley60b02ae2015-09-23 16:35:18 -070083 onTransact->returnType = types->BoolType();
Adam Lesinskiffa16862014-01-23 18:17:42 -080084 onTransact->name = "onTransact";
85 onTransact->parameters.push_back(this->transact_code);
86 onTransact->parameters.push_back(this->transact_data);
87 onTransact->parameters.push_back(this->transact_reply);
88 onTransact->parameters.push_back(this->transact_flags);
89 onTransact->statements = new StatementBlock;
Christopher Wiley60b02ae2015-09-23 16:35:18 -070090 onTransact->exceptions.push_back(types->RemoteExceptionType());
Adam Lesinskiffa16862014-01-23 18:17:42 -080091 this->elements.push_back(onTransact);
92 this->transact_switch = new SwitchStatement(this->transact_code);
93
94 onTransact->statements->Add(this->transact_switch);
95 MethodCall* superCall = new MethodCall(SUPER_VALUE, "onTransact", 4,
96 this->transact_code, this->transact_data,
97 this->transact_reply, this->transact_flags);
98 onTransact->statements->Add(new ReturnStatement(superCall));
99}
100
101StubClass::~StubClass()
102{
103}
104
105void
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700106StubClass::make_as_interface(const Type *interfaceType,
107 JavaTypeNamespace* types)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800108{
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700109 Variable* obj = new Variable(types->IBinderType(), "obj");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800110
111 Method* m = new Method;
112 m->comment = "/**\n * Cast an IBinder object into an ";
113 m->comment += interfaceType->QualifiedName();
114 m->comment += " interface,\n";
115 m->comment += " * generating a proxy if needed.\n */";
116 m->modifiers = PUBLIC | STATIC;
117 m->returnType = interfaceType;
118 m->name = "asInterface";
119 m->parameters.push_back(obj);
120 m->statements = new StatementBlock;
121
122 IfStatement* ifstatement = new IfStatement();
123 ifstatement->expression = new Comparison(obj, "==", NULL_VALUE);
124 ifstatement->statements = new StatementBlock;
125 ifstatement->statements->Add(new ReturnStatement(NULL_VALUE));
126 m->statements->Add(ifstatement);
127
128 // IInterface iin = obj.queryLocalInterface(DESCRIPTOR)
129 MethodCall* queryLocalInterface = new MethodCall(obj, "queryLocalInterface");
130 queryLocalInterface->arguments.push_back(new LiteralExpression("DESCRIPTOR"));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700131 IInterfaceType* iinType = new IInterfaceType(types);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800132 Variable *iin = new Variable(iinType, "iin");
133 VariableDeclaration* iinVd = new VariableDeclaration(iin, queryLocalInterface, NULL);
134 m->statements->Add(iinVd);
135
136 // Ensure the instance type of the local object is as expected.
137 // One scenario where this is needed is if another package (with a
138 // different class loader) runs in the same process as the service.
139
140 // if (iin != null && iin instanceof <interfaceType>) return (<interfaceType>) iin;
141 Comparison* iinNotNull = new Comparison(iin, "!=", NULL_VALUE);
142 Comparison* instOfCheck = new Comparison(iin, " instanceof ",
143 new LiteralExpression(interfaceType->QualifiedName()));
144 IfStatement* instOfStatement = new IfStatement();
145 instOfStatement->expression = new Comparison(iinNotNull, "&&", instOfCheck);
146 instOfStatement->statements = new StatementBlock;
147 instOfStatement->statements->Add(new ReturnStatement(new Cast(interfaceType, iin)));
148 m->statements->Add(instOfStatement);
149
150 string proxyType = interfaceType->QualifiedName();
151 proxyType += ".Stub.Proxy";
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700152 NewExpression* ne = new NewExpression(types->Find(proxyType));
Adam Lesinskiffa16862014-01-23 18:17:42 -0800153 ne->arguments.push_back(obj);
154 m->statements->Add(new ReturnStatement(ne));
155
156 this->elements.push_back(m);
157}
158
159
160
161// =================================================
162class ProxyClass : public Class
163{
164public:
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700165 ProxyClass(const JavaTypeNamespace* types, const Type* type,
166 const InterfaceType* interfaceType);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800167 virtual ~ProxyClass();
168
169 Variable* mRemote;
170 bool mOneWay;
171};
172
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700173ProxyClass::ProxyClass(const JavaTypeNamespace* types,
174 const Type* type, const InterfaceType* interfaceType)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800175 :Class()
176{
177 this->modifiers = PRIVATE | STATIC;
178 this->what = Class::CLASS;
179 this->type = type;
180 this->interfaces.push_back(interfaceType);
181
182 mOneWay = interfaceType->OneWay();
183
184 // IBinder mRemote
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700185 mRemote = new Variable(types->IBinderType(), "mRemote");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800186 this->elements.push_back(new Field(PRIVATE, mRemote));
187
188 // Proxy()
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700189 Variable* remote = new Variable(types->IBinderType(), "remote");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800190 Method* ctor = new Method;
191 ctor->name = "Proxy";
192 ctor->statements = new StatementBlock;
193 ctor->parameters.push_back(remote);
194 ctor->statements->Add(new Assignment(mRemote, remote));
195 this->elements.push_back(ctor);
196
197 // IBinder asBinder()
198 Method* asBinder = new Method;
199 asBinder->modifiers = PUBLIC | OVERRIDE;
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700200 asBinder->returnType = types->IBinderType();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800201 asBinder->name = "asBinder";
202 asBinder->statements = new StatementBlock;
203 asBinder->statements->Add(new ReturnStatement(mRemote));
204 this->elements.push_back(asBinder);
205}
206
207ProxyClass::~ProxyClass()
208{
209}
210
211// =================================================
212static void
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700213generate_new_array(const Type* t, StatementBlock* addTo, Variable* v,
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700214 Variable* parcel, JavaTypeNamespace* types)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800215{
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700216 Variable* len = new Variable(types->IntType(), v->name + "_length");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800217 addTo->Add(new VariableDeclaration(len, new MethodCall(parcel, "readInt")));
218 IfStatement* lencheck = new IfStatement();
219 lencheck->expression = new Comparison(len, "<", new LiteralExpression("0"));
220 lencheck->statements->Add(new Assignment(v, NULL_VALUE));
221 lencheck->elseif = new IfStatement();
222 lencheck->elseif->statements->Add(new Assignment(v,
223 new NewArrayExpression(t, len)));
224 addTo->Add(lencheck);
225}
226
227static void
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700228generate_write_to_parcel(const Type* t, StatementBlock* addTo, Variable* v,
Adam Lesinskiffa16862014-01-23 18:17:42 -0800229 Variable* parcel, int flags)
230{
231 if (v->dimension == 0) {
232 t->WriteToParcel(addTo, v, parcel, flags);
233 }
234 if (v->dimension == 1) {
235 t->WriteArrayToParcel(addTo, v, parcel, flags);
236 }
237}
238
239static void
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700240generate_create_from_parcel(const Type* t, StatementBlock* addTo, Variable* v,
Adam Lesinskiffa16862014-01-23 18:17:42 -0800241 Variable* parcel, Variable** cl)
242{
243 if (v->dimension == 0) {
244 t->CreateFromParcel(addTo, v, parcel, cl);
245 }
246 if (v->dimension == 1) {
247 t->CreateArrayFromParcel(addTo, v, parcel, cl);
248 }
249}
250
251static void
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700252generate_read_from_parcel(const Type* t, StatementBlock* addTo, Variable* v,
Adam Lesinskiffa16862014-01-23 18:17:42 -0800253 Variable* parcel, Variable** cl)
254{
255 if (v->dimension == 0) {
256 t->ReadFromParcel(addTo, v, parcel, cl);
257 }
258 if (v->dimension == 1) {
259 t->ReadArrayFromParcel(addTo, v, parcel, cl);
260 }
261}
262
263
264static void
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800265generate_constant(const AidlConstant& constant, Class* interface)
266{
267 Constant* decl = new Constant;
268 decl->name = constant.GetName();
269 decl->value = constant.GetValue();
270
271 interface->elements.push_back(decl);
272}
273
274static void
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700275generate_method(const AidlMethod& method, Class* interface,
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700276 StubClass* stubClass, ProxyClass* proxyClass, int index,
277 JavaTypeNamespace* types)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800278{
Adam Lesinskiffa16862014-01-23 18:17:42 -0800279 int i;
280 bool hasOutParams = false;
281
Casey Dahlinf4a93112015-10-05 16:58:09 -0700282 const bool oneway = proxyClass->mOneWay || method.IsOneway();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800283
284 // == the TRANSACT_ constant =============================================
285 string transactCodeName = "TRANSACTION_";
Casey Dahlinf4a93112015-10-05 16:58:09 -0700286 transactCodeName += method.GetName();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800287
288 char transactCodeValue[60];
289 sprintf(transactCodeValue, "(android.os.IBinder.FIRST_CALL_TRANSACTION + %d)", index);
290
291 Field* transactCode = new Field(STATIC | FINAL,
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700292 new Variable(types->IntType(), transactCodeName));
Adam Lesinskiffa16862014-01-23 18:17:42 -0800293 transactCode->value = transactCodeValue;
294 stubClass->elements.push_back(transactCode);
295
296 // == the declaration in the interface ===================================
297 Method* decl = new Method;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700298 decl->comment = method.GetComments();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800299 decl->modifiers = PUBLIC;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700300 decl->returnType = types->Find(method.GetType().GetName());
Casey Dahlinf7a421c2015-10-05 17:24:28 -0700301 decl->returnTypeDimension = method.GetType().IsArray() ? 1 : 0;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700302 decl->name = method.GetName();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800303
Casey Dahlinf4a93112015-10-05 16:58:09 -0700304 for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800305 decl->parameters.push_back(new Variable(
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700306 types->Find(arg->GetType().GetName()), arg->GetName(),
Casey Dahlinf7a421c2015-10-05 17:24:28 -0700307 arg->GetType().IsArray() ? 1 : 0));
Adam Lesinskiffa16862014-01-23 18:17:42 -0800308 }
309
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700310 decl->exceptions.push_back(types->RemoteExceptionType());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800311
312 interface->elements.push_back(decl);
313
314 // == the stub method ====================================================
315
316 Case* c = new Case(transactCodeName);
317
Casey Dahlinf4a93112015-10-05 16:58:09 -0700318 MethodCall* realCall = new MethodCall(THIS_VALUE, method.GetName());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800319
320 // interface token validation is the very first thing we do
321 c->statements->Add(new MethodCall(stubClass->transact_data,
322 "enforceInterface", 1, new LiteralExpression("DESCRIPTOR")));
323
324 // args
325 Variable* cl = NULL;
326 VariableFactory stubArgs("_arg");
Casey Dahlinf4a93112015-10-05 16:58:09 -0700327 for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) {
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700328 const Type* t = types->Find(arg->GetType().GetName());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800329 Variable* v = stubArgs.Get(t);
Casey Dahlinf7a421c2015-10-05 17:24:28 -0700330 v->dimension = arg->GetType().IsArray() ? 1 : 0;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800331
332 c->statements->Add(new VariableDeclaration(v));
333
Casey Dahlinc378c992015-09-29 16:50:40 -0700334 if (arg->GetDirection() & AidlArgument::IN_DIR) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800335 generate_create_from_parcel(t, c->statements, v,
336 stubClass->transact_data, &cl);
337 } else {
Casey Dahlinf7a421c2015-10-05 17:24:28 -0700338 if (!arg->GetType().IsArray()) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800339 c->statements->Add(new Assignment(v, new NewExpression(v->type)));
Casey Dahlinf7a421c2015-10-05 17:24:28 -0700340 } else {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800341 generate_new_array(v->type, c->statements, v,
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700342 stubClass->transact_data, types);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800343 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800344 }
345
346 realCall->arguments.push_back(v);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800347 }
348
349 // the real call
350 Variable* _result = NULL;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700351 if (method.GetType().GetName() == "void") {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800352 c->statements->Add(realCall);
353
354 if (!oneway) {
355 // report that there were no exceptions
356 MethodCall* ex = new MethodCall(stubClass->transact_reply,
357 "writeNoException", 0);
358 c->statements->Add(ex);
359 }
360 } else {
361 _result = new Variable(decl->returnType, "_result",
362 decl->returnTypeDimension);
363 c->statements->Add(new VariableDeclaration(_result, realCall));
364
365 if (!oneway) {
366 // report that there were no exceptions
367 MethodCall* ex = new MethodCall(stubClass->transact_reply,
368 "writeNoException", 0);
369 c->statements->Add(ex);
370 }
371
372 // marshall the return value
373 generate_write_to_parcel(decl->returnType, c->statements, _result,
374 stubClass->transact_reply,
375 Type::PARCELABLE_WRITE_RETURN_VALUE);
376 }
377
378 // out parameters
379 i = 0;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700380 for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) {
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700381 const Type* t = types->Find(arg->GetType().GetName());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800382 Variable* v = stubArgs.Get(i++);
383
Casey Dahlinc378c992015-09-29 16:50:40 -0700384 if (arg->GetDirection() & AidlArgument::OUT_DIR) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800385 generate_write_to_parcel(t, c->statements, v,
386 stubClass->transact_reply,
387 Type::PARCELABLE_WRITE_RETURN_VALUE);
388 hasOutParams = true;
389 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800390 }
391
392 // return true
393 c->statements->Add(new ReturnStatement(TRUE_VALUE));
394 stubClass->transact_switch->cases.push_back(c);
395
396 // == the proxy method ===================================================
397 Method* proxy = new Method;
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700398 proxy->comment = method.GetComments();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800399 proxy->modifiers = PUBLIC | OVERRIDE;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700400 proxy->returnType = types->Find(method.GetType().GetName());
Casey Dahlinf7a421c2015-10-05 17:24:28 -0700401 proxy->returnTypeDimension = method.GetType().IsArray() ? 1 : 0;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700402 proxy->name = method.GetName();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800403 proxy->statements = new StatementBlock;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700404 for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800405 proxy->parameters.push_back(new Variable(
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700406 types->Find(arg->GetType().GetName()), arg->GetName(),
Casey Dahlinf7a421c2015-10-05 17:24:28 -0700407 arg->GetType().IsArray() ? 1 : 0));
Adam Lesinskiffa16862014-01-23 18:17:42 -0800408 }
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700409 proxy->exceptions.push_back(types->RemoteExceptionType());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800410 proxyClass->elements.push_back(proxy);
411
412 // the parcels
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700413 Variable* _data = new Variable(types->ParcelType(), "_data");
Adam Lesinskiffa16862014-01-23 18:17:42 -0800414 proxy->statements->Add(new VariableDeclaration(_data,
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700415 new MethodCall(types->ParcelType(), "obtain")));
Adam Lesinskiffa16862014-01-23 18:17:42 -0800416 Variable* _reply = NULL;
417 if (!oneway) {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700418 _reply = new Variable(types->ParcelType(), "_reply");
419 proxy->statements->Add(
420 new VariableDeclaration(
421 _reply,
422 new MethodCall(types->ParcelType(), "obtain")));
Adam Lesinskiffa16862014-01-23 18:17:42 -0800423 }
424
425 // the return value
426 _result = NULL;
Casey Dahlinf4a93112015-10-05 16:58:09 -0700427 if (method.GetType().GetName() != "void") {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800428 _result = new Variable(proxy->returnType, "_result",
Casey Dahlinf7a421c2015-10-05 17:24:28 -0700429 method.GetType().IsArray() ? 1 : 0);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800430 proxy->statements->Add(new VariableDeclaration(_result));
431 }
432
433 // try and finally
434 TryStatement* tryStatement = new TryStatement();
435 proxy->statements->Add(tryStatement);
436 FinallyStatement* finallyStatement = new FinallyStatement();
437 proxy->statements->Add(finallyStatement);
438
439 // the interface identifier token: the DESCRIPTOR constant, marshalled as a string
440 tryStatement->statements->Add(new MethodCall(_data, "writeInterfaceToken",
441 1, new LiteralExpression("DESCRIPTOR")));
442
443 // the parameters
Casey Dahlinf4a93112015-10-05 16:58:09 -0700444 for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) {
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700445 const Type* t = types->Find(arg->GetType().GetName());
Casey Dahlinf7a421c2015-10-05 17:24:28 -0700446 Variable* v = new Variable(t, arg->GetName(), arg->GetType().IsArray() ? 1 : 0);
Casey Dahlinc378c992015-09-29 16:50:40 -0700447 AidlArgument::Direction dir = arg->GetDirection();
Casey Dahlinf7a421c2015-10-05 17:24:28 -0700448 if (dir == AidlArgument::OUT_DIR && arg->GetType().IsArray()) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800449 IfStatement* checklen = new IfStatement();
450 checklen->expression = new Comparison(v, "==", NULL_VALUE);
451 checklen->statements->Add(new MethodCall(_data, "writeInt", 1,
452 new LiteralExpression("-1")));
453 checklen->elseif = new IfStatement();
454 checklen->elseif->statements->Add(new MethodCall(_data, "writeInt",
455 1, new FieldVariable(v, "length")));
456 tryStatement->statements->Add(checklen);
457 }
Casey Dahlinc378c992015-09-29 16:50:40 -0700458 else if (dir & AidlArgument::IN_DIR) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800459 generate_write_to_parcel(t, tryStatement->statements, v, _data, 0);
460 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800461 }
462
463 // the transact call
464 MethodCall* call = new MethodCall(proxyClass->mRemote, "transact", 4,
465 new LiteralExpression("Stub." + transactCodeName),
466 _data, _reply ? _reply : NULL_VALUE,
467 new LiteralExpression(
468 oneway ? "android.os.IBinder.FLAG_ONEWAY" : "0"));
469 tryStatement->statements->Add(call);
470
471 // throw back exceptions.
472 if (_reply) {
473 MethodCall* ex = new MethodCall(_reply, "readException", 0);
474 tryStatement->statements->Add(ex);
475 }
476
477 // returning and cleanup
478 if (_reply != NULL) {
479 if (_result != NULL) {
480 generate_create_from_parcel(proxy->returnType,
481 tryStatement->statements, _result, _reply, &cl);
482 }
483
484 // the out/inout parameters
Casey Dahlinf4a93112015-10-05 16:58:09 -0700485 for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) {
Casey Dahlinf2d23f72015-10-02 16:19:19 -0700486 const Type* t = types->Find(arg->GetType().GetName());
Casey Dahlinf7a421c2015-10-05 17:24:28 -0700487 Variable* v = new Variable(t, arg->GetName(), arg->GetType().IsArray() ? 1 : 0);
Casey Dahlinc378c992015-09-29 16:50:40 -0700488 if (arg->GetDirection() & AidlArgument::OUT_DIR) {
Adam Lesinskiffa16862014-01-23 18:17:42 -0800489 generate_read_from_parcel(t, tryStatement->statements,
490 v, _reply, &cl);
491 }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800492 }
493
494 finallyStatement->statements->Add(new MethodCall(_reply, "recycle"));
495 }
496 finallyStatement->statements->Add(new MethodCall(_data, "recycle"));
497
498 if (_result != NULL) {
499 proxy->statements->Add(new ReturnStatement(_result));
500 }
501}
502
503static void
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700504generate_interface_descriptors(StubClass* stub, ProxyClass* proxy,
505 const JavaTypeNamespace* types)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800506{
507 // the interface descriptor transaction handler
508 Case* c = new Case("INTERFACE_TRANSACTION");
509 c->statements->Add(new MethodCall(stub->transact_reply, "writeString",
510 1, new LiteralExpression("DESCRIPTOR")));
511 c->statements->Add(new ReturnStatement(TRUE_VALUE));
512 stub->transact_switch->cases.push_back(c);
513
514 // and the proxy-side method returning the descriptor directly
515 Method* getDesc = new Method;
516 getDesc->modifiers = PUBLIC;
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700517 getDesc->returnType = types->StringType();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800518 getDesc->returnTypeDimension = 0;
519 getDesc->name = "getInterfaceDescriptor";
520 getDesc->statements = new StatementBlock;
521 getDesc->statements->Add(new ReturnStatement(new LiteralExpression("DESCRIPTOR")));
522 proxy->elements.push_back(getDesc);
523}
524
525Class*
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700526generate_binder_interface_class(const AidlInterface* iface,
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700527 JavaTypeNamespace* types)
Adam Lesinskiffa16862014-01-23 18:17:42 -0800528{
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700529 const InterfaceType* interfaceType = static_cast<const InterfaceType*>(
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700530 types->Find(iface->GetCanonicalName()));
Adam Lesinskiffa16862014-01-23 18:17:42 -0800531
532 // the interface class
533 Class* interface = new Class;
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700534 interface->comment = iface->GetComments();
Adam Lesinskiffa16862014-01-23 18:17:42 -0800535 interface->modifiers = PUBLIC;
536 interface->what = Class::INTERFACE;
537 interface->type = interfaceType;
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700538 interface->interfaces.push_back(types->IInterfaceType());
Adam Lesinskiffa16862014-01-23 18:17:42 -0800539
540 // the stub inner class
541 StubClass* stub = new StubClass(
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700542 types->Find(iface->GetCanonicalName() + ".Stub"),
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700543 interfaceType, types);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800544 interface->elements.push_back(stub);
545
546 // the proxy inner class
547 ProxyClass* proxy = new ProxyClass(
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700548 types,
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700549 types->Find(iface->GetCanonicalName() + ".Stub.Proxy"),
Adam Lesinskiffa16862014-01-23 18:17:42 -0800550 interfaceType);
551 stub->elements.push_back(proxy);
552
553 // stub and proxy support for getInterfaceDescriptor()
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700554 generate_interface_descriptors(stub, proxy, types);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800555
Casey Dahlind40e2fe2015-11-24 14:06:52 -0800556 // all the declared constants of the interface
557 for (const auto& item : iface->GetConstants()) {
558 generate_constant(*item, interface);
559 }
560
Adam Lesinskiffa16862014-01-23 18:17:42 -0800561 // all the declared methods of the interface
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700562 for (const auto& item : iface->GetMethods()) {
Casey Dahlin5c69deb2015-10-01 14:44:12 -0700563 generate_method(*item, interface, stub, proxy,
Casey Dahlinf4a93112015-10-05 16:58:09 -0700564 item->GetId(), types);
Adam Lesinskiffa16862014-01-23 18:17:42 -0800565 }
566
567 return interface;
568}
569
Christopher Wileydb154a52015-09-28 16:32:25 -0700570} // namespace java
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700571} // namespace android
572} // namespace aidl