Java’s package private visibility is an underrated feature. When you omit any visibility modifier in Java, then the default (for most objects) is package private, i.e. the object is visible only to types in the same package:

class YouDontSeeMe {}
class YouDontSeeMeEither {}

In fact, a compilation unit (the .java file) can contain multiple such classes. You don’t have to create a file per package private type. You could even put all of these types in your package-info.java file, it doesn’t matter.

When using jOOQ’s code generator, things are generated as public types per default, as you are likely going to use this generated code everywhere. You can still restrict access using Java 9’s module system if you want.

But occasionally, even with jOOQ generated code, package private visibility can be useful, if some data access package wants to hide its implementation details from other packages in the module.

Here’s an example code generation configuration to make this happen:


  
    
      com.example.codegen.SinglePackageStrategy

      
      
      
    

    

      
      NONE
    

    
      com.example

      
      false
    
  

That wasn’t too hard? Using this approach, you can ensure that your jOOQ generated code never leaks into any client code that shouldn’t see jOOQ types.



Credit goes to the respective owner!

Leave a Reply

Your email address will not be published. Required fields are marked *