Search code examples
jooq

Using full java object package paths inline in autogenerated code


We have a pretty comic situation: a Postgres DB with a schema called Internal:

public class Internal extends SchemaImpl

Now we have to create an enum, with one of the values being called... Internal. The autogenerated code for this enum doesn't compile due to the collision between the enum name and the schema name, returned by getSchema() method:

///
import blabla.jooq.internal.Internal;
///
@Generated(
        value = {
            "http://www.jooq.org",
            "jOOQ version:3.11.12"
        },
        comments = "This class is generated by jOOQ"
    )
    @SuppressWarnings({ "all", "unchecked", "rawtypes" })
    public enum TypeEnum implements EnumType {
    
        Internal("Internal"), External("External");
    /////
         /**
         * {@inheritDoc}
         */
        @Override
        public Schema getSchema() {
            return Internal.INTERNAL; //<<< The compiler thinks Internal is the enum value and not the schema name static object and fails 
        }

The two options to fix this are:

  1. Rename the enum (which for some reason we would like to avoid)
  2. Make the autogenerated code have the package names being included with the object references inline

Is there any configuration that will let us achieve option 2? TIA


Solution

  • Bug in the code generator

    That looks like a bug in jOOQ's code generator. I've created: https://github.com/jOOQ/jOOQ/issues/13692

    Work around by renaming generated objects

    You can work around such bugs by using a "generator strategy":

    With such a strategy, you can rename either the schema or enum class name, or both, of course, depending on your tastes:

    <configuration>
      <generator>
        <strategy>
          <matchers>
            <schemas>
              <schema>
                <expression>INTERNAL</expression>
                <schemaClass>
                  <transform>PASCAL</transform>
                  <expression>$0_SCHEMA</expression>
                </schemaClass>
              </schema>
            </schemas>
          
            <enums>
              <enum>
                <expression>INTERNAL</expression>
                <enumClass>
                  <transform>PASCAL</transform>
                  <expression>$0_ENUM</expression>
                </enumClass>
              </enum>
            </enums>
          </matchers>
        </strategy>
      </generator>
    </configuration>
    

    Work around by avoiding imports for certain types

    Alternatively, you can specify which type references should always remain fully qualified, rather than imported, see:

    For example:

    <configuration>
      <generator>
        <generate>
          <fullyQualifiedTypes>.*\.INTERNAL</fullyQualifiedTypes>
        </generate>
      </generator>
    </configuration>