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> |
Inseob Kim | 4584b8f | 2019-06-17 15:52:48 +0900 | [diff] [blame] | 26 | #include <filesystem> |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 27 | #include <regex> |
| 28 | #include <string> |
| 29 | |
| 30 | #include "CodeWriter.h" |
| 31 | #include "Common.h" |
| 32 | #include "sysprop.pb.h" |
| 33 | |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 34 | using android::base::Result; |
| 35 | |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 36 | namespace { |
| 37 | |
| 38 | constexpr const char* kIndent = " "; |
| 39 | |
| 40 | constexpr const char* kJavaFileImports = |
Inseob Kim | 38569c7 | 2019-07-30 18:36:28 +0900 | [diff] [blame] | 41 | R"(import android.os.SystemProperties; |
Inseob Kim | 3f452c0 | 2020-03-31 13:41:19 +0900 | [diff] [blame] | 42 | import android.util.Log; |
Inseob Kim | 1ca03f3 | 2019-06-08 16:31:59 +0900 | [diff] [blame] | 43 | |
Inseob Kim | 5131322 | 2020-01-15 14:37:49 +0900 | [diff] [blame] | 44 | import java.lang.StringBuilder; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 45 | import java.util.ArrayList; |
| 46 | import java.util.function.Function; |
| 47 | import java.util.List; |
Todd Kennedy | e37e10d | 2019-07-17 12:56:11 -0700 | [diff] [blame] | 48 | import java.util.Locale; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 49 | import java.util.Optional; |
| 50 | import java.util.StringJoiner; |
Inseob Kim | 9c5147d | 2019-03-06 11:36:36 +0900 | [diff] [blame] | 51 | import java.util.stream.Collectors; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 52 | |
| 53 | )"; |
| 54 | |
| 55 | constexpr const char* kJavaParsersAndFormatters = |
Inseob Kim | 5131322 | 2020-01-15 14:37:49 +0900 | [diff] [blame] | 56 | R"s(private static Boolean tryParseBoolean(String str) { |
Todd Kennedy | e37e10d | 2019-07-17 12:56:11 -0700 | [diff] [blame] | 57 | switch (str.toLowerCase(Locale.US)) { |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 58 | case "1": |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 59 | case "true": |
| 60 | return Boolean.TRUE; |
| 61 | case "0": |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 62 | case "false": |
| 63 | return Boolean.FALSE; |
| 64 | default: |
| 65 | return null; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | private static Integer tryParseInteger(String str) { |
| 70 | try { |
| 71 | return Integer.valueOf(str); |
| 72 | } catch (NumberFormatException e) { |
| 73 | return null; |
| 74 | } |
| 75 | } |
| 76 | |
Inseob Kim | 22133ec | 2020-10-28 22:08:07 +0900 | [diff] [blame] | 77 | private static Integer tryParseUInt(String str) { |
| 78 | try { |
| 79 | return Integer.parseUnsignedInt(str); |
| 80 | } catch (NumberFormatException e) { |
| 81 | return null; |
| 82 | } |
| 83 | } |
| 84 | |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 85 | private static Long tryParseLong(String str) { |
| 86 | try { |
| 87 | return Long.valueOf(str); |
| 88 | } catch (NumberFormatException e) { |
| 89 | return null; |
| 90 | } |
| 91 | } |
| 92 | |
Inseob Kim | 22133ec | 2020-10-28 22:08:07 +0900 | [diff] [blame] | 93 | private static Long tryParseULong(String str) { |
| 94 | try { |
| 95 | return Long.parseUnsignedLong(str); |
| 96 | } catch (NumberFormatException e) { |
| 97 | return null; |
| 98 | } |
| 99 | } |
| 100 | |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 101 | private static Double tryParseDouble(String str) { |
| 102 | try { |
| 103 | return Double.valueOf(str); |
| 104 | } catch (NumberFormatException e) { |
| 105 | return null; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | private static String tryParseString(String str) { |
Inseob Kim | e76e272 | 2018-12-13 00:18:25 +0900 | [diff] [blame] | 110 | return "".equals(str) ? null : str; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | private static <T extends Enum<T>> T tryParseEnum(Class<T> enumType, String str) { |
| 114 | try { |
Todd Kennedy | e37e10d | 2019-07-17 12:56:11 -0700 | [diff] [blame] | 115 | return Enum.valueOf(enumType, str.toUpperCase(Locale.US)); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 116 | } catch (IllegalArgumentException e) { |
| 117 | return null; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | private static <T> List<T> tryParseList(Function<String, T> elementParser, String str) { |
Inseob Kim | e76e272 | 2018-12-13 00:18:25 +0900 | [diff] [blame] | 122 | if ("".equals(str)) return new ArrayList<>(); |
Inseob Kim | 4c474eb | 2018-11-27 12:17:21 +0900 | [diff] [blame] | 123 | |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 124 | List<T> ret = new ArrayList<>(); |
| 125 | |
Inseob Kim | 5131322 | 2020-01-15 14:37:49 +0900 | [diff] [blame] | 126 | int p = 0; |
| 127 | for (;;) { |
| 128 | StringBuilder sb = new StringBuilder(); |
| 129 | while (p < str.length() && str.charAt(p) != ',') { |
| 130 | if (str.charAt(p) == '\\') ++p; |
| 131 | if (p == str.length()) break; |
| 132 | sb.append(str.charAt(p++)); |
| 133 | } |
| 134 | ret.add(elementParser.apply(sb.toString())); |
| 135 | if (p == str.length()) break; |
| 136 | ++p; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | private static <T extends Enum<T>> List<T> tryParseEnumList(Class<T> enumType, String str) { |
Inseob Kim | e76e272 | 2018-12-13 00:18:25 +0900 | [diff] [blame] | 143 | if ("".equals(str)) return new ArrayList<>(); |
Inseob Kim | 4c474eb | 2018-11-27 12:17:21 +0900 | [diff] [blame] | 144 | |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 145 | List<T> ret = new ArrayList<>(); |
| 146 | |
| 147 | for (String element : str.split(",")) { |
Inseob Kim | 4c474eb | 2018-11-27 12:17:21 +0900 | [diff] [blame] | 148 | ret.add(tryParseEnum(enumType, element)); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | return ret; |
| 152 | } |
| 153 | |
Inseob Kim | 5131322 | 2020-01-15 14:37:49 +0900 | [diff] [blame] | 154 | private static String escape(String str) { |
| 155 | return str.replaceAll("([\\\\,])", "\\\\$1"); |
| 156 | } |
| 157 | |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 158 | private static <T> String formatList(List<T> list) { |
| 159 | StringJoiner joiner = new StringJoiner(","); |
| 160 | |
| 161 | for (T element : list) { |
Inseob Kim | 5131322 | 2020-01-15 14:37:49 +0900 | [diff] [blame] | 162 | joiner.add(element == null ? "" : escape(element.toString())); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | return joiner.toString(); |
| 166 | } |
Inseob Kim | f346e50 | 2019-01-04 10:32:39 +0900 | [diff] [blame] | 167 | |
Inseob Kim | 22133ec | 2020-10-28 22:08:07 +0900 | [diff] [blame] | 168 | private static String formatUIntList(List<Integer> list) { |
| 169 | StringJoiner joiner = new StringJoiner(","); |
| 170 | |
| 171 | for (Integer element : list) { |
| 172 | joiner.add(element == null ? "" : escape(Integer.toUnsignedString(element))); |
| 173 | } |
| 174 | |
| 175 | return joiner.toString(); |
| 176 | } |
| 177 | |
| 178 | private static String formatULongList(List<Long> list) { |
| 179 | StringJoiner joiner = new StringJoiner(","); |
| 180 | |
| 181 | for (Long element : list) { |
| 182 | joiner.add(element == null ? "" : escape(Long.toUnsignedString(element))); |
| 183 | } |
| 184 | |
| 185 | return joiner.toString(); |
| 186 | } |
| 187 | |
Inseob Kim | f346e50 | 2019-01-04 10:32:39 +0900 | [diff] [blame] | 188 | private static <T extends Enum<T>> String formatEnumList(List<T> list, Function<T, String> elementFormatter) { |
| 189 | StringJoiner joiner = new StringJoiner(","); |
| 190 | |
| 191 | for (T element : list) { |
| 192 | joiner.add(element == null ? "" : elementFormatter.apply(element)); |
| 193 | } |
| 194 | |
| 195 | return joiner.toString(); |
| 196 | } |
Inseob Kim | 5131322 | 2020-01-15 14:37:49 +0900 | [diff] [blame] | 197 | )s"; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 198 | |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 199 | const std::regex kRegexDot{"\\."}; |
| 200 | const std::regex kRegexUnderscore{"_"}; |
| 201 | |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 202 | std::string GetJavaTypeName(const sysprop::Property& prop); |
| 203 | std::string GetJavaEnumTypeName(const sysprop::Property& prop); |
| 204 | std::string GetJavaPackageName(const sysprop::Properties& props); |
| 205 | std::string GetJavaClassName(const sysprop::Properties& props); |
Inseob Kim | f346e50 | 2019-01-04 10:32:39 +0900 | [diff] [blame] | 206 | std::string GetParsingExpression(const sysprop::Property& prop); |
| 207 | std::string GetFormattingExpression(const sysprop::Property& prop); |
Inseob Kim | 38569c7 | 2019-07-30 18:36:28 +0900 | [diff] [blame] | 208 | std::string GenerateJavaClass(const sysprop::Properties& props, |
| 209 | sysprop::Scope scope); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 210 | |
| 211 | std::string GetJavaEnumTypeName(const sysprop::Property& prop) { |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 212 | return ApiNameToIdentifier(prop.api_name()) + "_values"; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | std::string GetJavaTypeName(const sysprop::Property& prop) { |
| 216 | switch (prop.type()) { |
| 217 | case sysprop::Boolean: |
| 218 | return "Boolean"; |
| 219 | case sysprop::Integer: |
Inseob Kim | 22133ec | 2020-10-28 22:08:07 +0900 | [diff] [blame] | 220 | case sysprop::UInt: |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 221 | return "Integer"; |
| 222 | case sysprop::Long: |
Inseob Kim | 22133ec | 2020-10-28 22:08:07 +0900 | [diff] [blame] | 223 | case sysprop::ULong: |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 224 | return "Long"; |
| 225 | case sysprop::Double: |
| 226 | return "Double"; |
| 227 | case sysprop::String: |
| 228 | return "String"; |
| 229 | case sysprop::Enum: |
| 230 | return GetJavaEnumTypeName(prop); |
| 231 | case sysprop::BooleanList: |
| 232 | return "List<Boolean>"; |
| 233 | case sysprop::IntegerList: |
Inseob Kim | 22133ec | 2020-10-28 22:08:07 +0900 | [diff] [blame] | 234 | case sysprop::UIntList: |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 235 | return "List<Integer>"; |
| 236 | case sysprop::LongList: |
Inseob Kim | 22133ec | 2020-10-28 22:08:07 +0900 | [diff] [blame] | 237 | case sysprop::ULongList: |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 238 | return "List<Long>"; |
| 239 | case sysprop::DoubleList: |
| 240 | return "List<Double>"; |
| 241 | case sysprop::StringList: |
| 242 | return "List<String>"; |
| 243 | case sysprop::EnumList: |
| 244 | return "List<" + GetJavaEnumTypeName(prop) + ">"; |
| 245 | default: |
| 246 | __builtin_unreachable(); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | std::string GetParsingExpression(const sysprop::Property& prop) { |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 251 | switch (prop.type()) { |
| 252 | case sysprop::Boolean: |
Inseob Kim | 3f452c0 | 2020-03-31 13:41:19 +0900 | [diff] [blame] | 253 | return "Optional.ofNullable(tryParseBoolean(value))"; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 254 | case sysprop::Integer: |
Inseob Kim | 3f452c0 | 2020-03-31 13:41:19 +0900 | [diff] [blame] | 255 | return "Optional.ofNullable(tryParseInteger(value))"; |
Inseob Kim | 22133ec | 2020-10-28 22:08:07 +0900 | [diff] [blame] | 256 | case sysprop::UInt: |
| 257 | return "Optional.ofNullable(tryParseUInt(value))"; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 258 | case sysprop::Long: |
Inseob Kim | 3f452c0 | 2020-03-31 13:41:19 +0900 | [diff] [blame] | 259 | return "Optional.ofNullable(tryParseLong(value))"; |
Inseob Kim | 22133ec | 2020-10-28 22:08:07 +0900 | [diff] [blame] | 260 | case sysprop::ULong: |
| 261 | return "Optional.ofNullable(tryParseULong(value))"; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 262 | case sysprop::Double: |
Inseob Kim | 3f452c0 | 2020-03-31 13:41:19 +0900 | [diff] [blame] | 263 | return "Optional.ofNullable(tryParseDouble(value))"; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 264 | case sysprop::String: |
Inseob Kim | 3f452c0 | 2020-03-31 13:41:19 +0900 | [diff] [blame] | 265 | return "Optional.ofNullable(tryParseString(value))"; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 266 | case sysprop::Enum: |
Inseob Kim | 3f452c0 | 2020-03-31 13:41:19 +0900 | [diff] [blame] | 267 | return "Optional.ofNullable(tryParseEnum(" + GetJavaEnumTypeName(prop) + |
| 268 | ".class, value))"; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 269 | case sysprop::EnumList: |
Inseob Kim | 0773b94 | 2018-10-04 19:29:27 +0900 | [diff] [blame] | 270 | return "tryParseEnumList(" + GetJavaEnumTypeName(prop) + |
| 271 | ".class, " |
| 272 | "value)"; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 273 | default: |
| 274 | break; |
| 275 | } |
| 276 | |
| 277 | // The remaining cases are lists for types other than Enum which share the |
| 278 | // same parsing function "tryParseList" |
| 279 | std::string element_parser; |
| 280 | |
| 281 | switch (prop.type()) { |
| 282 | case sysprop::BooleanList: |
| 283 | element_parser = "v -> tryParseBoolean(v)"; |
| 284 | break; |
| 285 | case sysprop::IntegerList: |
| 286 | element_parser = "v -> tryParseInteger(v)"; |
| 287 | break; |
| 288 | case sysprop::LongList: |
| 289 | element_parser = "v -> tryParseLong(v)"; |
| 290 | break; |
| 291 | case sysprop::DoubleList: |
| 292 | element_parser = "v -> tryParseDouble(v)"; |
| 293 | break; |
| 294 | case sysprop::StringList: |
| 295 | element_parser = "v -> tryParseString(v)"; |
| 296 | break; |
Inseob Kim | 22133ec | 2020-10-28 22:08:07 +0900 | [diff] [blame] | 297 | case sysprop::UIntList: |
| 298 | element_parser = "v -> tryParseUInt(v)"; |
| 299 | break; |
| 300 | case sysprop::ULongList: |
| 301 | element_parser = "v -> tryParseULong(v)"; |
| 302 | break; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 303 | default: |
| 304 | __builtin_unreachable(); |
| 305 | } |
| 306 | |
Inseob Kim | 0773b94 | 2018-10-04 19:29:27 +0900 | [diff] [blame] | 307 | return "tryParseList(" + element_parser + ", value)"; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 308 | } |
| 309 | |
Inseob Kim | f346e50 | 2019-01-04 10:32:39 +0900 | [diff] [blame] | 310 | std::string GetFormattingExpression(const sysprop::Property& prop) { |
Inseob Kim | 9c5147d | 2019-03-06 11:36:36 +0900 | [diff] [blame] | 311 | if (prop.integer_as_bool()) { |
| 312 | if (prop.type() == sysprop::Boolean) { |
| 313 | // Boolean -> Integer String |
| 314 | return "(value ? \"1\" : \"0\")"; |
| 315 | } else { |
| 316 | // List<Boolean> -> String directly |
| 317 | return "value.stream().map(" |
| 318 | "x -> x == null ? \"\" : (x ? \"1\" : \"0\"))" |
| 319 | ".collect(Collectors.joining(\",\"))"; |
| 320 | } |
Inseob Kim | f346e50 | 2019-01-04 10:32:39 +0900 | [diff] [blame] | 321 | } |
Inseob Kim | 22133ec | 2020-10-28 22:08:07 +0900 | [diff] [blame] | 322 | |
| 323 | switch (prop.type()) { |
| 324 | case sysprop::Enum: |
| 325 | return "value.getPropValue()"; |
| 326 | case sysprop::EnumList: |
| 327 | return "formatEnumList(value, " + GetJavaEnumTypeName(prop) + |
| 328 | "::getPropValue)"; |
| 329 | case sysprop::UInt: |
| 330 | return "Integer.toUnsignedString(value)"; |
| 331 | case sysprop::ULong: |
| 332 | return "Long.toUnsignedString(value)"; |
| 333 | case sysprop::UIntList: |
| 334 | return "formatUIntList(value)"; |
| 335 | case sysprop::ULongList: |
| 336 | return "formatULongList(value)"; |
| 337 | default: |
| 338 | break; |
| 339 | } |
| 340 | |
| 341 | return IsListProp(prop) ? "formatList(value)" : "value.toString()"; |
Inseob Kim | f346e50 | 2019-01-04 10:32:39 +0900 | [diff] [blame] | 342 | } |
| 343 | |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 344 | std::string GetJavaPackageName(const sysprop::Properties& props) { |
| 345 | const std::string& module = props.module(); |
| 346 | return module.substr(0, module.rfind('.')); |
| 347 | } |
| 348 | |
| 349 | std::string GetJavaClassName(const sysprop::Properties& props) { |
| 350 | const std::string& module = props.module(); |
| 351 | return module.substr(module.rfind('.') + 1); |
| 352 | } |
| 353 | |
Inseob Kim | 38569c7 | 2019-07-30 18:36:28 +0900 | [diff] [blame] | 354 | std::string GenerateJavaClass(const sysprop::Properties& props, |
| 355 | sysprop::Scope scope) { |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 356 | std::string package_name = GetJavaPackageName(props); |
| 357 | std::string class_name = GetJavaClassName(props); |
| 358 | |
| 359 | CodeWriter writer(kIndent); |
| 360 | writer.Write("%s", kGeneratedFileFooterComments); |
| 361 | writer.Write("package %s;\n\n", package_name.c_str()); |
| 362 | writer.Write("%s", kJavaFileImports); |
| 363 | writer.Write("public final class %s {\n", class_name.c_str()); |
| 364 | writer.Indent(); |
| 365 | writer.Write("private %s () {}\n\n", class_name.c_str()); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 366 | writer.Write("%s", kJavaParsersAndFormatters); |
| 367 | |
| 368 | for (int i = 0; i < props.prop_size(); ++i) { |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 369 | const sysprop::Property& prop = props.prop(i); |
| 370 | |
Inseob Kim | 38569c7 | 2019-07-30 18:36:28 +0900 | [diff] [blame] | 371 | // skip if scope is internal and we are generating public class |
| 372 | if (prop.scope() > scope) continue; |
| 373 | |
| 374 | writer.Write("\n"); |
| 375 | |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 376 | std::string prop_id = ApiNameToIdentifier(prop.api_name()).c_str(); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 377 | std::string prop_type = GetJavaTypeName(prop); |
| 378 | |
| 379 | if (prop.type() == sysprop::Enum || prop.type() == sysprop::EnumList) { |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 380 | writer.Write("public static enum %s {\n", |
| 381 | GetJavaEnumTypeName(prop).c_str()); |
| 382 | writer.Indent(); |
Inseob Kim | 472fb63 | 2020-03-21 03:29:39 +0900 | [diff] [blame] | 383 | std::vector<std::string> values = ParseEnumValues(prop.enum_values()); |
Inseob Kim | f346e50 | 2019-01-04 10:32:39 +0900 | [diff] [blame] | 384 | for (int i = 0; i < values.size(); ++i) { |
| 385 | const std::string& name = values[i]; |
| 386 | writer.Write("%s(\"%s\")", ToUpper(name).c_str(), name.c_str()); |
| 387 | if (i + 1 < values.size()) { |
| 388 | writer.Write(",\n"); |
| 389 | } else { |
| 390 | writer.Write(";\n"); |
| 391 | } |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 392 | } |
Inseob Kim | f346e50 | 2019-01-04 10:32:39 +0900 | [diff] [blame] | 393 | writer.Write( |
| 394 | "private final String propValue;\n" |
| 395 | "private %s(String propValue) {\n", |
| 396 | GetJavaEnumTypeName(prop).c_str()); |
| 397 | writer.Indent(); |
| 398 | writer.Write("this.propValue = propValue;\n"); |
| 399 | writer.Dedent(); |
| 400 | writer.Write( |
| 401 | "}\n" |
| 402 | "public String getPropValue() {\n"); |
| 403 | writer.Indent(); |
| 404 | writer.Write("return propValue;\n"); |
| 405 | writer.Dedent(); |
| 406 | writer.Write("}\n"); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 407 | writer.Dedent(); |
| 408 | writer.Write("}\n\n"); |
| 409 | } |
| 410 | |
Inseob Kim | 5e64f89 | 2019-06-17 15:09:48 +0900 | [diff] [blame] | 411 | if (prop.deprecated()) { |
| 412 | writer.Write("@Deprecated\n"); |
| 413 | } |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 414 | |
Inseob Kim | e76e272 | 2018-12-13 00:18:25 +0900 | [diff] [blame] | 415 | if (IsListProp(prop)) { |
| 416 | writer.Write("public static %s %s() {\n", prop_type.c_str(), |
| 417 | prop_id.c_str()); |
Inseob Kim | e76e272 | 2018-12-13 00:18:25 +0900 | [diff] [blame] | 418 | } else { |
| 419 | writer.Write("public static Optional<%s> %s() {\n", prop_type.c_str(), |
| 420 | prop_id.c_str()); |
Inseob Kim | 3f452c0 | 2020-03-31 13:41:19 +0900 | [diff] [blame] | 421 | } |
| 422 | writer.Indent(); |
| 423 | writer.Write("String value = SystemProperties.get(\"%s\");\n", |
| 424 | prop.prop_name().c_str()); |
| 425 | if (!prop.legacy_prop_name().empty()) { |
| 426 | // SystemProperties.get() returns "" (empty string) when the property |
| 427 | // doesn't exist |
| 428 | writer.Write("if (\"\".equals(value)) {\n"); |
Inseob Kim | e76e272 | 2018-12-13 00:18:25 +0900 | [diff] [blame] | 429 | writer.Indent(); |
Inseob Kim | 3f452c0 | 2020-03-31 13:41:19 +0900 | [diff] [blame] | 430 | writer.Write( |
| 431 | "Log.d(\"%s\", \"prop %s doesn't exist; fallback to legacy prop " |
| 432 | "%s\");\n", |
| 433 | class_name.c_str(), prop.prop_name().c_str(), |
| 434 | prop.legacy_prop_name().c_str()); |
| 435 | writer.Write("value = SystemProperties.get(\"%s\");\n", |
| 436 | prop.legacy_prop_name().c_str()); |
Inseob Kim | e76e272 | 2018-12-13 00:18:25 +0900 | [diff] [blame] | 437 | writer.Dedent(); |
| 438 | writer.Write("}\n"); |
| 439 | } |
Inseob Kim | 3f452c0 | 2020-03-31 13:41:19 +0900 | [diff] [blame] | 440 | writer.Write("return %s;\n", GetParsingExpression(prop).c_str()); |
| 441 | writer.Dedent(); |
| 442 | writer.Write("}\n"); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 443 | |
Inseob Kim | 4ab20d3 | 2019-12-03 13:44:55 +0900 | [diff] [blame] | 444 | if (prop.access() != sysprop::Readonly) { |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 445 | writer.Write("\n"); |
Inseob Kim | 5e64f89 | 2019-06-17 15:09:48 +0900 | [diff] [blame] | 446 | if (prop.deprecated()) { |
| 447 | writer.Write("@Deprecated\n"); |
| 448 | } |
Inseob Kim | 0773b94 | 2018-10-04 19:29:27 +0900 | [diff] [blame] | 449 | 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] | 450 | prop_type.c_str()); |
| 451 | writer.Indent(); |
Inseob Kim | bd987ed | 2018-11-01 11:07:15 +0900 | [diff] [blame] | 452 | writer.Write("SystemProperties.set(\"%s\", value == null ? \"\" : %s);\n", |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 453 | prop.prop_name().c_str(), |
Inseob Kim | f346e50 | 2019-01-04 10:32:39 +0900 | [diff] [blame] | 454 | GetFormattingExpression(prop).c_str()); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 455 | writer.Dedent(); |
Inseob Kim | 14e5187 | 2018-10-25 14:27:33 +0900 | [diff] [blame] | 456 | writer.Write("}\n"); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | |
| 460 | writer.Dedent(); |
| 461 | writer.Write("}\n"); |
| 462 | |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 463 | return writer.Code(); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 464 | } |
| 465 | |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 466 | } // namespace |
| 467 | |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 468 | Result<void> GenerateJavaLibrary(const std::string& input_file_path, |
Inseob Kim | 38569c7 | 2019-07-30 18:36:28 +0900 | [diff] [blame] | 469 | sysprop::Scope scope, |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 470 | const std::string& java_output_dir) { |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 471 | sysprop::Properties props; |
| 472 | |
Bernie Innocenti | 7ac8da4 | 2020-02-06 23:30:12 +0900 | [diff] [blame] | 473 | if (auto res = ParseProps(input_file_path); res.ok()) { |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 474 | props = std::move(*res); |
| 475 | } else { |
| 476 | return res.error(); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 477 | } |
| 478 | |
Inseob Kim | 38569c7 | 2019-07-30 18:36:28 +0900 | [diff] [blame] | 479 | std::string java_result = GenerateJavaClass(props, scope); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 480 | std::string package_name = GetJavaPackageName(props); |
| 481 | std::string java_package_dir = |
| 482 | java_output_dir + "/" + std::regex_replace(package_name, kRegexDot, "/"); |
| 483 | |
Inseob Kim | 4584b8f | 2019-06-17 15:52:48 +0900 | [diff] [blame] | 484 | std::error_code ec; |
| 485 | std::filesystem::create_directories(java_package_dir, ec); |
| 486 | if (ec) { |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 487 | return Errorf("Creating directory to {} failed: {}", java_package_dir, |
| 488 | ec.message()); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 489 | } |
| 490 | |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 491 | std::string class_name = GetJavaClassName(props); |
| 492 | std::string java_output_file = java_package_dir + "/" + class_name + ".java"; |
| 493 | if (!android::base::WriteStringToFile(java_result, java_output_file)) { |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 494 | return ErrnoErrorf("Writing generated java class to {} failed", |
| 495 | java_output_file); |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 496 | } |
| 497 | |
Inseob Kim | 053b83d | 2019-06-26 13:41:51 +0900 | [diff] [blame] | 498 | return {}; |
Inseob Kim | 5f8f32c | 2018-08-24 11:10:44 +0900 | [diff] [blame] | 499 | } |