A idea-plugin for Java/Scala, support custom code template.
A idea-plugin for Java/Scala to generate code, support custom code template.
This plugin generates code from right click 'Generate...' menu while focused on a Java/Scala class. The output class can be customized using a provided velocity template to format the code based on the origin class and optional selected classes. There are many features are contributed by Dmitry Karlinsky and I appreciate that.
The following features are available:
@see
doc for override method.Download Releases
To install a plugin from the disk in idea
This plugin generates code from right click 'Generate...' menu while focused on a Java/Scala class.
The plugin support multiple class to generate one code, you can set the class number of template to select multiple classes to use.
########################################################################################
##
## Common variables:
## $YEAR - yyyy
## $TIME - yyyy-MM-dd HH:mm:ss
## $USER - user.name
##
## Available variables:
## $class0 - the context class, alias: $class
## $class1 - the selected class, like $class1, $class2
## $ClassName - generate by the config of "Class Name", the generated class name
##
## Class Entry Structure:
## $class0.className - the class Name
## $class0.packageName - the packageName
## $class0.importList - the list of imported classes name
## $class0.fields - the list of the class fields
## - type: the field type
## - name: the field name
## - modifier: the field modifier, like "private",or "@Setter private" if include annotations
## $class0.allFields - the list of the class fields include all fields of superclass
## - type: the field type
## - name: the field name
## - modifier: the field modifier, like "private",or "@Setter private" if include annotations
## $class0.methods - the list of class methods
## - name: the method name
## - modifier: the method modifier, like "private static"
## - returnType: the method returnType
## - params: the method params, like "(String name)"
## $class0.allMethods - the list of class methods include all methods of superclass
## - name: the method name
## - modifier: the method modifier, like "private static"
## - returnType: the method returnType
## - params: the method params, like "(String name)"#
########################################################################################
If you want to add variables into template context, you can modified the source and package the plugin to meet your needs.
The injection of template context is executed by com.xiaohansong.codemaker.action.CodeMakerAction.generateCode
private String generateCode(CodeTemplate codeTemplate, List<ClassEntry> selectClasses) {
Map<String, Object> map = new HashMap<>();
for (int i = 0; i < selectClasses.size(); i++) {
map.put("class" + i, selectClasses.get(i));
}
Date now = new Date();
map.put("class", selectClasses.get(0));
map.put("YEAR", DateFormatUtils.format(now, "yyyy"));
map.put("TIME", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
map.put("USER", System.getProperty("user.name"));
String className = VelocityUtil.evaluate(codeTemplate.getClassNameVm(), map);
map.put("ClassName", className);
map.put("utils", new Utils());
map.put("BR", "\n");
return VelocityUtil.evaluate(codeTemplate.getCodeTemplate(), map);
}