Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 1 | /* |
Ashley Rose | c1a4dec | 2018-12-13 18:06:30 -0500 | [diff] [blame] | 2 | * Copyright 2019 The Android Open Source Project |
Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 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 | package android.processor.view.inspector; |
| 18 | |
Ashley Rose | 89d6bce | 2019-03-01 19:24:50 -0500 | [diff] [blame] | 19 | |
Ashley Rose | 0b671da | 2019-01-25 15:41:29 -0500 | [diff] [blame] | 20 | import android.processor.view.inspector.InspectableClassModel.IntEnumEntry; |
| 21 | import android.processor.view.inspector.InspectableClassModel.IntFlagEntry; |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 22 | import android.processor.view.inspector.InspectableClassModel.Property; |
| 23 | |
Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 24 | import com.squareup.javapoet.ClassName; |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 25 | import com.squareup.javapoet.CodeBlock; |
| 26 | import com.squareup.javapoet.FieldSpec; |
Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 27 | import com.squareup.javapoet.JavaFile; |
| 28 | import com.squareup.javapoet.MethodSpec; |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 29 | import com.squareup.javapoet.NameAllocator; |
Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 30 | import com.squareup.javapoet.ParameterizedTypeName; |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 31 | import com.squareup.javapoet.TypeName; |
Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 32 | import com.squareup.javapoet.TypeSpec; |
| 33 | |
| 34 | import java.io.IOException; |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 35 | import java.util.ArrayList; |
| 36 | import java.util.Comparator; |
| 37 | import java.util.List; |
| 38 | import java.util.NoSuchElementException; |
Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 39 | import java.util.Optional; |
| 40 | |
| 41 | import javax.annotation.processing.Filer; |
| 42 | import javax.lang.model.element.Modifier; |
| 43 | |
| 44 | /** |
| 45 | * Generates a source file defining a {@link android.view.inspector.InspectionCompanion}. |
| 46 | */ |
| 47 | public final class InspectionCompanionGenerator { |
| 48 | private final Filer mFiler; |
| 49 | private final Class mRequestingClass; |
| 50 | |
| 51 | /** |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 52 | * The class name for {@code R.java}. |
| 53 | */ |
| 54 | private static final ClassName R_CLASS_NAME = ClassName.get("android", "R"); |
| 55 | |
| 56 | /** |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 57 | * The class name of {@link android.view.inspector.InspectionCompanion}. |
| 58 | */ |
| 59 | private static final ClassName INSPECTION_COMPANION = ClassName.get( |
| 60 | "android.view.inspector", "InspectionCompanion"); |
| 61 | |
| 62 | /** |
| 63 | * The class name of {@link android.view.inspector.PropertyMapper}. |
| 64 | */ |
| 65 | private static final ClassName PROPERTY_MAPPER = ClassName.get( |
| 66 | "android.view.inspector", "PropertyMapper"); |
| 67 | |
| 68 | /** |
| 69 | * The class name of {@link android.view.inspector.PropertyReader}. |
| 70 | */ |
| 71 | private static final ClassName PROPERTY_READER = ClassName.get( |
| 72 | "android.view.inspector", "PropertyReader"); |
| 73 | |
| 74 | /** |
Ashley Rose | 0b671da | 2019-01-25 15:41:29 -0500 | [diff] [blame] | 75 | * The class name of {@link android.view.inspector.IntEnumMapping}. |
| 76 | */ |
| 77 | private static final ClassName INT_ENUM_MAPPING = ClassName.get( |
| 78 | "android.view.inspector", "IntEnumMapping"); |
| 79 | |
| 80 | /** |
| 81 | * The class name of {@link android.view.inspector.IntFlagMapping}. |
| 82 | */ |
| 83 | private static final ClassName INT_FLAG_MAPPING = ClassName.get( |
| 84 | "android.view.inspector", "IntFlagMapping"); |
| 85 | |
| 86 | /** |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 87 | * The {@code mPropertiesMapped} field. |
| 88 | */ |
| 89 | private static final FieldSpec M_PROPERTIES_MAPPED = FieldSpec |
| 90 | .builder(TypeName.BOOLEAN, "mPropertiesMapped", Modifier.PRIVATE) |
| 91 | .initializer("false") |
| 92 | .addJavadoc( |
| 93 | "Set by {@link #mapProperties($T)} once properties have been mapped.\n", |
| 94 | PROPERTY_MAPPER) |
| 95 | .build(); |
| 96 | |
| 97 | /** |
| 98 | * The suffix of the generated class name after the class's binary name. |
| 99 | */ |
| 100 | private static final String GENERATED_CLASS_SUFFIX = "$$InspectionCompanion"; |
| 101 | |
| 102 | /** |
Ashley Rose | c1a4dec | 2018-12-13 18:06:30 -0500 | [diff] [blame] | 103 | * The null resource ID, copied to avoid a host dependency on platform code. |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 104 | * |
Aurimas Liutikas | d8ebfef | 2019-01-16 12:46:42 -0800 | [diff] [blame] | 105 | * @see android.content.res.Resources#ID_NULL |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 106 | */ |
Ashley Rose | c1a4dec | 2018-12-13 18:06:30 -0500 | [diff] [blame] | 107 | private static final int ID_NULL = 0; |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 108 | |
| 109 | /** |
Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 110 | * @param filer A filer to write the generated source to |
| 111 | * @param requestingClass A class object representing the class that invoked the generator |
| 112 | */ |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 113 | public InspectionCompanionGenerator(Filer filer, Class requestingClass) { |
Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 114 | mFiler = filer; |
| 115 | mRequestingClass = requestingClass; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Generate and write an inspection companion. |
| 120 | * |
| 121 | * @param model The model to generated |
| 122 | * @throws IOException From the Filer |
| 123 | */ |
| 124 | public void generate(InspectableClassModel model) throws IOException { |
| 125 | generateFile(model).writeTo(mFiler); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Generate a {@link JavaFile} from a model. |
| 130 | * |
| 131 | * This is package-public for testing. |
| 132 | * |
| 133 | * @param model The model to generate from |
| 134 | * @return A generated file of an {@link android.view.inspector.InspectionCompanion} |
| 135 | */ |
| 136 | JavaFile generateFile(InspectableClassModel model) { |
| 137 | return JavaFile |
| 138 | .builder(model.getClassName().packageName(), generateTypeSpec(model)) |
| 139 | .indent(" ") |
| 140 | .build(); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Generate a {@link TypeSpec} for the {@link android.view.inspector.InspectionCompanion} |
| 145 | * for the supplied model. |
| 146 | * |
| 147 | * @param model The model to generate from |
| 148 | * @return A TypeSpec of the inspection companion |
| 149 | */ |
| 150 | private TypeSpec generateTypeSpec(InspectableClassModel model) { |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 151 | final List<PropertyIdField> propertyIdFields = generatePropertyIdFields(model); |
| 152 | |
Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 153 | TypeSpec.Builder builder = TypeSpec |
| 154 | .classBuilder(generateClassName(model)) |
| 155 | .addModifiers(Modifier.PUBLIC, Modifier.FINAL) |
| 156 | .addSuperinterface(ParameterizedTypeName.get( |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 157 | INSPECTION_COMPANION, model.getClassName())) |
Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 158 | .addJavadoc("Inspection companion for {@link $T}.\n\n", model.getClassName()) |
| 159 | .addJavadoc("Generated by {@link $T}\n", getClass()) |
| 160 | .addJavadoc("on behalf of {@link $T}.\n", mRequestingClass) |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 161 | .addField(M_PROPERTIES_MAPPED); |
| 162 | |
| 163 | for (PropertyIdField propertyIdField : propertyIdFields) { |
| 164 | builder.addField(propertyIdField.mFieldSpec); |
| 165 | } |
| 166 | |
| 167 | builder.addMethod(generateMapProperties(propertyIdFields)) |
| 168 | .addMethod(generateReadProperties(model, propertyIdFields)); |
Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 169 | |
| 170 | generateGetNodeName(model).ifPresent(builder::addMethod); |
| 171 | |
| 172 | return builder.build(); |
| 173 | } |
| 174 | |
| 175 | /** |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 176 | * Build a list of {@link PropertyIdField}'s for a model. |
| 177 | * |
| 178 | * To insure idempotency of the generated code, this method sorts the list of properties |
| 179 | * alphabetically by name. |
| 180 | * |
| 181 | * A {@link NameAllocator} is used to ensure that the field names are valid Java identifiers, |
| 182 | * and it prevents overlaps in names by suffixing them as needed. |
| 183 | * |
| 184 | * @param model The model to get properties from |
| 185 | * @return A list of properties and fields |
| 186 | */ |
| 187 | private List<PropertyIdField> generatePropertyIdFields(InspectableClassModel model) { |
| 188 | final NameAllocator nameAllocator = new NameAllocator(); |
| 189 | final List<Property> sortedProperties = new ArrayList<>(model.getAllProperties()); |
| 190 | final List<PropertyIdField> propertyIdFields = new ArrayList<>(sortedProperties.size()); |
| 191 | |
| 192 | sortedProperties.sort(Comparator.comparing(Property::getName)); |
| 193 | |
| 194 | for (Property property : sortedProperties) { |
| 195 | // Format a property to a member field name like "someProperty" -> "mSomePropertyId" |
| 196 | final String memberName = String.format( |
| 197 | "m%s%sId", |
| 198 | property.getName().substring(0, 1).toUpperCase(), |
| 199 | property.getName().substring(1)); |
| 200 | final FieldSpec fieldSpec = FieldSpec |
| 201 | .builder(TypeName.INT, nameAllocator.newName(memberName), Modifier.PRIVATE) |
| 202 | .addJavadoc("Property ID of {@code $L}.\n", property.getName()) |
| 203 | .build(); |
| 204 | |
| 205 | propertyIdFields.add(new PropertyIdField(fieldSpec, property)); |
| 206 | } |
| 207 | |
| 208 | return propertyIdFields; |
| 209 | } |
| 210 | |
| 211 | /** |
Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 212 | * Generate a method definition for |
| 213 | * {@link android.view.inspector.InspectionCompanion#getNodeName()}, if needed. |
| 214 | * |
| 215 | * If {@link InspectableClassModel#getNodeName()} is empty, This method returns an empty |
| 216 | * optional, otherwise, it generates a simple method that returns the string value of the |
| 217 | * node name. |
| 218 | * |
| 219 | * @param model The model to generate from |
| 220 | * @return The method definition or an empty Optional |
| 221 | */ |
| 222 | private Optional<MethodSpec> generateGetNodeName(InspectableClassModel model) { |
| 223 | return model.getNodeName().map(nodeName -> MethodSpec.methodBuilder("getNodeName") |
| 224 | .addAnnotation(Override.class) |
| 225 | .addModifiers(Modifier.PUBLIC) |
| 226 | .returns(String.class) |
| 227 | .addStatement("return $S", nodeName) |
| 228 | .build()); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Generate a method definition for |
| 233 | * {@link android.view.inspector.InspectionCompanion#mapProperties( |
| 234 | * android.view.inspector.PropertyMapper)}. |
| 235 | * |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 236 | * @param propertyIdFields A list of properties to map to ID fields |
Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 237 | * @return The method definition |
| 238 | */ |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 239 | private MethodSpec generateMapProperties(List<PropertyIdField> propertyIdFields) { |
| 240 | final MethodSpec.Builder builder = MethodSpec.methodBuilder("mapProperties") |
Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 241 | .addAnnotation(Override.class) |
| 242 | .addModifiers(Modifier.PUBLIC) |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 243 | .addParameter(PROPERTY_MAPPER, "propertyMapper"); |
| 244 | |
| 245 | propertyIdFields.forEach(p -> builder.addStatement(generatePropertyMapperInvocation(p))); |
| 246 | builder.addStatement("$N = true", M_PROPERTIES_MAPPED); |
| 247 | |
| 248 | return builder.build(); |
Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Generate a method definition for |
| 253 | * {@link android.view.inspector.InspectionCompanion#readProperties( |
| 254 | * Object, android.view.inspector.PropertyReader)}. |
| 255 | * |
Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 256 | * @param model The model to generate from |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 257 | * @param propertyIdFields A list of properties and ID fields to read from |
Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 258 | * @return The method definition |
| 259 | */ |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 260 | private MethodSpec generateReadProperties( |
| 261 | InspectableClassModel model, |
| 262 | List<PropertyIdField> propertyIdFields) { |
| 263 | final MethodSpec.Builder builder = MethodSpec.methodBuilder("readProperties") |
Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 264 | .addAnnotation(Override.class) |
| 265 | .addModifiers(Modifier.PUBLIC) |
Ashley Rose | 0b671da | 2019-01-25 15:41:29 -0500 | [diff] [blame] | 266 | .addParameter(model.getClassName(), "node") |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 267 | .addParameter(PROPERTY_READER, "propertyReader") |
| 268 | .addCode(generatePropertyMapInitializationCheck()); |
| 269 | |
| 270 | for (PropertyIdField propertyIdField : propertyIdFields) { |
| 271 | builder.addStatement( |
Ashley Rose | 89d6bce | 2019-03-01 19:24:50 -0500 | [diff] [blame] | 272 | "propertyReader.read$L($N, node.$L)", |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 273 | methodSuffixForPropertyType(propertyIdField.mProperty.getType()), |
| 274 | propertyIdField.mFieldSpec, |
Ashley Rose | 89d6bce | 2019-03-01 19:24:50 -0500 | [diff] [blame] | 275 | propertyIdField.mProperty.getAccessor().invocation()); |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | return builder.build(); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Generate a statement maps a property with a {@link android.view.inspector.PropertyMapper}. |
| 283 | * |
| 284 | * @param propertyIdField The property model and ID field to generate from |
| 285 | * @return A statement that invokes property mapper method |
| 286 | */ |
| 287 | private CodeBlock generatePropertyMapperInvocation(PropertyIdField propertyIdField) { |
| 288 | final CodeBlock.Builder builder = CodeBlock.builder(); |
| 289 | final Property property = propertyIdField.mProperty; |
| 290 | final FieldSpec fieldSpec = propertyIdField.mFieldSpec; |
| 291 | |
| 292 | builder.add( |
| 293 | "$N = propertyMapper.map$L($S,$W", |
| 294 | fieldSpec, |
| 295 | methodSuffixForPropertyType(property.getType()), |
| 296 | property.getName()); |
| 297 | |
| 298 | if (property.isAttributeIdInferrableFromR()) { |
| 299 | builder.add("$T.attr.$L", R_CLASS_NAME, property.getName()); |
| 300 | } else { |
Ashley Rose | c1a4dec | 2018-12-13 18:06:30 -0500 | [diff] [blame] | 301 | if (property.getAttributeId() == ID_NULL) { |
| 302 | builder.add("$L", ID_NULL); |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 303 | } else { |
Ashley Rose | 0b671da | 2019-01-25 15:41:29 -0500 | [diff] [blame] | 304 | builder.add("$L", hexLiteral(property.getAttributeId())); |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
| 308 | switch (property.getType()) { |
| 309 | case INT_ENUM: |
Ashley Rose | 0b671da | 2019-01-25 15:41:29 -0500 | [diff] [blame] | 310 | builder.add(",$W"); |
| 311 | builder.add(generateIntEnumMappingBuilder(property.getIntEnumEntries())); |
| 312 | break; |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 313 | case INT_FLAG: |
Ashley Rose | 0b671da | 2019-01-25 15:41:29 -0500 | [diff] [blame] | 314 | builder.add(",$W"); |
| 315 | builder.add(generateIntFlagMappingBuilder(property.getIntFlagEntries())); |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 316 | break; |
| 317 | } |
| 318 | |
Ashley Rose | 0b671da | 2019-01-25 15:41:29 -0500 | [diff] [blame] | 319 | return builder.add(")").build(); |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Generate a check that throws |
| 324 | * {@link android.view.inspector.InspectionCompanion.UninitializedPropertyMapException} |
| 325 | * if the properties haven't been initialized. |
| 326 | * |
| 327 | * <pre> |
| 328 | * if (!mPropertiesMapped) { |
| 329 | * throw new InspectionCompanion.UninitializedPropertyMapException(); |
| 330 | * } |
| 331 | * </pre> |
| 332 | * |
| 333 | * @return A codeblock containing the property map initialization check |
| 334 | */ |
| 335 | private CodeBlock generatePropertyMapInitializationCheck() { |
| 336 | return CodeBlock.builder() |
| 337 | .beginControlFlow("if (!$N)", M_PROPERTIES_MAPPED) |
| 338 | .addStatement( |
| 339 | "throw new $T()", |
| 340 | INSPECTION_COMPANION.nestedClass("UninitializedPropertyMapException")) |
| 341 | .endControlFlow() |
Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 342 | .build(); |
| 343 | } |
| 344 | |
| 345 | /** |
Ashley Rose | 0b671da | 2019-01-25 15:41:29 -0500 | [diff] [blame] | 346 | * Generate an invocation of {@link android.view.inspector.IntEnumMapping.Builder}. |
| 347 | * |
| 348 | * <pre> |
| 349 | * new IntEnumMapping.Builder() |
| 350 | * .addValue("ONE", 1) |
| 351 | * .build() |
| 352 | * </pre> |
| 353 | * |
| 354 | * @return A codeblock containing the an int enum mapping builder |
| 355 | */ |
| 356 | private CodeBlock generateIntEnumMappingBuilder(List<IntEnumEntry> intEnumEntries) { |
| 357 | final ArrayList<IntEnumEntry> sortedEntries = new ArrayList<>(intEnumEntries); |
| 358 | sortedEntries.sort(Comparator.comparing(IntEnumEntry::getValue)); |
| 359 | |
| 360 | final CodeBlock.Builder builder = CodeBlock.builder() |
| 361 | .add("new $T()$>", INT_ENUM_MAPPING.nestedClass("Builder")); |
| 362 | |
| 363 | for (IntEnumEntry entry : sortedEntries) { |
| 364 | builder.add("\n.addValue($S, $L)", entry.getName(), entry.getValue()); |
| 365 | } |
| 366 | |
| 367 | return builder.add("\n.build()$<").build(); |
| 368 | } |
| 369 | |
| 370 | private CodeBlock generateIntFlagMappingBuilder(List<IntFlagEntry> intFlagEntries) { |
| 371 | final ArrayList<IntFlagEntry> sortedEntries = new ArrayList<>(intFlagEntries); |
| 372 | sortedEntries.sort(Comparator.comparing(IntFlagEntry::getName)); |
| 373 | |
| 374 | final CodeBlock.Builder builder = CodeBlock.builder() |
| 375 | .add("new $T()$>", INT_FLAG_MAPPING.nestedClass("Builder")); |
| 376 | |
| 377 | for (IntFlagEntry entry : sortedEntries) { |
| 378 | if (entry.hasMask()) { |
| 379 | builder.add( |
| 380 | "\n.addFlag($S, $L, $L)", |
| 381 | entry.getName(), |
| 382 | hexLiteral(entry.getTarget()), |
| 383 | hexLiteral(entry.getMask())); |
| 384 | } else { |
| 385 | builder.add( |
| 386 | "\n.addFlag($S, $L)", |
| 387 | entry.getName(), |
| 388 | hexLiteral(entry.getTarget())); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | return builder.add("\n.build()$<").build(); |
| 393 | } |
| 394 | |
| 395 | /** |
Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 396 | * Generate the final class name for the inspection companion from the model's class name. |
| 397 | * |
| 398 | * The generated class is added to the same package as the source class. If the class in the |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 399 | * model is a nested class, the nested class names are joined with {@code "$"}. The suffix |
| 400 | * {@code "$$InspectionCompanion"} is always added the the generated name. E.g.: For modeled |
| 401 | * class {@code com.example.Outer.Inner}, the generated class name will be |
| 402 | * {@code com.example.Outer$Inner$$InspectionCompanion}. |
Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 403 | * |
| 404 | * @param model The model to generate from |
| 405 | * @return A class name for the generated inspection companion class |
| 406 | */ |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 407 | private static ClassName generateClassName(InspectableClassModel model) { |
Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 408 | final ClassName className = model.getClassName(); |
| 409 | |
| 410 | return ClassName.get( |
| 411 | className.packageName(), |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 412 | String.join("$", className.simpleNames()) + GENERATED_CLASS_SUFFIX); |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * Get the suffix for a {@code map} or {@code read} method for a property type. |
| 417 | * |
| 418 | * @param type The requested property type |
| 419 | * @return A method suffix |
| 420 | */ |
| 421 | private static String methodSuffixForPropertyType(Property.Type type) { |
| 422 | switch (type) { |
| 423 | case BOOLEAN: |
| 424 | return "Boolean"; |
| 425 | case BYTE: |
| 426 | return "Byte"; |
| 427 | case CHAR: |
| 428 | return "Char"; |
| 429 | case DOUBLE: |
| 430 | return "Double"; |
| 431 | case FLOAT: |
| 432 | return "Float"; |
| 433 | case INT: |
| 434 | return "Int"; |
| 435 | case LONG: |
| 436 | return "Long"; |
| 437 | case SHORT: |
| 438 | return "Short"; |
| 439 | case OBJECT: |
| 440 | return "Object"; |
| 441 | case COLOR: |
| 442 | return "Color"; |
| 443 | case GRAVITY: |
| 444 | return "Gravity"; |
| 445 | case INT_ENUM: |
| 446 | return "IntEnum"; |
| 447 | case INT_FLAG: |
| 448 | return "IntFlag"; |
Ashley Rose | e891481 | 2019-03-05 17:12:00 -0500 | [diff] [blame] | 449 | case RESOURCE_ID: |
| 450 | return "ResourceId"; |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 451 | default: |
| 452 | throw new NoSuchElementException(String.format("No such property type, %s", type)); |
| 453 | } |
| 454 | } |
| 455 | |
Ashley Rose | 0b671da | 2019-01-25 15:41:29 -0500 | [diff] [blame] | 456 | private static String hexLiteral(int value) { |
| 457 | return String.format("0x%08x", value); |
| 458 | } |
| 459 | |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 460 | /** |
| 461 | * Value class that holds a {@link Property} and a {@link FieldSpec} for that property. |
| 462 | */ |
| 463 | private static final class PropertyIdField { |
| 464 | private final FieldSpec mFieldSpec; |
| 465 | private final Property mProperty; |
| 466 | |
| 467 | private PropertyIdField(FieldSpec fieldSpec, Property property) { |
| 468 | mFieldSpec = fieldSpec; |
| 469 | mProperty = property; |
| 470 | } |
Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 471 | } |
| 472 | } |