1 /*
2 * Copyright (C) 2016, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "generate_java.h"
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <map>
23 #include <memory>
24 #include <sstream>
25
26 #include <android-base/stringprintf.h>
27
28 #include "aidl_to_java.h"
29 #include "code_writer.h"
30 #include "logging.h"
31
32 using std::unique_ptr;
33 using ::android::aidl::java::Variable;
34 using std::string;
35
36 namespace android {
37 namespace aidl {
38 namespace java {
39
generate_java_interface(const string & filename,const AidlInterface * iface,const AidlTypenames & typenames,const IoDelegate & io_delegate,const Options & options)40 bool generate_java_interface(const string& filename, const AidlInterface* iface,
41 const AidlTypenames& typenames, const IoDelegate& io_delegate,
42 const Options& options) {
43 auto cl = generate_binder_interface_class(iface, typenames, options);
44
45 std::unique_ptr<Document> document =
46 std::make_unique<Document>("" /* no comment */, iface->GetPackage(), std::move(cl));
47
48 CodeWriterPtr code_writer = io_delegate.GetCodeWriter(filename);
49 document->Write(code_writer.get());
50
51 return true;
52 }
53
generate_java_parcel(const std::string & filename,const AidlStructuredParcelable * parcel,const AidlTypenames & typenames,const IoDelegate & io_delegate)54 bool generate_java_parcel(const std::string& filename, const AidlStructuredParcelable* parcel,
55 const AidlTypenames& typenames, const IoDelegate& io_delegate) {
56 auto cl = generate_parcel_class(parcel, typenames);
57
58 std::unique_ptr<Document> document =
59 std::make_unique<Document>("" /* no comment */, parcel->GetPackage(), std::move(cl));
60
61 CodeWriterPtr code_writer = io_delegate.GetCodeWriter(filename);
62 document->Write(code_writer.get());
63
64 return true;
65 }
66
generate_java_enum_declaration(const std::string & filename,const AidlEnumDeclaration * enum_decl,const AidlTypenames & typenames,const IoDelegate & io_delegate)67 bool generate_java_enum_declaration(const std::string& filename,
68 const AidlEnumDeclaration* enum_decl,
69 const AidlTypenames& typenames, const IoDelegate& io_delegate) {
70 CodeWriterPtr code_writer = io_delegate.GetCodeWriter(filename);
71 generate_enum(code_writer, enum_decl, typenames);
72 return true;
73 }
74
generate_java(const std::string & filename,const AidlDefinedType * defined_type,const AidlTypenames & typenames,const IoDelegate & io_delegate,const Options & options)75 bool generate_java(const std::string& filename, const AidlDefinedType* defined_type,
76 const AidlTypenames& typenames, const IoDelegate& io_delegate,
77 const Options& options) {
78 if (const AidlStructuredParcelable* parcelable = defined_type->AsStructuredParcelable();
79 parcelable != nullptr) {
80 return generate_java_parcel(filename, parcelable, typenames, io_delegate);
81 }
82
83 if (const AidlEnumDeclaration* enum_decl = defined_type->AsEnumDeclaration();
84 enum_decl != nullptr) {
85 return generate_java_enum_declaration(filename, enum_decl, typenames, io_delegate);
86 }
87
88 if (const AidlInterface* interface = defined_type->AsInterface(); interface != nullptr) {
89 return generate_java_interface(filename, interface, typenames, io_delegate, options);
90 }
91
92 CHECK(false) << "Unrecognized type sent for java generation.";
93 return false;
94 }
95
generate_parcel_class(const AidlStructuredParcelable * parcel,const AidlTypenames & typenames)96 std::unique_ptr<android::aidl::java::Class> generate_parcel_class(
97 const AidlStructuredParcelable* parcel, const AidlTypenames& typenames) {
98 auto parcel_class = std::make_unique<Class>();
99 parcel_class->comment = parcel->GetComments();
100 parcel_class->modifiers = PUBLIC;
101 parcel_class->what = Class::CLASS;
102 parcel_class->type = parcel->GetCanonicalName();
103 parcel_class->interfaces.push_back("android.os.Parcelable");
104 parcel_class->annotations = generate_java_annotations(*parcel);
105
106 for (const auto& variable : parcel->GetFields()) {
107 std::ostringstream out;
108 out << variable->GetType().GetComments() << "\n";
109 for (const auto& a : generate_java_annotations(variable->GetType())) {
110 out << a << "\n";
111 }
112 out << "public " << JavaSignatureOf(variable->GetType(), typenames) << " "
113 << variable->GetName();
114 if (variable->GetDefaultValue()) {
115 out << " = " << variable->ValueString(ConstantValueDecorator);
116 }
117 out << ";\n";
118 parcel_class->elements.push_back(std::make_shared<LiteralClassElement>(out.str()));
119 }
120
121 std::ostringstream out;
122 out << "public static final android.os.Parcelable.Creator<" << parcel->GetName() << "> CREATOR = "
123 << "new android.os.Parcelable.Creator<" << parcel->GetName() << ">() {\n";
124 out << " @Override\n";
125 out << " public " << parcel->GetName()
126 << " createFromParcel(android.os.Parcel _aidl_source) {\n";
127 out << " " << parcel->GetName() << " _aidl_out = new " << parcel->GetName() << "();\n";
128 out << " _aidl_out.readFromParcel(_aidl_source);\n";
129 out << " return _aidl_out;\n";
130 out << " }\n";
131 out << " @Override\n";
132 out << " public " << parcel->GetName() << "[] newArray(int _aidl_size) {\n";
133 out << " return new " << parcel->GetName() << "[_aidl_size];\n";
134 out << " }\n";
135 out << "};\n";
136 parcel_class->elements.push_back(std::make_shared<LiteralClassElement>(out.str()));
137
138 auto flag_variable = std::make_shared<Variable>("int", "_aidl_flag");
139 auto parcel_variable = std::make_shared<Variable>("android.os.Parcel", "_aidl_parcel");
140
141 auto write_method = std::make_shared<Method>();
142 write_method->modifiers = PUBLIC | OVERRIDE | FINAL;
143 write_method->returnType = "void";
144 write_method->name = "writeToParcel";
145 write_method->parameters.push_back(parcel_variable);
146 write_method->parameters.push_back(flag_variable);
147 write_method->statements = std::make_shared<StatementBlock>();
148
149 out.str("");
150 out << "int _aidl_start_pos = _aidl_parcel.dataPosition();\n"
151 << "_aidl_parcel.writeInt(0);\n";
152 write_method->statements->Add(std::make_shared<LiteralStatement>(out.str()));
153
154 for (const auto& field : parcel->GetFields()) {
155 string code;
156 CodeWriterPtr writer = CodeWriter::ForString(&code);
157 CodeGeneratorContext context{
158 .writer = *(writer.get()),
159 .typenames = typenames,
160 .type = field->GetType(),
161 .parcel = parcel_variable->name,
162 .var = field->GetName(),
163 .is_return_value = false,
164 };
165 WriteToParcelFor(context);
166 writer->Close();
167 write_method->statements->Add(std::make_shared<LiteralStatement>(code));
168 }
169
170 out.str("");
171 out << "int _aidl_end_pos = _aidl_parcel.dataPosition();\n"
172 << "_aidl_parcel.setDataPosition(_aidl_start_pos);\n"
173 << "_aidl_parcel.writeInt(_aidl_end_pos - _aidl_start_pos);\n"
174 << "_aidl_parcel.setDataPosition(_aidl_end_pos);\n";
175
176 write_method->statements->Add(std::make_shared<LiteralStatement>(out.str()));
177
178 parcel_class->elements.push_back(write_method);
179
180 auto read_method = std::make_shared<Method>();
181 read_method->modifiers = PUBLIC | FINAL;
182 read_method->returnType = "void";
183 read_method->name = "readFromParcel";
184 read_method->parameters.push_back(parcel_variable);
185 read_method->statements = std::make_shared<StatementBlock>();
186
187 out.str("");
188 out << "int _aidl_start_pos = _aidl_parcel.dataPosition();\n"
189 << "int _aidl_parcelable_size = _aidl_parcel.readInt();\n"
190 << "if (_aidl_parcelable_size < 0) return;\n"
191 << "try {\n";
192
193 read_method->statements->Add(std::make_shared<LiteralStatement>(out.str()));
194
195 out.str("");
196 out << " if (_aidl_parcel.dataPosition() - _aidl_start_pos >= _aidl_parcelable_size) return;\n";
197
198 std::shared_ptr<LiteralStatement> sizeCheck = nullptr;
199 // keep this across different fields in order to create the classloader
200 // at most once.
201 bool is_classloader_created = false;
202 for (const auto& field : parcel->GetFields()) {
203 string code;
204 CodeWriterPtr writer = CodeWriter::ForString(&code);
205 CodeGeneratorContext context{
206 .writer = *(writer.get()),
207 .typenames = typenames,
208 .type = field->GetType(),
209 .parcel = parcel_variable->name,
210 .var = field->GetName(),
211 .is_classloader_created = &is_classloader_created,
212 };
213 context.writer.Indent();
214 CreateFromParcelFor(context);
215 writer->Close();
216 read_method->statements->Add(std::make_shared<LiteralStatement>(code));
217 if (!sizeCheck) sizeCheck = std::make_shared<LiteralStatement>(out.str());
218 read_method->statements->Add(sizeCheck);
219 }
220
221 out.str("");
222 out << "} finally {\n"
223 << " _aidl_parcel.setDataPosition(_aidl_start_pos + _aidl_parcelable_size);\n"
224 << "}\n";
225
226 read_method->statements->Add(std::make_shared<LiteralStatement>(out.str()));
227
228 parcel_class->elements.push_back(read_method);
229
230 auto describe_contents_method = std::make_shared<Method>();
231 describe_contents_method->modifiers = PUBLIC | OVERRIDE;
232 describe_contents_method->returnType = "int";
233 describe_contents_method->name = "describeContents";
234 describe_contents_method->statements = std::make_shared<StatementBlock>();
235 describe_contents_method->statements->Add(std::make_shared<LiteralStatement>("return 0;\n"));
236 parcel_class->elements.push_back(describe_contents_method);
237
238 return parcel_class;
239 }
240
generate_enum(const CodeWriterPtr & code_writer,const AidlEnumDeclaration * enum_decl,const AidlTypenames & typenames)241 void generate_enum(const CodeWriterPtr& code_writer, const AidlEnumDeclaration* enum_decl,
242 const AidlTypenames& typenames) {
243 code_writer->Write(
244 "/*\n"
245 " * This file is auto-generated. DO NOT MODIFY.\n"
246 " */\n");
247
248 code_writer->Write("package %s;\n", enum_decl->GetPackage().c_str());
249 code_writer->Write("%s\n", enum_decl->GetComments().c_str());
250 for (const std::string& annotation : generate_java_annotations(*enum_decl)) {
251 code_writer->Write("%s", annotation.c_str());
252 }
253 code_writer->Write("public @interface %s {\n", enum_decl->GetName().c_str());
254 code_writer->Indent();
255 for (const auto& enumerator : enum_decl->GetEnumerators()) {
256 code_writer->Write("%s", enumerator->GetComments().c_str());
257 code_writer->Write(
258 "public static final %s %s = %s;\n",
259 JavaSignatureOf(enum_decl->GetBackingType(), typenames).c_str(),
260 enumerator->GetName().c_str(),
261 enumerator->ValueString(enum_decl->GetBackingType(), ConstantValueDecorator).c_str());
262 }
263 code_writer->Dedent();
264 code_writer->Write("}\n");
265 }
266
dump_location(const AidlNode & method)267 std::string dump_location(const AidlNode& method) {
268 return method.PrintLocation();
269 }
270
generate_java_unsupportedappusage_parameters(const AidlAnnotation & a)271 std::string generate_java_unsupportedappusage_parameters(const AidlAnnotation& a) {
272 const std::map<std::string, std::string> params = a.AnnotationParams(ConstantValueDecorator);
273 std::vector<string> parameters_decl;
274 for (const auto& name_and_param : params) {
275 const std::string& param_name = name_and_param.first;
276 const std::string& param_value = name_and_param.second;
277 parameters_decl.push_back(param_name + " = " + param_value);
278 }
279 parameters_decl.push_back("overrideSourcePosition=\"" + dump_location(a) + "\"");
280 return "(" + base::Join(parameters_decl, ", ") + ")";
281 }
282
generate_java_annotations(const AidlAnnotatable & a)283 std::vector<std::string> generate_java_annotations(const AidlAnnotatable& a) {
284 std::vector<std::string> result;
285 const AidlAnnotation* unsupported_app_usage = a.UnsupportedAppUsage();
286 if (a.IsHide()) {
287 result.emplace_back("@android.annotation.Hide");
288 }
289 if (unsupported_app_usage != nullptr) {
290 result.emplace_back("@android.compat.annotation.UnsupportedAppUsage" +
291 generate_java_unsupportedappusage_parameters(*unsupported_app_usage));
292 }
293 return result;
294 }
295
296 } // namespace java
297 } // namespace aidl
298 } // namespace android
299