| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1 | #include "generate_java.h" |
| Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 2 | |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3 | #include <string.h> |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 8 | #include <base/macros.h> |
| 9 | |
| Christopher Wiley | 775fa1f | 2015-09-22 15:00:12 -0700 | [diff] [blame] | 10 | #include "type_java.h" |
| Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 11 | |
| Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 12 | namespace android { |
| 13 | namespace aidl { |
| Christopher Wiley | db154a5 | 2015-09-28 16:32:25 -0700 | [diff] [blame] | 14 | namespace java { |
| Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 15 | |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 16 | // ================================================= |
| 17 | class StubClass : public Class |
| 18 | { |
| 19 | public: |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 20 | StubClass(const Type* type, const Type* interfaceType, |
| 21 | JavaTypeNamespace* types); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 22 | virtual ~StubClass(); |
| 23 | |
| 24 | Variable* transact_code; |
| 25 | Variable* transact_data; |
| 26 | Variable* transact_reply; |
| 27 | Variable* transact_flags; |
| 28 | SwitchStatement* transact_switch; |
| 29 | private: |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 30 | void make_as_interface(const Type* interfaceType, JavaTypeNamespace* types); |
| 31 | |
| 32 | DISALLOW_COPY_AND_ASSIGN(StubClass); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 33 | }; |
| 34 | |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 35 | StubClass::StubClass(const Type* type, const Type* interfaceType, |
| 36 | JavaTypeNamespace* types) |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 37 | :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 Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 43 | this->extends = types->BinderNativeType(); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 44 | this->interfaces.push_back(interfaceType); |
| 45 | |
| 46 | // descriptor |
| 47 | Field* descriptor = new Field(STATIC | FINAL | PRIVATE, |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 48 | new Variable(types->StringType(), "DESCRIPTOR")); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 49 | 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 Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 65 | make_as_interface(interfaceType, types); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 66 | |
| 67 | // asBinder |
| 68 | Method* asBinder = new Method; |
| 69 | asBinder->modifiers = PUBLIC | OVERRIDE; |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 70 | asBinder->returnType = types->IBinderType(); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 71 | 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 Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 77 | this->transact_code = new Variable(types->IntType(), "code"); |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 78 | this->transact_data = new Variable(types->ParcelType(), "data"); |
| 79 | this->transact_reply = new Variable(types->ParcelType(), "reply"); |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 80 | this->transact_flags = new Variable(types->IntType(), "flags"); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 81 | Method* onTransact = new Method; |
| 82 | onTransact->modifiers = PUBLIC | OVERRIDE; |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 83 | onTransact->returnType = types->BoolType(); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 84 | 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 Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 90 | onTransact->exceptions.push_back(types->RemoteExceptionType()); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 91 | 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 | |
| 101 | StubClass::~StubClass() |
| 102 | { |
| 103 | } |
| 104 | |
| 105 | void |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 106 | StubClass::make_as_interface(const Type *interfaceType, |
| 107 | JavaTypeNamespace* types) |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 108 | { |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 109 | Variable* obj = new Variable(types->IBinderType(), "obj"); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 110 | |
| 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 Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 131 | IInterfaceType* iinType = new IInterfaceType(types); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 132 | 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 Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 152 | NewExpression* ne = new NewExpression(types->Find(proxyType)); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 153 | ne->arguments.push_back(obj); |
| 154 | m->statements->Add(new ReturnStatement(ne)); |
| 155 | |
| 156 | this->elements.push_back(m); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | |
| 161 | // ================================================= |
| 162 | class ProxyClass : public Class |
| 163 | { |
| 164 | public: |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 165 | ProxyClass(const JavaTypeNamespace* types, const Type* type, |
| 166 | const InterfaceType* interfaceType); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 167 | virtual ~ProxyClass(); |
| 168 | |
| 169 | Variable* mRemote; |
| 170 | bool mOneWay; |
| 171 | }; |
| 172 | |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 173 | ProxyClass::ProxyClass(const JavaTypeNamespace* types, |
| 174 | const Type* type, const InterfaceType* interfaceType) |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 175 | :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 Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 185 | mRemote = new Variable(types->IBinderType(), "mRemote"); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 186 | this->elements.push_back(new Field(PRIVATE, mRemote)); |
| 187 | |
| 188 | // Proxy() |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 189 | Variable* remote = new Variable(types->IBinderType(), "remote"); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 190 | 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 Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 200 | asBinder->returnType = types->IBinderType(); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 201 | asBinder->name = "asBinder"; |
| 202 | asBinder->statements = new StatementBlock; |
| 203 | asBinder->statements->Add(new ReturnStatement(mRemote)); |
| 204 | this->elements.push_back(asBinder); |
| 205 | } |
| 206 | |
| 207 | ProxyClass::~ProxyClass() |
| 208 | { |
| 209 | } |
| 210 | |
| 211 | // ================================================= |
| 212 | static void |
| Christopher Wiley | 8f6816e | 2015-09-22 17:03:47 -0700 | [diff] [blame] | 213 | generate_new_array(const Type* t, StatementBlock* addTo, Variable* v, |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 214 | Variable* parcel, JavaTypeNamespace* types) |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 215 | { |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 216 | Variable* len = new Variable(types->IntType(), v->name + "_length"); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 217 | 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 | |
| 227 | static void |
| Christopher Wiley | 8f6816e | 2015-09-22 17:03:47 -0700 | [diff] [blame] | 228 | generate_write_to_parcel(const Type* t, StatementBlock* addTo, Variable* v, |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 229 | 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 | |
| 239 | static void |
| Christopher Wiley | 8f6816e | 2015-09-22 17:03:47 -0700 | [diff] [blame] | 240 | generate_create_from_parcel(const Type* t, StatementBlock* addTo, Variable* v, |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 241 | 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 | |
| 251 | static void |
| Christopher Wiley | 8f6816e | 2015-09-22 17:03:47 -0700 | [diff] [blame] | 252 | generate_read_from_parcel(const Type* t, StatementBlock* addTo, Variable* v, |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 253 | 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 | |
| 264 | static void |
| Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 265 | generate_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 | |
| 274 | static void |
| Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 275 | generate_method(const AidlMethod& method, Class* interface, |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 276 | StubClass* stubClass, ProxyClass* proxyClass, int index, |
| 277 | JavaTypeNamespace* types) |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 278 | { |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 279 | int i; |
| 280 | bool hasOutParams = false; |
| 281 | |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 282 | const bool oneway = proxyClass->mOneWay || method.IsOneway(); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 283 | |
| 284 | // == the TRANSACT_ constant ============================================= |
| 285 | string transactCodeName = "TRANSACTION_"; |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 286 | transactCodeName += method.GetName(); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 287 | |
| 288 | char transactCodeValue[60]; |
| 289 | sprintf(transactCodeValue, "(android.os.IBinder.FIRST_CALL_TRANSACTION + %d)", index); |
| 290 | |
| 291 | Field* transactCode = new Field(STATIC | FINAL, |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 292 | new Variable(types->IntType(), transactCodeName)); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 293 | transactCode->value = transactCodeValue; |
| 294 | stubClass->elements.push_back(transactCode); |
| 295 | |
| 296 | // == the declaration in the interface =================================== |
| 297 | Method* decl = new Method; |
| Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 298 | decl->comment = method.GetComments(); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 299 | decl->modifiers = PUBLIC; |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 300 | decl->returnType = types->Find(method.GetType().GetName()); |
| Casey Dahlin | f7a421c | 2015-10-05 17:24:28 -0700 | [diff] [blame] | 301 | decl->returnTypeDimension = method.GetType().IsArray() ? 1 : 0; |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 302 | decl->name = method.GetName(); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 303 | |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 304 | for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) { |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 305 | decl->parameters.push_back(new Variable( |
| Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 306 | types->Find(arg->GetType().GetName()), arg->GetName(), |
| Casey Dahlin | f7a421c | 2015-10-05 17:24:28 -0700 | [diff] [blame] | 307 | arg->GetType().IsArray() ? 1 : 0)); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 308 | } |
| 309 | |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 310 | decl->exceptions.push_back(types->RemoteExceptionType()); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 311 | |
| 312 | interface->elements.push_back(decl); |
| 313 | |
| 314 | // == the stub method ==================================================== |
| 315 | |
| 316 | Case* c = new Case(transactCodeName); |
| 317 | |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 318 | MethodCall* realCall = new MethodCall(THIS_VALUE, method.GetName()); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 319 | |
| 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 Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 327 | for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) { |
| Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 328 | const Type* t = types->Find(arg->GetType().GetName()); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 329 | Variable* v = stubArgs.Get(t); |
| Casey Dahlin | f7a421c | 2015-10-05 17:24:28 -0700 | [diff] [blame] | 330 | v->dimension = arg->GetType().IsArray() ? 1 : 0; |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 331 | |
| 332 | c->statements->Add(new VariableDeclaration(v)); |
| 333 | |
| Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 334 | if (arg->GetDirection() & AidlArgument::IN_DIR) { |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 335 | generate_create_from_parcel(t, c->statements, v, |
| 336 | stubClass->transact_data, &cl); |
| 337 | } else { |
| Casey Dahlin | f7a421c | 2015-10-05 17:24:28 -0700 | [diff] [blame] | 338 | if (!arg->GetType().IsArray()) { |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 339 | c->statements->Add(new Assignment(v, new NewExpression(v->type))); |
| Casey Dahlin | f7a421c | 2015-10-05 17:24:28 -0700 | [diff] [blame] | 340 | } else { |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 341 | generate_new_array(v->type, c->statements, v, |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 342 | stubClass->transact_data, types); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 343 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | realCall->arguments.push_back(v); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | // the real call |
| 350 | Variable* _result = NULL; |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 351 | if (method.GetType().GetName() == "void") { |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 352 | 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 Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 380 | for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) { |
| Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 381 | const Type* t = types->Find(arg->GetType().GetName()); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 382 | Variable* v = stubArgs.Get(i++); |
| 383 | |
| Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 384 | if (arg->GetDirection() & AidlArgument::OUT_DIR) { |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 385 | generate_write_to_parcel(t, c->statements, v, |
| 386 | stubClass->transact_reply, |
| 387 | Type::PARCELABLE_WRITE_RETURN_VALUE); |
| 388 | hasOutParams = true; |
| 389 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 390 | } |
| 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 Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 398 | proxy->comment = method.GetComments(); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 399 | proxy->modifiers = PUBLIC | OVERRIDE; |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 400 | proxy->returnType = types->Find(method.GetType().GetName()); |
| Casey Dahlin | f7a421c | 2015-10-05 17:24:28 -0700 | [diff] [blame] | 401 | proxy->returnTypeDimension = method.GetType().IsArray() ? 1 : 0; |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 402 | proxy->name = method.GetName(); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 403 | proxy->statements = new StatementBlock; |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 404 | for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) { |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 405 | proxy->parameters.push_back(new Variable( |
| Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 406 | types->Find(arg->GetType().GetName()), arg->GetName(), |
| Casey Dahlin | f7a421c | 2015-10-05 17:24:28 -0700 | [diff] [blame] | 407 | arg->GetType().IsArray() ? 1 : 0)); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 408 | } |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 409 | proxy->exceptions.push_back(types->RemoteExceptionType()); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 410 | proxyClass->elements.push_back(proxy); |
| 411 | |
| 412 | // the parcels |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 413 | Variable* _data = new Variable(types->ParcelType(), "_data"); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 414 | proxy->statements->Add(new VariableDeclaration(_data, |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 415 | new MethodCall(types->ParcelType(), "obtain"))); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 416 | Variable* _reply = NULL; |
| 417 | if (!oneway) { |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 418 | _reply = new Variable(types->ParcelType(), "_reply"); |
| 419 | proxy->statements->Add( |
| 420 | new VariableDeclaration( |
| 421 | _reply, |
| 422 | new MethodCall(types->ParcelType(), "obtain"))); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | // the return value |
| 426 | _result = NULL; |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 427 | if (method.GetType().GetName() != "void") { |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 428 | _result = new Variable(proxy->returnType, "_result", |
| Casey Dahlin | f7a421c | 2015-10-05 17:24:28 -0700 | [diff] [blame] | 429 | method.GetType().IsArray() ? 1 : 0); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 430 | 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 Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 444 | for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) { |
| Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 445 | const Type* t = types->Find(arg->GetType().GetName()); |
| Casey Dahlin | f7a421c | 2015-10-05 17:24:28 -0700 | [diff] [blame] | 446 | Variable* v = new Variable(t, arg->GetName(), arg->GetType().IsArray() ? 1 : 0); |
| Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 447 | AidlArgument::Direction dir = arg->GetDirection(); |
| Casey Dahlin | f7a421c | 2015-10-05 17:24:28 -0700 | [diff] [blame] | 448 | if (dir == AidlArgument::OUT_DIR && arg->GetType().IsArray()) { |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 449 | 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 Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 458 | else if (dir & AidlArgument::IN_DIR) { |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 459 | generate_write_to_parcel(t, tryStatement->statements, v, _data, 0); |
| 460 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 461 | } |
| 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 Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 485 | for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) { |
| Casey Dahlin | f2d23f7 | 2015-10-02 16:19:19 -0700 | [diff] [blame] | 486 | const Type* t = types->Find(arg->GetType().GetName()); |
| Casey Dahlin | f7a421c | 2015-10-05 17:24:28 -0700 | [diff] [blame] | 487 | Variable* v = new Variable(t, arg->GetName(), arg->GetType().IsArray() ? 1 : 0); |
| Casey Dahlin | c378c99 | 2015-09-29 16:50:40 -0700 | [diff] [blame] | 488 | if (arg->GetDirection() & AidlArgument::OUT_DIR) { |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 489 | generate_read_from_parcel(t, tryStatement->statements, |
| 490 | v, _reply, &cl); |
| 491 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 492 | } |
| 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 | |
| 503 | static void |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 504 | generate_interface_descriptors(StubClass* stub, ProxyClass* proxy, |
| 505 | const JavaTypeNamespace* types) |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 506 | { |
| 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 Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 517 | getDesc->returnType = types->StringType(); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 518 | 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 | |
| 525 | Class* |
| Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 526 | generate_binder_interface_class(const AidlInterface* iface, |
| Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 527 | JavaTypeNamespace* types) |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 528 | { |
| Christopher Wiley | 8f6816e | 2015-09-22 17:03:47 -0700 | [diff] [blame] | 529 | const InterfaceType* interfaceType = static_cast<const InterfaceType*>( |
| Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 530 | types->Find(iface->GetCanonicalName())); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 531 | |
| 532 | // the interface class |
| 533 | Class* interface = new Class; |
| Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 534 | interface->comment = iface->GetComments(); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 535 | interface->modifiers = PUBLIC; |
| 536 | interface->what = Class::INTERFACE; |
| 537 | interface->type = interfaceType; |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 538 | interface->interfaces.push_back(types->IInterfaceType()); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 539 | |
| 540 | // the stub inner class |
| 541 | StubClass* stub = new StubClass( |
| Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 542 | types->Find(iface->GetCanonicalName() + ".Stub"), |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 543 | interfaceType, types); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 544 | interface->elements.push_back(stub); |
| 545 | |
| 546 | // the proxy inner class |
| 547 | ProxyClass* proxy = new ProxyClass( |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 548 | types, |
| Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 549 | types->Find(iface->GetCanonicalName() + ".Stub.Proxy"), |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 550 | interfaceType); |
| 551 | stub->elements.push_back(proxy); |
| 552 | |
| 553 | // stub and proxy support for getInterfaceDescriptor() |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 554 | generate_interface_descriptors(stub, proxy, types); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 555 | |
| Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 556 | // all the declared constants of the interface |
| 557 | for (const auto& item : iface->GetConstants()) { |
| 558 | generate_constant(*item, interface); |
| 559 | } |
| 560 | |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 561 | // all the declared methods of the interface |
| Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 562 | for (const auto& item : iface->GetMethods()) { |
| Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 563 | generate_method(*item, interface, stub, proxy, |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 564 | item->GetId(), types); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | return interface; |
| 568 | } |
| 569 | |
| Christopher Wiley | db154a5 | 2015-09-28 16:32:25 -0700 | [diff] [blame] | 570 | } // namespace java |
| Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 571 | } // namespace android |
| 572 | } // namespace aidl |