Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java package politics

Tags:

java

package

I always doubt when creating packages, I want to take advantage of the package limited access but at the same time I want to have similar classes divided into packages. The problem comes when you understand that packages are not hierarchical in Java:

At first, packages appear to be hierarchical, but they are not. source

Imagine I have an API defined with its classes at foo.bar, only the classes the API client needs are set public. Then I have another package with some internal objects I need in the API defined at foo.bar.pojos, this classes need to be public so they can be accessed by foo.bar but this means the API client could also access them if the package foo.bar.pojos is imported.

What is the common package politic that should be followed?

like image 896
eliocs Avatar asked Jun 30 '11 09:06

eliocs


People also ask

What is Java packages and its significance?

A package in Java is used to group related classes. Think of it as a folder in a file directory. We use packages to avoid name conflicts, and to write a better maintainable code. Packages are divided into two categories: Built-in Packages (packages from the Java API)

What is the use of util package in Java?

Provides the classes necessary to create an applet and the classes an applet uses to communicate with its applet context. Contains all of the classes for creating user interfaces and for painting graphics and images. Provides interfaces and classes for transferring data between and within applications.

What are the classes in util package in Java?

util Description. Contains the collections framework, legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes (a string tokenizer, a random-number generator, and a bit array).


2 Answers

I've seen two ways of doing.

The first one consists in separating the public API and internal classes into two different artefacts (jars). The documentation is separated as well, and it's thus easy for the end user to make the distinction between what is internal and what is not. But it sometimes make things more complex to have two jars, two source trees, etc.

The second one consists in delivering a single jar, but have a good documentation allowing to know what's internal and what's not. The textual documentation can explain how to use the API (and thus avoids talking about the internals). And the javadoc can specify that a class is for internal use and is thus subject to changes.

like image 131
JB Nizet Avatar answered Sep 23 '22 10:09

JB Nizet


Yes, Java packages don't give you enough control over your dependencies. The classic way to deal with this is to put external APIs in one package and internal implementation classes in another, and rely on people's good sense to avoid creating dependencies on the latter.

With Maven and OSGI, you have an additional mechanism for managing dependencies between modules / bundles of packages. In the case of OSGI, you can explicitly declare some packages as not exported, and an OSGI aware development environment will prevent people creating harmful dependencies. Maven's module support is weaker, but at least it controls dependency cycles.

Finally, you could use custom PMD rules to enforce your project's modularization conventions ... in the same way that there are rules to discourage dependencies on Java's "com.sun.*" package tree.

like image 45
Stephen C Avatar answered Sep 21 '22 10:09

Stephen C