内推
【长期有效】欢迎加入字节跳动我的团队:内推链接
@CallSuper
必须调用父类的此方法,即super.xxx()
例如你写一些公共超类,例如BaseActivity时,你希望子类必须调用BaseActivity.onCreate()方法(例如你要在此做一些统计),你可以在BaseActivity.onCreate()上加上此注解。
@CheckResult
此方法的返回值必须得到处理。
例如:
@CheckResult
public boolean isValidStr(String srt) {
return TextUtils.isEmpty(str);
}
@Dimension
@Documented
@Retention(CLASS)
@Target({METHOD,PARAMETER,FIELD,LOCAL_VARIABLE,ANNOTATION_TYPE})
public @interface Dimension {
@DimensionUnit
int unit() default PX;
int DP = 0;
int PX = 1;
int SP = 2;
}
标志此值是Dimension,且可以指定单位。
如:
@Dimension(unit = Dimension.DP)
public int mCenterX; //自定义view中的圆心位置
@Px
public int mCenterY; //对于@Dimension(unit = Dimension.PX)的定义
@FloatRange、@IntRange
定义浮点型/整形数据范围
如:
@FloatRange(from = 0, to = 100)
public float mCenterX;
@GuardBy
@Target({ ElementType.FIELD, ElementType.METHOD })
@Retention(RetentionPolicy.CLASS)
public @interface GuardedBy {
String value();
}
此对象所使用的对象锁
如:
final Object objectLock = new Object();
{@literal @}GuardedBy("objectLock")
volatile Object object;
Object getObject() {
synchronized (objectLock) {
if (object == null) {
object = new Object();
}
}
return object;
}
@IntDef、@StringDef
@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;
}
限定Int、String常量的可取值
如:
@Retention(SOURCE)
@IntDef({NAVIGATION_MODE_STANDARD, NAVIGATION_MODE_LIST, NAVIGATION_MODE_TABS})
public @interface NavigationMode {}
public static final int NAVIGATION_MODE_STANDARD = 0;
public static final int NAVIGATION_MODE_LIST = 1;
public static final int NAVIGATION_MODE_TABS = 2;
...
public abstract void setNavigationMode(@NavigationMode int mode);
@NavigationMode
public abstract int getNavigationMode();
@MainThread、@UiThread、@WorkThread
此方法/构造器/类型只允许在主/UI/work线程操作
@RestrictTo
@Retention(CLASS)
@Target({ANNOTATION_TYPE,TYPE,METHOD,CONSTRUCTOR,FIELD,PACKAGE})
public @interface RestrictTo {
/**
* The scope to which usage should be restricted.
*/
Scope[] value();
enum Scope {
/**
* Restrict usage to code within the same library (e.g. the same
* gradle group ID and artifact ID).
*/
LIBRARY,
/**
* Restrict usage to code within the same group of libraries.
* This corresponds to the gradle group ID.
*/
LIBRARY_GROUP,
/**
* Restrict usage to code within the same group ID (based on gradle
* group ID). This is an alias for {@link #LIBRARY_GROUP}.
*
* @deprecated Use {@link #LIBRARY_GROUP} instead
*/
@Deprecated
GROUP_ID,
/**
* Restrict usage to tests.
*/
TESTS,
/**
* Restrict usage to subclasses of the enclosing class.
* <p>
* <strong>Note:</strong> This scope should not be used to annotate
* packages.
*/
SUBCLASSES,
}
}
这个类/方法等可以用于的范围,在public、private限定发方式不够用的情况下可以使用,
如:
@RestrictTo(RestrictTo.Scope.LIBRARY)
public void test() {
}
这个方法可能要用于library中的外部类,所以是public的。加上RestrictTo.Scope.LIBRARY只允许在library中调用,这样可以避免引入这个库的上层的误调用。