Source code and files generator with an intuitive API written in Java. With this tool you can create your sources with a minimal learning curve.
The goal of the project is to create natural, easy to use and self-documented Java API.
Every class that represents a model of source file is a subclass of SourceFile
.
To create a physical file use createSourceFile()
method.
To create a string representation of file content invoke toString()
.
The generated source code often needs some kind of reformatting.
The following examples are not a complete overview of available methods. They are just a simple demonstration of how to use the Source Gen-X tool.
Creating a sample main class:
Constant constant = new Constant("String", "GREETING", "\"Hello World!\"");
Method main = new Method(AccessModifier.PUBLIC, Modifier.STATIC, "void", "main")
.addParameter(new Parameter("String[]", "args"))
.setBody("\tSystem.out.println(GREETING);");
JavaClass mainClass = new JavaClass("sourcegenx.demo", "Application")
.addField(constant)
.addMethod(main);
mainClass.createSourceFile("/demo/src/main/java/sourcegenx/demo");
Output Application.java:
package sourcegenx.demo;
public class Application {
public static final String GREETING = "Hello World!";
public static void main(String[] args) {
System.out.println(GREETING);
}
}
Creating a sample DAO interface:
Import imp = new Import("sourcegenx.demo.entities.User");
InterfaceMethod findMethod = new InterfaceMethod("User", "findById")
.addParameter(new Parameter("long", "id"));
JavaInterface dao = new JavaInterface("sourcegenx.demo", "UserDao<User>")
.addImport(imp)
.addMethod(findMethod);
dao.createSourceFile("/demo/src/main/java/sourcegenx/demo", "UserDao");
Output UserDao.java:
package sourcegenx.demo;
import sourcegenx.demo.entities.User;
public interface UserDao<User> {
User findById(long id);
}
Creating a sample cardinal directions enum:
JavaEnum directions = new JavaEnum("sourcegenx.demo", "CardinalDirection")
.addValue(new EnumValue("NORTH"))
.addValue(new EnumValue("WEST"))
.addValue(new EnumValue("SOUTH"))
.addValue(new EnumValue("EAST"));
directions.createSourceFile("/demo/src/main/java/sourcegenx/demo");
Output CardinalDirection.java:
package sourcegenx.demo;
public enum CardinalDirection {
NORTH,
WEST,
SOUTH,
EAST;
}
Creating a sample secured annotation:
Annotation target = new Annotation("Target")
.addAttribute("value", "METHOD");
Annotation retention = new Annotation("Retention")
.addAttribute("value", "RUNTIME");
AnnotationElement allowedRoles = new AnnotationElement("String[]", "allowedRoles")
.setDefaultValue("{\"ADMIN\", \"CUSTOMER\"}");
JavaAnnotation secured = new JavaAnnotation("sourcegenx.demo", "Secured")
.addImport(new Import("java.lang.annotation.Retention"))
.addImport(new Import("java.lang.annotation.Target"))
.addImport(new Import(Modifier.STATIC, "java.lang.annotation.ElementType.METHOD"))
.addImport(new Import(Modifier.STATIC, "java.lang.annotation.RetentionPolicy.RUNTIME"))
.addAnnotation(target)
.addAnnotation(retention)
.addElement(allowedRoles);
secured.createSourceFile("/demo/src/main/java/sourcegenx/demo");
Output Secured.java:
package sourcegenx.demo;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Target(METHOD)
@Retention(RUNTIME)
public @interface Secured {
String[] allowedRoles() default {"ADMIN", "CUSTOMER"};
}
Creating a sample properties file:
ConfigProperties properties = new ConfigProperties()
.addProperty(new Property("database.name", "test"))
.addProperty(new Property("database.host", "localhost"));
properties.createSourceFile("/demo/src/main/resources", "application");
Output application.properties:
database.name=test
database.host=localhost
Creating a sample YAML file:
YamlNode root = new YamlNode("root")
.addChild(new YamlNode("message.default").addValue("Some message"))
.addChild(new YamlNode("message.special")
.addChild(new YamlNode("server").addValue("1"))
.addChild(new YamlNode("client").addValue("2"))
);
ConfigYaml yaml = new ConfigYaml().addNode(root);
yaml.createSourceFile("/demo/src/main/resources", "application");
Output application.yaml:
root:
message.default: Some message
message.special:
server: 1
client: 2
See test specifications for more samples:
- Java class spec
- Java interface spec
- Java enum spec
- Java annotation spec
- Config properties spec
- Config YAML spec
Source Gen-X is published under Apache License 2.0.