1 /*
2 * Copyright (C) 2015, 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
17 #include "type_java.h"
18
19 #include <sys/types.h>
20 #include <memory>
21
22 #include <android-base/strings.h>
23
24 #include "aidl_language.h"
25 #include "logging.h"
26
27 using std::string;
28
29 namespace android {
30 namespace aidl {
31 namespace java {
32
33 Expression* NULL_VALUE;
34 Expression* THIS_VALUE;
35 Expression* SUPER_VALUE;
36 Expression* TRUE_VALUE;
37 Expression* FALSE_VALUE;
38
39 // ================================================================
40
Type(const JavaTypeNamespace * types,const string & name,int kind,bool canWriteToParcel)41 Type::Type(const JavaTypeNamespace* types, const string& name, int kind, bool canWriteToParcel)
42 : Type(types, "", name, kind, canWriteToParcel, "", -1) {}
43
Type(const JavaTypeNamespace * types,const string & package,const string & name,int kind,bool canWriteToParcel,const string & declFile,int declLine)44 Type::Type(const JavaTypeNamespace* types, const string& package, const string& name, int kind,
45 bool canWriteToParcel, const string& declFile, int declLine)
46 : ValidatableType(kind, package, name, declFile, declLine),
47 m_types(types),
48 m_javaType((package.empty()) ? name : package + "." + name),
49 m_canWriteToParcel(canWriteToParcel) {}
50
InstantiableName() const51 string Type::InstantiableName() const { return JavaType(); }
52
BuildWriteToParcelFlags(int flags) const53 Expression* Type::BuildWriteToParcelFlags(int flags) const {
54 if (flags == 0) {
55 return new LiteralExpression("0");
56 }
57 if ((flags & PARCELABLE_WRITE_RETURN_VALUE) != 0) {
58 return new FieldVariable(m_types->ParcelableInterfaceType()->JavaType(),
59 "PARCELABLE_WRITE_RETURN_VALUE");
60 }
61 return new LiteralExpression("0");
62 }
63
64 // ================================================================
65
BasicType(const JavaTypeNamespace * types,const string & name,const string & marshallParcel,const string & unmarshallParcel,const string & writeArrayParcel,const string & createArrayParcel,const string & readArrayParcel)66 BasicType::BasicType(const JavaTypeNamespace* types, const string& name,
67 const string& marshallParcel, const string& unmarshallParcel,
68 const string& writeArrayParcel, const string& createArrayParcel,
69 const string& readArrayParcel)
70 : Type(types, name, ValidatableType::KIND_BUILT_IN, true),
71 m_marshallParcel(marshallParcel),
72 m_unmarshallParcel(unmarshallParcel) {
73 m_array_type.reset(new BasicArrayType(types, name, writeArrayParcel,
74 createArrayParcel, readArrayParcel));
75 }
76
BasicArrayType(const JavaTypeNamespace * types,const string & name,const string & writeArrayParcel,const string & createArrayParcel,const string & readArrayParcel)77 BasicArrayType::BasicArrayType(const JavaTypeNamespace* types, const string& name,
78 const string& writeArrayParcel, const string& createArrayParcel,
79 const string& readArrayParcel)
80 : Type(types, name, ValidatableType::KIND_BUILT_IN, true),
81 m_writeArrayParcel(writeArrayParcel),
82 m_createArrayParcel(createArrayParcel),
83 m_readArrayParcel(readArrayParcel) {}
84
85 // ================================================================
86
FileDescriptorType(const JavaTypeNamespace * types)87 FileDescriptorType::FileDescriptorType(const JavaTypeNamespace* types)
88 : Type(types, "java.io", "FileDescriptor", ValidatableType::KIND_BUILT_IN, true) {
89 m_array_type.reset(new FileDescriptorArrayType(types));
90 }
91
FileDescriptorArrayType(const JavaTypeNamespace * types)92 FileDescriptorArrayType::FileDescriptorArrayType(const JavaTypeNamespace* types)
93 : Type(types, "java.io", "FileDescriptor", ValidatableType::KIND_BUILT_IN, true) {}
94
95 // ================================================================
96
ParcelFileDescriptorType(const JavaTypeNamespace * types)97 ParcelFileDescriptorType::ParcelFileDescriptorType(const JavaTypeNamespace* types)
98 : Type(types, "android.os", "ParcelFileDescriptor", ValidatableType::KIND_BUILT_IN, true) {
99 m_array_type.reset(new ParcelFileDescriptorArrayType(types));
100 }
101
ParcelFileDescriptorArrayType(const JavaTypeNamespace * types)102 ParcelFileDescriptorArrayType::ParcelFileDescriptorArrayType(const JavaTypeNamespace* types)
103 : Type(types, "android.os", "ParcelFileDescriptor", ValidatableType::KIND_BUILT_IN, true) {}
104
105 // ================================================================
106
BooleanType(const JavaTypeNamespace * types)107 BooleanType::BooleanType(const JavaTypeNamespace* types)
108 : Type(types, "boolean", ValidatableType::KIND_BUILT_IN, true) {
109 m_array_type.reset(new BooleanArrayType(types));
110 }
111
BooleanArrayType(const JavaTypeNamespace * types)112 BooleanArrayType::BooleanArrayType(const JavaTypeNamespace* types)
113 : Type(types, "boolean", ValidatableType::KIND_BUILT_IN, true) {}
114
115 // ================================================================
116
CharType(const JavaTypeNamespace * types)117 CharType::CharType(const JavaTypeNamespace* types)
118 : Type(types, "char", ValidatableType::KIND_BUILT_IN, true) {
119 m_array_type.reset(new CharArrayType(types));
120 }
121
CharArrayType(const JavaTypeNamespace * types)122 CharArrayType::CharArrayType(const JavaTypeNamespace* types)
123 : Type(types, "char", ValidatableType::KIND_BUILT_IN, true) {}
124
125 // ================================================================
126
StringType(const JavaTypeNamespace * types,const std::string & package,const std::string & class_name)127 StringType::StringType(const JavaTypeNamespace* types, const std::string& package,
128 const std::string& class_name)
129 : Type(types, package, class_name, ValidatableType::KIND_BUILT_IN, true) {
130 m_array_type.reset(new StringArrayType(types));
131 }
132
StringArrayType(const JavaTypeNamespace * types)133 StringArrayType::StringArrayType(const JavaTypeNamespace* types)
134 : Type(types, "java.lang", "String", ValidatableType::KIND_BUILT_IN, true) {}
135
136 // ================================================================
137
CharSequenceType(const JavaTypeNamespace * types)138 CharSequenceType::CharSequenceType(const JavaTypeNamespace* types)
139 : Type(types, "java.lang", "CharSequence", ValidatableType::KIND_BUILT_IN, true) {}
140
141 // ================================================================
142
RemoteExceptionType(const JavaTypeNamespace * types)143 RemoteExceptionType::RemoteExceptionType(const JavaTypeNamespace* types)
144 : Type(types, "android.os", "RemoteException", ValidatableType::KIND_BUILT_IN, false) {}
145
146 // ================================================================
147
RuntimeExceptionType(const JavaTypeNamespace * types)148 RuntimeExceptionType::RuntimeExceptionType(const JavaTypeNamespace* types)
149 : Type(types, "java.lang", "RuntimeException", ValidatableType::KIND_BUILT_IN, false) {}
150
151 // ================================================================
152
IBinderType(const JavaTypeNamespace * types)153 IBinderType::IBinderType(const JavaTypeNamespace* types)
154 : Type(types, "android.os", "IBinder", ValidatableType::KIND_BUILT_IN, true) {
155 m_array_type.reset(new IBinderArrayType(types));
156 }
157
IBinderArrayType(const JavaTypeNamespace * types)158 IBinderArrayType::IBinderArrayType(const JavaTypeNamespace* types)
159 : Type(types, "android.os", "IBinder", ValidatableType::KIND_BUILT_IN, true) {}
160
161 // ================================================================
162
IInterfaceType(const JavaTypeNamespace * types)163 IInterfaceType::IInterfaceType(const JavaTypeNamespace* types)
164 : Type(types, "android.os", "IInterface", ValidatableType::KIND_BUILT_IN, false) {}
165
166 // ================================================================
167
BinderType(const JavaTypeNamespace * types)168 BinderType::BinderType(const JavaTypeNamespace* types)
169 : Type(types, "android.os", "Binder", ValidatableType::KIND_BUILT_IN, false) {}
170
171 // ================================================================
172
BinderProxyType(const JavaTypeNamespace * types)173 BinderProxyType::BinderProxyType(const JavaTypeNamespace* types)
174 : Type(types, "android.os", "BinderProxy", ValidatableType::KIND_BUILT_IN, false) {}
175
176 // ================================================================
177
ParcelType(const JavaTypeNamespace * types)178 ParcelType::ParcelType(const JavaTypeNamespace* types)
179 : Type(types, "android.os", "Parcel", ValidatableType::KIND_BUILT_IN, false) {}
180
181 // ================================================================
182
ParcelableInterfaceType(const JavaTypeNamespace * types)183 ParcelableInterfaceType::ParcelableInterfaceType(const JavaTypeNamespace* types)
184 : Type(types, "android.os", "Parcelable", ValidatableType::KIND_BUILT_IN, false) {}
185
186 // ================================================================
187
MapType(const JavaTypeNamespace * types)188 MapType::MapType(const JavaTypeNamespace* types)
189 : Type(types, "java.util", "Map", ValidatableType::KIND_BUILT_IN, true) {}
190
191 // ================================================================
192
ListType(const JavaTypeNamespace * types)193 ListType::ListType(const JavaTypeNamespace* types)
194 : Type(types, "java.util", "List", ValidatableType::KIND_BUILT_IN, true) {}
195
InstantiableName() const196 string ListType::InstantiableName() const { return "java.util.ArrayList"; }
197
198 // ================================================================
199
UserDataType(const JavaTypeNamespace * types,const string & package,const string & name,bool builtIn,bool canWriteToParcel,const string & declFile,int declLine)200 UserDataType::UserDataType(const JavaTypeNamespace* types, const string& package,
201 const string& name, bool builtIn, bool canWriteToParcel,
202 const string& declFile, int declLine)
203 : Type(types, package, name,
204 builtIn ? ValidatableType::KIND_BUILT_IN : ValidatableType::KIND_PARCELABLE,
205 canWriteToParcel, declFile, declLine) {
206 m_array_type.reset(new UserDataArrayType(types, package, name, builtIn,
207 canWriteToParcel, declFile,
208 declLine));
209 }
210
UserDataArrayType(const JavaTypeNamespace * types,const string & package,const string & name,bool builtIn,bool canWriteToParcel,const string & declFile,int declLine)211 UserDataArrayType::UserDataArrayType(const JavaTypeNamespace* types, const string& package,
212 const string& name, bool builtIn, bool canWriteToParcel,
213 const string& declFile, int declLine)
214 : Type(types, package, name,
215 builtIn ? ValidatableType::KIND_BUILT_IN : ValidatableType::KIND_PARCELABLE,
216 canWriteToParcel, declFile, declLine) {}
217
218 // ================================================================
219
InterfaceType(const JavaTypeNamespace * types,const string & package,const string & name,bool builtIn,const string & declFile,int declLine,const Type * stub,const Type * proxy,const Type * defaultImpl)220 InterfaceType::InterfaceType(const JavaTypeNamespace* types, const string& package,
221 const string& name, bool builtIn, const string& declFile, int declLine,
222 const Type* stub, const Type* proxy, const Type* defaultImpl)
223 : Type(types, package, name,
224 builtIn ? ValidatableType::KIND_BUILT_IN : ValidatableType::KIND_INTERFACE, true,
225 declFile, declLine),
226 stub_(stub),
227 proxy_(proxy),
228 defaultImpl_(defaultImpl) {}
229
230 // ================================================================
231
GenericListType(const JavaTypeNamespace * types,const Type * contained_type)232 GenericListType::GenericListType(const JavaTypeNamespace* types, const Type* contained_type)
233 : Type(types, "java.util", "List<" + contained_type->CanonicalName() + ">",
234 ValidatableType::KIND_BUILT_IN, true),
235 m_contained_type(contained_type) {}
236
InstantiableName() const237 string GenericListType::InstantiableName() const {
238 return "java.util.ArrayList<" + m_contained_type->JavaType() + ">";
239 }
240
241 // ================================================================
242
ClassLoaderType(const JavaTypeNamespace * types)243 ClassLoaderType::ClassLoaderType(const JavaTypeNamespace* types)
244 : Type(types, "java.lang", "ClassLoader", ValidatableType::KIND_BUILT_IN, false) {}
245
246 // ================================================================
247
Init()248 void JavaTypeNamespace::Init() {
249 Add(std::make_unique<BasicType>(this, "void", "XXX", "XXX", "XXX", "XXX", "XXX"));
250
251 AddAndSetMember(&m_bool_type, std::make_unique<BooleanType>(this));
252
253 Add(std::make_unique<BasicType>(this, "byte", "writeByte", "readByte", "writeByteArray",
254 "createByteArray", "readByteArray"));
255
256 Add(std::make_unique<CharType>(this));
257
258 AddAndSetMember(&m_int_type,
259 std::make_unique<BasicType>(this, "int", "writeInt", "readInt", "writeIntArray",
260 "createIntArray", "readIntArray"));
261
262 Add(std::make_unique<BasicType>(this, "long", "writeLong", "readLong", "writeLongArray",
263 "createLongArray", "readLongArray"));
264
265 Add(std::make_unique<BasicType>(this, "float", "writeFloat", "readFloat", "writeFloatArray",
266 "createFloatArray", "readFloatArray"));
267
268 Add(std::make_unique<BasicType>(this, "double", "writeDouble", "readDouble", "writeDoubleArray",
269 "createDoubleArray", "readDoubleArray"));
270
271 AddAndSetMember(&m_string_type, std::make_unique<class StringType>(this, "java.lang", "String"));
272 Add(std::make_unique<class StringType>(this, ::android::aidl::kAidlReservedTypePackage,
273 ::android::aidl::kUtf8InCppStringClass));
274
275 Add(std::make_unique<Type>(this, "java.lang", "Object", ValidatableType::KIND_BUILT_IN, false));
276
277 Add(std::make_unique<FileDescriptorType>(this));
278
279 Add(std::make_unique<ParcelFileDescriptorType>(this));
280
281 Add(std::make_unique<CharSequenceType>(this));
282
283 Add(std::make_unique<MapType>(this));
284
285 Add(std::make_unique<ListType>(this));
286
287 AddAndSetMember(&m_text_utils_type,
288 std::make_unique<Type>(this, "android.text", "TextUtils",
289 ValidatableType::KIND_BUILT_IN, false));
290
291 AddAndSetMember(&m_remote_exception_type, std::make_unique<class RemoteExceptionType>(this));
292
293 AddAndSetMember(&m_runtime_exception_type, std::make_unique<class RuntimeExceptionType>(this));
294
295 AddAndSetMember(&m_ibinder_type, std::make_unique<class IBinderType>(this));
296
297 AddAndSetMember(&m_iinterface_type, std::make_unique<class IInterfaceType>(this));
298
299 AddAndSetMember(&m_binder_native_type, std::make_unique<BinderType>(this));
300
301 AddAndSetMember(&m_binder_proxy_type, std::make_unique<class BinderProxyType>(this));
302
303 AddAndSetMember(&m_parcel_type, std::make_unique<class ParcelType>(this));
304
305 AddAndSetMember(&m_parcelable_interface_type,
306 std::make_unique<class ParcelableInterfaceType>(this));
307
308 AddAndSetMember(&m_context_type, std::make_unique<Type>(this, "android.content", "Context",
309 ValidatableType::KIND_BUILT_IN, false));
310
311 AddAndSetMember(&m_classloader_type, std::make_unique<class ClassLoaderType>(this));
312
313 NULL_VALUE = new LiteralExpression("null");
314 THIS_VALUE = new LiteralExpression("this");
315 SUPER_VALUE = new LiteralExpression("super");
316 TRUE_VALUE = new LiteralExpression("true");
317 FALSE_VALUE = new LiteralExpression("false");
318 }
319
AddParcelableType(const AidlParcelable & p,const std::string & filename)320 bool JavaTypeNamespace::AddParcelableType(const AidlParcelable& p,
321 const std::string& filename) {
322 return Add(
323 std::make_unique<UserDataType>(this, p.GetPackage(), p.GetName(), false, true, filename));
324 }
325
AddBinderType(const AidlInterface & b,const std::string & filename)326 bool JavaTypeNamespace::AddBinderType(const AidlInterface& b,
327 const std::string& filename) {
328 // for interfaces, add the stub, proxy, and interface types.
329 auto stub = std::make_unique<Type>(this, b.GetPackage(), b.GetName() + ".Stub",
330 ValidatableType::KIND_GENERATED, false, filename);
331 auto proxy = std::make_unique<Type>(this, b.GetPackage(), b.GetName() + ".Stub.Proxy",
332 ValidatableType::KIND_GENERATED, false, filename);
333 auto defaultImpl = std::make_unique<Type>(this, b.GetPackage(), b.GetName() + ".Default",
334 ValidatableType::KIND_GENERATED, false, filename);
335 auto type = std::make_unique<InterfaceType>(this, b.GetPackage(), b.GetName(), false, filename,
336 -1, stub.get(), proxy.get(), defaultImpl.get());
337
338 bool success = true;
339 success &= Add(std::move(type));
340 success &= Add(std::move(stub));
341 success &= Add(std::move(proxy));
342 success &= Add(std::move(defaultImpl));
343 return success;
344 }
345
AddListType(const std::string & contained_type_name)346 bool JavaTypeNamespace::AddListType(const std::string& contained_type_name) {
347 const Type* contained_type = FindTypeByCanonicalName(contained_type_name);
348 if (!contained_type) {
349 return false;
350 }
351 Add(std::make_unique<GenericListType>(this, contained_type));
352 return true;
353 }
354
AddMapType(const string &,const string &)355 bool JavaTypeNamespace::AddMapType(const string& /*key_type_name*/,
356 const string& /*value_type_name*/) {
357 LOG(ERROR) << "Don't know how to create a Map<K,V> container.";
358 return false;
359 }
360
361 } // namespace java
362 } // namespace aidl
363 } // namespace android
364