| Jeff Sharkey | f14ed64 | 2020-11-14 15:54:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | |
| Hai Zhang | e55b8ee | 2022-09-06 23:48:09 -0700 | [diff] [blame] | 17 | import com.android.modules.utils.TypedXmlPullParser; |
| 18 | import com.android.modules.utils.TypedXmlSerializer; |
| Jeff Sharkey | f14ed64 | 2020-11-14 15:54:22 -0700 | [diff] [blame] | 19 | |
| 20 | import com.android.internal.util.XmlUtils; |
| 21 | |
| 22 | import com.google.errorprone.refaster.annotation.AfterTemplate; |
| 23 | import com.google.errorprone.refaster.annotation.BeforeTemplate; |
| 24 | |
| 25 | /** |
| 26 | * Refaster templates that migrate callers to equivalent and more efficient |
| 27 | * {@link TypedXmlSerializer} and {@link TypedXmlPullParser} methods. |
| 28 | */ |
| 29 | public class EfficientXml { |
| 30 | class IntToString { |
| 31 | @BeforeTemplate |
| 32 | void beforeToString(TypedXmlSerializer out, String n, int v) throws Exception { |
| 33 | out.attribute(null, n, Integer.toString(v)); |
| 34 | } |
| 35 | |
| 36 | @BeforeTemplate |
| 37 | void beforeValueOf(TypedXmlSerializer out, String n, int v) throws Exception { |
| 38 | out.attribute(null, n, String.valueOf(v)); |
| 39 | } |
| 40 | |
| 41 | @BeforeTemplate |
| 42 | void beforeUtils(TypedXmlSerializer out, String n, int v) throws Exception { |
| 43 | XmlUtils.writeIntAttribute(out, n, v); |
| 44 | } |
| 45 | |
| 46 | @BeforeTemplate |
| 47 | void beforeRadix(TypedXmlSerializer out, String n, int v) throws Exception { |
| 48 | out.attribute(null, n, Integer.toString(v, 10)); |
| 49 | } |
| 50 | |
| 51 | @AfterTemplate |
| 52 | void after(TypedXmlSerializer out, String n, int v) throws Exception { |
| 53 | out.attributeInt(null, n, v); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | class IntToStringHex { |
| 58 | @BeforeTemplate |
| 59 | void beforeToHexString(TypedXmlSerializer out, String n, int v) throws Exception { |
| 60 | out.attribute(null, n, Integer.toHexString(v)); |
| 61 | } |
| 62 | |
| 63 | @BeforeTemplate |
| 64 | void beforeRadix(TypedXmlSerializer out, String n, int v) throws Exception { |
| 65 | out.attribute(null, n, Integer.toString(v, 16)); |
| 66 | } |
| 67 | |
| 68 | @AfterTemplate |
| 69 | void after(TypedXmlSerializer out, String n, int v) throws Exception { |
| 70 | out.attributeIntHex(null, n, v); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | class IntFromString { |
| 75 | @BeforeTemplate |
| 76 | int beforeParse(TypedXmlPullParser in, String n) throws Exception { |
| 77 | return Integer.parseInt(in.getAttributeValue(null, n)); |
| 78 | } |
| 79 | |
| 80 | @BeforeTemplate |
| 81 | int beforeUtils(TypedXmlPullParser in, String n) throws Exception { |
| 82 | return XmlUtils.readIntAttribute(in, n); |
| 83 | } |
| 84 | |
| 85 | @BeforeTemplate |
| 86 | int beforeRadix(TypedXmlPullParser in, String n) throws Exception { |
| 87 | return Integer.parseInt(in.getAttributeValue(null, n), 10); |
| 88 | } |
| 89 | |
| 90 | @AfterTemplate |
| 91 | int after(TypedXmlPullParser in, String n) throws Exception { |
| 92 | return in.getAttributeInt(null, n); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | class IntFromStringDefault { |
| 97 | @BeforeTemplate |
| 98 | int before(TypedXmlPullParser in, String n, int d) throws Exception { |
| 99 | return XmlUtils.readIntAttribute(in, n, d); |
| 100 | } |
| 101 | |
| 102 | @AfterTemplate |
| 103 | int after(TypedXmlPullParser in, String n, int d) throws Exception { |
| 104 | return in.getAttributeInt(null, n, d); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | class IntFromStringHex { |
| 109 | @BeforeTemplate |
| 110 | int beforeParse(TypedXmlPullParser in, String n) throws Exception { |
| 111 | return Integer.parseInt(in.getAttributeValue(null, n), 16); |
| 112 | } |
| 113 | |
| 114 | @AfterTemplate |
| 115 | int after(TypedXmlPullParser in, String n) throws Exception { |
| 116 | return in.getAttributeIntHex(null, n); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | class LongToString { |
| 121 | @BeforeTemplate |
| 122 | void beforeToString(TypedXmlSerializer out, String n, long v) throws Exception { |
| 123 | out.attribute(null, n, Long.toString(v)); |
| 124 | } |
| 125 | |
| 126 | @BeforeTemplate |
| 127 | void beforeValueOf(TypedXmlSerializer out, String n, long v) throws Exception { |
| 128 | out.attribute(null, n, String.valueOf(v)); |
| 129 | } |
| 130 | |
| 131 | @BeforeTemplate |
| 132 | void beforeUtils(TypedXmlSerializer out, String n, long v) throws Exception { |
| 133 | XmlUtils.writeLongAttribute(out, n, v); |
| 134 | } |
| 135 | |
| 136 | @BeforeTemplate |
| 137 | void beforeRadix(TypedXmlSerializer out, String n, long v) throws Exception { |
| 138 | out.attribute(null, n, Long.toString(v, 10)); |
| 139 | } |
| 140 | |
| 141 | @AfterTemplate |
| 142 | void after(TypedXmlSerializer out, String n, long v) throws Exception { |
| 143 | out.attributeLong(null, n, v); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | class LongToStringHex { |
| 148 | @BeforeTemplate |
| 149 | void beforeToHexString(TypedXmlSerializer out, String n, long v) throws Exception { |
| 150 | out.attribute(null, n, Long.toHexString(v)); |
| 151 | } |
| 152 | |
| 153 | @BeforeTemplate |
| 154 | void beforeRadix(TypedXmlSerializer out, String n, long v) throws Exception { |
| 155 | out.attribute(null, n, Long.toString(v, 16)); |
| 156 | } |
| 157 | |
| 158 | @AfterTemplate |
| 159 | void after(TypedXmlSerializer out, String n, long v) throws Exception { |
| 160 | out.attributeLongHex(null, n, v); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | class LongFromString { |
| 165 | @BeforeTemplate |
| 166 | long beforeParse(TypedXmlPullParser in, String n) throws Exception { |
| 167 | return Long.parseLong(in.getAttributeValue(null, n)); |
| 168 | } |
| 169 | |
| 170 | @BeforeTemplate |
| 171 | long beforeUtils(TypedXmlPullParser in, String n) throws Exception { |
| 172 | return XmlUtils.readLongAttribute(in, n); |
| 173 | } |
| 174 | |
| 175 | @BeforeTemplate |
| 176 | long beforeRadix(TypedXmlPullParser in, String n) throws Exception { |
| 177 | return Long.parseLong(in.getAttributeValue(null, n), 10); |
| 178 | } |
| 179 | |
| 180 | @AfterTemplate |
| 181 | long after(TypedXmlPullParser in, String n) throws Exception { |
| 182 | return in.getAttributeLong(null, n); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | class LongFromStringDefault { |
| 187 | @BeforeTemplate |
| 188 | long before(TypedXmlPullParser in, String n, long d) throws Exception { |
| 189 | return XmlUtils.readLongAttribute(in, n, d); |
| 190 | } |
| 191 | |
| 192 | @AfterTemplate |
| 193 | long after(TypedXmlPullParser in, String n, long d) throws Exception { |
| 194 | return in.getAttributeLong(null, n, d); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | class LongFromStringHex { |
| 199 | @BeforeTemplate |
| 200 | long beforeParse(TypedXmlPullParser in, String n) throws Exception { |
| 201 | return Long.parseLong(in.getAttributeValue(null, n), 16); |
| 202 | } |
| 203 | |
| 204 | @AfterTemplate |
| 205 | long after(TypedXmlPullParser in, String n) throws Exception { |
| 206 | return in.getAttributeLongHex(null, n); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | class FloatToString { |
| 211 | @BeforeTemplate |
| 212 | void beforeToString(TypedXmlSerializer out, String n, float v) throws Exception { |
| 213 | out.attribute(null, n, Float.toString(v)); |
| 214 | } |
| 215 | |
| 216 | @BeforeTemplate |
| 217 | void beforeValueOf(TypedXmlSerializer out, String n, float v) throws Exception { |
| 218 | out.attribute(null, n, String.valueOf(v)); |
| 219 | } |
| 220 | |
| 221 | @BeforeTemplate |
| 222 | void beforeUtils(TypedXmlSerializer out, String n, float v) throws Exception { |
| 223 | XmlUtils.writeFloatAttribute(out, n, v); |
| 224 | } |
| 225 | |
| 226 | @AfterTemplate |
| 227 | void after(TypedXmlSerializer out, String n, float v) throws Exception { |
| 228 | out.attributeFloat(null, n, v); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | class FloatFromString { |
| 233 | @BeforeTemplate |
| 234 | float beforeParse(TypedXmlPullParser in, String n) throws Exception { |
| 235 | return Float.parseFloat(in.getAttributeValue(null, n)); |
| 236 | } |
| 237 | |
| 238 | @BeforeTemplate |
| 239 | float beforeUtils(TypedXmlPullParser in, String n) throws Exception { |
| 240 | return XmlUtils.readFloatAttribute(in, n); |
| 241 | } |
| 242 | |
| 243 | @AfterTemplate |
| 244 | float after(TypedXmlPullParser in, String n) throws Exception { |
| 245 | return in.getAttributeFloat(null, n); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | class DoubleToString { |
| 250 | @BeforeTemplate |
| 251 | void beforeToString(TypedXmlSerializer out, String n, double v) throws Exception { |
| 252 | out.attribute(null, n, Double.toString(v)); |
| 253 | } |
| 254 | |
| 255 | @BeforeTemplate |
| 256 | void beforeValueOf(TypedXmlSerializer out, String n, double v) throws Exception { |
| 257 | out.attribute(null, n, String.valueOf(v)); |
| 258 | } |
| 259 | |
| 260 | @AfterTemplate |
| 261 | void after(TypedXmlSerializer out, String n, double v) throws Exception { |
| 262 | out.attributeDouble(null, n, v); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | class DoubleFromString { |
| 267 | @BeforeTemplate |
| 268 | double beforeParse(TypedXmlPullParser in, String n) throws Exception { |
| 269 | return Double.parseDouble(in.getAttributeValue(null, n)); |
| 270 | } |
| 271 | |
| 272 | @AfterTemplate |
| 273 | double after(TypedXmlPullParser in, String n) throws Exception { |
| 274 | return in.getAttributeDouble(null, n); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | class BooleanToString { |
| 279 | @BeforeTemplate |
| 280 | void beforeToString(TypedXmlSerializer out, String n, boolean v) throws Exception { |
| 281 | out.attribute(null, n, Boolean.toString(v)); |
| 282 | } |
| 283 | |
| 284 | @BeforeTemplate |
| 285 | void beforeValueOf(TypedXmlSerializer out, String n, boolean v) throws Exception { |
| 286 | out.attribute(null, n, String.valueOf(v)); |
| 287 | } |
| 288 | |
| 289 | @AfterTemplate |
| 290 | void after(TypedXmlSerializer out, String n, boolean v) throws Exception { |
| 291 | out.attributeBoolean(null, n, v); |
| 292 | } |
| 293 | } |
| 294 | |
| Jeff Sharkey | 1f5d9af | 2020-11-19 11:52:53 -0700 | [diff] [blame] | 295 | class BooleanToStringTrue { |
| 296 | @BeforeTemplate |
| 297 | void before(TypedXmlSerializer out, String n) throws Exception { |
| 298 | out.attribute(null, n, "true"); |
| 299 | } |
| 300 | |
| 301 | @AfterTemplate |
| 302 | void after(TypedXmlSerializer out, String n) throws Exception { |
| 303 | out.attributeBoolean(null, n, true); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | class BooleanToStringFalse { |
| 308 | @BeforeTemplate |
| 309 | void before(TypedXmlSerializer out, String n) throws Exception { |
| 310 | out.attribute(null, n, "false"); |
| 311 | } |
| 312 | |
| 313 | @AfterTemplate |
| 314 | void after(TypedXmlSerializer out, String n) throws Exception { |
| 315 | out.attributeBoolean(null, n, false); |
| 316 | } |
| 317 | } |
| 318 | |
| Jeff Sharkey | f14ed64 | 2020-11-14 15:54:22 -0700 | [diff] [blame] | 319 | class BooleanFromString { |
| 320 | @BeforeTemplate |
| 321 | boolean beforeParse(TypedXmlPullParser in, String n) throws Exception { |
| 322 | return Boolean.parseBoolean(in.getAttributeValue(null, n)); |
| 323 | } |
| 324 | |
| 325 | @BeforeTemplate |
| 326 | boolean beforeUtils(TypedXmlPullParser in, String n) throws Exception { |
| 327 | return XmlUtils.readBooleanAttribute(in, n); |
| 328 | } |
| 329 | |
| 330 | @AfterTemplate |
| 331 | boolean after(TypedXmlPullParser in, String n) throws Exception { |
| 332 | return in.getAttributeBoolean(null, n, false); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | class BooleanFromStringDefault { |
| 337 | @BeforeTemplate |
| 338 | boolean before(TypedXmlPullParser in, String n, boolean d) throws Exception { |
| 339 | return XmlUtils.readBooleanAttribute(in, n, d); |
| 340 | } |
| 341 | |
| 342 | @AfterTemplate |
| 343 | boolean after(TypedXmlPullParser in, String n, boolean d) throws Exception { |
| 344 | return in.getAttributeBoolean(null, n, d); |
| 345 | } |
| 346 | } |
| 347 | } |