gpt4 book ai didi

android - 如何测试 IntDef 定义中是否包含 int

转载 作者:太空狗 更新时间:2023-10-29 13:59:56 25 4
gpt4 key购买 nike

以下面的例子为例。

错误信息

Must be one of: DownloadRequest.STATUS_UNKNOWN, DownloadRequest.STATUS_DOWNLOADING, DownloadRequest.STATUS_COMPLETE, DownloadRequest.STATUS_PAUSED, DownloadRequest.STATUS_CANCELLED, DownloadRequest.STATUS_TEMP_PAUSED, DownloadRequest.STATUS_QUEUED

查询数据库

使用 Cursor 从数据库中提取数据并将其放入我的自定义类中。

request.setStatus(cursor.getInt(COLUMN_STATUS));

设置自定义生成器

这个自定义类方法接受一个整数并设置它的内部状态。整数由 IntDef 验证注释。

@NonNull
public Builder setStatus(@StatusEnum int status) {
mInstance.mStatus = status;
return this;
}

IntDef 注释

@IntDef({STATUS_UNKNOWN, STATUS_DOWNLOADING, STATUS_COMPLETE, STATUS_PAUSED, STATUS_CANCELLED, STATUS_TEMP_PAUSED, STATUS_QUEUED})
@Retention(RetentionPolicy.SOURCE)
public @interface StatusEnum {}

问题

进入数据库的唯一值是由 StatusEnum 注释给出的值。这在所有步骤中都得到验证。我仍然会验证来自数据库的信息。

如何测试 IntDef 定义中是否存在整数。我可以测试给定的整数是常量之一。然而,这将增加 future 的代码维护。

最佳答案

不幸的是,您无法查看 @IntDef 的值,因为正如您在源代码中看到的那样 [1]注释的:

@Retention(SOURCE)
@Target({ANNOTATION_TYPE})
public @interface IntDef {
/** Defines the allowed constants for this element */
long[] value() default {};

/** Defines whether the constants can be used as a flag, or just as an enum (the default) */
boolean flag() default false;
}

标记为SOURCE作为 RetentionPolicy:

SOURCE
Annotations are to be discarded by the compiler.

因此,您无法在运行时进行迭代。

关于android - 如何测试 IntDef 定义中是否包含 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36577760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com