• Stars
    star
    129
  • Rank 278,255 (Top 6 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created about 6 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Java Code Generation Framework

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.7-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>com.javax0.geci</groupId>
    <artifactId>javageci-engine</artifactId>
    <scope>test</scope>
    <version>1.6.7-SNAPSHOT</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:

More Repositories

1

License3j

Free Licence Management Library
Java
575
star
2

fluflu

Fluent Api Creator
Java
73
star
3

jamal

Jamal is document maintenance automation
Java
57
star
4

License3jRepl

Read Eval Print Loop application for License3j library to manage licenses and keys
Java
45
star
5

immutator

Java library to create immutable version of an object run time
Java
37
star
6

djcproxy

Dynamic Java Class Proxy
Java
20
star
7

jscc

Auxilliary classes to help compilation of Java source generated on the fly
Java
17
star
8

jScriptBasic

ScriptBasic for Java
Java
12
star
9

openregime

Open Source documents that can be used in software development
10
star
10

module-test

Testing Java 9 module behaviour
Java
9
star
11

jdsl

Java
8
star
12

yamaledt

JUnit 5 Parameterized Test Yaml Test Data Source
Java
6
star
13

repl

A simple library to support REPL applications
Java
5
star
14

mdsnippet

include code fragment into markdown document during maven site build
Java
5
star
15

jscglib

Java Source Code Generation Library
Java
4
star
16

ScriptBasic

The original source repository for the classic ScriptBasic interpreter as or www.scriptbasc.com
C
4
star
17

gongoshoppomatic

EPAM ASMT preparation
Java
3
star
18

vtstream

Virtual Thread Stream Library
Java
3
star
19

ouroboros

Ouroboros interpreter
Java
2
star
20

BLOG

Simple and small projects that serve as examples for blog articles
Java
2
star
21

concordionsnippet

Snippet tool to be used with Concordion
Java
2
star
22

intern

A simple utility to intern Java objects
Java
2
star
23

aptools

Simple library to help annotation processing programs
Java
1
star
24

jshboot

Java
1
star
25

impostor

Java
1
star
26

jsonedit

a simple json form editor based on jQuery
1
star
27

compare-go-java-devdays2018-peter-verhas

presentation at devdays 2018 in Vilnius
1
star
28

abstractchain

Demonstration of abstract Java class chain
Java
1
star
29

pyama

Python based modular text processor
Python
1
star
30

refi

Some reflection tools
Java
1
star
31

scriapt

Annotation processor starting external script during compilation
Java
1
star
32

pushbuilder

Java
1
star
33

socodot

Source Code Documentation Tool to preprocess documentation using Velocity
Java
1
star
34

commentOrNotToComment

Challange accepted as for http://blog.jooq.org/2013/02/26/the-golden-rules-of-code-documentation/
Java
1
star