summaryrefslogtreecommitdiff
path: root/core/java/com/android/internal/util/DataClass.java
blob: ee139d90888f201b5ae124113121b4b9138a9321 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.internal.util;

import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;

import android.annotation.IntDef;
import android.annotation.Nullable;
import android.annotation.StringDef;
import android.os.Parcelable;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
public @interface DataClass {

    /**
     * Generates {@link Parcelable#writeToParcel}, {@link Parcelable#describeContents} and a
     * {@link Parcelable.Creator}.
     *
     * Can be implicitly requested by adding "implements Parcelable" to class signature
     *
     * You can provide custom parceling logic by using a {@link ParcelWith} annotation with a
     * custom {@link Parcelling} subclass.
     *
     * Alternatively, for one-off customizations you can declare methods like:
     * {@code void parcelFieldName(Parcel dest, int flags)}
     * {@code static FieldType unparcelFieldName(Parcel in)}
     */
    boolean genParcelable() default false;

    /**
     * Generates a simple "parcelable" .aidl file alongside the original .java file
     *
     * If not explicitly requested/suppressed, is on iff {@link #genParcelable} is on
     */
    boolean genAidl() default false;

    /**
     * Generates getters for each field.
     *
     * You can request for getter to lazily initialize your field by declaring a method like:
     * {@code FieldType lazyInitFieldName()}
     *
     * You can request for the lazy initialization to be thread safe my marking the field volatile.
     */
    boolean genGetters() default true;

    /**
     * {@link #genGetters} with @hide
     */
    boolean genHiddenGetters() default false;

    /**
     * Generates setters for each field.
     */
    boolean genSetters() default false;

    /**
     * {@link #genSetters} with @hide
     */
    boolean genHiddenSetters() default false;

    /**
     * Generates a public constructor with each field initialized from a parameter and optionally
     * some user-defined state validation at the end.
     *
     * Uses field {@link Nullable nullability}/default value presence to determine optional
     * parameters.
     *
     * Requesting a {@link #genBuilder} suppresses public constructor generation by default.
     *
     * You receive a callback at the end of constructor call by declaring the method:
     * {@code void onConstructed()}
     * This is the place to put any custom validation logic.
     */
    boolean genConstructor() default true;

    /**
     * {@link #genConstructor} with @hide
     */
    boolean genHiddenConstructor() default false;

    /**
     * Generates a Builder for your class.
     *
     * Uses a package-private constructor under the hood, so same rules hold as for
     * {@link #genConstructor()}
     */
    boolean genBuilder() default false;

    /**
     * {@link #genBuilder} with @hide
     */
    boolean genHiddenBuilder() default false;

    /**
     * Generates a structural {@link Object#equals} + {@link Object#hashCode}.
     *
     * You can customize individual fields' logic by declaring methods like:
     * {@link boolean fieldNameEquals(ClassName otherInstance)}
     * {@link boolean fieldNameEquals(FieldType otherValue)}
     * {@link int fieldNameHashCode()}
     */
    boolean genEqualsHashCode() default false;

    /**
     * Generates a structural {@link Object#toString}.
     *
     * You can customize individual fields' logic by declaring methods like:
     * {@link String fieldNameToString()}
     */
    boolean genToString() default false;

    /**
     * Generates a utility method that takes a {@link PerObjectFieldAction per-field callback}
     * and calls it once for each field with its name and value.
     *
     * If some fields are of primitive types, and additional overload is generated that takes
     * multiple callbacks, specialized for used primitive types to avoid auto-boxing, e.g.
     * {@link PerIntFieldAction}.
     */
    boolean genForEachField() default false;

    /**
     * Generates a constructor that copies the given instance of the same class.
     */
    boolean genCopyConstructor() default false;

    /**
     * {@link #genCopyConstructor} with @hide
     */
    boolean genHiddenCopyConstructor() default false;

    /**
     * Generates constant annotations({@link IntDef}/{@link StringDef}) for any constant groups
     * with common prefix.
     * The annotation names are based on the common prefix.
     *
     * For int constants this additionally generates the corresponding static *ToString method and
     * uses it in {@link Object#toString}.
     *
     * Additionally, any fields you annotate with the generated constants will be automatically
     * validated in constructor.
     *
     * Int constants specified as hex(0x..) are considered to be flags, which is taken into account
     * for in their *ToString and validation.
     *
     * You can optionally override the name of the generated annotation by annotating each constant
     * with the desired annotation name.
     *
     * Unless suppressed, is implied by presence of constants with common prefix.
     */
    boolean genConstDefs() default true;

    /**
     * {@link #genConstDefs} with @hide
     */
    boolean genHiddenConstDefs() default false;


    /**
     * Allows specifying custom parcelling logic based on reusable
     * {@link Parcelling} implementations
     */
    @Retention(RetentionPolicy.SOURCE)
    @Target(FIELD)
    @interface ParcelWith {
        Class<? extends Parcelling> value();
    }

    /**
     * Allows specifying a singular name for a builder's plural field name e.g. 'name' for 'mNames'
     * Used for Builder's {@code addName(String name)} methods
     */
    @Retention(RetentionPolicy.SOURCE)
    @Target(FIELD)
    @interface PluralOf {
        String value();
    }

    /**
     * Marks that any annotations following it are applicable to each element of the
     * collection/array, as opposed to itself.
     */
    @Retention(RetentionPolicy.SOURCE)
    @Target({FIELD, METHOD, PARAMETER, LOCAL_VARIABLE})
    @interface Each {}

    /**
     * @deprecated to be used by code generator exclusively
     */
    @Deprecated
    @Retention(RetentionPolicy.SOURCE)
    @Target({METHOD})
    @interface Generated {
        long time();
        String codegenVersion();
        String sourceFile();
        String inputSignatures() default "";

        /**
         * @deprecated to be used by code generator exclusively
         */
        @Deprecated
        @Retention(RetentionPolicy.SOURCE)
        @Target({FIELD, METHOD, ANNOTATION_TYPE, CONSTRUCTOR, TYPE})
        @interface Member {}
    }

    /**
     * Opt out of generating {@link #genConstDefs IntDef/StringDef}s for annotated constant
     */
    @Retention(RetentionPolicy.SOURCE)
    @Target({FIELD})
    @interface SuppressConstDefsGeneration {}

    /**
     * A class-level annotation to suppress methods' generation by name
     */
    @Retention(RetentionPolicy.SOURCE)
    @Target({TYPE})
    @interface Suppress {
        String[] value();
    }

    /**
     * Mark that the field should have a {@link Nullable} argument for its setter.
     */
    @Retention(RetentionPolicy.SOURCE)
    @Target({FIELD})
    @interface MaySetToNull {}

    /**
     * Callback used by {@link #genForEachField}.
     *
     * @param <THIS> The enclosing data class instance.
     *              Can be used to try and avoid capturing values from outside of the lambda,
     *              minimizing allocations.
     */
    interface PerObjectFieldAction<THIS> {
        void acceptObject(THIS self, String fieldName, Object fieldValue);
    }

    /**
     * A specialization of {@link PerObjectFieldAction} called exclusively for int fields to avoid
     * boxing.
     */
    interface PerIntFieldAction<THIS> {
        void acceptInt(THIS self, String fieldName, int fieldValue);
    }
}