Java Class Generator

This utility library helps to generate Java classes based on provided class schemas with some predefined code generation parameters.

License

License

Categories

Categories

Java Languages
GroupId

GroupId

com.github.vladislavsevruk
ArtifactId

ArtifactId

java-class-generator
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

pom.sha512
Description

Description

Java Class Generator
This utility library helps to generate Java classes based on provided class schemas with some predefined code generation parameters.
Project URL

Project URL

https://github.com/VladislavSevruk/JavaClassGenerator
Source Code Management

Source Code Management

https://github.com/VladislavSevruk/JavaClassGenerator/tree/master

Download java-class-generator

Dependencies

runtime (2)

Group / Artifact Type Version
org.apache.logging.log4j : log4j-api jar 2.13.0
org.apache.logging.log4j : log4j-core jar 2.13.0

Project Modules

There are no modules declared in this project.

Build Status Quality Gate Status Code Coverage Maven Central

Java Class Generator

This utility library helps to generate java classes based on provided class schemas with some predefined code generation parameters.

Table of contents

Getting started

To add library to your project perform next steps:

Maven

Add the following dependency to your pom.xml:

<dependency>
      <groupId>com.github.vladislavsevruk</groupId>
      <artifactId>java-class-generator</artifactId>
      <version>1.0.0</version>
</dependency>

Gradle

Add the following dependency to your build.gradle:

implementation 'com.github.vladislavsevruk:java-class-generator:1.0.0'

Class generation configuration

Library supports some configuration parameters for class generation which are present at JavaClassGeneratorConfig class. Here is short description for them:

  • Path to target folder - points to folder where generated classes should be placed.
  • Indents to use - indicates what indents should be used for class generation. Possible variants are represented by Indent class.
  • Add Jackson library annotations - indicates if Jackson library annotations should be added for schema items that implements JacksonSchemaField interface.
  • Use Lombok library annotations - indicates if Lombok library annotations should be added for generated classes.
  • Add empty line between fields - indicates if empty line should be added between class fields.
  • Sort fields by modifiers - indicates if class fields should be sorted by modifiers in following order:
    • public static final FieldType ...
    • public static FieldType ...
    • public FieldType ...
    • protected static final FieldType ...
    • protected static FieldType ...
    • protected FieldType ...
    • static final FieldType ...
    • static FieldType ...
    • FieldType ...
    • private static final FieldType ...
    • private static FieldType ...
    • private FieldType ...

Usage

For generation single class it's required to implement SchemaObject interface with information about class elements (library has schemas of some basic Java classes which can be used for class schema elements description). Once schema is ready all you need is to call

// creating generator config with path to target folder
JavaClassGeneratorConfig config = JavaClassGeneratorConfig.builder()
        .pathToTargetFolder("generated/classes").build();
// generating schema object with target class elements description
SchemaObject schemaObject = generateSchemaObject();
// generating class according to received schema and config with target folder
new JavaClassGenerator().generateJavaClass(config, schemaObject);

Also several classes can be generated at once using SchemaObjectStorage (library has default implementation of this interface):

// creating generator config with path to target folder
JavaClassGeneratorConfig config = JavaClassGeneratorConfig.builder()
        .pathToTargetFolder("generated/classes").build();
// generating several schema objects with class elements description
SchemaObject schemaObject1 = generateSchemaObject1();
SchemaObject schemaObject2 = generateSchemaObject2();
// create storage instance
SchemaObjectStorageImpl storage = new SchemaObjectStorageImpl();
// put generated schema objects to storage
storage.put(schemaObject1.getName(), schemaObject1);
storage.put(schemaObject2.getName(), schemaObject2);
// generating class according to received schema and config with target folder
new JavaClassGenerator().generateJavaClasses(config, storage);

Customization

Add custom class content generators

You can add your own class content generators to customize logic of class elements generation. Simply implement JavaClassContentGeneratorProvider interface and add it to ClassContentGeneratorProviderStorage from context (you can reach it calling ClassGenerationContextManager.getContext().getClassContentGeneratorProviderStorage()). Library has default implementations of this interface for simple class, enums and interfaces that can be overridden to update logic of some elements generation.

License

This project is licensed under the MIT License, you can read the full text here.

Versions

Version
1.0.1
1.0.0