Scarass
Scarass

Reputation: 944

Is it safe to use classes annotated with @RestrictTo

I have found a good usage for RoomSQLiteQuery class, but it is annotated with @RestrictTo. And even thought IDE shows warning it compiles normally and it also looks it works normally.

@RestrictTo only serves to give a warning when we shouldn't use something, it cannot actually forbid you to use it, right? It will allways work in production?

So I just wanted to check that so that I'm sure it'll work.

No need to give advice "Don't use it, that's bad...", I'm just asking whether it's SAFE to use. (even thought you might ask me why do I need if you're really intersted :))

EDIT:

I have coppied the code that uses RoomSQLiteQuery (class annotated with @RestrictTo) from file that was generated by room. So if I weren't able to use than how can room use IT when generating code? Because room generated classes are in the same package.
Also isn't it true that only using access modifiers we can restrict the usage of some class/function/whaterever?

Upvotes: 3

Views: 1846

Answers (1)

hifromantal
hifromantal

Reputation: 1764

@RestrictTo

Denotes that the annotated element should only be accessed from within a specific scope (as defined by RestrictTo.Scope).

It is used to denote that a specific block of code is was written for a specific purpose, and it may have not been intended to be used outside of that scope.

This can mean many things without a context: the creators of the library may not want to take responsibility of the code being used for unintended purposes - especially when we are talking about a library backed by the creators of the OS.

As they documented on the source code, this class is used as an intermediary and

This class is used as an intermediate place to keep binding arguments so that we can run Cursor queries with correct types rather than passing everything as a string.

You can always copy the code (with proper attribution and aligning with the LICENSE!), but then you loose the support from the 3rd party library you copied the code from - as they never intended that to be used outside.

In short, it is generally recommended NOT to use @RestrictedTo classes, and if you do, be prepared to maintain it yourself.

Upvotes: 3

Related Questions