Knarf
Knarf

Reputation: 2156

Checkstyle to check that a certain class is only used inside a certain package or subpackage

We have a utility class : JsonRequestBuilder

I only want that this class is used inside "datamanager" packages (or subpackages), and not inside ui code and so on.

So JsonRequestBuilder may be used in packages like

be.knarf.datamanager
be.knarf.datamanager.special it is also ok

But not in packages like

be.knarf.screens

Anyone knows how to do this ? Can this be done with one of the regex checks ?

Upvotes: 2

Views: 410

Answers (2)

barfuin
barfuin

Reputation: 17494

With Checkstyle, this is commonly achieved using the ImportControl check. It basically lets you specify which imports are allowed in which packages, and checks that on your codebase.

One caveat is that it will not find fully qualified references to classes in the code, it only checks imports. But that is still quite helpful.

Upvotes: 1

Viktor Mellgren
Viktor Mellgren

Reputation: 4506

Sorry for not answering with checkstyle, but can't you put the JsonRequestBuilder in the be.knarf.datamanager package and remove the public modifier from the class, that way it should be package private.

Upvotes: 1

Related Questions