Use dataclass codegen on a few initial framework classes

Test: presubmit
Change-Id: Ide3c21596f6489fdb1db8f72e1436a01b10f56ed
This commit is contained in:
Eugene Susla
2019-08-06 18:47:14 -07:00
parent c7ccc2cd5e
commit 3b2fe616fd
9 changed files with 396 additions and 130 deletions

View File

@@ -798,7 +798,10 @@ java_defaults {
"--multi-dex",
],
plugins: ["view-inspector-annotation-processor"],
plugins: [
"view-inspector-annotation-processor",
"staledataclass-annotation-processor",
],
}
filegroup {

View File

@@ -30,6 +30,8 @@ import android.util.ArrayMap;
import android.util.SparseIntArray;
import android.view.autofill.AutofillId;
import com.android.internal.util.DataClass;
import java.util.LinkedList;
/**
@@ -46,58 +48,36 @@ import java.util.LinkedList;
* to {@link FillResponse.Builder#setClientState(Bundle)} to avoid interpreting
* the UI state again while saving.
*/
@DataClass(
genHiddenConstructor = true,
genAidl = false)
public final class FillContext implements Parcelable {
/**
* The id of the {@link FillRequest fill request} this context
* corresponds to. This is useful to associate your custom client
* state with every request to avoid reinterpreting the UI when saving
* user data.
*/
private final int mRequestId;
/**
* The screen content.
*/
private final @NonNull AssistStructure mStructure;
/**
* The AutofillId of the view that triggered autofill.
*/
private final @NonNull AutofillId mFocusedId;
/**
* Lookup table AutofillId->ViewNode to speed up {@link #findViewNodesByAutofillIds}
* This is purely a cache and can be deleted at any time
*/
@Nullable private ArrayMap<AutofillId, AssistStructure.ViewNode> mViewNodeLookupTable;
private transient @Nullable ArrayMap<AutofillId, AssistStructure.ViewNode> mViewNodeLookupTable;
/** @hide */
public FillContext(int requestId, @NonNull AssistStructure structure,
@NonNull AutofillId autofillId) {
mRequestId = requestId;
mStructure = structure;
mFocusedId = autofillId;
}
private FillContext(Parcel parcel) {
this(parcel.readInt(), parcel.readParcelable(null), parcel.readParcelable(null));
}
/**
* Gets the id of the {@link FillRequest fill request} this context
* corresponds to. This is useful to associate your custom client
* state with every request to avoid reinterpreting the UI when saving
* user data.
*
* @return The request id.
*/
public int getRequestId() {
return mRequestId;
}
/**
* @return The screen content.
*/
@NonNull
public AssistStructure getStructure() {
return mStructure;
}
/**
* @return the AutofillId of the view that triggered autofill.
*/
@NonNull
public AutofillId getFocusedId() {
return mFocusedId;
}
@Override
public String toString() {
if (!sDebug) return super.toString();
@@ -105,18 +85,6 @@ public final class FillContext implements Parcelable {
return "FillContext [reqId=" + mRequestId + ", focusedId=" + mFocusedId + "]";
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeInt(mRequestId);
parcel.writeParcelable(mStructure, flags);
parcel.writeParcelable(mFocusedId, flags);
}
/**
* Finds {@link ViewNode ViewNodes} that have the requested ids.
*
@@ -190,18 +158,119 @@ public final class FillContext implements Parcelable {
return foundNodes;
}
public static final @android.annotation.NonNull Parcelable.Creator<FillContext> CREATOR =
new Parcelable.Creator<FillContext>() {
@Override
@NonNull
public FillContext createFromParcel(Parcel parcel) {
return new FillContext(parcel);
}
// Code below generated by codegen v1.0.0.
//
// DO NOT MODIFY!
//
// To regenerate run:
// $ codegen $ANDROID_BUILD_TOP/frameworks/base/core/java/android/service/autofill/FillContext.java
//
// CHECKSTYLE:OFF Generated code
/**
* Creates a new FillContext.
*
* @param requestId
* The id of the {@link FillRequest fill request} this context
* corresponds to. This is useful to associate your custom client
* state with every request to avoid reinterpreting the UI when saving
* user data.
* @param structure
* The screen content.
* @param focusedId
* The AutofillId of the view that triggered autofill.
* @hide
*/
@DataClass.Generated.Member
public FillContext(
int requestId,
@NonNull AssistStructure structure,
@NonNull AutofillId focusedId) {
this.mRequestId = requestId;
this.mStructure = structure;
com.android.internal.util.AnnotationValidations.validate(
NonNull.class, null, mStructure);
this.mFocusedId = focusedId;
com.android.internal.util.AnnotationValidations.validate(
NonNull.class, null, mFocusedId);
// onConstructed(); // You can define this method to get a callback
}
/**
* The id of the {@link FillRequest fill request} this context
* corresponds to. This is useful to associate your custom client
* state with every request to avoid reinterpreting the UI when saving
* user data.
*/
@DataClass.Generated.Member
public int getRequestId() {
return mRequestId;
}
/**
* The screen content.
*/
@DataClass.Generated.Member
public @NonNull AssistStructure getStructure() {
return mStructure;
}
/**
* The AutofillId of the view that triggered autofill.
*/
@DataClass.Generated.Member
public @NonNull AutofillId getFocusedId() {
return mFocusedId;
}
@Override
@DataClass.Generated.Member
public void writeToParcel(Parcel dest, int flags) {
// You can override field parcelling by defining methods like:
// void parcelFieldName(Parcel dest, int flags) { ... }
dest.writeInt(mRequestId);
dest.writeTypedObject(mStructure, flags);
dest.writeTypedObject(mFocusedId, flags);
}
@Override
@DataClass.Generated.Member
public int describeContents() { return 0; }
@DataClass.Generated.Member
public static final @NonNull Parcelable.Creator<FillContext> CREATOR
= new Parcelable.Creator<FillContext>() {
@Override
@NonNull
public FillContext[] newArray(int size) {
return new FillContext[size];
}
@Override
@SuppressWarnings({"unchecked", "RedundantCast"})
public FillContext createFromParcel(Parcel in) {
// You can override field unparcelling by defining methods like:
// static FieldType unparcelFieldName(Parcel in) { ... }
int requestId = in.readInt();
AssistStructure structure = (AssistStructure) in.readTypedObject(AssistStructure.CREATOR);
AutofillId focusedId = (AutofillId) in.readTypedObject(AutofillId.CREATOR);
return new FillContext(
requestId,
structure,
focusedId);
}
};
@DataClass.Generated(
time = 1565152135263L,
codegenVersion = "1.0.0",
sourceFile = "frameworks/base/core/java/android/service/autofill/FillContext.java",
inputSignatures = "private final int mRequestId\nprivate final @android.annotation.NonNull android.app.assist.AssistStructure mStructure\nprivate final @android.annotation.NonNull android.view.autofill.AutofillId mFocusedId\nprivate transient @android.annotation.Nullable android.util.ArrayMap<android.view.autofill.AutofillId,android.app.assist.AssistStructure.ViewNode> mViewNodeLookupTable\npublic @java.lang.Override java.lang.String toString()\npublic @android.annotation.NonNull android.app.assist.AssistStructure.ViewNode[] findViewNodesByAutofillIds(android.view.autofill.AutofillId[])\nclass FillContext extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genHiddenConstructor=true, genAidl=false)")
@Deprecated
private void __metadata() {}
}

View File

@@ -24,6 +24,7 @@ import android.os.Parcel;
import android.os.Parcelable;
import android.view.View;
import com.android.internal.util.DataClass;
import com.android.internal.util.Preconditions;
import java.lang.annotation.Retention;
@@ -39,6 +40,10 @@ import java.util.List;
*
* @see AutofillService#onFillRequest(FillRequest, android.os.CancellationSignal, FillCallback)
*/
@DataClass(
genToString = true,
genHiddenConstructor = true,
genHiddenConstDefs = true)
public final class FillRequest implements Parcelable {
/**
@@ -63,54 +68,45 @@ public final class FillRequest implements Parcelable {
* is called. For example, standard {@link android.widget.TextView} views show an
* {@code AUTOFILL} option in the overflow menu that triggers such request.
*/
public static final int FLAG_MANUAL_REQUEST = 0x1;
public static final @RequestFlags int FLAG_MANUAL_REQUEST = 0x1;
/**
* Indicates this request was made using
* <a href="AutofillService.html#CompatibilityMode">compatibility mode</a>.
*/
public static final int FLAG_COMPATIBILITY_MODE_REQUEST = 0x2;
public static final @RequestFlags int FLAG_COMPATIBILITY_MODE_REQUEST = 0x2;
/** @hide */
public static final int INVALID_REQUEST_ID = Integer.MIN_VALUE;
/** @hide */
@IntDef(flag = true, prefix = { "FLAG_" }, value = {
FLAG_MANUAL_REQUEST, FLAG_COMPATIBILITY_MODE_REQUEST
})
@Retention(RetentionPolicy.SOURCE)
@interface RequestFlags{}
private final int mId;
private final @RequestFlags int mFlags;
private final @NonNull ArrayList<FillContext> mContexts;
private final @Nullable Bundle mClientState;
private FillRequest(@NonNull Parcel parcel) {
mId = parcel.readInt();
mContexts = new ArrayList<>();
parcel.readParcelableList(mContexts, null);
mClientState = parcel.readBundle();
mFlags = parcel.readInt();
}
/** @hide */
public FillRequest(int id, @NonNull ArrayList<FillContext> contexts,
@Nullable Bundle clientState, @RequestFlags int flags) {
mId = id;
mFlags = Preconditions.checkFlagsArgument(flags,
FLAG_MANUAL_REQUEST | FLAG_COMPATIBILITY_MODE_REQUEST);
mContexts = Preconditions.checkCollectionElementsNotNull(contexts, "contexts");
mClientState = clientState;
}
/**
* Gets the unique id of this request.
*/
public int getId() {
return mId;
}
private final int mId;
/**
* Gets the contexts associated with each previous fill request.
*
* <p><b>Note:</b> Starting on Android {@link android.os.Build.VERSION_CODES#Q}, it could also
* include contexts from requests whose {@link SaveInfo} had the
* {@link SaveInfo#FLAG_DELAY_SAVE} flag.
*/
private final @NonNull List<FillContext> mFillContexts;
/**
* Gets the latest client state bundle set by the service in a
* {@link FillResponse.Builder#setClientState(Bundle) fill response}.
*
* <p><b>Note:</b> Prior to Android {@link android.os.Build.VERSION_CODES#P}, only client state
* bundles set by {@link FillResponse.Builder#setClientState(Bundle)} were considered. On
* Android {@link android.os.Build.VERSION_CODES#P} and higher, bundles set in the result of
* an authenticated request through the
* {@link android.view.autofill.AutofillManager#EXTRA_CLIENT_STATE} extra are
* also considered (and take precedence when set).
*
* @return The client state.
*/
private final @Nullable Bundle mClientState;
/**
* Gets the flags associated with this request.
@@ -118,8 +114,105 @@ public final class FillRequest implements Parcelable {
* @return any combination of {@link #FLAG_MANUAL_REQUEST} and
* {@link #FLAG_COMPATIBILITY_MODE_REQUEST}.
*/
public @RequestFlags int getFlags() {
return mFlags;
private final @RequestFlags int mFlags;
private void onConstructed() {
Preconditions.checkCollectionElementsNotNull(mFillContexts, "contexts");
}
// Code below generated by codegen v1.0.0.
//
// DO NOT MODIFY!
//
// To regenerate run:
// $ codegen $ANDROID_BUILD_TOP/frameworks/base/core/java/android/service/autofill/FillRequest.java
//
// CHECKSTYLE:OFF Generated code
/** @hide */
@IntDef(flag = true, prefix = "FLAG_", value = {
FLAG_MANUAL_REQUEST,
FLAG_COMPATIBILITY_MODE_REQUEST
})
@Retention(RetentionPolicy.SOURCE)
@DataClass.Generated.Member
public @interface RequestFlags {}
/** @hide */
@DataClass.Generated.Member
public static String requestFlagsToString(@RequestFlags int value) {
return com.android.internal.util.BitUtils.flagsToString(
value, FillRequest::singleRequestFlagsToString);
}
@DataClass.Generated.Member
static String singleRequestFlagsToString(@RequestFlags int value) {
switch (value) {
case FLAG_MANUAL_REQUEST:
return "FLAG_MANUAL_REQUEST";
case FLAG_COMPATIBILITY_MODE_REQUEST:
return "FLAG_COMPATIBILITY_MODE_REQUEST";
default: return Integer.toHexString(value);
}
}
/**
* Creates a new FillRequest.
*
* @param id
* Gets the unique id of this request.
* @param fillContexts
* Gets the contexts associated with each previous fill request.
*
* <p><b>Note:</b> Starting on Android {@link android.os.Build.VERSION_CODES#Q}, it could also
* include contexts from requests whose {@link SaveInfo} had the
* {@link SaveInfo#FLAG_DELAY_SAVE} flag.
* @param clientState
* Gets the latest client state bundle set by the service in a
* {@link FillResponse.Builder#setClientState(Bundle) fill response}.
*
* <p><b>Note:</b> Prior to Android {@link android.os.Build.VERSION_CODES#P}, only client state
* bundles set by {@link FillResponse.Builder#setClientState(Bundle)} were considered. On
* Android {@link android.os.Build.VERSION_CODES#P} and higher, bundles set in the result of
* an authenticated request through the
* {@link android.view.autofill.AutofillManager#EXTRA_CLIENT_STATE} extra are
* also considered (and take precedence when set).
* @param flags
* Gets the flags associated with this request.
*
* @return any combination of {@link #FLAG_MANUAL_REQUEST} and
* {@link #FLAG_COMPATIBILITY_MODE_REQUEST}.
* @hide
*/
@DataClass.Generated.Member
public FillRequest(
int id,
@NonNull List<FillContext> fillContexts,
@Nullable Bundle clientState,
@RequestFlags int flags) {
this.mId = id;
this.mFillContexts = fillContexts;
com.android.internal.util.AnnotationValidations.validate(
NonNull.class, null, mFillContexts);
this.mClientState = clientState;
this.mFlags = flags;
Preconditions.checkFlagsArgument(
mFlags,
FLAG_MANUAL_REQUEST
| FLAG_COMPATIBILITY_MODE_REQUEST);
onConstructed();
}
/**
* Gets the unique id of this request.
*/
@DataClass.Generated.Member
public int getId() {
return mId;
}
/**
@@ -129,13 +222,9 @@ public final class FillRequest implements Parcelable {
* include contexts from requests whose {@link SaveInfo} had the
* {@link SaveInfo#FLAG_DELAY_SAVE} flag.
*/
@DataClass.Generated.Member
public @NonNull List<FillContext> getFillContexts() {
return mContexts;
}
@Override
public String toString() {
return "FillRequest: [id=" + mId + ", flags=" + mFlags + ", ctxts= " + mContexts + "]";
return mFillContexts;
}
/**
@@ -151,33 +240,89 @@ public final class FillRequest implements Parcelable {
*
* @return The client state.
*/
@DataClass.Generated.Member
public @Nullable Bundle getClientState() {
return mClientState;
}
@Override
public int describeContents() {
return 0;
/**
* Gets the flags associated with this request.
*
* @return any combination of {@link #FLAG_MANUAL_REQUEST} and
* {@link #FLAG_COMPATIBILITY_MODE_REQUEST}.
*/
@DataClass.Generated.Member
public @RequestFlags int getFlags() {
return mFlags;
}
@Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeInt(mId);
parcel.writeParcelableList(mContexts, flags);
parcel.writeBundle(mClientState);
parcel.writeInt(mFlags);
@DataClass.Generated.Member
public String toString() {
// You can override field toString logic by defining methods like:
// String fieldNameToString() { ... }
return "FillRequest { " +
"id = " + mId + ", " +
"fillContexts = " + mFillContexts + ", " +
"clientState = " + mClientState + ", " +
"flags = " + requestFlagsToString(mFlags) +
" }";
}
public static final @android.annotation.NonNull Parcelable.Creator<FillRequest> CREATOR =
new Parcelable.Creator<FillRequest>() {
@Override
public FillRequest createFromParcel(Parcel parcel) {
return new FillRequest(parcel);
}
@Override
@DataClass.Generated.Member
public void writeToParcel(Parcel dest, int flags) {
// You can override field parcelling by defining methods like:
// void parcelFieldName(Parcel dest, int flags) { ... }
byte flg = 0;
if (mClientState != null) flg |= 0x4;
dest.writeByte(flg);
dest.writeInt(mId);
dest.writeParcelableList(mFillContexts, flags);
if (mClientState != null) dest.writeBundle(mClientState);
dest.writeInt(mFlags);
}
@Override
@DataClass.Generated.Member
public int describeContents() { return 0; }
@DataClass.Generated.Member
public static final @NonNull Parcelable.Creator<FillRequest> CREATOR
= new Parcelable.Creator<FillRequest>() {
@Override
public FillRequest[] newArray(int size) {
return new FillRequest[size];
}
@Override
@SuppressWarnings({"unchecked", "RedundantCast"})
public FillRequest createFromParcel(Parcel in) {
// You can override field unparcelling by defining methods like:
// static FieldType unparcelFieldName(Parcel in) { ... }
byte flg = in.readByte();
int id = in.readInt();
List<FillContext> fillContexts = new ArrayList<>();
in.readParcelableList(fillContexts, FillContext.class.getClassLoader());
Bundle clientState = (flg & 0x4) == 0 ? null : in.readBundle();
int flags = in.readInt();
return new FillRequest(
id,
fillContexts,
clientState,
flags);
}
};
@DataClass.Generated(
time = 1565152134349L,
codegenVersion = "1.0.0",
sourceFile = "frameworks/base/core/java/android/service/autofill/FillRequest.java",
inputSignatures = "public static final @android.service.autofill.FillRequest.RequestFlags int FLAG_MANUAL_REQUEST\npublic static final @android.service.autofill.FillRequest.RequestFlags int FLAG_COMPATIBILITY_MODE_REQUEST\npublic static final int INVALID_REQUEST_ID\nprivate final int mId\nprivate final @android.annotation.NonNull java.util.List<android.service.autofill.FillContext> mFillContexts\nprivate final @android.annotation.Nullable android.os.Bundle mClientState\nprivate final @android.service.autofill.FillRequest.RequestFlags int mFlags\nprivate void onConstructed()\nclass FillRequest extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genToString=true, genHiddenConstructor=true, genHiddenConstDefs=true)")
@Deprecated
private void __metadata() {}
}

View File

@@ -64,11 +64,21 @@ public @interface DataClass {
*/
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.
@@ -84,6 +94,11 @@ public @interface DataClass {
*/
boolean genConstructor() default true;
/**
* {@link #genConstructor} with @hide
*/
boolean genHiddenConstructor() default false;
/**
* Generates a Builder for your class.
*
@@ -92,6 +107,11 @@ public @interface DataClass {
*/
boolean genBuilder() default false;
/**
* {@link #genBuilder} with @hide
*/
boolean genHiddenBuilder() default false;
/**
* Generates a structural {@link Object#equals} + {@link Object#hashCode}.
*
@@ -125,6 +145,11 @@ public @interface DataClass {
*/
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.
@@ -146,6 +171,11 @@ public @interface DataClass {
*/
boolean genConstDefs() default true;
/**
* {@link #genConstDefs} with @hide
*/
boolean genHiddenConstDefs() default false;
/**
* Allows specifying custom parcelling logic based on reusable

View File

@@ -122,9 +122,13 @@ class ClassPrinter(
if (cliArgs.contains("--$kebabCase")) return true
val annotationKey = "gen$upperCamelCase"
val annotationHiddenKey = "genHidden$upperCamelCase"
if (dataClassAnnotationFeatures.containsKey(annotationKey)) {
return dataClassAnnotationFeatures[annotationKey]!!
}
if (dataClassAnnotationFeatures.containsKey(annotationHiddenKey)) {
return dataClassAnnotationFeatures[annotationHiddenKey]!!
}
if (cliArgs.contains("--all")) return true
if (hidden) return true
@@ -144,11 +148,17 @@ class ClassPrinter(
}
}
val FeatureFlag.hidden
get(): Boolean = when {
cliArgs.contains("--hidden-$kebabCase") -> true
this == FeatureFlag.BUILD_UPON -> FeatureFlag.BUILDER.hidden
else -> false
val FeatureFlag.hidden: Boolean
get(): Boolean {
val annotationHiddenKey = "genHidden$upperCamelCase"
if (dataClassAnnotationFeatures.containsKey(annotationHiddenKey)) {
return dataClassAnnotationFeatures[annotationHiddenKey]!!
}
return when {
cliArgs.contains("--hidden-$kebabCase") -> true
this == FeatureFlag.BUILD_UPON -> FeatureFlag.BUILDER.hidden
else -> false
}
}
var currentIndent = INDENT_SINGLE

View File

@@ -85,7 +85,6 @@ data class FieldInfo(
classInfo.classAst.methods.find {
it.nameAsString == "default$NameUpperCamel" && it.parameters.isEmpty()
}?.run { return "$nameAsString()" }
if (FieldClass == "List") return "${classPrinter.memberRef("java.util.Collections.emptyList")}()"
return null
}
val hasDefault get() = defaultExpr != null

View File

@@ -68,12 +68,15 @@ fun ClassPrinter.generateConstDef(consts: List<Pair<VariableDeclarator, FieldDec
}
}
val visibility = if (consts[0].second.isPublic) "public" else "/* package-*/"
val visibility = if (consts[0].second.isPublic) "public" else "/* package-private */"
val Retention = classRef("java.lang.annotation.Retention")
val RetentionPolicySource = memberRef("java.lang.annotation.RetentionPolicy.SOURCE")
val ConstDef = classRef("android.annotation.${type.capitalize()}Def")
if (FeatureFlag.CONST_DEFS.hidden) {
+"/** @hide */"
}
"@$ConstDef(${if_(flag, "flag = true, ")}prefix = \"${prefix}_\", value = {" {
names.forEachLastAware { name, isLast ->
+"$name${if_(!isLast, ",")}"
@@ -85,6 +88,9 @@ fun ClassPrinter.generateConstDef(consts: List<Pair<VariableDeclarator, FieldDec
+""
if (type == "int") {
if (FeatureFlag.CONST_DEFS.hidden) {
+"/** @hide */"
}
+GENERATED_MEMBER_HEADER
val methodDefLine = "$visibility static String ${AnnotationName.decapitalize()}ToString(" +
"@$AnnotationName int value)"

View File

@@ -97,7 +97,7 @@ private fun ClassPrinter.getFullClassName(type: Type): String {
getFullClassName(buildString {
type.scope.ifPresent { append(it).append(".") }
append(type.nameAsString)
}) + (type.typeArguments.orElse(null)?.let { args -> args.joinToString(", ") {getFullClassName(it)}}?.let { "<$it>" } ?: "")
}) + (type.typeArguments.orElse(null)?.let { args -> args.joinToString(",") {getFullClassName(it)}}?.let { "<$it>" } ?: "")
} else getFullClassName(type.asString())
}

View File

@@ -66,10 +66,14 @@ class StaleDataclassProcessor: AbstractProcessor() {
if (dataClassAnnotation == null) {
dataClassAnnotation = annotations.find {
it.qualifiedName.toString() == DATACLASS_ANNOTATION_NAME
}
} ?: return true
}
val generatedAnnotatedElements = roundEnv.getElementsAnnotatedWith(generatedAnnotation)
val generatedAnnotatedElements = if (generatedAnnotation != null) {
roundEnv.getElementsAnnotatedWith(generatedAnnotation)
} else {
emptySet()
}
generatedAnnotatedElements.forEach {
processSingleFile(it)
}