1 /* 2 * Copyright (C) 2021 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 package com.android.car.internal.util; 17 18 import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 19 import static java.lang.annotation.ElementType.CONSTRUCTOR; 20 import static java.lang.annotation.ElementType.FIELD; 21 import static java.lang.annotation.ElementType.LOCAL_VARIABLE; 22 import static java.lang.annotation.ElementType.METHOD; 23 import static java.lang.annotation.ElementType.PARAMETER; 24 import static java.lang.annotation.ElementType.TYPE; 25 26 import android.annotation.IntDef; 27 import android.annotation.Nullable; 28 import android.annotation.StringDef; 29 import android.os.Parcelable; 30 31 import java.lang.annotation.ElementType; 32 import java.lang.annotation.Retention; 33 import java.lang.annotation.RetentionPolicy; 34 import java.lang.annotation.Target; 35 36 37 /** 38 * {@code com.android.internal.util.DataClass} replacement for car-lib. 39 * 40 * @hide 41 */ 42 @Retention(RetentionPolicy.SOURCE) 43 @Target(ElementType.TYPE) 44 public @interface DataClass { 45 46 /** 47 * Generates {@link Parcelable#writeToParcel}, {@link Parcelable#describeContents} and a 48 * {@link Parcelable.Creator}. 49 * 50 * Can be implicitly requested by adding "implements Parcelable" to class signature 51 * 52 * You can provide custom parceling logic by using a {@link ParcelWith} annotation with a 53 * custom {@link Parcelling} subclass. 54 * 55 * Alternatively, for one-off customizations you can declare methods like: 56 * {@code void parcelFieldName(Parcel dest, int flags)} 57 * {@code static FieldType unparcelFieldName(Parcel in)} 58 * 59 * @hide 60 */ genParcelable()61 boolean genParcelable() default false; 62 63 /** 64 * Generates a simple "parcelable" .aidl file alongside the original .java file 65 * 66 * If not explicitly requested/suppressed, is on iff {@link #genParcelable} is on 67 */ genAidl()68 boolean genAidl() default false; 69 70 /** 71 * Generates getters for each field. 72 * 73 * You can request for getter to lazily initialize your field by declaring a method like: 74 * {@code FieldType lazyInitFieldName()} 75 * 76 * You can request for the lazy initialization to be thread safe my marking the field volatile. 77 */ genGetters()78 boolean genGetters() default true; 79 80 /** 81 * {@link #genGetters} with @hide 82 */ genHiddenGetters()83 boolean genHiddenGetters() default false; 84 85 /** 86 * Generates setters for each field. 87 */ genSetters()88 boolean genSetters() default false; 89 90 /** 91 * {@link #genSetters} with @hide 92 */ genHiddenSetters()93 boolean genHiddenSetters() default false; 94 95 /** 96 * Generates a public constructor with each field initialized from a parameter and optionally 97 * some user-defined state validation at the end. 98 * 99 * Uses field {@link Nullable nullability}/default value presence to determine optional 100 * parameters. 101 * 102 * Requesting a {@link #genBuilder} suppresses public constructor generation by default. 103 * 104 * You receive a callback at the end of constructor call by declaring the method: 105 * {@code void onConstructed()} 106 * This is the place to put any custom validation logic. 107 */ genConstructor()108 boolean genConstructor() default true; 109 110 /** 111 * {@link #genConstructor} with @hide 112 */ genHiddenConstructor()113 boolean genHiddenConstructor() default false; 114 115 /** 116 * Generates a Builder for your class. 117 * 118 * Uses a package-private constructor under the hood, so same rules hold as for 119 * {@link #genConstructor()} 120 */ genBuilder()121 boolean genBuilder() default false; 122 123 /** 124 * {@link #genBuilder} with @hide 125 */ genHiddenBuilder()126 boolean genHiddenBuilder() default false; 127 128 /** 129 * Generates a structural {@link Object#equals} + {@link Object#hashCode}. 130 * 131 * You can customize individual fields' logic by declaring methods like: 132 * {@link boolean fieldNameEquals(ClassName otherInstance)} 133 * {@link boolean fieldNameEquals(FieldType otherValue)} 134 * {@link int fieldNameHashCode()} 135 */ genEqualsHashCode()136 boolean genEqualsHashCode() default false; 137 138 /** 139 * Generates a structural {@link Object#toString}. 140 * 141 * You can customize individual fields' logic by declaring methods like: 142 * {@link String fieldNameToString()} 143 */ genToString()144 boolean genToString() default false; 145 146 /** 147 * Generates a utility method that takes a {@link PerObjectFieldAction per-field callback} 148 * and calls it once for each field with its name and value. 149 * 150 * If some fields are of primitive types, and additional overload is generated that takes 151 * multiple callbacks, specialized for used primitive types to avoid auto-boxing, e.g. 152 * {@link PerIntFieldAction}. 153 */ genForEachField()154 boolean genForEachField() default false; 155 156 /** 157 * Generates a constructor that copies the given instance of the same class. 158 */ genCopyConstructor()159 boolean genCopyConstructor() default false; 160 161 /** 162 * {@link #genCopyConstructor} with @hide 163 */ genHiddenCopyConstructor()164 boolean genHiddenCopyConstructor() default false; 165 166 /** 167 * Generates constant annotations({@link IntDef}/{@link StringDef}) for any constant groups 168 * with common prefix. 169 * The annotation names are based on the common prefix. 170 * 171 * For int constants this additionally generates the corresponding static *ToString method and 172 * uses it in {@link Object#toString}. 173 * 174 * Additionally, any fields you annotate with the generated constants will be automatically 175 * validated in constructor. 176 * 177 * Int constants specified as hex(0x..) are considered to be flags, which is taken into account 178 * for in their *ToString and validation. 179 * 180 * You can optionally override the name of the generated annotation by annotating each constant 181 * with the desired annotation name. 182 * 183 * Unless suppressed, is implied by presence of constants with common prefix. 184 */ genConstDefs()185 boolean genConstDefs() default true; 186 187 /** 188 * {@link #genConstDefs} with @hide 189 */ genHiddenConstDefs()190 boolean genHiddenConstDefs() default false; 191 192 193 /** 194 * Allows specifying custom parcelling logic based on reusable 195 * {@link Parcelling} implementations 196 */ 197 @Retention(RetentionPolicy.SOURCE) 198 @Target(FIELD) 199 @interface ParcelWith { value()200 Class<? extends Parcelling> value(); 201 } 202 203 /** 204 * Allows specifying a singular name for a builder's plural field name e.g. 'name' for 'mNames' 205 * Used for Builder's {@code addName(String name)} methods 206 */ 207 @Retention(RetentionPolicy.SOURCE) 208 @Target(FIELD) 209 @interface PluralOf { value()210 String value(); 211 } 212 213 /** 214 * Marks that any annotations following it are applicable to each element of the 215 * collection/array, as opposed to itself. 216 */ 217 @Retention(RetentionPolicy.SOURCE) 218 @Target({FIELD, METHOD, PARAMETER, LOCAL_VARIABLE}) 219 @interface Each {} 220 221 /** 222 * @deprecated to be used by code generator exclusively 223 */ 224 @Deprecated 225 @Retention(RetentionPolicy.SOURCE) 226 @Target({METHOD}) 227 @interface Generated { time()228 long time(); codegenVersion()229 String codegenVersion(); sourceFile()230 String sourceFile(); inputSignatures()231 String inputSignatures() default ""; 232 233 /** 234 * @deprecated to be used by code generator exclusively 235 */ 236 @Deprecated 237 @Retention(RetentionPolicy.SOURCE) 238 @Target({FIELD, METHOD, ANNOTATION_TYPE, CONSTRUCTOR, TYPE}) 239 @interface Member {} 240 } 241 242 /** 243 * Opt out of generating {@link #genConstDefs IntDef/StringDef}s for annotated constant 244 */ 245 @Retention(RetentionPolicy.SOURCE) 246 @Target({FIELD}) 247 @interface SuppressConstDefsGeneration {} 248 249 /** 250 * A class-level annotation to suppress methods' generation by name 251 */ 252 @Retention(RetentionPolicy.SOURCE) 253 @Target({TYPE}) 254 @interface Suppress { value()255 String[] value(); 256 } 257 258 /** 259 * Mark that the field should have a {@link Nullable} argument for its setter. 260 */ 261 @Retention(RetentionPolicy.SOURCE) 262 @Target({FIELD}) 263 @interface MaySetToNull {} 264 265 /** 266 * Callback used by {@link #genForEachField}. 267 * 268 * @param <THIS> The enclosing data class instance. 269 * Can be used to try and avoid capturing values from outside of the lambda, 270 * minimizing allocations. 271 */ 272 interface PerObjectFieldAction<THIS> { acceptObject(THIS self, String fieldName, Object fieldValue)273 void acceptObject(THIS self, String fieldName, Object fieldValue); 274 } 275 276 /** 277 * A specialization of {@link PerObjectFieldAction} called exclusively for int fields to avoid 278 * boxing. 279 * 280 * @param <THIS> The enclosing data class instance. 281 */ 282 interface PerIntFieldAction<THIS> { acceptInt(THIS self, String fieldName, int fieldValue)283 void acceptInt(THIS self, String fieldName, int fieldValue); 284 } 285 } 286