diff --git a/src/main/java/org/fulib/ClassModelGenerator.java b/src/main/java/org/fulib/ClassModelGenerator.java new file mode 100644 index 00000000..f0a95081 --- /dev/null +++ b/src/main/java/org/fulib/ClassModelGenerator.java @@ -0,0 +1,69 @@ +package org.fulib; + +import org.fulib.classmodel.ClassModel; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * Provides a model for the code generation phase. + * + * @since 1.6 + */ +public class ClassModelGenerator +{ + private final ClassModel classModel; + private final List generators = new ArrayList<>(); + + public ClassModelGenerator(ClassModel classModel) + { + this.classModel = classModel; + } + + public ClassModel getClassModel() + { + return this.classModel; + } + + public List getGenerators() + { + return Collections.unmodifiableList(this.generators); + } + + public ClassModelGenerator withGenerator(AbstractGenerator generator) + { + this.generators.add(generator); + return this; + } + + public ClassModelGenerator withoutGenerator(AbstractGenerator generator) + { + this.generators.remove(generator); + return this; + } + + /** + * Applies the given plugin to this generator. + * + * @param plugin + * the plugin to apply + */ + public void apply(Plugin plugin) + { + plugin.apply(this); + } + + /** + * Invokes all generators. + * + * @see AbstractGenerator#generate(ClassModel) + */ + public void generate() + { + for (final AbstractGenerator generator : this.generators) + { + generator.generate(this.classModel); + } + } +} diff --git a/src/main/java/org/fulib/Plugin.java b/src/main/java/org/fulib/Plugin.java new file mode 100644 index 00000000..1b9d03b0 --- /dev/null +++ b/src/main/java/org/fulib/Plugin.java @@ -0,0 +1,20 @@ +package org.fulib; + +/** + * A generic plugin. + * + * @param + * The type of component this plugin can be used with. + * + * @since 1.6 + */ +public interface Plugin +{ + /** + * Applies this plugin to the component. + * + * @param component + * the target component + */ + void apply(T component); +} diff --git a/src/main/java/org/fulib/builder/ClassModelDecorator.java b/src/main/java/org/fulib/builder/ClassModelDecorator.java new file mode 100644 index 00000000..aca24eb4 --- /dev/null +++ b/src/main/java/org/fulib/builder/ClassModelDecorator.java @@ -0,0 +1,31 @@ +package org.fulib.builder; + +import org.fulib.ClassModelGenerator; + +/** + * ClassModelDecorator specifies hooks for various steps of the fulib lifecycle. + * + * @since 1.6 + */ +public interface ClassModelDecorator +{ + /** + * Hook for modifying the class meta model. + * + * @param m + * the class model manager + */ + default void decorate(ClassModelManager m) + { + } + + /** + * Hook for modifying the code generation phase. + * + * @param generator + * the generator + */ + default void decorate(ClassModelGenerator generator) + { + } +} diff --git a/src/main/java/org/fulib/builder/ClassModelDecorators.java b/src/main/java/org/fulib/builder/ClassModelDecorators.java new file mode 100644 index 00000000..5a83b076 --- /dev/null +++ b/src/main/java/org/fulib/builder/ClassModelDecorators.java @@ -0,0 +1,21 @@ +package org.fulib.builder; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Annotation for specifying which classes in a package are {@link ClassModelDecorator}s. + * + * @since 1.6 + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.PACKAGE) +public @interface ClassModelDecorators +{ + /** + * @return the classes in this package that implement {@link ClassModelDecorator} + */ + Class[] value(); +} diff --git a/src/main/java/org/fulib/builder/ClassModelManager.java b/src/main/java/org/fulib/builder/ClassModelManager.java index 93d3bedf..6bafb7be 100644 --- a/src/main/java/org/fulib/builder/ClassModelManager.java +++ b/src/main/java/org/fulib/builder/ClassModelManager.java @@ -1,6 +1,7 @@ package org.fulib.builder; import org.fulib.Fulib; +import org.fulib.Plugin; import org.fulib.StrUtil; import org.fulib.classmodel.*; import org.fulib.util.Validator; @@ -243,6 +244,19 @@ public ClassModelBuilder asBuilder() return new ClassModelBuilder(this.classModel); } + /** + * Applies the given plugin to this manager. + * + * @param plugin + * the plugin to apply + * + * @since 1.6 + */ + public void apply(Plugin plugin) + { + plugin.apply(this); + } + // --------------- Settings --------------- /** diff --git a/src/main/java/org/fulib/tables/TableGeneratorPlugin.java b/src/main/java/org/fulib/tables/TableGeneratorPlugin.java new file mode 100644 index 00000000..b3862a14 --- /dev/null +++ b/src/main/java/org/fulib/tables/TableGeneratorPlugin.java @@ -0,0 +1,19 @@ +package org.fulib.tables; + +import org.fulib.ClassModelGenerator; +import org.fulib.Plugin; +import org.fulib.TablesGenerator; + +/** + * A plugin that uses registers the {@link TablesGenerator}. + * + * @since 1.6 + */ +public class TableGeneratorPlugin implements Plugin +{ + @Override + public void apply(ClassModelGenerator component) + { + component.withGenerator(new TablesGenerator()); + } +}