Java Code Generation Framework

Related tags

Spring Boot java
Overview

Java::Geci

Java::Geci is a library for generating Java code. You can execute code generation programs using Java::Geci to generate new source code or modify existing Java source files. This way, the programmer can use metaprogramming to express code shorter and more expressively than it would be possible in pure Java.

The framework:

  • discovers the files that need generated code,

  • provides easy to use API to generate code,

  • writes the generated code into the source code.

The code generating program should focus on the actual code structure it wants to create.

When do you need Java::Geci?

There are several occasions when we need generated code. IDEs (Eclipse, NetBeans, IntelliJ) support the simplest of such scenarios. They can create setters, getters, constructors, equals() and hashCode() methods in different ways. There are two significant problems with that solution.

  • One is that you trigger the code generation manually. If the developer forgets to re-generate the code after a change, the generated code may become stale.

  • The other problem is that the code generation possibilities are not extendable. There is only a minimal code set that the tools can generate, and the developer cannot easily extend these.

Java::Geci eliminates these two problems. The mode of the execution ensures that all the generated code is up-to-date. The code generation runs from a unit test, that you can start from your favourite IDE or from the build tool If you forgot to update the generated code after the program affecting the generated code changed, the test would fail. This is not a problem in this case, because the test also updates the generated code. The next time you run the test it will see the up-to-date generated code, and it will not fail.

Java::Geci also has a straightforward API supporting code generators, so it is simple to create new code generators that are project-specific. You do not even need to package your code generator classes. Just put them into a test package and execute Java::Geci during the test phase of the build process.

Java::Geci already includes several readily available code generators. Some of them are simple and may be found in other tools, some of them are unique. The generators packaged with the core package are:

  • fluent API classes and interfaces

  • internal builders into the class

  • repetitive code iteratively

  • record classes (that are Java 8 compatible and can easily be replaced with Java 14+ records)

  • code that converts an object to a Map and back

  • delegation methods (under development)

  • setter and getter (just as an example, there are so many setter/getter generators…)

How to use Java::Geci

Add the Java::Geci modules that you want to use in your project as a dependency. To do that with Maven, use the following dependencies:

<dependency>
    <!-- This is optional, you can use your own annotations or comment config -->
    <groupId>com.javax0.geci</groupId>
    <artifactId>javageci-annotation</artifactId>
    <version>1.6.6</version>
</dependency>
<dependency>
    <groupId>com.javax0.geci</groupId>
    <artifactId>javageci-engine</artifactId>
    <scope>test</scope>
    <version>1.6.6</version>
</dependency>

This documentation contains the latest development version. You can see the latest non-SNAPSHOT version in the link for release versions release history documentation.

There are other modules, but you do not need to declare a dependency on them as the engine module has a transitive dependency, and thus Maven automatically will use them.

Note
Note that this does not include out-of-the-box generators. Check the individual documentations to see what additional dependencies you might need for those.

Since Java::Geci works during test execution, the dependencies have a test scope. The exception is the annotation module. This module defines an annotation that you can use to configure the code generation during test time (using reflection). Because of that, the retention of this module is run-time. Although the annotation is not used in production, it remains in the byte code, and thus it has to be the default maven scope.

If for any reason production dependency must not include Java::Geci, you can configure the generators using comments, or you can use the annotation interfaces that you can define inside your project. Copy Geci.java and Gecis.java to your project into any package, and Java::Geci will recognize that they are to there configure a generator. Java::Geci is very flexible and recognizes any annotation so long as long the simple name of the annotation is Geci (and even a bit more).

If you have a look at the test code TestAccessor.java in the test module you can see the following sample code:

    @Test
    void testAccessor() throws Exception {
        Geci geci;
        Assertions.assertFalse(
                (geci = new Geci()).source(maven().module("javageci-examples").mainSource())
                        .register(Accessor.builder().build())
                        .generate(),
                geci.failed());
    }

The test runs during the build process, and it generates code whenever that is needed. The return value answers the question: "Was code generated?". It is true when new code was generated or false if everything was up-to-date. The Assertions.assertFalse checks this return value, and if Java::Geci generated new code, your test (and your build) fails. In this case, you have to restart your build. Java::Geci will recognize that the code it could generate is already there, and the test passes.

For further information see the following content:

Comments
  • Maybe choose a less concerning library name

    Maybe choose a less concerning library name

    Great work on this library! But sometimes names mean other things that originally intended. I suggest changing the name of this library as the word "geci" has a particulary pejorative meaning in some contexts. Especially in the Hungarian language [1] :) Just a suggestion. [1] https://topszotar.hu/magyarangol/geci

    opened by andrask 3
  • Bump xstream from 1.4.15 to 1.4.18

    Bump xstream from 1.4.15 to 1.4.18

    Bumps xstream from 1.4.15 to 1.4.18.

    Commits

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
    • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
    • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
    • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language

    You can disable automated security fix PRs for this repo from the Security Alerts page.

    dependencies 
    opened by dependabot[bot] 2
  • Question: Different than Lombok?

    Question: Different than Lombok?

    Hey sorry to post here but I saw your post on the Apache mailing list about Commons Lang and boilerplate code.

    From looking at your documentation how is your code different than Project Lombok?? https://projectlombok.org/

    I was looking through your docs and didn't see anything that was different or newer than Lombok. Just curious.

    opened by melloware 2
  • Chained source() calls overwrite previous sources instead of adding them

    Chained source() calls overwrite previous sources instead of adding them

    Describe the bug When calling the following method in javax0.geci.engine.Geci, it overwrites previous calls because it uses the same key.

    public Geci source(String... directory) {
            directories.put(set(""), directory);
            return this;
        }
    

    To Reproduce Chain multiple .source calls after each other without specifying a Source.Set as the first parameter.

    Expected behavior Instead of overwriting the previous sources, the class should use an ID, that identifes different source calls?

    opened by Michael1993 2
  • Confusing tutorial

    Confusing tutorial

    In the first tutorial, there is this snippet:

    @Test
    void testAccessor() throws Exception {
        Geci geci;
        Assertions.assertFalse(
                (geci = new Geci()).source(maven().module("javageci-examples").mainSource())
                        .register(Accessor.builder().build())
                        .generate(),
                geci.failed());
    }
    

    This is confusing because if you add the "required" dependencies (listed as)

    <dependency>
        <!-- This is optional, you can use your own annotations or comment config -->
        <groupId>com.javax0.geci</groupId>
        <artifactId>javageci-annotation</artifactId>
        <version>1.5.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.javax0.geci</groupId>
        <artifactId>javageci-engine</artifactId>
        <scope>test</scope>
        <version>1.5.1-SNAPSHOT</version>
    </dependency>
    

    then trying to import Geci shows three possible choices:

    • an interface
    • a class
    • an annotation

    This is confusing a little or, at the very least, annoying. Does Geci really need to be an interface? Do we expect other people to provide new/additional implementations of this interface? If not, it warrants an investigation if it could be removed, replaced or renamed.

    Another confusing thing is that Accessor can not be imported. It is part of the javageci-core artifact which javageci-engine does not require. Either it should be used by javageci-engine or the bit "There are other modules, but you do not need to declare dependency on them as the engine module has transitive dependency and thus maven automatically will use them." has to be rewritten.

    opened by Michael1993 1
  • Even more streams.

    Even more streams.

    Replaced a for loop with a much simpler forEach in Segment. Replaced a nested for/if loop with a chained stream call in MethodCollection. Refactored getAllMethodsSorted to follow the same pattern as getAllFieldsSorted - also replaced loops with streams in GeciReflectionTools.

    opened by Michael1993 1
  • Update TUTORIAL_USE.md

    Update TUTORIAL_USE.md

    Closes #43

    The confusing sentence was reworded, mentioning javageci-core. The tutorial explicitly states javax0.geci.engine.Geci is used.

    Broke the tutorial into smaller segments.

    opened by Michael1993 0
  • Update README.md

    Update README.md

    After reading through the README.md I have made some minor changes.

    For example:

    • Fixed some typing mistakes.
    • Minor style improvements thoughout the file.
    • Some improvements in the punctuation.
    opened by 4TT1L4 0
  • Bump xstream from 1.4.15 to 1.4.19

    Bump xstream from 1.4.15 to 1.4.19

    Bumps xstream from 1.4.15 to 1.4.19.

    Commits

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
    • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
    • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
    • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language

    You can disable automated security fix PRs for this repo from the Security Alerts page.

    dependencies 
    opened by dependabot[bot] 0
Releases(1.6.4)
  • 1.6.4(Oct 25, 2021)

  • 1.6.3(Jul 14, 2021)

    Using this release all you need to use the Jamal macro generator is to add the Jav::Geci Test Engine to the dependency list and to have at least one JUnit 5 test in your code.

    This release can also create DIFF files so you can easily compare what was overwritten with what.

    Java 8 is NOT SUPPORTED anymore. Java GECI needs Java 11 as a minimum. Docugen is removed and is not supported anymore. Use Jamal to handle snippets.

    Jamal version 1.7.9 is used.

    Source code(tar.gz)
    Source code(zip)
  • 1.6.2.1(Feb 1, 2021)

    Same as 1.6.2 except top-level pom.xml was modified so that the deployment to maven central is automatic. Up to 1.6.1 there was an error that uploaded the artifacts and left the repo open and did not release.

    Jamal 1.6.3 is used, where you can

     {%#for (a,b,c) in `END`list,with,), in, it `END` = ... %}
    
    Source code(tar.gz)
    Source code(zip)
  • 1.6.2(Feb 1, 2021)

  • 1.6.1(Jan 7, 2021)

    Support macros for Jamal templating were redesigned and it uses now Jamal 1.5.3 There is a sample to generate unit test proxy static inner class

    Source code(tar.gz)
    Source code(zip)
  • 1.6.0(Dec 13, 2020)

    Documentation update. It should have been 1.5.1 but accidentally it became 1.6.0

    This version uses the OSSH deploy plugin and is configured to send the artifacts automatically to maven central without manual hacking.

    Source code(tar.gz)
    Source code(zip)
  • 1.5.0(Aug 20, 2020)

    • A fix in the fluent generator. In prior versions when a grammar started with an optional method or with an optional expression (e.g.: zero or more times alternative methods, anything that is (...)* in the syntax description) then the generated interface structure required ONE or more invocations instead of ZERO or more.

    • Fluent generator was annotated with @AnnotationBuilder and hence, there is a @Fluent annotation in core-annorations.

    • Jdocify was developed.

    • GeciException thrown from the generators are caught, enriched with the source file information the generator was working on and then thrown again in the Geci process. This eliminates the need to fetch the file name from the source code and to add it to the exception where it is originally thrown.

    • Lexical analyser keeps the original format of the characters and strings and it can also be queried in case the generator needs to know the exact escape sequences in the string or character literal.

    • Generators replying on the lexical analysis do not need to delete a lexical element from the list of lexical elements of a source file and insert a new one when it can simply be done replacing the lexeme string. It is not possible to change the type of the lexical element though.

    • A bug prevented the proper comparing of any Java file that contained string or character with \n or \r literals. The code was throwing an exception thus code generation was aborted.

    • A bug prevented the proper comparing of any Java file that contained hexadecimal long literals. The code was throwing an exception thus code generation was aborted.

    Source code(tar.gz)
    Source code(zip)
  • 1.4.0(Nov 25, 2019)

  • 1.3.0(Oct 28, 2019)

    • Support Java 8

      The code is still Java 11 level, but there is no use of any Java 8+ JDK class or method. The build features a JVM8 profile that will create Java 8 compatible byte code. The generated JavaDoc in the 1.3.0-JVM8 release is erroneous and there is no intention to fix it. The source and JavaDoc jars are available from the normal 1.3.0 release.

    • generators can access lexical elements lists

      There is a new class JavaLexed that can be used to match the JavaSource using pattern matching (kind of regex but based on lexical elements instead of characters) and it is also possible to update/modify the source code.

    • Record generator

      There is a new generator that creates classes that mimic the proposed Java record functionality that will be available in the future some time. This generator also demonstrate how to use the JavaLexed class in generators that want to modify the Java source on lexical element level.

    • document generation supports JavaDoc

      You can insert doclets into JavaDoc code without special code inserter generator (the special code inserter is part of the library now).

    Source code(tar.gz)
    Source code(zip)
    release-1.3.0-JVM8.zip(625.27 KB)
    release-1.3.0.zip(4.27 MB)
  • 1.2.0(Jul 26, 2019)

    This is a major feature release with many new features.

    • Documentation generation snippet support,
    • Generator configuration unified and supported by code generation
    • parameter parsing supports " inside string, other escape sequences, and numeric and boolean values
    • multi-line segments headers are supported in markdown documents
    • Java generated code is compared on the lexical level and thus reformatted code is not regenerated
    Source code(tar.gz)
    Source code(zip)
    00parent_release.zip(2.05 KB)
    01api_release.zip(520.23 KB)
    02annotation_release.zip(405.92 KB)
    03tools_release.zip(850.12 KB)
    04core_release.zip(727.84 KB)
    05engine_release.zip(484.55 KB)
    06jamal_release.zip(462.59 KB)
  • 1.1.0(Jan 29, 2019)

    • Fluent API changes:

      • Fluent API can be defined in a single string using a simple syntax definition calling the fluent API building fluent API method syntax()
      • Wrapper contains only those methods that are actually used in fluent API building
      • Builder methods can also be private, protected or package private and they do not need to be public. They are wrapped anyway.
      • You can explicitly include a method into the fluent API wrapper calling include()
    • Geci core changes:

      • Generators that work on Java classes and are implemented extending the AbstractGenerator automatically get the configuration from comment if the class is not annotated
      • Any annotation can be used that is named Geci not only the predefined in the given library
      • Annotation parameters that are defined and have string value are appended to the configuration. It makes sense when the annotation is defined for the using project, as the provided @Geci annotation does not have other parameters only value
      • File collections can be limited using regular expressions matching file names.
      • Generation throws an error if generators are configured so that they do not work on any source.
    Source code(tar.gz)
    Source code(zip)
    00parent_release.zip(1.74 KB)
    01api_release.zip(439.74 KB)
    02annotation_release.zip(405.32 KB)
    03tools_release.zip(615.44 KB)
    04core_release.zip(601.90 KB)
    05engine_release.zip(437.82 KB)
    06jamal_release.zip(400.22 KB)
Owner
Peter Verhas
Senior Software at EPAM Systems
Peter Verhas
Runtime code generation for the Java virtual machine.

Byte Buddy runtime code generation for the Java virtual machine Byte Buddy is a code generation and manipulation library for creating and modifying Ja

Rafael Winterhalter 5.3k Dec 27, 2022
JJazzLab-X is a Midi-based framework dedicated to backing tracks generation

JJazzLab-X is a Midi-based framework dedicated to backing tracks generation -some people talk about "play-along songs" or “auto-accompaniment applications”. You type in chord symbols, select a rhythm (style), then the application generates a complete backing track with drums, bass, guitar, piano, strings, etc.

Jerome Lelasseux 247 Jan 3, 2023
💡极致性能的企业级Java服务器框架,RPC,游戏服务器框架,web应用服务器框架。(Extreme fast enterprise Java server framework, can be RPC, game server framework, web server framework.)

?? 为性能而生的万能服务器框架 ?? Ⅰ. zfoo简介 ?? 性能炸裂,天生异步,Actor设计思想,无锁化设计,基于Spring的MVC式用法的万能RPC框架 极致序列化,原生集成的目前二进制序列化和反序列化速度最快的 zfoo protocol 作为网络通讯协议 高可拓展性,单台服务器部署,

null 1k Jan 1, 2023
Exemplos - Aulas de Spring - Generation

Spring Aulas sobre Spring - Generation Aulas Aula 06 (Clique aqui) - Serviços personalizados - Blog Pessoal Aula 05 (Clique aqui) - Spring Security Ex

Rafael Queiróz 2 Mar 4, 2022
Projeto realizado em SQUAD na Generation Brasil

projeto-integrador Projeto integrador realizado em SQUAD na Generation Brasil Autores Caique Batista Inaie Forster João Claúdio Melissa Amorim Sâmila

João Claudio 5 Nov 25, 2021
Swerve control, simulation, and trajectory generation / following. Everything you need for swerve.

BearSwerve BearSwerve is intended to be an all in one swerve solution including: teleop driving, simulation, trajectory following and more. It combine

null 7 Dec 28, 2022
Project developed in Block 1 through the Generation bootcamp

PROJETO JAVA - CCHIV COMBATE CONTRA O HIV COMO SURGIU ? É um software criado para facilitar a forma de atender o público de pessoas portadoras do HIV

Paulo Brisola 4 Jul 5, 2022
Back-End da rede social AdaTech para Projeto Integrador da Generation Brasil

Back-End da rede social AdaTech para Projeto Integrador da Generation Brasil

AdaTech.Projeto.Integrador 6 Apr 18, 2022
Projeto realizado durante o bootcamp da Generation.

Projeto de conclusão do BlOCO - I QUAL A MISSÃO DO PROJETO? Disseminar a cultura de doação voluntária de sangue entre a população. O POSSO DOAR nasceu

Kathleen Caroline 1 Oct 19, 2021
GalaxyCDC is a core component of PolarDB-X which is responsible for global binary log generation, publication and subscription.

中文文档 What is ApsaraDB GalaxyCDC ? GalaxyCDC is a core component of PolarDB-X which is responsible for global binary log generation, publication and su

null 56 Dec 19, 2022
OpenAPI JSON Schema Generator allows auto-generation of API client libraries with a focus on JSON schema given an OpenAPI Spec

OpenAPI JSON Schema Generator IMPORTANT: before the first release, one will need to build the project locally to use the enhancements, bug fixes in th

OpenAPI Tools 5 Dec 31, 2022
Team 5468's 2022 FRC robot code. This code is written in Java and is based off of WPILib's Java control system and utilizes a command based system

FRC 2022 Team 5468's 2022 FRC robot code. This code is written in Java and is based off of WPILib's Java control system and utilizes a command based s

null 4 Oct 4, 2022
Business Application Platform - no-code/low-code platform to build business applications

Orienteer What is Orienteer Orienteer is Business Application Platform: Easy creation of business applications Extendable to fit your needs Dynamic da

Orienteer 189 Dec 6, 2022
This template makes it easy to organize FTC code and allows for the Autonomous and TeleOp periods to share code.

FTC Code Organizer This template created by team 19458 Equilibrium.exe makes it easy to keep your code organized and allows the Autonomous and TeleOp

FTC 19458 Equilibrium.exe 5 Nov 10, 2022
An assistance platform made using Spring framework that analyses your code, and helps you either to start a devops project, or to turn an existing project into a devops project using open source software (Git, Docker, Jenkins..)

DevOpsify Description An assistance platform made using Spring framework that analyses your code, and helps you either to start a devops project, or t

obaydah bouifadene 14 Nov 8, 2022
Lattice is a powerful, lightweight business extension invoke framework. By using the Lattice framework, complex business customization can be efficiently organized and managed.

Lattice Framework Introduction Lattice is a powerful, lightweight business extension invoke framework. By using the Lattice framework, complex busines

null 41 Dec 30, 2022
Java Design Patterns code examples

Java Design Patterns code examples Behavioral In software engineering, behavioral design patterns are design patterns that identify common communicati

Gaboso™ 3 Jun 29, 2022
Repository with Backend code for InnoTutor project. It is written on Java/Spring.

Backend ᅠ ᅠ Developers: Daniil Livitn, Roman Soldatov Contents Requirements API Database Google credentials Hosting and CI How to install locally Code

InnoTutor 20 Sep 17, 2022
Google App Engine Standard Environment Source Code for Java 8 and Java11

Google App Engine Standard Environment Source Code for Java 8 and Java11. This is a repository that contains the Java Source Code for Google App Engin

Google Cloud Platform 167 Jan 2, 2023