blob: 0e4a570d6211ea0b9feabbc618eb4df001288336 [file] [log] [blame]
Inseob Kim5f8f32c2018-08-24 11:10:44 +09001/*
2 * Copyright (C) 2018 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#define LOG_TAG "sysprop_java_gen"
18
19#include "JavaGen.h"
20
21#include <android-base/file.h>
22#include <android-base/logging.h>
23#include <android-base/stringprintf.h>
24#include <android-base/strings.h>
25#include <cerrno>
26#include <regex>
27#include <string>
28
29#include "CodeWriter.h"
30#include "Common.h"
31#include "sysprop.pb.h"
32
33namespace {
34
35constexpr const char* kIndent = " ";
36
37constexpr const char* kJavaFileImports =
38 R"(import android.annotation.SystemApi;
39
Inseob Kim0773b942018-10-04 19:29:27 +090040import android.os.SystemProperties;
Inseob Kim5f8f32c2018-08-24 11:10:44 +090041import java.util.ArrayList;
42import java.util.function.Function;
43import java.util.List;
44import java.util.Optional;
45import java.util.StringJoiner;
46
47)";
48
49constexpr const char* kJavaParsersAndFormatters =
50 R"(private static Boolean tryParseBoolean(String str) {
51 switch (str.toLowerCase()) {
52 case "1":
Inseob Kim5f8f32c2018-08-24 11:10:44 +090053 case "true":
54 return Boolean.TRUE;
55 case "0":
Inseob Kim5f8f32c2018-08-24 11:10:44 +090056 case "false":
57 return Boolean.FALSE;
58 default:
59 return null;
60 }
61}
62
63private static Integer tryParseInteger(String str) {
64 try {
65 return Integer.valueOf(str);
66 } catch (NumberFormatException e) {
67 return null;
68 }
69}
70
71private static Long tryParseLong(String str) {
72 try {
73 return Long.valueOf(str);
74 } catch (NumberFormatException e) {
75 return null;
76 }
77}
78
79private static Double tryParseDouble(String str) {
80 try {
81 return Double.valueOf(str);
82 } catch (NumberFormatException e) {
83 return null;
84 }
85}
86
87private static String tryParseString(String str) {
Inseob Kim14e51872018-10-25 14:27:33 +090088 return str.length() == 0 ? null : str;
Inseob Kim5f8f32c2018-08-24 11:10:44 +090089}
90
91private static <T extends Enum<T>> T tryParseEnum(Class<T> enumType, String str) {
92 try {
93 return Enum.valueOf(enumType, str);
94 } catch (IllegalArgumentException e) {
95 return null;
96 }
97}
98
99private static <T> List<T> tryParseList(Function<String, T> elementParser, String str) {
Inseob Kim4c474eb2018-11-27 12:17:21 +0900100 if (str == null || str.equals("")) return null;
101
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900102 List<T> ret = new ArrayList<>();
103
104 for (String element : str.split(",")) {
Inseob Kim4c474eb2018-11-27 12:17:21 +0900105 ret.add(elementParser.apply(element));
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900106 }
107
108 return ret;
109}
110
111private static <T extends Enum<T>> List<T> tryParseEnumList(Class<T> enumType, String str) {
Inseob Kim4c474eb2018-11-27 12:17:21 +0900112 if (str == null || str.equals("")) return null;
113
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900114 List<T> ret = new ArrayList<>();
115
116 for (String element : str.split(",")) {
Inseob Kim4c474eb2018-11-27 12:17:21 +0900117 ret.add(tryParseEnum(enumType, element));
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900118 }
119
120 return ret;
121}
122
123private static <T> String formatList(List<T> list) {
124 StringJoiner joiner = new StringJoiner(",");
125
126 for (T element : list) {
Inseob Kim4c474eb2018-11-27 12:17:21 +0900127 joiner.add(element == null ? "" : element.toString());
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900128 }
129
130 return joiner.toString();
131}
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900132)";
133
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900134const std::regex kRegexDot{"\\."};
135const std::regex kRegexUnderscore{"_"};
136
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900137std::string GetJavaTypeName(const sysprop::Property& prop);
138std::string GetJavaEnumTypeName(const sysprop::Property& prop);
Inseob Kim4c474eb2018-11-27 12:17:21 +0900139std::string GetJavaListElementTypeName(const sysprop::Property& prop);
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900140std::string GetJavaPackageName(const sysprop::Properties& props);
141std::string GetJavaClassName(const sysprop::Properties& props);
142bool IsListProp(const sysprop::Property& prop);
Inseob Kim14e51872018-10-25 14:27:33 +0900143void WriteJavaAnnotation(CodeWriter& writer, sysprop::Scope scope);
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900144bool GenerateJavaClass(const sysprop::Properties& props,
145 std::string* java_result, std::string* err);
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900146
147std::string GetJavaEnumTypeName(const sysprop::Property& prop) {
Inseob Kim14e51872018-10-25 14:27:33 +0900148 return ApiNameToIdentifier(prop.api_name()) + "_values";
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900149}
150
151std::string GetJavaTypeName(const sysprop::Property& prop) {
152 switch (prop.type()) {
153 case sysprop::Boolean:
154 return "Boolean";
155 case sysprop::Integer:
156 return "Integer";
157 case sysprop::Long:
158 return "Long";
159 case sysprop::Double:
160 return "Double";
161 case sysprop::String:
162 return "String";
163 case sysprop::Enum:
164 return GetJavaEnumTypeName(prop);
165 case sysprop::BooleanList:
166 return "List<Boolean>";
167 case sysprop::IntegerList:
168 return "List<Integer>";
169 case sysprop::LongList:
170 return "List<Long>";
171 case sysprop::DoubleList:
172 return "List<Double>";
173 case sysprop::StringList:
174 return "List<String>";
175 case sysprop::EnumList:
176 return "List<" + GetJavaEnumTypeName(prop) + ">";
177 default:
178 __builtin_unreachable();
179 }
180}
181
Inseob Kim4c474eb2018-11-27 12:17:21 +0900182std::string GetJavaListElementTypeName(const sysprop::Property& prop) {
183 switch (prop.type()) {
184 case sysprop::BooleanList:
185 return "Boolean";
186 case sysprop::IntegerList:
187 return "Integer";
188 case sysprop::LongList:
189 return "Long";
190 case sysprop::DoubleList:
191 return "Double";
192 case sysprop::StringList:
193 return "String";
194 case sysprop::EnumList:
195 return GetJavaEnumTypeName(prop);
196 default:
197 return "";
198 }
199}
200
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900201std::string GetParsingExpression(const sysprop::Property& prop) {
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900202 switch (prop.type()) {
203 case sysprop::Boolean:
Inseob Kim0773b942018-10-04 19:29:27 +0900204 return "tryParseBoolean(value)";
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900205 case sysprop::Integer:
Inseob Kim0773b942018-10-04 19:29:27 +0900206 return "tryParseInteger(value)";
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900207 case sysprop::Long:
Inseob Kim0773b942018-10-04 19:29:27 +0900208 return "tryParseLong(value)";
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900209 case sysprop::Double:
Inseob Kim0773b942018-10-04 19:29:27 +0900210 return "tryParseDouble(value)";
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900211 case sysprop::String:
Inseob Kim0773b942018-10-04 19:29:27 +0900212 return "tryParseString(value)";
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900213 case sysprop::Enum:
Inseob Kim0773b942018-10-04 19:29:27 +0900214 return "tryParseEnum(" + GetJavaEnumTypeName(prop) + ".class, value)";
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900215 case sysprop::EnumList:
Inseob Kim0773b942018-10-04 19:29:27 +0900216 return "tryParseEnumList(" + GetJavaEnumTypeName(prop) +
217 ".class, "
218 "value)";
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900219 default:
220 break;
221 }
222
223 // The remaining cases are lists for types other than Enum which share the
224 // same parsing function "tryParseList"
225 std::string element_parser;
226
227 switch (prop.type()) {
228 case sysprop::BooleanList:
229 element_parser = "v -> tryParseBoolean(v)";
230 break;
231 case sysprop::IntegerList:
232 element_parser = "v -> tryParseInteger(v)";
233 break;
234 case sysprop::LongList:
235 element_parser = "v -> tryParseLong(v)";
236 break;
237 case sysprop::DoubleList:
238 element_parser = "v -> tryParseDouble(v)";
239 break;
240 case sysprop::StringList:
241 element_parser = "v -> tryParseString(v)";
242 break;
243 default:
244 __builtin_unreachable();
245 }
246
Inseob Kim0773b942018-10-04 19:29:27 +0900247 return "tryParseList(" + element_parser + ", value)";
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900248}
249
250std::string GetJavaPackageName(const sysprop::Properties& props) {
251 const std::string& module = props.module();
252 return module.substr(0, module.rfind('.'));
253}
254
255std::string GetJavaClassName(const sysprop::Properties& props) {
256 const std::string& module = props.module();
257 return module.substr(module.rfind('.') + 1);
258}
259
260bool IsListProp(const sysprop::Property& prop) {
261 switch (prop.type()) {
262 case sysprop::BooleanList:
263 case sysprop::IntegerList:
264 case sysprop::LongList:
265 case sysprop::DoubleList:
266 case sysprop::StringList:
267 case sysprop::EnumList:
268 return true;
269 default:
270 return false;
271 }
272}
273
Inseob Kim14e51872018-10-25 14:27:33 +0900274void WriteJavaAnnotation(CodeWriter& writer, sysprop::Scope scope) {
275 switch (scope) {
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900276 case sysprop::System:
277 writer.Write("@SystemApi\n");
278 break;
279 case sysprop::Internal:
280 writer.Write("/** @hide */\n");
281 break;
282 default:
283 break;
284 }
285}
286
287bool GenerateJavaClass(const sysprop::Properties& props,
288 std::string* java_result,
289 [[maybe_unused]] std::string* err) {
Inseob Kim1a58c042018-11-01 16:37:34 +0900290 sysprop::Scope classScope = sysprop::Internal;
291
292 for (int i = 0; i < props.prop_size(); ++i) {
293 // Get least restrictive scope among props. For example, if all props
294 // are internal, class can be as well internal. However, class should
295 // be public or system if at least one prop is so.
296 classScope = std::min(classScope, props.prop(i).scope());
297 }
298
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900299 std::string package_name = GetJavaPackageName(props);
300 std::string class_name = GetJavaClassName(props);
301
302 CodeWriter writer(kIndent);
303 writer.Write("%s", kGeneratedFileFooterComments);
304 writer.Write("package %s;\n\n", package_name.c_str());
305 writer.Write("%s", kJavaFileImports);
Inseob Kim1a58c042018-11-01 16:37:34 +0900306 WriteJavaAnnotation(writer, classScope);
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900307 writer.Write("public final class %s {\n", class_name.c_str());
308 writer.Indent();
309 writer.Write("private %s () {}\n\n", class_name.c_str());
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900310 writer.Write("%s", kJavaParsersAndFormatters);
311
312 for (int i = 0; i < props.prop_size(); ++i) {
313 writer.Write("\n");
314
315 const sysprop::Property& prop = props.prop(i);
316
Inseob Kim14e51872018-10-25 14:27:33 +0900317 std::string prop_id = ApiNameToIdentifier(prop.api_name()).c_str();
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900318 std::string prop_type = GetJavaTypeName(prop);
319
320 if (prop.type() == sysprop::Enum || prop.type() == sysprop::EnumList) {
Inseob Kim1a58c042018-11-01 16:37:34 +0900321 if (prop.scope() != classScope) {
322 WriteJavaAnnotation(writer, prop.scope());
323 }
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900324 writer.Write("public static enum %s {\n",
325 GetJavaEnumTypeName(prop).c_str());
326 writer.Indent();
327 for (const std::string& name :
328 android::base::Split(prop.enum_values(), "|")) {
329 writer.Write("%s,\n", name.c_str());
330 }
331 writer.Dedent();
332 writer.Write("}\n\n");
333 }
334
Inseob Kim1a58c042018-11-01 16:37:34 +0900335 if (prop.scope() != classScope) {
336 WriteJavaAnnotation(writer, prop.scope());
337 }
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900338
339 writer.Write("public static Optional<%s> %s() {\n", prop_type.c_str(),
340 prop_id.c_str());
341 writer.Indent();
Inseob Kim14e51872018-10-25 14:27:33 +0900342 writer.Write("String value = SystemProperties.get(\"%s\");\n",
343 prop.prop_name().c_str());
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900344 writer.Write("return Optional.ofNullable(%s);\n",
345 GetParsingExpression(prop).c_str());
346 writer.Dedent();
Inseob Kim0773b942018-10-04 19:29:27 +0900347 writer.Write("}\n");
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900348
Inseob Kim14e51872018-10-25 14:27:33 +0900349 if (prop.access() != sysprop::Readonly) {
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900350 writer.Write("\n");
Inseob Kim1a58c042018-11-01 16:37:34 +0900351 if (classScope != sysprop::Internal) {
352 WriteJavaAnnotation(writer, sysprop::Internal);
353 }
Inseob Kim0773b942018-10-04 19:29:27 +0900354 writer.Write("public static void %s(%s value) {\n", prop_id.c_str(),
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900355 prop_type.c_str());
356 writer.Indent();
Inseob Kimbd987ed2018-11-01 11:07:15 +0900357 writer.Write("SystemProperties.set(\"%s\", value == null ? \"\" : %s);\n",
Inseob Kim14e51872018-10-25 14:27:33 +0900358 prop.prop_name().c_str(),
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900359 IsListProp(prop) ? "formatList(value)" : "value.toString()");
360 writer.Dedent();
Inseob Kim14e51872018-10-25 14:27:33 +0900361 writer.Write("}\n");
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900362 }
Inseob Kim4c474eb2018-11-27 12:17:21 +0900363
364 if (IsListProp(prop)) {
365 writer.Write("\n");
366 if (prop.scope() != classScope) {
367 WriteJavaAnnotation(writer, prop.scope());
368 }
369
370 std::string element_type = GetJavaListElementTypeName(prop);
371
372 writer.Write("public static Optional<%s> %s(int index) {\n",
373 element_type.c_str(), prop_id.c_str());
374 writer.Indent();
375 writer.Write(
376 "return %s().filter(list -> 0 <= index && index < list.size())\n",
377 prop_id.c_str());
378 writer.Write(" .map(list -> list.get(index));\n");
379 writer.Dedent();
380 writer.Write("}\n");
381 }
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900382 }
383
384 writer.Dedent();
385 writer.Write("}\n");
386
387 *java_result = writer.Code();
388 return true;
389}
390
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900391} // namespace
392
393bool GenerateJavaLibrary(const std::string& input_file_path,
Inseob Kim0773b942018-10-04 19:29:27 +0900394 const std::string& java_output_dir, std::string* err) {
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900395 sysprop::Properties props;
396
397 if (!ParseProps(input_file_path, &props, err)) {
398 return false;
399 }
400
Inseob Kim0773b942018-10-04 19:29:27 +0900401 std::string java_result;
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900402
403 if (!GenerateJavaClass(props, &java_result, err)) {
404 return false;
405 }
406
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900407 std::string package_name = GetJavaPackageName(props);
408 std::string java_package_dir =
409 java_output_dir + "/" + std::regex_replace(package_name, kRegexDot, "/");
410
411 if (!IsDirectory(java_package_dir) && !CreateDirectories(java_package_dir)) {
412 *err = "Creating directory to " + java_package_dir +
413 " failed: " + strerror(errno);
414 return false;
415 }
416
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900417 std::string class_name = GetJavaClassName(props);
418 std::string java_output_file = java_package_dir + "/" + class_name + ".java";
419 if (!android::base::WriteStringToFile(java_result, java_output_file)) {
420 *err = "Writing generated java class to " + java_output_file +
421 " failed: " + strerror(errno);
422 return false;
423 }
424
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900425 return true;
426}