Java’s package deal non-public visibility is an underrated function. Whenever you omit any visibility modifier in Java, then the default (for many objects) is package deal non-public, i.e. the item is seen solely to sorts in the identical package deal:
class YouDontSeeMe {}
class YouDontSeeMeEither {}
In actual fact, a compilation unit (the .java
file) can include a number of such courses. You don’t must create a file per package deal non-public sort. You can even put all of those sorts in your package-info.java
file, it doesn’t matter.
When utilizing jOOQ’s code generator, issues are generated as public
sorts per default, as you’re doubtless going to make use of this generated code in every single place. You’ll be able to nonetheless limit entry utilizing Java 9’s module
system if you would like.
However often, even with jOOQ generated code, package deal non-public visibility could be helpful, if some knowledge entry package deal needs to cover its implementation particulars from different packages within the module.
Right here’s an instance code era configuration to make this occur:
<configuration>
<generator>
<technique>
<identify>com.instance.codegen.SinglePackageStrategy</identify>
<!-- Generates all objects in the identical package deal -->
<!-- Ranging from jOOQ 3.19, you'll be able to declare the technique code right here
This can simplify your code era setup. In older
variations, simply put the category in an auxiliary construct module and
add it as a dependency.
-->
<java><![CDATA[package com.example.codegen;
import org.jooq.codegen.DefaultGeneratorStrategy;
import org.jooq.codegen.GeneratorStrategy.Mode;
import org.jooq.meta.Definition;
public class SinglePackageStrategy extends DefaultGeneratorStrategy {
@Override
public String getJavaPackageName(Definition definition, Mode mode) {
return getTargetPackage();
}
}]]></java>
</technique>
<generate>
<!-- Removes the "public" visibility modifier in every single place -->
<visibilityModifier>NONE</visibilityModifier>
</generate>
<goal>
<packageName>com.instance</packageName>
<!-- This can be necessary if producing code in src/most important/java!
It can forestall cleansing the opposite package deal listing contents.
Alternatively, use a separate goal <listing/>
-->
<clear>false</clear>
</goal>
</generator>
</configuration>
That wasn’t too arduous? Utilizing this method, you’ll be able to make sure that your jOOQ generated code by no means leaks into any shopper code that shouldn’t see jOOQ sorts.