My favorite internal DSL in java is
https://www.jooq.org/
because it lets you write code in a real programming language (SQL stored procedures) as an internal DSL, see...
https://www.jooq.org/javadoc/latest/org.jooq/org/jooq/Block.html
I'm pretty convinced it is possible to write Java-in-Java and I started hacking on this a year ago
https://github.com/paulhoule/ferocity/blob/main/ferocity0/src/test/java/com/ontology2/ferocity/UrFierce.java
the idea of ferocity is that you can write Java syntax trees in a style kinda like JooQ or Lisp (like in the above
unit tests) and then either evaluate the syntax tree with a primitive interpreter or turn it into Java code that can be fed back through Javac.
It sounds crazy but I think it is a powerful strategy for code generation, the plan I had was to write the generation 0 ferocity, get it up to the point where it could compile stubs for the Java standard library so if you want to write say
you would write
where of generates the literal (an Expression), instead of passing objects in you pass in Expressions. That 𝔣 is a Unicode character and these are used all over the place, for
instance sometimes it adds double-angle brackets to fill in the types of a function name if it is ambiguous because type erasure means you can't distinguish between Expression<This> and Expression<That>.
What I found out was that language that the expression interpreter can process wants to be bigger than Java, for instance you can quote an Expression and have it be processed as an Expression in the interpreter to do 'syntactic macros' like they have in LISP. You can't (I think) serialize those to Java code but you can interpret them. Similarly there is a concept that includes variables, parameters that it is useful to define new kinds of.
I'd convinced myself that there wasn't any fundamental problem, just a lot of work, to realize this and figured the easy way to do it was write ferocity1 using ferocity0 to do code generation but then I got distracted by some Arduino project. I probably should have finished it before starting pidove but...