Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 33 | namespace { |
| 34 | |
| 35 | constexpr const char* kIndent = " "; |
| 36 | |
| 37 | constexpr const char* kJavaFileImports = |
| 38 | R"(import android.annotation.SystemApi; |
| 39 | |
Inseob Kim | 0773b94 | 2018-10-04 19:29:27 +0900 | [diff] [blame] | 40 | import android.os.SystemProperties; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 41 | import java.util.ArrayList; |
| 42 | import java.util.function.Function; |
| 43 | import java.util.List; |
| 44 | import java.util.Optional; |
| 45 | import java.util.StringJoiner; |
| 46 | |
| 47 | )"; |
| 48 | |
| 49 | constexpr const char* kJavaParsersAndFormatters = |
| 50 | R"(private static Boolean tryParseBoolean(String str) { |
| 51 | switch (str.toLowerCase()) { |
| 52 | case "1": |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 53 | case "true": |
| 54 | return Boolean.TRUE; |
| 55 | case "0": |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 56 | case "false": |
| 57 | return Boolean.FALSE; |
| 58 | default: |
| 59 | return null; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | private static Integer tryParseInteger(String str) { |
| 64 | try { |
| 65 | return Integer.valueOf(str); |
| 66 | } catch (NumberFormatException e) { |
| 67 | return null; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | private static Long tryParseLong(String str) { |
| 72 | try { |
| 73 | return Long.valueOf(str); |
| 74 | } catch (NumberFormatException e) { |
| 75 | return null; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | private static Double tryParseDouble(String str) { |
| 80 | try { |
| 81 | return Double.valueOf(str); |
| 82 | } catch (NumberFormatException e) { |
| 83 | return null; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | private static String tryParseString(String str) { |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 88 | return str.length() == 0 ? null : str; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | private 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 | |
| 99 | private static <T> List<T> tryParseList(Function<String, T> elementParser, String str) { |
| 100 | List<T> ret = new ArrayList<>(); |
| 101 | |
| 102 | for (String element : str.split(",")) { |
| 103 | T parsed = elementParser.apply(element); |
| 104 | if (parsed == null) { |
| 105 | return null; |
| 106 | } |
| 107 | ret.add(parsed); |
| 108 | } |
| 109 | |
| 110 | return ret; |
| 111 | } |
| 112 | |
| 113 | private static <T extends Enum<T>> List<T> tryParseEnumList(Class<T> enumType, String str) { |
| 114 | List<T> ret = new ArrayList<>(); |
| 115 | |
| 116 | for (String element : str.split(",")) { |
| 117 | T parsed = tryParseEnum(enumType, element); |
| 118 | if (parsed == null) { |
| 119 | return null; |
| 120 | } |
| 121 | ret.add(parsed); |
| 122 | } |
| 123 | |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | private static <T> String formatList(List<T> list) { |
| 128 | StringJoiner joiner = new StringJoiner(","); |
| 129 | |
| 130 | for (T element : list) { |
| 131 | joiner.add(element.toString()); |
| 132 | } |
| 133 | |
| 134 | return joiner.toString(); |
| 135 | } |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 136 | )"; |
| 137 | |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 138 | const std::regex kRegexDot{"\\."}; |
| 139 | const std::regex kRegexUnderscore{"_"}; |
| 140 | |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 141 | std::string GetJavaTypeName(const sysprop::Property& prop); |
| 142 | std::string GetJavaEnumTypeName(const sysprop::Property& prop); |
| 143 | std::string GetJavaPackageName(const sysprop::Properties& props); |
| 144 | std::string GetJavaClassName(const sysprop::Properties& props); |
| 145 | bool IsListProp(const sysprop::Property& prop); |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 146 | void WriteJavaAnnotation(CodeWriter& writer, sysprop::Scope scope); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 147 | bool GenerateJavaClass(const sysprop::Properties& props, |
| 148 | std::string* java_result, std::string* err); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 149 | |
| 150 | std::string GetJavaEnumTypeName(const sysprop::Property& prop) { |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 151 | return ApiNameToIdentifier(prop.api_name()) + "_values"; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | std::string GetJavaTypeName(const sysprop::Property& prop) { |
| 155 | switch (prop.type()) { |
| 156 | case sysprop::Boolean: |
| 157 | return "Boolean"; |
| 158 | case sysprop::Integer: |
| 159 | return "Integer"; |
| 160 | case sysprop::Long: |
| 161 | return "Long"; |
| 162 | case sysprop::Double: |
| 163 | return "Double"; |
| 164 | case sysprop::String: |
| 165 | return "String"; |
| 166 | case sysprop::Enum: |
| 167 | return GetJavaEnumTypeName(prop); |
| 168 | case sysprop::BooleanList: |
| 169 | return "List<Boolean>"; |
| 170 | case sysprop::IntegerList: |
| 171 | return "List<Integer>"; |
| 172 | case sysprop::LongList: |
| 173 | return "List<Long>"; |
| 174 | case sysprop::DoubleList: |
| 175 | return "List<Double>"; |
| 176 | case sysprop::StringList: |
| 177 | return "List<String>"; |
| 178 | case sysprop::EnumList: |
| 179 | return "List<" + GetJavaEnumTypeName(prop) + ">"; |
| 180 | default: |
| 181 | __builtin_unreachable(); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | std::string GetParsingExpression(const sysprop::Property& prop) { |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 186 | switch (prop.type()) { |
| 187 | case sysprop::Boolean: |
Inseob Kim | 0773b94 | 2018-10-04 19:29:27 +0900 | [diff] [blame] | 188 | return "tryParseBoolean(value)"; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 189 | case sysprop::Integer: |
Inseob Kim | 0773b94 | 2018-10-04 19:29:27 +0900 | [diff] [blame] | 190 | return "tryParseInteger(value)"; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 191 | case sysprop::Long: |
Inseob Kim | 0773b94 | 2018-10-04 19:29:27 +0900 | [diff] [blame] | 192 | return "tryParseLong(value)"; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 193 | case sysprop::Double: |
Inseob Kim | 0773b94 | 2018-10-04 19:29:27 +0900 | [diff] [blame] | 194 | return "tryParseDouble(value)"; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 195 | case sysprop::String: |
Inseob Kim | 0773b94 | 2018-10-04 19:29:27 +0900 | [diff] [blame] | 196 | return "tryParseString(value)"; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 197 | case sysprop::Enum: |
Inseob Kim | 0773b94 | 2018-10-04 19:29:27 +0900 | [diff] [blame] | 198 | return "tryParseEnum(" + GetJavaEnumTypeName(prop) + ".class, value)"; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 199 | case sysprop::EnumList: |
Inseob Kim | 0773b94 | 2018-10-04 19:29:27 +0900 | [diff] [blame] | 200 | return "tryParseEnumList(" + GetJavaEnumTypeName(prop) + |
| 201 | ".class, " |
| 202 | "value)"; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 203 | default: |
| 204 | break; |
| 205 | } |
| 206 | |
| 207 | // The remaining cases are lists for types other than Enum which share the |
| 208 | // same parsing function "tryParseList" |
| 209 | std::string element_parser; |
| 210 | |
| 211 | switch (prop.type()) { |
| 212 | case sysprop::BooleanList: |
| 213 | element_parser = "v -> tryParseBoolean(v)"; |
| 214 | break; |
| 215 | case sysprop::IntegerList: |
| 216 | element_parser = "v -> tryParseInteger(v)"; |
| 217 | break; |
| 218 | case sysprop::LongList: |
| 219 | element_parser = "v -> tryParseLong(v)"; |
| 220 | break; |
| 221 | case sysprop::DoubleList: |
| 222 | element_parser = "v -> tryParseDouble(v)"; |
| 223 | break; |
| 224 | case sysprop::StringList: |
| 225 | element_parser = "v -> tryParseString(v)"; |
| 226 | break; |
| 227 | default: |
| 228 | __builtin_unreachable(); |
| 229 | } |
| 230 | |
Inseob Kim | 0773b94 | 2018-10-04 19:29:27 +0900 | [diff] [blame] | 231 | return "tryParseList(" + element_parser + ", value)"; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | std::string GetJavaPackageName(const sysprop::Properties& props) { |
| 235 | const std::string& module = props.module(); |
| 236 | return module.substr(0, module.rfind('.')); |
| 237 | } |
| 238 | |
| 239 | std::string GetJavaClassName(const sysprop::Properties& props) { |
| 240 | const std::string& module = props.module(); |
| 241 | return module.substr(module.rfind('.') + 1); |
| 242 | } |
| 243 | |
| 244 | bool IsListProp(const sysprop::Property& prop) { |
| 245 | switch (prop.type()) { |
| 246 | case sysprop::BooleanList: |
| 247 | case sysprop::IntegerList: |
| 248 | case sysprop::LongList: |
| 249 | case sysprop::DoubleList: |
| 250 | case sysprop::StringList: |
| 251 | case sysprop::EnumList: |
| 252 | return true; |
| 253 | default: |
| 254 | return false; |
| 255 | } |
| 256 | } |
| 257 | |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 258 | void WriteJavaAnnotation(CodeWriter& writer, sysprop::Scope scope) { |
| 259 | switch (scope) { |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 260 | case sysprop::System: |
| 261 | writer.Write("@SystemApi\n"); |
| 262 | break; |
| 263 | case sysprop::Internal: |
| 264 | writer.Write("/** @hide */\n"); |
| 265 | break; |
| 266 | default: |
| 267 | break; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | bool GenerateJavaClass(const sysprop::Properties& props, |
| 272 | std::string* java_result, |
| 273 | [[maybe_unused]] std::string* err) { |
Inseob Kim | 1a58c04 | 2018-11-01 16:37:34 +0900 | [diff] [blame] | 274 | sysprop::Scope classScope = sysprop::Internal; |
| 275 | |
| 276 | for (int i = 0; i < props.prop_size(); ++i) { |
| 277 | // Get least restrictive scope among props. For example, if all props |
| 278 | // are internal, class can be as well internal. However, class should |
| 279 | // be public or system if at least one prop is so. |
| 280 | classScope = std::min(classScope, props.prop(i).scope()); |
| 281 | } |
| 282 | |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 283 | std::string package_name = GetJavaPackageName(props); |
| 284 | std::string class_name = GetJavaClassName(props); |
| 285 | |
| 286 | CodeWriter writer(kIndent); |
| 287 | writer.Write("%s", kGeneratedFileFooterComments); |
| 288 | writer.Write("package %s;\n\n", package_name.c_str()); |
| 289 | writer.Write("%s", kJavaFileImports); |
Inseob Kim | 1a58c04 | 2018-11-01 16:37:34 +0900 | [diff] [blame] | 290 | WriteJavaAnnotation(writer, classScope); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 291 | writer.Write("public final class %s {\n", class_name.c_str()); |
| 292 | writer.Indent(); |
| 293 | writer.Write("private %s () {}\n\n", class_name.c_str()); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 294 | writer.Write("%s", kJavaParsersAndFormatters); |
| 295 | |
| 296 | for (int i = 0; i < props.prop_size(); ++i) { |
| 297 | writer.Write("\n"); |
| 298 | |
| 299 | const sysprop::Property& prop = props.prop(i); |
| 300 | |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 301 | std::string prop_id = ApiNameToIdentifier(prop.api_name()).c_str(); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 302 | std::string prop_type = GetJavaTypeName(prop); |
| 303 | |
| 304 | if (prop.type() == sysprop::Enum || prop.type() == sysprop::EnumList) { |
Inseob Kim | 1a58c04 | 2018-11-01 16:37:34 +0900 | [diff] [blame] | 305 | if (prop.scope() != classScope) { |
| 306 | WriteJavaAnnotation(writer, prop.scope()); |
| 307 | } |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 308 | writer.Write("public static enum %s {\n", |
| 309 | GetJavaEnumTypeName(prop).c_str()); |
| 310 | writer.Indent(); |
| 311 | for (const std::string& name : |
| 312 | android::base::Split(prop.enum_values(), "|")) { |
| 313 | writer.Write("%s,\n", name.c_str()); |
| 314 | } |
| 315 | writer.Dedent(); |
| 316 | writer.Write("}\n\n"); |
| 317 | } |
| 318 | |
Inseob Kim | 1a58c04 | 2018-11-01 16:37:34 +0900 | [diff] [blame] | 319 | if (prop.scope() != classScope) { |
| 320 | WriteJavaAnnotation(writer, prop.scope()); |
| 321 | } |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 322 | |
| 323 | writer.Write("public static Optional<%s> %s() {\n", prop_type.c_str(), |
| 324 | prop_id.c_str()); |
| 325 | writer.Indent(); |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 326 | writer.Write("String value = SystemProperties.get(\"%s\");\n", |
| 327 | prop.prop_name().c_str()); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 328 | writer.Write("return Optional.ofNullable(%s);\n", |
| 329 | GetParsingExpression(prop).c_str()); |
| 330 | writer.Dedent(); |
Inseob Kim | 0773b94 | 2018-10-04 19:29:27 +0900 | [diff] [blame] | 331 | writer.Write("}\n"); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 332 | |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 333 | if (prop.access() != sysprop::Readonly) { |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 334 | writer.Write("\n"); |
Inseob Kim | 1a58c04 | 2018-11-01 16:37:34 +0900 | [diff] [blame] | 335 | if (classScope != sysprop::Internal) { |
| 336 | WriteJavaAnnotation(writer, sysprop::Internal); |
| 337 | } |
Inseob Kim | 0773b94 | 2018-10-04 19:29:27 +0900 | [diff] [blame] | 338 | writer.Write("public static void %s(%s value) {\n", prop_id.c_str(), |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 339 | prop_type.c_str()); |
| 340 | writer.Indent(); |
Inseob Kim | bd987ed | 2018-11-01 11:07:15 +0900 | [diff] [blame] | 341 | writer.Write("SystemProperties.set(\"%s\", value == null ? \"\" : %s);\n", |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 342 | prop.prop_name().c_str(), |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 343 | IsListProp(prop) ? "formatList(value)" : "value.toString()"); |
| 344 | writer.Dedent(); |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 345 | writer.Write("}\n"); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
| 349 | writer.Dedent(); |
| 350 | writer.Write("}\n"); |
| 351 | |
| 352 | *java_result = writer.Code(); |
| 353 | return true; |
| 354 | } |
| 355 | |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 356 | } // namespace |
| 357 | |
| 358 | bool GenerateJavaLibrary(const std::string& input_file_path, |
Inseob Kim | 0773b94 | 2018-10-04 19:29:27 +0900 | [diff] [blame] | 359 | const std::string& java_output_dir, std::string* err) { |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 360 | sysprop::Properties props; |
| 361 | |
| 362 | if (!ParseProps(input_file_path, &props, err)) { |
| 363 | return false; |
| 364 | } |
| 365 | |
Inseob Kim | 0773b94 | 2018-10-04 19:29:27 +0900 | [diff] [blame] | 366 | std::string java_result; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 367 | |
| 368 | if (!GenerateJavaClass(props, &java_result, err)) { |
| 369 | return false; |
| 370 | } |
| 371 | |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 372 | std::string package_name = GetJavaPackageName(props); |
| 373 | std::string java_package_dir = |
| 374 | java_output_dir + "/" + std::regex_replace(package_name, kRegexDot, "/"); |
| 375 | |
| 376 | if (!IsDirectory(java_package_dir) && !CreateDirectories(java_package_dir)) { |
| 377 | *err = "Creating directory to " + java_package_dir + |
| 378 | " failed: " + strerror(errno); |
| 379 | return false; |
| 380 | } |
| 381 | |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 382 | std::string class_name = GetJavaClassName(props); |
| 383 | std::string java_output_file = java_package_dir + "/" + class_name + ".java"; |
| 384 | if (!android::base::WriteStringToFile(java_result, java_output_file)) { |
| 385 | *err = "Writing generated java class to " + java_output_file + |
| 386 | " failed: " + strerror(errno); |
| 387 | return false; |
| 388 | } |
| 389 | |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 390 | return true; |
| 391 | } |