Importing a library as a private package

I know that we can create a batch class in java. Thus, the class is internal and is available only in the specified module:

class MyPackagePrivateClass { ... } 

Now I am developing an Android library, which I named LibraryA , and I want to use the existing LibraryB in my own LibraryA . How can I prevent LibraryA user LibraryA using LibraryA directly?

Is there any concept, like a packaged private library or something like that?

Update (for those who ask "why do I need this?")

LibraryB has the LibraryB methods:

 public QueryBuilder select(String... columns); 

But I am convinced that we should use the Type-Safe Enum pattern and prevent users from passing strings to these methods (given the problems of maintenance and refactoring). So I decided to wrap these methods in LibraryA :

 public TypedQueryBuilder select(Column... columns) { queryBuilder = queryBuilder.select(toString(columns)); return this; } 

Therefore, users of my library should use the Typed methods you provided ( Column is a safe type enum here). But if they have access to the original method, they can use them instead, and I tend to prohibit it.

+5
source share
3 answers

In Java with polymorphism, you cannot hide a public method of an extended class.

I think that you could archive your goal with the Facade Pattern : hide all the complex logic and in this case control access and, if necessary, implements some interfaces.

+1
source

Jigsaw project is trying to achieve the same goal. But it can take a long time to wait for it to be ported to Android. Until then, the best IMO solution is to use private package methods. The user will be able to understand that he should not use these methods.

In any case, it is impossible to ultimately prevent the library user from doing certain things, because you can use the reflection or replacement class to get rid of unpleasant restrictions. But this can cause the user not to do certain things - what can be done.

0
source

Make a jar file from library B and import it into library A.

fooobar.com/questions/16098 / ...

-1
source

Source: https://habr.com/ru/post/1261975/


All Articles