Android: Android Support "RestrictTo" Annotation

I understood android support androids in which I came across @RestrictTo annotation; This explains what a developer of different areas can define. Can anyone explain in detail with some example how to use these annotations?

Any findings would be appreciated!

+5


source to share


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


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







All Articles