Java’s bundle personal visibility is an underrated function. Whenever you omit any visibility modifier in Java, then the default (for many objects) is bundle personal, i.e. the item is seen solely to varieties in the identical bundle:
class YouDontSeeMe {}
class YouDontSeeMeEither {}
In actual fact, a compilation unit (the .java
file) can comprise a number of such lessons. You don’t should create a file per bundle personal sort. You can even put all of those varieties in your package-info.java
file, it doesn’t matter.
When utilizing jOOQ’s code generator, issues are generated as public
varieties per default, as you might be doubtless going to make use of this generated code all over the place. You’ll be able to nonetheless prohibit entry utilizing Java 9’s module
system if you’d like.
However often, even with jOOQ generated code, bundle personal visibility could be helpful, if some information entry bundle needs to cover its implementation particulars from different packages within the module.
Right here’s an instance code technology configuration to make this occur:
<configuration>
<generator>
<technique>
<identify>com.instance.codegen.SinglePackageStrategy</identify>
<!-- Generates all objects in the identical bundle -->
<!-- Ranging from jOOQ 3.19, you'll be able to declare the technique code right here
This may simplify your code technology 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 all over the place -->
<visibilityModifier>NONE</visibilityModifier>
</generate>
<goal>
<packageName>com.instance</packageName>
<!-- This can be vital if producing code in src/essential/java!
It is going to stop cleansing the opposite bundle listing contents.
Alternatively, use a separate goal <listing/>
-->
<clear>false</clear>
</goal>
</generator>
</configuration>
That wasn’t too arduous? Utilizing this strategy, you’ll be able to be sure that your jOOQ generated code by no means leaks into any shopper code that shouldn’t see jOOQ varieties.