This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository eugene. See https://gitlab.nuiton.org/nuiton/eugene.git commit c67e2807edcb3901c87b27b5e7a4208dc88d6ca8 Author: Tony CHEMIT <chemit@codelutin.com> Date: Thu Sep 15 21:26:11 2016 +0200 Add optional Action class + improve generated class code (using a supplier (Java8...) to ApplicationConfig (See #4036) --- .../templates/ApplicationConfigTransformer.java | 29 +++++++- .../plugin/GenerateApplicationConfigMojo.java | 80 ++++++++++++++-------- 2 files changed, 76 insertions(+), 33 deletions(-) diff --git a/eugene-config-templates/src/main/java/org/nuiton/eugene/config/templates/ApplicationConfigTransformer.java b/eugene-config-templates/src/main/java/org/nuiton/eugene/config/templates/ApplicationConfigTransformer.java index e1eff3a..4742ff6 100644 --- a/eugene-config-templates/src/main/java/org/nuiton/eugene/config/templates/ApplicationConfigTransformer.java +++ b/eugene-config-templates/src/main/java/org/nuiton/eugene/config/templates/ApplicationConfigTransformer.java @@ -30,6 +30,7 @@ public class ApplicationConfigTransformer extends ObjectModelTransformerToJava { private static final Log log = LogFactory.getLog(ApplicationConfigTransformer.class); public static final String PROP_OPTION_CLASS_NAME = "optionClassName"; + public static final String PROP_ACTION_CLASS_NAME = "actionClassName"; @Override public void transformFromClass(ObjectModelClass clazz) { @@ -67,8 +68,13 @@ public class ApplicationConfigTransformer extends ObjectModelTransformerToJava { String optionClassName = getProperty(PROP_OPTION_CLASS_NAME); String optionClassSimpleName = GeneratorUtil.getSimpleName(optionClassName); + + String actionClassName = getProperty(PROP_ACTION_CLASS_NAME); + ObjectModelClass output = createAbstractClass(abstractClassName, packageName); + addInterface(output, "java.util.function.Supplier<ApplicationConfig>"); + addImport(output, "java.util.function.Supplier"); addImport(output, optionClassName); if (log.isInfoEnabled()) { @@ -77,15 +83,32 @@ public class ApplicationConfigTransformer extends ObjectModelTransformerToJava { addAttribute(output, "applicationConfig", ApplicationConfig.class, "", ObjectModelJavaModifier.PRIVATE); ObjectModelOperation constructor = addConstructor(output, ObjectModelJavaModifier.PROTECTED); - setOperationBody(constructor, "" - /*{ + StringBuilder builder = new StringBuilder(); + builder.append("" + /*{ this.applicationConfig = new ApplicationConfig(); this.applicationConfig.loadDefaultOptions(<%=optionClassSimpleName%>.values()); }*/ ); + if (actionClassName != null) { + addImport(output, actionClassName); + String actionClassSimpleName = GeneratorUtil.getSimpleName(actionClassName); + builder.append("" + /*{ + for (<%=actionClassSimpleName%> a : <%=actionClassSimpleName%>.values()) { + + for (String alias : a.getAliases()) { + applicationConfig.addActionAlias(alias, a.getAction()); + } + } + }*/ + ); + } + setOperationBody(constructor, builder.toString()); ObjectModelOperation getApplicationConfig = addOperation( - output, "getApplicationConfig", ApplicationConfig.class, ObjectModelJavaModifier.PROTECTED); + output, "get", ApplicationConfig.class, ObjectModelJavaModifier.PUBLIC); + addAnnotation(output, getApplicationConfig, Override.class); setOperationBody(getApplicationConfig, "" /*{ return applicationConfig; diff --git a/eugene-maven-plugin/src/main/java/org/nuiton/eugene/plugin/GenerateApplicationConfigMojo.java b/eugene-maven-plugin/src/main/java/org/nuiton/eugene/plugin/GenerateApplicationConfigMojo.java index 31f7116..372f629 100644 --- a/eugene-maven-plugin/src/main/java/org/nuiton/eugene/plugin/GenerateApplicationConfigMojo.java +++ b/eugene-maven-plugin/src/main/java/org/nuiton/eugene/plugin/GenerateApplicationConfigMojo.java @@ -46,6 +46,7 @@ import org.nuiton.eugene.plugin.parser.java.Java8Lexer; import org.nuiton.eugene.plugin.parser.java.Java8Parser; import org.nuiton.plugin.AbstractPlugin; import org.nuiton.plugin.PluginHelper; +import org.nuiton.plugin.PluginWithEncoding; import org.nuiton.version.Version; import javax.swing.KeyStroke; @@ -66,23 +67,23 @@ import java.util.Properties; * Created on 28/08/16. * * @author Tony Chemit - chemit@codelutin.com - * @since 3.6 + * @since 3.0 */ @Mojo(name = "generate-config", defaultPhase = LifecyclePhase.GENERATE_SOURCES, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME) -public class GenerateApplicationConfigMojo extends AbstractPlugin { +public class GenerateApplicationConfigMojo extends AbstractPlugin implements PluginWithEncoding { /** * To set the package fully qualified name of the generated class. * * By default, will use groupId.artifactId (with {@code -} replaced by {@code .}) plus {@code .config}. */ - @Parameter(property = "i18n.packageName") + @Parameter(property = "eugene.packageName") private String packageName; /** * Model name (will prefix the generated files names with it). */ - @Parameter(property = "i18n.modelName", required = true) + @Parameter(property = "eugene.modelName", required = true) private String modelName; /** @@ -90,25 +91,31 @@ public class GenerateApplicationConfigMojo extends AbstractPlugin { * * By default, will use groupId.artifactId (with {@code -} replaced by {@code .}) plus {@code .config.modelNameOption}. */ - @Parameter(property = "i18n.inputClassName") - private String inputClassName; + @Parameter(property = "eugene.optionsClassName") + private String optionsClassName; + + /** + * The fully qualified name of the enum class of actions to scan to generate the configuration java files. + * + * By default, will use groupId.artifactId (with {@code -} replaced by {@code .}) plus {@code .config.modelNameAction}. + */ + @Parameter(property = "eugene.actionsClassName") + private String actionsClassName; /** * The source directory where to scan options java file. */ - @Parameter(property = "i18n.sourceDirectory", defaultValue = "${basedir}/src/main/java", required = true) + @Parameter(property = "eugene.sourceDirectory", defaultValue = "${basedir}/src/main/java", required = true) private File sourceDirectory; /** * The root directory where to generated. */ - @Parameter(property = "i18n.outputdirectory", defaultValue = "${basedir}/target/generated-sources/java", required = true) + @Parameter(property = "eugene.outputdirectory", defaultValue = "${basedir}/target/generated-sources/java", required = true) private File outputdirectory; /** * Pour activer le mode verbeux. - * - * @since 2.0.0 */ @Parameter(property = "eugene.verbose", defaultValue = "${maven.verbose}") protected boolean verbose; @@ -118,27 +125,18 @@ public class GenerateApplicationConfigMojo extends AbstractPlugin { * * <b>Note:</b> If nothing is filled here, we will use the system * property {@code file.encoding}. - * - * @since 2.0.0 */ @Parameter(property = "eugene.encoding", defaultValue = "${project.build.sourceEncoding}") protected String encoding; /** - * Encoding to be used for generation of files. * - * <b>Note:</b> If nothing is filled here, we will use the system - * property {@code file.encoding}. - * - * @since 2.0.0 */ @Parameter(property = "eugene.prefix", defaultValue = "Config") protected String prefix; /** * Maven project. - * - * @since 2.0.0 */ @Parameter(defaultValue = "${project}", readonly = true) protected MavenProject project; @@ -156,27 +154,45 @@ public class GenerateApplicationConfigMojo extends AbstractPlugin { getLog().info("Use package name: " + packageName); } - if (inputClassName == null) { + if (optionsClassName == null) { - inputClassName = packageName + "." + modelName + prefix + "Option"; + optionsClassName = packageName + "." + modelName + prefix + "Option"; - File file = PluginHelper.getFile(sourceDirectory, inputClassName.split("\\.")); + File file = PluginHelper.getFile(sourceDirectory, optionsClassName.split("\\.")); File javaFile = new File(file.getParentFile(), file.getName() + ".java"); if (!javaFile.exists()) { prefix = "Configuration"; - inputClassName = packageName + "." + modelName + prefix + "Option"; + optionsClassName = packageName + "." + modelName + prefix + "Option"; - file = PluginHelper.getFile(sourceDirectory, inputClassName.split("\\.")); + file = PluginHelper.getFile(sourceDirectory, optionsClassName.split("\\.")); javaFile = new File(file.getParentFile(), file.getName() + ".java"); if (!javaFile.exists()) { throw new MojoExecutionException("Can't find option file at: " + javaFile); } } - getLog().info("Will scan options from: " + inputClassName); + getLog().info("Detected configuration options class: " + optionsClassName); + + } + + if (actionsClassName == null) { + + actionsClassName = packageName + "." + modelName + prefix + "Action"; + + File file = PluginHelper.getFile(sourceDirectory, actionsClassName.split("\\.")); + File javaFile = new File(file.getParentFile(), file.getName() + ".java"); + + if (javaFile.exists()) { + + getLog().info("Detected configuration actions class: " + actionsClassName); + } else { + + // won't be used + actionsClassName = null; + } } @@ -187,11 +203,12 @@ public class GenerateApplicationConfigMojo extends AbstractPlugin { model = new ObjectModelImpl(); model.setName(modelName); - getLog().info("Model name: " + model.getName()); + String className = modelName + prefix; + getLog().info("Config class name: " + className); - File file = PluginHelper.getFile(sourceDirectory, inputClassName.split("\\.")); + File file = PluginHelper.getFile(sourceDirectory, optionsClassName.split("\\.")); File javaFile = new File(file.getParentFile(), file.getName() + ".java"); String content = FileUtils.readFileToString(javaFile, encoding); @@ -220,8 +237,6 @@ public class GenerateApplicationConfigMojo extends AbstractPlugin { attribute.setName(JavaGeneratorUtil.convertConstantNameToVariableName(entry.getKey())); attribute.setType(entry.getValue()); aClass.addAttribute(attribute); - - } Properties templateProperties = new Properties(); @@ -230,7 +245,10 @@ public class GenerateApplicationConfigMojo extends AbstractPlugin { templateProperties.put(Template.PROP_VERBOSE, verbose); templateProperties.put(Template.PROP_OVERWRITE, true); templateProperties.put(Template.PROP_CLASS_LOADER, loader); - templateProperties.put(ApplicationConfigTransformer.PROP_OPTION_CLASS_NAME, inputClassName); + templateProperties.put(ApplicationConfigTransformer.PROP_OPTION_CLASS_NAME, optionsClassName); + if (actionsClassName != null) { + templateProperties.put(ApplicationConfigTransformer.PROP_ACTION_CLASS_NAME, actionsClassName); + } template = new ApplicationConfigTransformer(); template.setConfiguration(new DefaultTemplateConfiguration(templateProperties)); @@ -267,10 +285,12 @@ public class GenerateApplicationConfigMojo extends AbstractPlugin { return project; } + @Override public String getEncoding() { return encoding; } + @Override public void setEncoding(String encoding) { this.encoding = encoding; } -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.