Question: importing a library as a package private

Question

importing a library as a package private

Answers 3
Added at 2016-12-28 09:12
Tags
Question

I know we can create a package private class in java. So the class is Internal and only accessible in the specified module:

class MyPackagePrivateClass
{
    ...
}

Now I am developing an android library which i named LibraryA and I wish to use an existing LibraryB in my own LibraryA. How is it possible to prevent user of LibraryA from using the LibraryB directly?

Is there any concept like package private library or anything like that?

Update (For those who ask 'why do I need this?')

In LibraryB there are methods like this:

public QueryBuilder select(String... columns);

But I strongly am of the view that we should use Type-Safe Enum pattern and prevent users from passing strings to these methods(Considering maintenance and refactoring issues). So I decided to wrap those 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 I provided (Column is a type safe enum here). But if they have access to the original method they may use those instead and I tend to forbid this.

Answers
nr: #1 dodano: 2016-12-28 09:12

Make a jar file out of library B and import it in library A.

http://stackoverflow.com/a/1051705/6897626

nr: #2 dodano: 2016-12-28 12:12

In Java, with polymorphism, you can't hide a public method of an extended class.

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

nr: #3 dodano: 2016-12-29 08:12

Jigsaw project tries to reach the same goal. But it can take long enough to wait until it would be ported to Android. Until then the best solution IMO is to use package private methods. User will be able to understand that he shouldn't use these methods.

Anyway it's impossible to ultimately forbid library user to do certain things, because one can use reflection or substitute class to get rid of nasty restrictions. But it's possible to encourage user not to do certain things - that's what can be done.

Source Show
◀ Wstecz