Android: Android Support "RestrictTo" Annotation
2 answers
Used for metaprogram access modifiers. Java will allow access to any method public
from anywhere, and @RestrictTo
refers to RestrictTo.Scope
extends access restrictions to other scopes, not known by Java itself.
GROUP_ID
LIBRARY
LIBRARY_GROUP
SUBCLASSES
TESTS
Where, for example, SUBCLASSES
will act like protected
being accessible from anywhere if the developer wants to.
Basically, you can treat it as suggestions and not any coercive forced compilers.
+2
source to share
RestrictTo annotation is used to restrict the scope of the variable to which it is annotated. Few scopes that are listed in the RestrictTo annotation are LIBRARY, LIBRARY_GROUP, TESTS, SUBCLASSES. When the variable is annotated with the restrictTo annotation, variable attribute won't be listed as a suggestion in android studio.
For example, if I annotate the variable in the getter
@RestrictTo(RestrictTo.Scope.LIBRARY)
public @Nullable StudentInfo getInfo() {
return mStudentInfo;
}
class StudentInfo {
private String mAddress
@RestrictTo(RestrictTo.Scope.LIBRARY)
StudentInfo(String address) {
mAddress = address
}
public String getAddress() {
return mAddress
}
}
In the above example since StudentInfo is restricted with the scope of LIBRARY, getAddress of the studentInfo won't be listed as suggestion in IDE when called from outside the scope of library.
-1
source to share