Creating a custom generator for builder classes

49 views
Skip to first unread message

Adam Zell

unread,
Oct 22, 2019, 2:46:09 AM10/22/19
to jOOQ User Group
I have been playing around with immutable POJO support in org.jooq.codegen.DefaultGenerator.  This has been working well, but can be unwieldy when the POJO has more than 4-5 fields.  From https://www.jooq.org/doc/3.12/manual/code-generation/codegen-pojos/ :

immutablePojos: Immutable POJOs have final members and no setters. All members must be passed to the constructor

As Java lacks named parameters, calling the POJO constructor with many arguments can be confusing.  Therefore I thought that something like https://immutables.github.io/factory.html#pojo-constructors would be a nice fix.  The quickest implementation appeared to be overriding JavaGenerator::generatePojoMultiConstructor and inserting the necessary annotations.  My first attempt is at https://github.com/azell/jooq-utils/blob/master/generators/src/main/java/com/github/azell/jooq_utils/generators/BuilderGenerator.java .  Full POC can be found at https://github.com/azell/jooq-utils .

I ended up copy and pasting almost all of the code from the JavaGenerator class.  The builder-specific logic is 6 lines of code.  Is there a better way to do this?

Lukas Eder

unread,
Oct 22, 2019, 3:58:35 AM10/22/19
to jOOQ User Group
Hi Adam,

Thanks a lot for sharing this. I'm positive this will be useful for quite a few people on this list.

We're currently not investing too much in this area ourselves, as the alternatives are much more compelling. E.g. using Kotlin or other languages, or waiting until we finally have java.lang.Record. I have high hopes for the impact of the latter on our ecosystem. The closest we can offer is the <fluentSetters/> code generation configuration flag (which works for mutable POJOs).

Of course, writing your own generator is a viable solution here as well. I don't think there's anything wrong with this approach - I've recommended it numerous times in the past.

Thanks,
Lukas

--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jooq-user/b10707e6-df9a-40a6-b0e1-b8317b05e563%40googlegroups.com.

Adam Zell

unread,
Oct 23, 2019, 3:15:36 AM10/23/19
to jOOQ User Group
Thank you for the feedback.  One thing I noticed is that with Postgres 11.5, jOOQ 3.12.2 is not detecting an identity column.  Given the following SQL:

CREATE TABLE public.jooq (id INTEGER GENERATED BY DEFAULT AS IDENTITY NOT NULL, name VARCHAR(126) NOT NULL, CONSTRAINT JOOQ_PKEY PRIMARY KEY (id));

testdb=# \d jooq;
                                    Table "public.jooq"
 Column |          Type          | Collation | Nullable |             Default              
--------+------------------------+-----------+----------+----------------------------------
 id     | integer                |           | not null | generated by default as identity
 name   | character varying(126) |           | not null | 
Indexes:
    "jooq_pkey" PRIMARY KEY, btree (id)

But column.getType(resolver()).isIdentity() returns false.

To unsubscribe from this group and stop receiving emails from it, send an email to jooq...@googlegroups.com.

Lukas Eder

unread,
Oct 23, 2019, 4:35:14 AM10/23/19
to jOOQ User Group

To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jooq-user/eb006d84-3d00-4f11-9ff6-37221efeeede%40googlegroups.com.

Samir Faci

unread,
Nov 2, 2019, 6:22:14 PM11/2/19
to jooq...@googlegroups.com
I'm not sure if this is helpful but I've Incorporated lombok into our code base at work that had similar functionality to immutable lib.  The focus is removing boiler plate rather than making pojos immutable but might be useful to your custom code generator. 



Just a thought. 



Reply all
Reply to author
Forward
0 new messages