This repository has been archived by the owner on May 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
User defined plugins from Maven properties
- Loading branch information
Showing
20 changed files
with
481 additions
and
270 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 0 additions & 36 deletions
36
...src/main/java/uk/gov/justice/generation/pojo/generators/plugin/DefaultPluginProvider.java
This file was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
...c/main/java/uk/gov/justice/generation/pojo/generators/plugin/ModifyingPluginProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package uk.gov.justice.generation.pojo.generators.plugin; | ||
|
||
import uk.gov.justice.generation.pojo.generators.plugin.classgenerator.ClassModifyingPlugin; | ||
import uk.gov.justice.generation.pojo.generators.plugin.typename.TypeModifyingPlugin; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* The standard implementation of {@link PluginProvider} that takes a List of {@link | ||
* ClassModifyingPlugin} and a List of {@link TypeModifyingPlugin}. | ||
*/ | ||
public class ModifyingPluginProvider implements PluginProvider { | ||
|
||
private final List<ClassModifyingPlugin> classModifyingPlugins; | ||
private final List<TypeModifyingPlugin> typeModifyingPlugins; | ||
|
||
/** | ||
* Constructor to take a List of {@link ClassModifyingPlugin} and a List of {@link | ||
* TypeModifyingPlugin}. | ||
* | ||
* @param classModifyingPlugins the List of {@link ClassModifyingPlugin} | ||
* @param typeModifyingPlugins the List of {@link TypeModifyingPlugin} | ||
*/ | ||
public ModifyingPluginProvider(final List<ClassModifyingPlugin> classModifyingPlugins, | ||
final List<TypeModifyingPlugin> typeModifyingPlugins) { | ||
this.classModifyingPlugins = classModifyingPlugins; | ||
this.typeModifyingPlugins = typeModifyingPlugins; | ||
} | ||
|
||
@Override | ||
public List<ClassModifyingPlugin> classModifyingPlugins() { | ||
return classModifyingPlugins; | ||
} | ||
|
||
@Override | ||
public List<TypeModifyingPlugin> typeModifyingPlugins() { | ||
return typeModifyingPlugins; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 152 additions & 8 deletions
160
...src/main/java/uk/gov/justice/generation/pojo/generators/plugin/PluginProviderFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,172 @@ | ||
package uk.gov.justice.generation.pojo.generators.plugin; | ||
|
||
import static java.lang.String.format; | ||
import static java.util.stream.Collectors.partitioningBy; | ||
import static java.util.stream.Collectors.toList; | ||
|
||
import uk.gov.justice.generation.pojo.generators.plugin.classgenerator.AddFieldsAndMethodsToClassPlugin; | ||
import uk.gov.justice.generation.pojo.generators.plugin.classgenerator.ClassModifyingPlugin; | ||
import uk.gov.justice.generation.pojo.generators.plugin.classgenerator.GenerateBuilderForClassPlugin; | ||
import uk.gov.justice.generation.pojo.generators.plugin.classgenerator.builder.BuilderGeneratorFactory; | ||
import uk.gov.justice.generation.pojo.generators.plugin.typename.TypeModifyingPlugin; | ||
import uk.gov.justice.maven.generator.io.files.parser.core.GeneratorConfig; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* Creates a {@link PluginProvider} for both {@link ClassModifyingPlugin} and {@link | ||
* TypeModifyingPlugin}. ClassModifyingPlugins consist of a merge of default ClassModifyingPlugins | ||
* and any user defined plugins added to the Maven Plugin generatorProperties tag. | ||
* TypeModifyingPlugins consist of only plugins added to the Maven Plugin generatorProperties tag. | ||
* | ||
* The default {@link ClassModifyingPlugin} are: | ||
* | ||
* {@link AddFieldsAndMethodsToClassPlugin} | ||
* {@link GenerateBuilderForClassPlugin} | ||
* | ||
* For example the following adds the MakeClassSerializable to the default plugins and adds three | ||
* TypeModifyingPlugins: | ||
* | ||
* <pre> | ||
* {@code | ||
* | ||
* <configuration> | ||
* ... | ||
* <generatorProperties> | ||
* <plugins> | ||
* uk.gov.justice.generation.pojo.generators.plugin.classgenerator.MakeClassSerializablePlugin, | ||
* uk.gov.justice.generation.pojo.generators.plugin.typename.SupportJavaOptionalsPlugin, | ||
* uk.gov.justice.generation.pojo.generators.plugin.typename.SupportUuidsPlugin, | ||
* uk.gov.justice.generation.pojo.generators.plugin.typename.SupportZonedDateTimePlugin | ||
* </plugins> | ||
* </generatorProperties> | ||
* ... | ||
* </configuration> | ||
* | ||
* } | ||
* </pre> | ||
* | ||
* If the default ClassModifyingPlugins are not required and other plugins can be used instead, the | ||
* then the excludeDefaultPlugins tag can be set to true, for example: | ||
* | ||
* <pre> | ||
* {@code | ||
* | ||
* <configuration> | ||
* ... | ||
* <generatorProperties> | ||
* <excludeDefaultPlugins>true</excludeDefaultPlugins> | ||
* <plugins> | ||
* ... | ||
* </plugins> | ||
* </generatorProperties> | ||
* ... | ||
* </configuration> | ||
* | ||
* } | ||
* </pre> | ||
*/ | ||
public class PluginProviderFactory { | ||
|
||
private static final String POJO_PLUGIN_PROVIDER = "pojo-plugin-provider"; | ||
private static final String EXCLUDE_DEFAULT_PROPERTY = "excludeDefaultPlugins"; | ||
private static final String PLUGINS_PROPERTY = "plugins"; | ||
|
||
private static final boolean CLASS_MODIFYING_PLUGINS = true; | ||
private static final boolean TYPE_MODIFYING_PLUGINS = false; | ||
|
||
private static final String UNABLE_TO_CREATE_INSTANCE_MESSAGE = "Unable to create instance of pojo plugin with class name %s"; | ||
private static final String INCORRECT_CLASS_TYPE_MESSAGE = "Incorrect Class Type, Class name: %s, does not implement ClassModifyingPlugin or TypeModifyingPlugin."; | ||
|
||
/** | ||
* Create a {@link PluginProvider} using the settings from the {@link GeneratorConfig} | ||
* | ||
* @param generatorConfig the generatorConfig suppying the generatorProperties | ||
* @return a {@link PluginProvider} instance | ||
*/ | ||
public PluginProvider createFor(final GeneratorConfig generatorConfig) { | ||
final Map<String, String> generatorProperties = generatorConfig.getGeneratorProperties(); | ||
final Map<Boolean, List<Object>> pluginTypes = partitionPluginsAccordingToType(generatorProperties); | ||
|
||
return new ModifyingPluginProvider( | ||
defaultAndCustomClassModifyingPlugins(generatorProperties, pluginTypes), | ||
typeModifyingPlugins(pluginTypes)); | ||
} | ||
|
||
private Map<Boolean, List<Object>> partitionPluginsAccordingToType(final Map<String, String> generatorProperties) { | ||
return allInstancesOfPluginsFrom(generatorProperties) | ||
.collect(partitioningBy(plugin -> plugin instanceof ClassModifyingPlugin)); | ||
} | ||
|
||
private List<ClassModifyingPlugin> defaultAndCustomClassModifyingPlugins(final Map<String, String> generatorProperties, | ||
final Map<Boolean, List<Object>> pluginTypes) { | ||
return Stream | ||
.concat( | ||
defaultClassModifyingPlugins(generatorProperties), | ||
userDefinedClassModifyingPlugins(pluginTypes)) | ||
.collect(toList()); | ||
} | ||
|
||
private List<TypeModifyingPlugin> typeModifyingPlugins(final Map<Boolean, List<Object>> pluginTypes) { | ||
return pluginTypes.get(TYPE_MODIFYING_PLUGINS) | ||
.stream() | ||
.map(this::castToTypeModifyingPlugin) | ||
.collect(toList()); | ||
} | ||
|
||
if (generatorProperties.containsKey(POJO_PLUGIN_PROVIDER)) { | ||
final String className = generatorProperties.get(POJO_PLUGIN_PROVIDER); | ||
private TypeModifyingPlugin castToTypeModifyingPlugin(final Object plugin) { | ||
try { | ||
return (TypeModifyingPlugin) plugin; | ||
} catch (final ClassCastException e) { | ||
throw new PluginProviderException(format(INCORRECT_CLASS_TYPE_MESSAGE, plugin.getClass().getName()), e); | ||
} | ||
} | ||
|
||
private Stream<ClassModifyingPlugin> userDefinedClassModifyingPlugins(final Map<Boolean, List<Object>> pluginTypes) { | ||
return pluginTypes.get(CLASS_MODIFYING_PLUGINS) | ||
.stream() | ||
.map(plugin -> (ClassModifyingPlugin) plugin); | ||
} | ||
|
||
try { | ||
return (PluginProvider) Class.forName(className).newInstance(); | ||
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException | ClassCastException e) { | ||
throw new PluginProviderException(format("Unable to create instance of pojo plugin provider with class name %s", className), e); | ||
private Stream<ClassModifyingPlugin> defaultClassModifyingPlugins(final Map<String, String> generatorProperties) { | ||
if (isExcludeDefaultPlugins(generatorProperties)) { | ||
return Stream.empty(); | ||
} | ||
|
||
return Stream.of( | ||
new AddFieldsAndMethodsToClassPlugin(), | ||
new GenerateBuilderForClassPlugin(new BuilderGeneratorFactory())); | ||
} | ||
|
||
private boolean isExcludeDefaultPlugins(final Map<String, String> generatorProperties) { | ||
if (generatorProperties.containsKey(EXCLUDE_DEFAULT_PROPERTY)) { | ||
return Boolean.valueOf(generatorProperties.get(EXCLUDE_DEFAULT_PROPERTY)); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private Stream<Object> allInstancesOfPluginsFrom(final Map<String, String> generatorProperties) { | ||
if (generatorProperties.containsKey(PLUGINS_PROPERTY)) { | ||
final String pluginValues = generatorProperties.get(PLUGINS_PROPERTY); | ||
|
||
if (!pluginValues.isEmpty()) { | ||
final String[] plugins = pluginValues.split(","); | ||
|
||
return Stream.of(plugins) | ||
.map(this::pluginInstance); | ||
} | ||
} | ||
|
||
return new DefaultPluginProvider(); | ||
return Stream.empty(); | ||
} | ||
|
||
private Object pluginInstance(final String className) { | ||
try { | ||
return Class.forName(className.trim()).newInstance(); | ||
} catch (final InstantiationException | IllegalAccessException | ClassNotFoundException e) { | ||
throw new PluginProviderException(format(UNABLE_TO_CREATE_INSTANCE_MESSAGE, className), e); | ||
} | ||
} | ||
} |
49 changes: 0 additions & 49 deletions
49
...test/java/uk/gov/justice/generation/pojo/generators/plugin/DefaultPluginProviderTest.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.