Skip to content

sadasidha/java-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

java-template

A simple and more readable tool generate Java Source code

Usage

  1. #insert. String can be inserted in this
#insert imports_line;
  1. #comment. Comment will be ignroed all together.
#comment this is just comment. 
#comment this lines will not be available in our resulted source code
  1. ##VARIABLE##. All variables in the current template file will be replaced, except repeat block. Every variable name starts with ## and ends with ##
#comment example
String fileName = "##FILE_NAME##";
  1. #import. Can be imported multiple time. After improting variables can be updated.
#comment example
#import resource_path
  1. import_once. Can bee imported only once. Variables can be updated after improting.
#comment example
#import_once resource_path
  1. Code block that can be injected more than once. Code block start with #start followed by a name. Block will be end with #end followed by the exact same name. Can be loaded only once.
#comment example
int i;
#start file_process_block
  i = ##VALUE_TO_SET##;
  fileOpen(##FILE_NAME##, ##VALUE_TO_SET##);
#end file_process_block

Example

  1. template file
// main.template
package ##package_name##;
#import_once imports

public class ##class_name## {
    public static void main(String []args) {
        List<String> strs = new ArrayList<String>();
        #start value_set
        strs.add("##string##");
        #end value_set
        for(String s: strs) {
            System.out.print(s);
        }
        System.out.println("##final_message##");
    }
}
  1. imports.template
import java.util.List;
import java.util.ArrayList;
  1. Processing the block
import org.junit.jupiter.api.Test;

public class SimpleExampleTest {
    @Test
    public void execute() {
        TemplateProcessor tp = new TemplateProcessor(this.getClass(), "main");
        tp.setValue("package_name", "hello.world");
        tp.setValue("class_name", "HelloWorld");
        tp.setValue("final_message", "");
        tp.addImportBlock("imports");
        tp.addRepeatBlock("value_set", "n1").setValue("string", "Hello");
        tp.addRepeatBlock("value_set", "n2").setValue("string", " World");
        System.out.println(tp.toString());
    }
}
  1. Generated Code
package hello.world;

// Importing imports
import java.util.List;
import java.util.ArrayList;

public class HelloWorld {
    public static void main(String []args) {
        List<String> strs = new ArrayList<String>();
        // Repeat block: value_set starts
        strs.add("Hello");
        strs.add(" World");
        // Repeat block: value_set ends
        for(String s: strs) {
            System.out.print(s);
        }
        System.out.println("");
    }
}

Including in your source code

  1. Clone this repository
  2. execute install.sh
bash install.sh
  1. add the following lines in your pom.xml
<dependency>
	<groupId>simple.mind</groupId>
	<artifactId>java-template</artifactId>
	<version>0.0.1</version>
</dependency>

note: Use at your own risk

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published